(三)springboot + mybatis plus集成AutoGenerator快速搭建项目

注明 : AutoGenerator 是 MyBatis-Plus 的代码生成器,通过 AutoGenerator 可以快速生成 Entity、Mapper、Mapper XML、Service、Controller 等各个模块的代码,极大的提升了开发效率。下文中也会有通过springboot集成mybatis-plus的AutoGenerator时踩过坑的单独介绍,闲话不多说直接上干货

源码下载地址Gitee

项目搭建

这里就不过多阐述springboot+mybatis的项目搭建了,如果有不会的童鞋可以看我的上篇博客进行搭建。

pom.xml 展示



    4.0.0

    com.lqf
    springboot-mybatis-plus-genrator
    0.0.1-SNAPSHOT
    jar

    springboot-mybatis-plus-genrator
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.5.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-freemarker
        
        
            org.springframework.boot
            spring-boot-starter-jdbc
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.projectlombok
            lombok
            true
        
        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.0.3
        
        
            mysql
            mysql-connector-java
            runtime
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    




mybatis-plus.properties展示(自动生成的位置做成了配置)

#此处为本项目src所在路径(代码生成器输出路径)
OutputDir=D:/V/springboot-mybatis-plus-genrator/src/main/java
#mapper.xml的生成位置
OutputDirXml=D:/V/springboot-mybatis-plus-genrator/src/main/resources
#数据库表名(此处切不可为空,如果为空,则默认读取数据库的所有表名)
tableName=fy_currency_log
#装代码的文件夹名
className=crm
#设置作者
author=lqf
#正常情况下,下面的代码无需修改!!!!!!!!!!
#自定义包路径
parent=com.lqf.springbootmybatisplusgenrator
#数据库地址
url=jdbc:mysql://******:3306/crm?useUnicode=true&characterEncoding=utf-8&tinyInt1isBit=false
userName=***
password=***

代码生成器的java类

package com.lqf.springbootmybatisplusgenrator;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

