springboot 集成 mybatis-plus
- 前言
- 实战
- 代码生成器
- 自动填充字段
前言
正如MyBatis-Plus官网所说,MyBatis-Plus 是一个 MyBatis 的增强工具,提供了强大的CRUD操作,支持主键自动生成,代码生成器,自动填充字段等等,具体请到官网详细学习MyBatis-Plus官网
实战
代码生成器
- 创建springboot项目
- 引入相关依赖
<!--mybatis-plus--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.10.1</version></dependency><!--mybatis-plus代码生成--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-generator</artifactId><version>3.5.10.1</version></dependency><!--模板引擎--><dependency><groupId>org.freemarker</groupId><artifactId>freemarker</artifactId><version>2.3.32</version></dependency><!-- mysql驱动 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.22</version></dependency>
问题解决:java.lang.NoClassDefFoundError: freemarker/template/Configuration
3. 启动类上包扫描、mapper扫描
@SpringBootApplication(scanBasePackages={"com.gpyj"})
@MapperScan({"com.gpyj.data.dao","com.gpyj.data2.dao"})
public class DataApplication {public static void main(String[] args) {SpringApplication.run(DataApplication.class, args);}}
- yaml配置mapper目录
mybatis:mapper-locations: classpath:mapper/**/*.xml
- 创建代码生成器类
这里主要配置以下几个配置,基本也就够用了,更多的定制化,请参考官网:
- 数据库连接信息
- 全局配置(主要配置公共的输出目录)
- 数据源配置(自定义类型转换器实现)
- 包配置(这些配置能决定你的文件生成到哪里)
- 策略配置(这些配置能决定开启哪些文件生成,以及自定义文件名称等等)
package com.gpyj.data;import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;import java.nio.file.Paths;
import java.sql.Types;
import java.util.Collections;public class MybatisPlusGenerator {public static void main(String[] args) {String projectPath = Paths.get(System.getProperty("user.dir")).toString();System.out.println("path: " + projectPath);String url = "jdbc:mysql://localhost:3306/test20190411?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";String username = "root";String password = "root";FastAutoGenerator.create(url, username, password).globalConfig(builder -> {builder.author("dong") // 设置作者.enableSwagger() // 开启 swagger 模式.outputDir(projectPath + "/src/main/java") // 指定输出目录.disableOpenDir();}).dataSourceConfig(builder ->builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {int typeCode = metaInfo.getJdbcType().TYPE_CODE;if (typeCode == Types.SMALLINT) {// 自定义类型转换return DbColumnType.INTEGER;}return typeRegistry.getColumnType(metaInfo);})).packageConfig(builder -> {builder.parent("com.gpyj.data2") // 设置父包名.entity("dao.bean") // 设置实体类包名.mapper("dao") // 设置 Mapper 接口包名.service("service") // 设置 Service 接口包名.serviceImpl("service.impl") // 设置 Service 实现类包名
// .xml("mappers") // 设置 Mapper XML 文件包名.pathInfo(Collections.singletonMap(OutputFile.xml, projectPath + "/src/main/resources/mapper/mapper")); // 设置路径配置信息}).strategyConfig(builder ->{// 表名String tStockMacd = "t_stock_macd";// 其他文件名String fileName = "Mp" + "StockMacd";builder.addInclude(tStockMacd) // 设置需要生成的表名.entityBuilder().enableLombok() // 启用 Lombok.enableTableFieldAnnotation() // 启用字段注解.convertFileName((a) -> fileName + "Po").serviceBuilder().convertServiceFileName((a) -> fileName + "Service").convertServiceImplFileName((a) -> fileName + "ServiceImpl").mapperBuilder().convertMapperFileName((a) -> fileName + "Dao").convertXmlFileName((a) -> fileName + "Dao").controllerBuilder().disable().convertFileName((a) -> fileName + "Controller");}).templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板.execute();}}
- 执行以上代码生成器类main方法,就会在对应的路径生成文件,在Controller里面简单写个例子
package com.gpyj.data2.controller;import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gpyj.data2.dao.bean.MpStockMacdPo;
import com.gpyj.data2.service.MpStockMacdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.List;@RestController
@RequestMapping("/test2")
public class TestController2 {@AutowiredMpStockMacdService stockMacdService;@GetMapping("/macd")public String test(){List<MpStockMacdPo> list = stockMacdService.list(Wrappers.lambdaQuery(MpStockMacdPo.class).eq(MpStockMacdPo::getCode,"000166.XSHE"));list.forEach(e -> System.out.println(e));return "test";}
}
自动填充字段
https://baomidou.com/guides/auto-fill-field/