1. 什么是逆向工程
mybatis的一个主要的特点就是需要程序员自己编写sql,那么如果表太多的话,难免会很麻烦,所以mybatis官方提供了一个逆向工程,可以针对单表自动生成mybatis执行所需要的代码(包括mapper.xml、mapper.java、be..)。一般在开发中,常用的逆向工程方式是通过数据库的表生成代码。
2. 使用逆向工程
使用mybatis的逆向工程,需要导入逆向工程的jar包,我用的是mybatis-generator-core-1.3.2,我们可以直接在maven中央仓库拿到,下面开始总结一下mybatis逆向工程的使用步骤。
2.1 新建一个工程(重要)我们要新建一个web工程,这个工程专门用来使用逆向工程生成代码的。有些人可能会问,为什么要新建一个工程呢?直接在原来工程中你想生成不就可以了么?确实是这样,可以在原来的工程中生成,但是有风险,因为mybatis是根据配置文件来生成的(下面会说到),如果生成的路径中有相同的文件,那么就会覆盖原来的文件,这样会有风险。所以开发中一般都会新建一个java工程来生成,然后将生成的文件拷贝到自己的工程中,这也不麻烦,而且很安全。
2.1 生成代码的配置文件
mybatis逆向工程生成代码需要一个配置文件,名字随便起。然后mybatis会根据这个配置文件中的配置,生成相应的代码。下载好了jar包后,里面有帮助文档,打开后里面有配置文件的模板,这里就不再赘述了,下面先把配置文件写好:
<?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> <context id="testTables" targetRuntime="MyBatis3"> <commentGenerator> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <property name="suppressAllComments" value="true" /> </commentGenerator> <!--数据库连接的信息:驱动类、连接地址、用户名、密码 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/car_cms" userId="root" password=" "> </jdbcConnection> <!-- <jdbcConnection driverClass="oracle.jdbc.OracleDriver" connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:yycg" userId="yycg" password="yycg"> </jdbcConnection> --> <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal --> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- targetProject:生成PO类的位置 --> <javaModelGenerator targetPackage="cn.gxxy.ssm.po" targetProject=".\src"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false" /> <!-- 从数据库返回的值被清理前后的空格 --> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- targetProject:mapper映射文件生成的位置 --> <sqlMapGenerator targetPackage="cn.gxxy.ssm.mapper" targetProject=".\src"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false" /> </sqlMapGenerator> <!-- targetPackage:mapper接口生成的位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="cn.gxxy.ssm.mapper" targetProject=".\src"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false" /> </javaClientGenerator> <!-- 指定数据库表 --> <table tableName="employee" domainObjectName="Employee"></table> <table tableName="member" domainObjectName="Member"></table> <table tableName="appointment" domainObjectName="Appointment"></table> <table tableName="performance" domainObjectName="Performance"></table> <table tableName="orders" domainObjectName="Orders"></table> <table tableName="card" domainObjectName="Card"></table> <table tableName="car_type" domainObjectName="Car_Type"></table> <table tableName="serviceT" domainObjectName="ServiceT"></table> <table tableName="service_type" domainObjectName="Service_Type"></table> <table tableName="after_sale" domainObjectName="After_Sale"></table> <table tableName="goods" domainObjectName="Goods"></table> <table tableName="goods_type" domainObjectName="Goods_Type"></table> <table tableName="repertory" domainObjectName="Repertory"></table> <table tableName="expend" domainObjectName="Expend"></table> <table tableName="comment" domainObjectName="Comment"></table> <table tableName="users" domainObjectName="Users"></table> <!-- <table schema="" tableName="sys_user"></table> <table schema="" tableName="sys_role"></table> <table schema="" tableName="sys_permission"></table> <table schema="" tableName="sys_user_role"></table> <table schema="" tableName="sys_role_permission"></table> --> <!-- 有些表的字段需要指定java类型 <table schema="" tableName=""> <columnOverride column="" javaType="" /> </table> --> </context> </generatorConfiguration>2.3 编写测试类
配置文件搞好了,我们就开始编写测试类用于执行mybatis逆向工程:
import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.config.Configuration; import org.mybatis.generator.config.xml.ConfigurationParser; import org.mybatis.generator.exception.XMLParserException; import org.mybatis.generator.internal.DefaultShellCallback; public class GeneratorSqlmap { public void generator() throws Exception{ List<String> warnings = new ArrayList<String>(); boolean overwrite = true; //指定 逆向工程配置文件 File configFile = new File("generatorConfig.xml"); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(configFile); DefaultShellCallback callback = new DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); myBatisGenerator.generate(null); } public static void main(String[] args) throws Exception { try { GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap(); generatorSqlmap.generator(); } catch (Exception e) { e.printStackTrace(); } } }
2.4 执行测试类
当我们编写完测试类后我们便可以执行它用于生成我们所需的自动生成mybatis执行所需要的代码(包括mapper.xml、mapper.java、be..)
等