/**
 * 

* 代码生成器 *

*/ public class MpGenerator { public static void main(String[] args) throws InterruptedException { //用来获取Mybatis-Plus.properties文件的配置信息 final ResourceBundle rb = ResourceBundle.getBundle("mybatis-plus"); // 代码生成器 AutoGenerator mpg = new AutoGenerator(); // 全局配置 GlobalConfig gc = new GlobalConfig(); gc.setOutputDir(rb.getString("OutputDir")); gc.setOpen(false); gc.setBaseResultMap(true); gc.setBaseColumnList(true); gc.setAuthor(rb.getString("author")); gc.setMapperName("%sMapper"); gc.setXmlName("%sMapper"); gc.setServiceName("%sService"); gc.setServiceImplName("%sServiceImpl"); gc.setControllerName("%sController"); mpg.setGlobalConfig(gc); // 数据源配置 DataSourceConfig dsc = new DataSourceConfig(); dsc.setDbType(DbType.MYSQL); dsc.setUrl(rb.getString("url")); dsc.setDriverName("com.mysql.jdbc.Driver"); dsc.setUsername(rb.getString("userName")); dsc.setPassword(rb.getString("password")); mpg.setDataSource(dsc); // 包配置 PackageConfig pc = new PackageConfig(); pc.setParent(rb.getString("parent")); pc.setController("controller." + rb.getString("className")); pc.setService("service." + rb.getString("className")); pc.setServiceImpl("service." + rb.getString("className") + ".impl"); pc.setEntity("bean." + rb.getString("className")); pc.setMapper("dao." + rb.getString("className")); mpg.setPackageInfo(pc); // 自定义配置 InjectionConfig cfg = new InjectionConfig() { @Override public void initMap() { // to do nothing } }; List focList = new ArrayList<>(); focList.add(new FileOutConfig("/templates/mapper.xml.ftl") { @Override public String outputFile(TableInfo tableInfo) { // 自定义输入文件名称 return rb.getString("OutputDirXml") + "/mapper/" + rb.getString("className") + "/" + tableInfo.getEntityName() + StringPool.DOT_XML; } }); cfg.setFileOutConfigList(focList); mpg.setCfg(cfg); mpg.setTemplate(new TemplateConfig().setXml(null)); // 策略配置 StrategyConfig strategy = new StrategyConfig(); strategy.setNaming(NamingStrategy.underline_to_camel); strategy.setColumnNaming(NamingStrategy.underline_to_camel); strategy.setEntityLombokModel(true); strategy.setInclude(new String[]{rb.getString("tableName")}); mpg.setStrategy(strategy); mpg.setTemplateEngine(new FreemarkerTemplateEngine()); mpg.execute(); } }

上方代码中有很多的配置,想了解配置含义的请查看官方解析

到这里为止你就可以自动生成一整套的 Entity、Mapper、Mapper XML、Service、Controller 等各个模块的代码。

下面开始介绍不同系列的坑

坑1 (错误日志)

"C:\Program Files\Java\jdk1.8.0_101\bin\java.exe" "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA 2018.1.3\lib\idea_rt.jar=62234:D:\Program Files\JetBrains\IntelliJ IDEA 2018.1.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_101\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\rt.jar;D:\V\springboot-mybatis-plus-genrator\target\classes;D:\Program Files (x86)\mvn_repo\repo\org\springframework\boot\spring-boot-starter-jdbc\2.0.5.RELEASE\spring-boot-starter-jdbc-2.0.5.RELEASE.jar;D:\Program Files (x86)\mvn_repo\repo\org\springframework\boot\spring-boot-starter\2.0.5.RELEASE\spring-boot-starter-2.0.5.RELEASE.jar;D:\Program Files (x86)\mvn_repo\repo\org\springframework\boot\spring-boot\2.0.5.RELEASE\spring-boot-2.0.5.RELEASE.jar;D:\Program Files (x86)\mvn_repo\repo\org\springframework\boot\spring-boot-starter-logging\2.0.5.RELEASE\spring-boot-starter-logging-2.0.5.RELEASE.jar;D:\Program Files (x86)\mvn_repo\repo\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;D:\Program Files (x86)\mvn_repo\repo\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;D:\Program Files (x86)\mvn_repo\repo\org\apache\logging\log4j\log4j-to-slf4j\2.10.0\log4j-to-slf4j-2.10.0.jar;D:\Program Files (x86)\mvn_repo\repo\org\apache\logging\log4j\log4j-api\2.10.0\log4j-api-2.10.0.jar;D:\Program Files (x86)\mvn_repo\repo\org\slf4j\jul-to-slf4j\1.7.25\jul-to-slf4j-1.7.25.jar;D:\Program Files (x86)\mvn_repo\repo\javax\annotation\javax.annotation-api\1.3.2\javax.annotation-api-1.3.2.jar;D:\Program Files (x86)\mvn_repo\repo\org\yaml\snakeyaml\1.19\snakeyaml-1.19.jar;D:\Program Files (x86)\mvn_repo\repo\com\zaxxer\HikariCP\2.7.9\HikariCP-2.7.9.jar;D:\Program Files (x86)\mvn_repo\repo\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;D:\Program Files (x86)\mvn_repo\repo\org\springframework\spring-jdbc\5.0.9.RELEASE\spring-jdbc-5.0.9.RELEASE.jar;D:\Program Files (x86)\mvn_repo\repo\org\springframework\spring-beans\5.0.9.RELEASE\spring-beans-5.0.9.RELEASE.jar;D:\Program Files (x86)\mvn_repo\repo\org\springframework\spring-tx\5.0.9.RELEASE\spring-tx-5.0.9.RELEASE.jar;D:\Program Files (x86)\mvn_repo\repo\org\springframework\boot\spring-boot-starter-web\2.0.5.RELEASE\spring-boot-starter-web-2.0.5.RELEASE.jar;D:\Program Files (x86)\mvn_repo\repo\org\springframework\boot\spring-boot-starter-json\2.0.5.RELEASE\spring-boot-starter-json-2.0.5.RELEASE.jar;D:\Program Files (x86)\mvn_repo\repo\com\fasterxml\jackson\core\jackson-databind\2.9.6\jackson-databind-2.9.6.jar;D:\Program Files (x86)\mvn_repo\repo\com\fasterxml\jackson\core\jackson-annotations\2.9.0\jackson-annotations-2.9.0.jar;D:\Program Files (x86)\mvn_repo\repo\com\fasterxml\jackson\core\jackson-core\2.9.6\jackson-core-2.9.6.jar;D:\Program Files (x86)\mvn_repo\repo\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.9.6\jackson-datatype-jdk8-2.9.6.jar;D:\Program Files (x86)\mvn_repo\repo\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.9.6\jackson-datatype-jsr310-2.9.6.jar;D:\Program Files (x86)\mvn_repo\repo\com\fasterxml\jackson\module\jackson-module-parameter-names\2.9.6\jackson-module-parameter-names-2.9.6.jar;D:\Program Files (x86)\mvn_repo\repo\org\springframework\boot\spring-boot-starter-tomcat\2.0.5.RELEASE\spring-boot-starter-tomcat-2.0.5.RELEASE.jar;D:\Program Files (x86)\mvn_repo\repo\org\apache\tomcat\embed\tomcat-embed-core\8.5.34\tomcat-embed-core-8.5.34.jar;D:\Program Files (x86)\mvn_repo\repo\org\apache\tomcat\embed\tomcat-embed-el\8.5.34\tomcat-embed-el-8.5.34.jar;D:\Program Files (x86)\mvn_repo\repo\org\apache\tomcat\embed\tomcat-embed-websocket\8.5.34\tomcat-embed-websocket-8.5.34.jar;D:\Program Files (x86)\mvn_repo\repo\org\hibernate\validator\hibernate-validator\6.0.12.Final\hibernate-validator-6.0.12.Final.jar;D:\Program Files (x86)\mvn_repo\repo\javax\validation\validation-api\2.0.1.Final\validation-api-2.0.1.Final.jar;D:\Program Files (x86)\mvn_repo\repo\org\jboss\logging\jboss-logging\3.3.2.Final\jboss-logging-3.3.2.Final.jar;D:\Program Files (x86)\mvn_repo\repo\com\fasterxml\classmate\1.3.4\classmate-1.3.4.jar;D:\Program Files (x86)\mvn_repo\repo\org\springframework\spring-web\5.0.9.RELEASE\spring-web-5.0.9.RELEASE.jar;D:\Program Files (x86)\mvn_repo\repo\org\springframework\spring-webmvc\5.0.9.RELEASE\spring-webmvc-5.0.9.RELEASE.jar;D:\Program Files (x86)\mvn_repo\repo\org\springframework\spring-aop\5.0.9.RELEASE\spring-aop-5.0.9.RELEASE.jar;D:\Program Files (x86)\mvn_repo\repo\org\springframework\spring-context\5.0.9.RELEASE\spring-context-5.0.9.RELEASE.jar;D:\Program Files (x86)\mvn_repo\repo\org\springframework\spring-expression\5.0.9.RELEASE\spring-expression-5.0.9.RELEASE.jar;D:\Program Files (x86)\mvn_repo\repo\org\projectlombok\lombok\1.16.22\lombok-1.16.22.jar;D:\Program Files (x86)\mvn_repo\repo\com\baomidou\mybatis-plus-boot-starter\3.0.3\mybatis-plus-boot-starter-3.0.3.jar;D:\Program Files (x86)\mvn_repo\repo\com\baomidou\mybatis-plus\3.0.3\mybatis-plus-3.0.3.jar;D:\Program Files (x86)\mvn_repo\repo\com\baomidou\mybatis-plus-extension\3.0.3\mybatis-plus-extension-3.0.3.jar;D:\Program Files (x86)\mvn_repo\repo\com\baomidou\mybatis-plus-core\3.0.3\mybatis-plus-core-3.0.3.jar;D:\Program Files (x86)\mvn_repo\repo\com\baomidou\mybatis-plus-annotation\3.0.3\mybatis-plus-annotation-3.0.3.jar;D:\Program Files (x86)\mvn_repo\repo\com\github\jsqlparser\jsqlparser\1.2\jsqlparser-1.2.jar;D:\Program Files (x86)\mvn_repo\repo\org\mybatis\mybatis-spring\1.3.2\mybatis-spring-1.3.2.jar;D:\Program Files (x86)\mvn_repo\repo\org\mybatis\mybatis\3.4.6\mybatis-3.4.6.jar;D:\Program Files (x86)\mvn_repo\repo\com\baomidou\mybatis-plus-generator\3.0.3\mybatis-plus-generator-3.0.3.jar;D:\Program Files (x86)\mvn_repo\repo\org\springframework\boot\spring-boot-autoconfigure\2.0.5.RELEASE\spring-boot-autoconfigure-2.0.5.RELEASE.jar;D:\Program Files (x86)\mvn_repo\repo\org\springframework\boot\spring-boot-configuration-processor\2.0.5.RELEASE\spring-boot-configuration-processor-2.0.5.RELEASE.jar;D:\Program Files (x86)\mvn_repo\repo\mysql\mysql-connector-java\5.1.47\mysql-connector-java-5.1.47.jar;D:\Program Files (x86)\mvn_repo\repo\org\springframework\spring-core\5.0.9.RELEASE\spring-core-5.0.9.RELEASE.jar;D:\Program Files (x86)\mvn_repo\repo\org\springframework\spring-jcl\5.0.9.RELEASE\spring-jcl-5.0.9.RELEASE.jar" com.lqf.springbootmybatisplusgenrator.MpGenerator
20:04:10.529 [main] DEBUG com.baomidou.mybatisplus.generator.AutoGenerator - ==========================准备生成文件...==========================
Exception in thread "main" java.lang.NoClassDefFoundError: freemarker/template/Configuration
	at com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine.init(FreemarkerTemplateEngine.java:45)
	at com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine.init(FreemarkerTemplateEngine.java:38)
	at com.baomidou.mybatisplus.generator.AutoGenerator.execute(AutoGenerator.java:98)
	at com.lqf.springbootmybatisplusgenrator.MpGenerator.main(MpGenerator.java:91)
Caused by: java.lang.ClassNotFoundException: freemarker.template.Configuration
	at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
	... 4 more

Process finished with exit code 1

看上面的错误java.lang.NoClassDefFoundError: freemarker/template/Configuration
问题是找不到freemarker/template/Configuration这是怎么引起的呢

注意: freemarker我们那里用到了 ,看下面的代码

 focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输入文件名称
                return rb.getString("OutputDirXml") + "/mapper/" + rb.getString("className") + "/" + tableInfo.getEntityName() + StringPool.DOT_XML;
            }
        });
        cfg.setFileOutConfigList(focList);

我们在使用new FileOutConfig("/templates/mapper.xml.ftl") 生成模板的时候是需要依赖freemarker包所以我们需要在pom文件中引入

 
            org.springframework.boot
            spring-boot-starter-freemarker
        

这里是官方文档没有介绍到的,坑1

坑2

字段类型为 bit、tinyint(1) 时映射为 boolean 类型这个时候MyBatis 是不会自动处理该映射的需要修改请求连接添加参数 tinyInt1isBit=false如下

jdbc:mysql://127.0.0.1:3306/mp?tinyInt1isBit=false

否则会报很多类型转换 boolean的错误

记得生成成功之后,在测试运行的时候要在主启动类上添加@MapperScan(value = “”)哦。

欢迎大家的指正和探讨,

上一篇 : (二)springboot+mybatis plus快速构建项目
下一篇 : (四)springboot + mybatis plus详细拆解CRUD

你可能感兴趣的:(springboot,mybatis,plus,AutoGenerator,日积月累,程序人生,java,springboot,mybatis,plus)