mybatis-plus使用说明

mybatis-plus可以参考官网中集成https://baomidou.com/,本文只简单介绍如何集成。

1. 引入依赖

引入数据库连接池依赖

<dependency>
    <groupId>com.zaxxer</groupId>
    <artifactId>HikariCP</artifactId>
</dependency>
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.5.2</version>
</dependency>

引入mysql驱动

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

引入自动生成工具

<dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
</dependency>
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-generator</artifactId>
    <version>3.5.2</version>
    <scope>test</scope>
</dependency>

2. 自动生成mybatis映射

通过自动生成功能,映射数据库实体文件

修改生成类的数据库连接信息和需要生成的表

/**
 * mybatis自动生成
 * @author: fallsea
 * @version 1.0
 */
public class Generator {

    /**
     * 数据库地址
     */
    private static final String URL = "jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai&useSSL=false&useUnicode=true&characterEncoding=utf-8";

    /**
     * 用户名
     */
    private static final String  USERNAME = "root";

    /**
     * 密码
     */
    private static final String PASSWORD = "123456";

    /**
     * 作者
     */
    private static final String AUTHOR = "fallsea";

    /**
     * 父包名
     */
    private static final String PARENT_PACKAGE = "com.wueasy.demo";

    /**
     * mybatis xml路径
     */
    private static final String MAPPER_XML_PATH = "/mybatis/demo";

    /**
     * 表名称
     */
    private static final String TABLE_NAME = "test";


    public static void main(String[] args) throws Exception {


        String relativelyPath=System.getProperty("user.dir"); 

        FastAutoGenerator.create(URL, USERNAME, PASSWORD)
        .globalConfig(builder -> {
            builder.author(AUTHOR) // 设置作者
                .fileOverride() // 覆盖已生成文件
                .disableOpenDir()
                .dateType(DateType.ONLY_DATE)
                .outputDir(FilenameUtils.normalize(relativelyPath+"/src/main/java")); // 指定输出目录
        })
        .packageConfig(builder -> {
            builder.parent(PARENT_PACKAGE) // 设置父包名
                .moduleName("") // 设置父包模块名
                .entity("entity")
                .mapper("mapper")
                .pathInfo(Collections.singletonMap(OutputFile.xml,FilenameUtils.normalize(relativelyPath+"/src/main/resources"+MAPPER_XML_PATH))); // 设置mapperXml生成路径
        })
        .strategyConfig(builder -> {
            builder.addInclude(TABLE_NAME) // 设置需要生成的表名
         // 过滤表前缀,生成的类名会去掉这个前缀
            .addTablePrefix("t_")

            // 第一阶段
            // 是否生成 entity:是
            .entityBuilder()
            // 开启lombok
            .enableLombok()
            .disableSerialVersionUID()
            // 开启实体时字段注解。 会在生成的实体类的字段上,添加注解: @TableField("nickname")
            .enableTableFieldAnnotation()
            // 设置主键Id生成策略,设置为默认的雪花算法(ASSIGN_ID)
            .idType(IdType.AUTO)
            // 会在实体类的该字段上追加注解[@TableField(value = "create_time", fill = FieldFill.INSERT)]
//            .addTableFills(new Column("create_time", FieldFill.INSERT))
            // 会在实体类的该字段上追加注解[@TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE)]
//            .addTableFills(new Column("update_time", FieldFill.INSERT_UPDATE))
            // 第二阶段
            .mapperBuilder()
            .enableMapperAnnotation()
            // 启用 BaseResultMap 生成。 会在 mapper.xml文件生成[通用查询映射结果]配置。
            .enableBaseResultMap()
            // 启用 BaseColumnList。 会在mapper.xml文件生成[通用查询结果列 ]配置
            .enableBaseColumnList()
            .build()
            ;
        })
        .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板
        .templateConfig(consumer->{
            consumer.disable(TemplateType.CONTROLLER,TemplateType.SERVICE,TemplateType.SERVICEIMPL).build();
        })
        .execute();
    }


}

3. 启用分页配置

使用mybatis-plus自带的分页插件,不推荐使用PageHelper分页工具

/**
 * mybatis plus 配置
 * @author: fallsea
 * @version 1.0
 */
@Configuration
public class MybatisPlusConfig {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

}

4. 数据库配置和mybatis扫描

