This paper introduces the integration of Spring Boot and MyBatis-Plus framework to improve the interaction with the database. MyBatis-Plus persistence layer framework technology is more powerful than MyBatis, higher efficiency, built-in multiple add, delete, change and check methods, very suitable for use with Spring Boot combination.

MyBatisPlus website

Import dependence

 <! -- MyBatisPlus -->
 <dependency>
     <groupId>com.baomidou</groupId>
     <artifactId>mybatis-plus-boot-starter</artifactId>
     <version>Latest Version</version>
 </dependency>
 <! -- Mysql dependency -->
 <dependency>
     <groupId>mysql</groupId>
     <artifactId>mysql-connector-java</artifactId>
 </dependency>
 <! -- lombok -->
 <dependency>
     <groupId>org.projectlombok</groupId>
     <artifactId>lombok</artifactId>
 </dependency>
Copy the code

Design database

 SET FOREIGN_KEY_CHECKS=0;
 ​
 DROP TABLE IF EXISTS `user`;
 CREATE TABLE `user` (
   `id` int(11) NOT NULL AUTO_INCREMENT,
   `username` varchar(32) NOT NULL.`password` varchar(32) NOT NULL,
   PRIMARY KEY (`id`))ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
 ​
 INSERT INTO `user` VALUES ('1'.'jack'.'123');
 INSERT INTO `user` VALUES ('2'.'tom'.'123');
Copy the code

configurationapplication.yml

 server:
   port: 8888    # port
 spring
   datasource:
     driver-class-name: com.mysql.jdbc.Driver    # driver name
     url: JDBC: mysql: / / 127.0.0.1:3306 / springboot_demo? useUnicode=true&characterEncoding=UTF8&useSSL=false&allowMultiQueries=true&serverTimezone=Asia/Shanghai    Database connection url
     username: root  # username
     password: root  # your password
Copy the code

Create the entity class POJO

User.java

 @Data // Lombok needs to be installed in IDEA
 public class User {
     private Integer id;
     private String username;
     private String password;
 }
Copy the code

Create mapper

Inherit BaseMapper

, built-in default single table add, delete, change check methods

UserMapper.java

 public interface UserMapper extends BaseMapper<User> {}Copy the code

Modifying the startup class

MyBatisPlusDemoApplication.java

 @SpringBootApplication
 @MapperScan("com.tom.mapper")   // Let Spring scan and load into mapper
 public class MyBatisPlusDemoApplication {
     public static void main(String[] args) { SpringApplication.run(MyBatisPlusDemoApplication.class, args); }}Copy the code

Create a service

UserService.javainterface

 public interface UserService {
     List<User> queryUserList(a);  // Query all users
 }
Copy the code

UserServiceImpl.javaThe implementation class

 @Service  // Add the business object instantiation to the SpringIOC container
 public class UserServiceImpl implements UserService {
     @Autowired
     private UserMapper userMapper;
     
     // Query all users
     @Override
     public List<User> queryUserList(a) {
         return userMapper.selectList(null); // mapper built-in methods}}Copy the code

Create a controller

The Controller annotation is different in Spring

Using the @restController annotation all methods return data as jSON-formatted strings (REST-based Web services controller)

Using the annotation @Controller is the front-end response to an HTML static page (traditional Spring MVC view Controller)

The key difference is how the HTTP response body is created

UserController.java

@RestController  // Add the object to the SpringIOC container
public class UserController {
    @Autowired
    private UserService userService;

    /** * Query all users * in user table@return* /
    @GetMapping("/userList")
    public List<User> queryUserList(a){
        returnuserService.queryUserList(); }}Copy the code

Printing SQL Logs

Methods a

logging:
  level:
    com.tom.mapper: debug
Copy the code

Method 2

mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # enable SQL logging
Copy the code