逆向工程:通过数据库中已存在数据表,反向生成java中的实体类(生成对应的ORM持久层代码)
1.创建项目
代码生成器是项目的一个工具组件。
创建不使用模板的maven项目。
引入逆向工程的依赖。
- mysql-connector-java
- mybatis-generator-core
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.17</version> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.4.0</version> </dependency>
2.配置生成规则
描述数据库中的表,生成对应的Java类和映射配置文件(即pojo和mapper)。生成规则是一个普通的配置文件。
在maven项目的resources目录下,创建配置文件:generator.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> <!--在windows中,实现db2java需要依赖组件, 通常情况下,该组件在IBM的SQLLIB里面的, mysql是不需要的 --> <!--<classPathEntry location="/Program Files/IBM/SQLLIB/java/db2java.zip" />--> <!-- targetRuntime:目标mybatis版本 --> <context id="generateTables" targetRuntime="MyBatis3"> <!-- 序列化 --> <!-- <plugin type="org.mybatis.generator.plugins.SerializablePlugin" />--> <!-- 关于生成代码的注释配置 --> <commentGenerator> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <property name="suppressAllComments" value="true"/> <property name="suppressDate" value="true"/> </commentGenerator> <!--数据库连接的信息 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/web_emp?serverTimezone=PRC" userId="root" password="root"> <!-- 防止生成其他数据库下重名表 --> <property name="nullCatalogMeansCurrent" value="true"/> </jdbcConnection> <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer, 为 true时把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal --> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- targetProject:生成POJO类的位置 --> <javaModelGenerator targetPackage="com.chao.entity" targetProject=".\src\main\java"> <!-- 生成无参和全参的构造函数,并且resultMap中会根据构造函数注入 --> <!--<property name="constructorBased" value="true"/>--> <!-- enableSubPackages:是否让schema作为包的后缀,默认值false --> <!-- <property name="enableSubPackages" value="false"/>--> <!-- 从数据库返回的值被清理前后的空格 --> <property name="trimStrings" value="true"/> <!-- 不生成setter方法,生成全参构造器 --> <!--<property name="immutable" value="true"/>--> <!-- 为所有实体类集成统一的一个父类 --> <!--<property name="rootClass" value="com.neutech.entity.A"/>--> </javaModelGenerator> <!-- mapper的xml的配置 --> <sqlMapGenerator targetPackage="com.chao.mapper" targetProject=".\src\main\resources"> <!-- 与实体类处一个意思 --> <!--<property name="enableSubPackages" value="true"/>--> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false"/> </sqlMapGenerator> <!-- targetPackage:mapper接口生成的位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.chao.mapper" targetProject=".\src\main\java"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false"/> </javaClientGenerator> <!-- 指定数据库表 保留Example例子 --> <!-- tableName:数据库中的表名 domainObjectName:生成的实体类类名 --> <table tableName="we_user" domainObjectName="User" enableSelectByExample="false" enableCountByExample="false" enableDeleteByExample="false" enableUpdateByExample="false"></table> <!--<table schema="" tableName="数据库名" domainObjectName="实体类名"/>--> </context> </generatorConfiguration>
3.逆向工程主方法
创建代码生成器:代码生成器
import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.config.Configuration; import org.mybatis.generator.config.xml.ConfigurationParser; import org.mybatis.generator.internal.DefaultShellCallback; import java.util.ArrayList; import java.util.List; public class CodeGenerator { public static void main(String[] args) throws Exception{ List<String> warnings = new ArrayList<String>(); boolean overwrite = true; ConfigurationParser cp = new ConfigurationParser(warnings); //通过类加载器来加载配置文件,默认在maven项目的resources/ Configuration config = cp.parseConfiguration(CodeGenerator.class.getClassLoader().getResourceAsStream("generator.xml")); DefaultShellCallback callback = new DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); myBatisGenerator.generate(null); } }
4. 出现问题
4.1 数据库中 tinyint 自动转化为Boolean类型
让tinyint转化为 Integer类型,需要在拥有该字段的表中加上以下属性配置
<columnOverride column="status" javaType="Integer"/>
4.2 数据库中 text 直接创建了新类,继承原来类
让text转化为String类型,需要在拥有该字段的表中加上以下属性配置
<columnOverride column="sub_images" jdbcType="VARCHAR"/>
<columnOverride column="detail" jdbcType="VARCHAR"/>
整表浏览:
<table tableName="s_product" domainObjectName="Product" enableSelectByExample="false"
enableCountByExample="false" enableDeleteByExample="false" enableUpdateByExample="false">
<columnOverride column="sub_images" jdbcType="VARCHAR"/>
<columnOverride column="detail" jdbcType="VARCHAR"/>
</table>