启动类上增加mapper扫描包

@MapperScan("com.wueasy.demo.mapper")

数据库连接配置

spring: 
  datasource:
    type: com.zaxxer.hikari.HikariDataSource
    url: jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai&useSSL=false&useUnicode=true&characterEncoding=utf-8
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 123456
    hikari:
      minimum-idle: 5 #池中最小空闲连接数量,默认值10
      idle-timeout: 30000 #一个连接idle状态的最大时长(毫秒),超时则被释放(retired),缺省:10s
      maximum-pool-size: 15 # 池中最大连接数(包括空闲和正在使用的连接)
      auto-commit: true # 是否自动提交池中返回的连接
      pool-name: HikariCP # 连接池的名字
      max-lifetime: 120000 # 连接池中连接的最大生命周期
      connection-timeout: 30000 # 连接超时时间。默认值为30s
      connection-test-query: SELECT 1 # 测试连接

mybatis-plus:
  mapper-locations: classpath*:mybatis/**/*.xml

5. 接口开发

开发数据库增删改查操作

/**
 * test
 * @author: fallsea
 * @version 1.0
 */
@RestController
@RequestMapping("/test")
public class TestController
{
    @Autowired
    private TestMapper testMapper;

    /**
     * 新增
     * @author: fallsea
     * @param dto
     * @return
     */
    @RequestMapping(value = "/add", method = { RequestMethod.POST }, produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<Void> add(@RequestBody @Valid TestAddDto dto) {
        Test test = new Test();
        BeanUtils.copyProperties(dto, test);
        test.setCreatedDate(new Date());
        testMapper.insert(test);
        return new Result<Void>();
    }

    /**
     * 修改
     * @author: fallsea
     * @param dto
     * @return
     */
    @RequestMapping(value = "/edit", method = { RequestMethod.POST }, produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<Void> edit(@RequestBody @Valid TestEditDto dto) {
        Test test = new Test();
        BeanUtils.copyProperties(dto, test);
        test.setCreatedDate(new Date());
        testMapper.updateById(test);
        return new Result<Void>();
    }

    /**
     * 查询单个
     * @author: fallsea
     * @param dto
     * @return
     */
    @RequestMapping(value = "/get", method = { RequestMethod.POST }, produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<TestVo> get(@RequestBody @Valid TestIdDto dto) {
        Test test = testMapper.selectById(dto.getId());
        if(null==test) {
            throw new InvokeException(-1, "记录不存在");
        }
        TestVo vo = new TestVo();
        BeanUtils.copyProperties(test, vo);
        return new Result<TestVo>().setData(vo);
    }

    /**
     * 删除
     * @author: fallsea
     * @param dto
     * @return
     */
    @RequestMapping(value = "/delete", method = { RequestMethod.POST }, produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<Void> delete(@RequestBody @Valid TestIdDto dto) {
        Test test = testMapper.selectById(dto.getId());
        if(null==test) {
            throw new InvokeException(-1, "记录不存在");
        }
        testMapper.deleteById(dto.getId());
        return new Result<Void>();
    }

    /**
     * 分页
     * @author: fallsea
     * @param dto
     * @return
     */
    @RequestMapping(value = "/queryPage", method = { RequestMethod.POST }, produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<Page<TestVo>> queryPage(@RequestBody @Valid TestQueryPageDto dto) {
        IPage<Test> page = testMapper.selectPageList(PageDTO.of(dto.getPageNum(), dto.getPageSize()),dto.getName());
        //旧的转新的page
        Page<TestVo> newPage = new Page<>();
        newPage.setPageNum(page.getCurrent());
        newPage.setPages(page.getPages());
        newPage.setPageSize((int) page.getSize());
        newPage.setTotal(page.getTotal());
        if(null!=page.getRecords() && !page.getRecords().isEmpty()) {
            List<TestVo> newList = new ArrayList<>();
            for (Test test : page.getRecords()) {
                TestVo vo = new TestVo();
                BeanUtils.copyProperties(test, vo);
                newList.add(vo);
            }
            newPage.setList(newList);
        }
        return new Result<Page<TestVo>>().setData(newPage);
    }


}
Copyright © wueasy.com 2017-2022 all right reserved,powered by Gitbook未经允许,禁止以任何形式传播 修订时间: 2022-09-24

results matching ""

    No results matching ""