Mysql8+mybatisGenerator (mysql 8的逆向工程)
最近试了一下mysql8的逆向工程工具
1.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "../mybatis-generate-core/src/main/resources/org/mybatis/generator/config/xml/mybatis-generator-config_1_0.dtd"> <generatorConfiguration> <context id="myTable"> <property name="javaFileEncoding" value="UTF-8"/> <!--哪个依赖合适用哪个--> <!-- <plugin type="org.mybatis.generator.plugins.UserDefinePlugin"/>--> <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin> <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/baidu?characterEncoding=UTF-8&autoReconnect=true&serverTimezone=GMT%2B8&useSSL=false" userId="root" password="123456"> <property name="remarksReporting" value="true"/> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> <property name="yp_dateTime2String" value="true"/> <property name="yp_projectName" value="prs"/> </javaTypeResolver> <!-- domain实体 --> <!--D:\code\haiyu\src\main\java\com\haiyu\manager\pojo--> <javaModelGenerator targetPackage="com.baidu.entity" targetProject="D:/myBatisGenerator/src/main/java"> <!--<property name="rootClass" value="com.yp.custtrack.domain.BaseDomain"/>--> </javaModelGenerator> <!-- xml映射文件 --> <!--D:\code\haiyu\src\main\resources\mapper--> <sqlMapGenerator targetPackage="mapper" targetProject="D:/myBatisGenerator/src/main/resources"> </sqlMapGenerator> <!-- dao 接口 --> <!--D:\code\haiyu\src\main\java\com\haiyu\manager\dao--> <javaClientGenerator targetPackage="com.baidu.dao" targetProject="D:/myBatisGenerator/src/main/java" type="XMLMAPPER"> <!--是否允许建立子包(对应MySql的scheme)--> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <table tableName="sys_user" domainObjectName="SysUser"/> </context> </generatorConfiguration>
主要点: 使用 com.mysql.cj.jdbc.Driver驱动
2.pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.myBatisGenerator</groupId> <artifactId>myBatisGenerator</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.4</version> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.13</version> <scope>runtime</scope> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.0</version> </dependency> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.4</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.10.RELEASE</version> </dependency> </dependencies> </project>
主要点: 使用 mysql-connector-java 的jar包要用8的版本
3.Test
package com.baidu; import org.junit.Test; import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.config.Configuration; import org.mybatis.generator.config.xml.ConfigurationParser; import org.mybatis.generator.exception.InvalidConfigurationException; import org.mybatis.generator.internal.DefaultShellCallback; import java.util.ArrayList; import java.util.List; public class MyBatisGeneratorTest { @Test public void doGenerate() throws Exception { List<String> warnings = new ArrayList<String>(); ConfigurationParser cp = new ConfigurationParser(warnings); //Configuration config = cp.parseConfiguration(this.getClass().getClassLoader().getResourceAsStream("generatorConfig_for_oracle(wmsserv).xml")); //String xmlName="generatorConfig.xml"; String xmlName = "generatorConfig_mysql8.xml"; //mysql 8 Configuration config = cp.parseConfiguration(this.getClass().getClassLoader().getResourceAsStream(xmlName)); DefaultShellCallback shellCallback = new DefaultShellCallback(true); try { MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, shellCallback, warnings); myBatisGenerator.generate(null); System.out.println("逆向工程生成数据成功!!!"); } catch (InvalidConfigurationException e) { //失败 } } }
4.效果 (mapper接口的@Repository注解或@Mapper注解可以手动添加一下)
5.GitHub传送门
https://github.com/ColoZhu/myBatisGenerator.git