springboot quickly integrates mybatis-plus

Introduction
Mybatis-Plus(简称MP) is an enhancement tool of Mybatis. It only enhances without changing on the basis of Mybatis, and is born to simplify development and improve efficiency. This is the official definition. For more introduction and features of mybatis-plus, you can refer to the official website of mybatis-plus. So how is it enhanced? In fact, it has already encapsulated some crud methods, we don't need to write xml anymore, just call these methods directly, just like JPA.

springBoot quickly integrates mybatis-plus
一、pom文件引入mybatis-plus依赖

 <dependencies>
        <!--web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!--mysql驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.25</version>
        </dependency>
        <!--mybatis-plus-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.1.0</version>
        </dependency>
        <!---test-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

注:The main thing here is the mybatis-plus-boot-starter dependency, other dependencies are basic

二、基础配置 application.properties

#mysql数据库
spring.datasource.url= jdbc:mysql://127.0.0.1:3306/springbootdemo?characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=GMT%2B8
spring.datasource.username= root
spring.datasource.password= 111111
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

#mybatis
#开启驼峰
mybatis.configuration.map-underscore-to-camel-case=true
#打印日志
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

#mybatis-plus配置
mybatis-plus.mapper-locations=classpath*:/mappers/*.xml
#主键类型  0:"数据库ID自增", 1:"用户输入ID",2:"全局唯一ID (数字类型唯一ID)", 3:"全局唯一ID UUID";
mybatis-plus.global-config.id-type=0
#字段策略 0:"忽略判断",1:"非 NULL 判断"),2:"非空判断"
mybatis-plus.global-config.field-strategy= 1
#驼峰下划线转换
mybatis-plus.global-config.db-column-underline=true

三、MybatisPlusConfig

/**
 * MybatisPlus配置类
 */
@EnableTransactionManagement
@Configuration
@MapperScan(basePackages = "com.example.demo.mapper")
public class MybatisPlusConfig {

    /**
     * 分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}

四、测试

  • The entity class primary key uses the @TableId(value = "id",type = IdType.AUTO)specified type
    @TableField(exist = false):表示该属性不为数据库表字段,但又是必须使用的。

    @Data
    public class SysUser implements Serializable { private static final long serialVersionUID = 1L; /** * primary key / @TableId(value = "id", type = IdType.AUTO) private Integer id; / * * username / private String username ; / * * password / private String password; / * * phone / private String phone; / * * email / private String email; / * * created time / private Date createDate; / * * updated time /





























    private Date updateDate;
    /
    *
    * sys_user
    */
    }

  • New SysUserMapper inherits BaseMapper

    public interface SysUserMapper extends BaseMapper { }

  • test

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 插入
     */
    @Test
    public void insert() {
        SysUser sysUser=new SysUser();
        sysUser.setPhone("18787878787");
        sysUser.setPassword("123456");
        sysUser.setUsername("root");
        sysUser.setCreateDate(new Date());
        sysUser.setUpdateDate(new Date());
        sysUserMapper.insert(sysUser);
    }
    
    /**
     * 分页查询
     */
    @Test
    public void selectByPage() {
        Page<SysUser> page=new Page(1,1);
        IPage p= sysUserMapper.selectPage(page,new QueryWrapper<SysUser>().eq("password","123456"));
        System.out.println(p.getRecords());
    }
    
  • Pagination is generally used

    List getCallLogList(ApiLogDTO dto, Page page); mapper
    IPage getCallLogList(ApiLogDTO dto); IService
    serviceImpl
    @Override
    public IPage getCallLogList(ApiLogDTO dto) { Page page=new Page(dto.getCurrent(),dto.getSize()); Listlist= apiLogMapper.getCallLogList(dto,page); page.setRecords(list); return page; } controller @PostMapping("/call/log") @ApiOperation(“api调用日志列表”) public IPage callLog(@ModelAttribute ApiLogDTO dto){ return sysService.getCallLogList(dto); }










Attach the directory structure
insert image description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324150187&siteId=291194637