introduce:
Mybatis-Plus (MP for short) is a Mybatis enhancement tool. Based on Mybatis, it only enhances and does not change, and is born to simplify development and improve efficiency. (Excerpt from mybatis-plus official website) Although Mybatis has provided us with great convenience, it still has shortcomings. The existence of MP is to slightly make up for the shortcomings of Mybatis. When we use Mybatis, we will find that whenever we want to write a business logic, we have to write a method in the DAO layer, and then correspond to a SQL. Even if we simply query a condition, we have to add a new method in the DAO layer. In response to this problem, MP is a framework that combines the advantages of Mybatis and Hibernate. It provides the convenience of Hibernate's single-table CURD operation while retaining the features of Mybatis.
This chapter only teaches you how to use MybatisPlus. If you want to understand in-depth how the underlying layer is implemented, you can download the source code to interpret it on the official website.
1. Create a project
I won’t go step by step here. I will directly give the created project structure. At the end of this chapter, I will give the source code address that needs to be found to be downloaded.
2. Introduce dependencies
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.chaoqi</groupId> <artifactId>springboot_mybatisplus</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springboot_mybatisplus</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> <relativePath/> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--Add jsp dependencies--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> <!-- SpringBoot - MyBatis reverse engineering --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.2</version> </dependency> <!-- MyBatis General Mapper --> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>1.1.4</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <configurationFile>src/main/resources/generatorConfig.xml</configurationFile> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> <executions> <execution> <id>Generate MyBatis Artifacts</id> <goals> <goal>generate</goal> </goals> </execution> </execution> </execution> </execution> <dependencies> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>3.5.0</version> </dependency> </dependencies> </plugin> </plugins> </build></project>
3. Edit application.yml
server: port: 8080spring: mvc: view: prefix: /WEB-INF/jsp/ suffix: .jsp datasource: url: jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8&useUnicode=true&useSSL=false username: root password: 123456 driver-class-name: com.mysql.jdbc.Drivermybatis: mapper-locations: classpath:mapper/*.xml type-aliases-package: com.chaoqi.springboot_mybatisplus.domain
4. Reverse generation of pojo, mapper
Create generatorConfig.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"><generatorConfiguration> <properties resource="application.yml"/> <classPathEntry location="D:/mysql/mysql-connector-java-5.1.46/mysql-connector-java-5.1.46.jar"/> <context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat"> <property name="beginningDelimiter" value="`"/> <property name="endDelimiter" value="`"/> <property name="javaFileEncoding" value="UTF-8"/> <plugin type="tk.mybatis.mapper.generator.MapperPlugin"> <property name="mappers" value="tk.mybatis.mapper.common.Mapper"/> </plugin> <!-- Comment--> <commentGenerator> <!-- Whether to generate comments for timestamps --> <property name="suppressDate" value="true"/> <!-- Whether to remove automatically generated comments true: Yes: false: No--> <property name="suppressAllComments" value="false"/> </commentGenerator> <!-- JDBC Connection--> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8" userId="root" password="123456"> </jdbcConnection> <!-- Generate entity class address--> <javaModelGenerator targetPackage="com.chaoqi.springboot_mybatisplus.dao.domain" targetProject="src/main/java"/> <!-- Generate mapper xml file--> <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"/> <!-- Generate mapper xml corresponding Client--> <javaClientGenerator targetPackage="com.chaoqi.springboot_mybatisplus.dao.mapper" targetProject="src/main/java" type="XMLMAPPER"/> <!-- Configuration table information--> <table tableName="%"> <!--mysql configuration--> <generatedKey column="id" sqlStatement="Mysql"/> <!--oracle configuration--> <!--<generatedKey column="id" sqlStatement="select SEQ_{1}.nextval from dual" identity="false" type="pre"/>--> </table> </context></generatorConfiguration>maven run generator
The generated project structure is as follows
5. Integrate mybatisplus
Create service interface and service implementation class
package com.chaoqi.springboot_mybatisplus.service;import com.chaoqi.springboot_mybatisplus.dao.domain.MusicInfo;import java.util.List;public interface MusicInfoService { public List<MusicInfo> getMusicInfo();} package com.chaoqi.springboot_mybatisplus.service.impl;import com.chaoqi.springboot_mybatisplus.dao.domain.MusicInfo;import com.chaoqi.springboot_mybatisplus.dao.mapper.MusicInfoMapper;import com.chaoqi.springboot_mybatisplus.service.MusicInfoService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.List;@Servicepublic class MusicInfoServiceImpl implements MusicInfoService { @Autowired private MusicInfoMapper musicInfoMapper; @Override public List<MusicInfo> getMusicInfo() { List<MusicInfo> musicInfos = musicInfoMapper.selectAll(); return musicInfos; }}Create Controller
package com.chaoqi.springboot_mybatisplus.web;import com.chaoqi.springboot_mybatisplus.dao.domain.MusicInfo;import com.chaoqi.springboot_mybatisplus.service.MusicInfoService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.List;@RestController@RequestMapping(value = "/music")public class MusicInfoController { @Autowired private MusicInfoService musicInfoService; @RequestMapping("/showMusic") public List<MusicInfo> getMusicInfo() { List<MusicInfo> musicInfo1 = musicInfoService.getMusicInfo(); return musicInfo1; }}Here I don't write SQL for mapper. Some simple SQLmybatiplus are encapsulated, saving a lot of development time. If it is some complex SQL, it can also be implemented by writing native SQL. In my second blog, I talked about the integration of springboot+springmvc+mybatis project.
Run SpringbootMybatisplusApplication main function
package com.chaoqi.springboot_mybatisplus;import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication@MapperScan("com.chaoqi.springboot_mybatisplus.dao.mapper")public class SpringbootMybatisplusApplication { public static void main(String[] args) { SpringApplication.run(SpringbootMybatisplusApplication.class, args); }}View the database
Below are some methods for encapsulation of mybatisplus. How to use these methods specifically. Interested friends can check the source code. Mybatisplus also has a powerful pagination function. If you are interested, you can also learn here.
(Source code download address)
Finally, thank you for watching. Please forgive me for your lack of experience in writing blogs.