JAVA - SpringBoot项目引用generator生成 Mybatis文件
在spring官网https://start.spring.io/自动生成springboot项目,这里选择项目自动依赖web/mybatis/mysql,然后点击Generate Project下载项目到本地。
集成generator
从官网获取generatorConfig文件
http://www.mybatis.org/generator/configreference/xmlconfig.html
复制以下内容到~/src/main/resources/generator目录,命名generatorConfig.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <generatorConfiguration> <classPathEntry location="/Program Files/IBM/SQLLIB/java/db2java.zip"/> <context id="DB2Tables" targetRuntime="MyBatis3"> <jdbcConnection driverClass="COM.ibm.db2.jdbc.app.DB2Driver" connectionURL="jdbc:db2:TEST" userId="db2admin" password="db2admin"> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <javaModelGenerator targetPackage="test.model" targetProject="MBGTestProjectsrc"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <sqlMapGenerator targetPackage="test.xml" targetProject="MBGTestProjectsrc"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <javaClientGenerator type="XMLMAPPER" targetPackage="test.dao" targetProject="MBGTestProjectsrc"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <table schema="DB2ADMIN" tableName="ALLTYPES" domainObjectName="Customer"> <property name="useActualColumnNames" value="true"/> <generatedKey column="ID" sqlStatement="DB2" identity="true"/> <columnOverride column="DATE_FIELD" property="startDate"/> <ignoreColumn column="FRED"/> <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR"/> </table> </context> </generatorConfiguration>
说明:复制到idea项目中mybatis-generator-config_1_0.dtd可能会标红,因为这个文件依赖mybatis-generator-core这个包。
在pom.xml加入依赖即可解决标红问题,记得刷新maven。
在pom.xml 中增加引用包
<dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.5</version> </dependency>
generatorConfiguration这个标签下的子标签,有如下约束
<!ELEMENT generatorConfiguration (properties?, classPathEntry*, context+)>
第一个子标签如果有则必须是properties
第二个子标签如果有则必须是classPathEntry
第三哥标签context必须有一个或者多个
同样的context子标签有如下约束
<!ELEMENT context (property*, plugin*, commentGenerator?, (connectionFactory | jdbcConnection), javaTypeResolver?, javaModelGenerator, sqlMapGenerator?, javaClientGenerator?, table+)>
注意:标签有序
修改为mysql可用的配置文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <generatorConfiguration> <!--<properties resource="application.properties"/>--> <!--这里注释掉,后面集成插件的时候,在pom.xml文件导入驱动--> <!--<classPathEntry location="~/mysql/mysql-connector-java-8.0.19.jar" />--> <!--1,MyBatis3:默认的值; 2,MyBatis3Simple:生成的mapper和xml比较简洁干净--> <context id="Mysql" targetRuntime="MyBatis3" defaultModelType="flat"> <property name="beginningDelimiter" value="`"/> <property name="endingDelimiter" value="`"/> <property name="javaFileEncoding" value="UTF-8"/> <!--pressAllComments:MyBatis3默认为false,true则默认不生成注释 suppressDate:MyBatis3默认为false,默认不生成时间戳--> <commentGenerator> <property name="suppressDate" value="true"/> <property name="suppressAllComments" value="true"/> </commentGenerator> <!--新版mysql驱动用的是com.mysql.cj.jdbc.Driver,老版本的是com.mysql.jdbc.Driver--> <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/db1?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC" userId="root" password="123456"> </jdbcConnection> <javaModelGenerator targetPackage="com.example.recordboot.entity" targetProject="src/main/java"/> <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"/> <javaClientGenerator targetPackage="com.example.recordboot.dao" targetProject="src/main/java" type="XMLMAPPER"/> <!--去掉example代码--> <table tableName="tbl_order" domainObjectName="TblOrder" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> <!--% 表示全部表--> <!--mysql 配置--> <generatedKey column="id" sqlStatement="Mysql" identity="true"/> <!--oracle 配置--> <!-- <generatedKey column="id" sqlStatement="select SEQ_{1}.nextval from dual" identity="false" type="pre"/> --> </table> </context> </generatorConfiguration>
注:
在 jdbcConnection 的connectionURL 配置中,需解决时区问题与乱码问题。
连接串后面加上:?useUnicode=true&characterEncoding=UTF-8,完整示例:jdbc:mysql://localhost:3306/dbname?useUnicode=true&characterEncoding=UTF-8 【可以解决中文乱码问题】
当然也可以联合起来用jdbc:mysql://localhost:3306/dbname?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
xml 中的特殊字符 & 需转义成 &
集成插件
在pom.xml中加入以下配置集成插件
<build> <plugins> <!--其他插件,复制下面内容即可,从<plugin>到</plugin>--> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile> <overwrite>true</overwrite> <verbose>true</verbose> </configuration> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.19</version> </dependency> </dependencies> </plugin> </plugins> </build>
mysql-connector-java 的版本可在这里查看
在pom.xml 中配置 编码UTF-8
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties>
刷新maven后,可以在idea的mavenProject里看到
双击即可运行,即可在项目中看到生成的文件。
引用 spring官网:https://start.spring.io/
引用MyBatis官网:http://mybatis.org/generator/configreference/xmlconfig.html
引用:https://blog.csdn.net/qq_25855857/article/details/87271766