Initial use of MybatisPlus

Initial use of MybatisPlus

Precautions for using MybatisPlus in springBoot

1. In the project, the dao of our dao layer does not need to write SQL statements by itself, but it needs to inherit an interface in the mapper, and the previous annotation @Repository, the specific code is as follows

package com.atguigu.mapper;

import com.atguigu.entity.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.springframework.stereotype.Repository;

@Repository
public interface UserMapper extends BaseMapper<User> {
    
    
}

2. There needs to be an annotation on the startup class, @MapperScan("com.atguigu.mapper")

//Specify the package where the interface to become the implementation class is located, and then all interfaces under the package will generate the corresponding implementation class after compilation

package com.atguigu;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.atguigu.mapper")
//指定要变成实现类的接口所在的包,然后包下面的所有接口在编译之后都会生成相应的实现类
public class MpApplication {
    
    

    public static void main(String[] args) {
    
    
        SpringApplication.run(MpApplication.class, args);
    }

}

3. In our configuration file

3.1 JDBC registration driver needs to use the latest

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

3.2 The following is to open the log in the configuration file, we can see our sql statement in the console

mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root
#下面的这一步是开启日志,在日志文件中可以看见我们的sql语句
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

3.3 What we need to pay special attention to here is that we have to add the time zone setting, the time in the East Eight District, because of the springboot version change.

serverTimezone=GMT%2B8

spring.datasource.url=jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=GMT%2B8

4. In our JavaBean, our id needs to use the application data type. For example, the following long type can only be used once, and it cannot be used the second time. It needs to use its packaging class Long

package com.atguigu.entity;

import lombok.Data;

/**
 * @Author Kilig Zong
 * @Date 2020/10/27 20:17
 * @Version 1.0
 */
@Data
public class User {
    
    
    //注意这个在mybatis_plus中可以作用在数据库中,但是类型需要是基本数据类型的包装类;
    private Long id;
    private String name;
    private  int age;
    private  String email;

    

}

5. In our test test class, we need to pay special attention to our annotations, such as @Test and @Runwith annotations and their role

package com.atguigu;

import com.atguigu.entity.User;
import com.atguigu.mapper.UserMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;
//这一步是将test环境和spring容器相融合
@RunWith(SpringRunner.class)
//提供了一个spring容器环境
@SpringBootTest
public class MpApplicationTests {
    
    

    @Autowired
    private UserMapper userMapper;
    /***
     * @author Kilig Zong
     * @date 2020/10/27 21:10
     * @description
     * @param
     * @return void
     **/
    //下面的这个方法是用来查询所有的用户
    @Test
   public void testFindAll(){
    
    
       List<User> users = userMapper.selectList(null);
       for (User user : users) {
    
    
           System.out.println(user);
       }
   }
   @Test
    public  void testSaveUser(){
    
    
       User user = new User();
       //id是我们mybatisplus帮我们生成的,不能是基本数据类型,必须是引用类型
        user.setAge(18);
        user.setName("吴世林");
        user.setEmail("[email protected]");
       int i = userMapper.insert(user);
       System.out.println(i);
   }

}

Guess you like

Origin blog.csdn.net/kiligggggggg/article/details/109325602