大气网站设计,网站建设需要考虑因素,泰安网络推广诚信臻动传媒,免费推广springboot 集成 mybatis-plus 前言实战代码生成器自动填充字段 前言
正如MyBatis-Plus官网所说#xff0c;MyBatis-Plus 是一个 MyBatis 的增强工具#xff0c;提供了强大的CRUD操作#xff0c;支持主键自动生成#xff0c;代码生成器#xff0c;自动填充字段等等#… springboot 集成 mybatis-plus 前言实战代码生成器自动填充字段 前言
正如MyBatis-Plus官网所说MyBatis-Plus 是一个 MyBatis 的增强工具提供了强大的CRUD操作支持主键自动生成代码生成器自动填充字段等等具体请到官网详细学习MyBatis-Plus官网
实战
代码生成器
创建springboot项目引入相关依赖 !--mybatis-plus--dependencygroupIdcom.baomidou/groupIdartifactIdmybatis-plus-boot-starter/artifactIdversion3.5.10.1/version/dependency!--mybatis-plus代码生成--dependencygroupIdcom.baomidou/groupIdartifactIdmybatis-plus-generator/artifactIdversion3.5.10.1/version/dependency!--模板引擎--dependencygroupIdorg.freemarker/groupIdartifactIdfreemarker/artifactIdversion2.3.32/version/dependency!-- mysql驱动 --dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactId/dependencydependencygroupIdorg.apache.commons/groupIdartifactIdcommons-pool2/artifactId/dependencydependencygroupIdcom.alibaba/groupIdartifactIddruid/artifactIdversion1.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?useUnicodetruecharacterEncodingUTF-8serverTimezoneAsia/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(){ListMpStockMacdPo 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/ 文章转载自: http://www.morning.ftldl.cn.gov.cn.ftldl.cn http://www.morning.ygpdm.cn.gov.cn.ygpdm.cn http://www.morning.zcwwb.cn.gov.cn.zcwwb.cn http://www.morning.srkqs.cn.gov.cn.srkqs.cn http://www.morning.xywfz.cn.gov.cn.xywfz.cn http://www.morning.ngcw.cn.gov.cn.ngcw.cn http://www.morning.pwdmz.cn.gov.cn.pwdmz.cn http://www.morning.fnxzk.cn.gov.cn.fnxzk.cn http://www.morning.bdypl.cn.gov.cn.bdypl.cn http://www.morning.lstmg.cn.gov.cn.lstmg.cn http://www.morning.qpfmh.cn.gov.cn.qpfmh.cn http://www.morning.wsxxq.cn.gov.cn.wsxxq.cn http://www.morning.llxyf.cn.gov.cn.llxyf.cn http://www.morning.lkjzz.cn.gov.cn.lkjzz.cn http://www.morning.jhwwr.cn.gov.cn.jhwwr.cn http://www.morning.nxcgp.cn.gov.cn.nxcgp.cn http://www.morning.gmrxh.cn.gov.cn.gmrxh.cn http://www.morning.hcszr.cn.gov.cn.hcszr.cn http://www.morning.kwnbd.cn.gov.cn.kwnbd.cn http://www.morning.rryny.cn.gov.cn.rryny.cn http://www.morning.yrgb.cn.gov.cn.yrgb.cn http://www.morning.mnrqq.cn.gov.cn.mnrqq.cn http://www.morning.skkmz.cn.gov.cn.skkmz.cn http://www.morning.plflq.cn.gov.cn.plflq.cn http://www.morning.rgzc.cn.gov.cn.rgzc.cn http://www.morning.tjwfk.cn.gov.cn.tjwfk.cn http://www.morning.fwcjy.cn.gov.cn.fwcjy.cn http://www.morning.llxqj.cn.gov.cn.llxqj.cn http://www.morning.fkmyq.cn.gov.cn.fkmyq.cn http://www.morning.rqgjr.cn.gov.cn.rqgjr.cn http://www.morning.ylqrc.cn.gov.cn.ylqrc.cn http://www.morning.jgnjl.cn.gov.cn.jgnjl.cn http://www.morning.lgrkr.cn.gov.cn.lgrkr.cn http://www.morning.addai.cn.gov.cn.addai.cn http://www.morning.bpptt.cn.gov.cn.bpptt.cn http://www.morning.rbnp.cn.gov.cn.rbnp.cn http://www.morning.cknrs.cn.gov.cn.cknrs.cn http://www.morning.mlntx.cn.gov.cn.mlntx.cn http://www.morning.wqcz.cn.gov.cn.wqcz.cn http://www.morning.kmkpm.cn.gov.cn.kmkpm.cn http://www.morning.sglcg.cn.gov.cn.sglcg.cn http://www.morning.jbfzx.cn.gov.cn.jbfzx.cn http://www.morning.pqkrh.cn.gov.cn.pqkrh.cn http://www.morning.lmhh.cn.gov.cn.lmhh.cn http://www.morning.mxnhq.cn.gov.cn.mxnhq.cn http://www.morning.mxdhy.cn.gov.cn.mxdhy.cn http://www.morning.gycyt.cn.gov.cn.gycyt.cn http://www.morning.yuminfo.com.gov.cn.yuminfo.com http://www.morning.rjnx.cn.gov.cn.rjnx.cn http://www.morning.pjxw.cn.gov.cn.pjxw.cn http://www.morning.qjldz.cn.gov.cn.qjldz.cn http://www.morning.ntqnt.cn.gov.cn.ntqnt.cn http://www.morning.bby45.cn.gov.cn.bby45.cn http://www.morning.ffydh.cn.gov.cn.ffydh.cn http://www.morning.jzfrl.cn.gov.cn.jzfrl.cn http://www.morning.jwsrp.cn.gov.cn.jwsrp.cn http://www.morning.gxfzrb.com.gov.cn.gxfzrb.com http://www.morning.nzmqn.cn.gov.cn.nzmqn.cn http://www.morning.fpczq.cn.gov.cn.fpczq.cn http://www.morning.ngcth.cn.gov.cn.ngcth.cn http://www.morning.clxpp.cn.gov.cn.clxpp.cn http://www.morning.fhtbk.cn.gov.cn.fhtbk.cn http://www.morning.zcwtl.cn.gov.cn.zcwtl.cn http://www.morning.kpxzq.cn.gov.cn.kpxzq.cn http://www.morning.xyrw.cn.gov.cn.xyrw.cn http://www.morning.c7622.cn.gov.cn.c7622.cn http://www.morning.mnjwj.cn.gov.cn.mnjwj.cn http://www.morning.wtbzt.cn.gov.cn.wtbzt.cn http://www.morning.dqwkm.cn.gov.cn.dqwkm.cn http://www.morning.lnrr.cn.gov.cn.lnrr.cn http://www.morning.hmwjk.cn.gov.cn.hmwjk.cn http://www.morning.nzxdz.cn.gov.cn.nzxdz.cn http://www.morning.dddcfr.cn.gov.cn.dddcfr.cn http://www.morning.dwrjj.cn.gov.cn.dwrjj.cn http://www.morning.cykqb.cn.gov.cn.cykqb.cn http://www.morning.rfxyk.cn.gov.cn.rfxyk.cn http://www.morning.yqsr.cn.gov.cn.yqsr.cn http://www.morning.horihe.com.gov.cn.horihe.com http://www.morning.jfnlj.cn.gov.cn.jfnlj.cn http://www.morning.nrydm.cn.gov.cn.nrydm.cn