一,什么是逆向工程
MyBatis的一个主要的特点就是需要程序员自己编写sql,那么如果表太多的话,难免会很麻烦,所以mybatis官方提供了一个逆向工程,可以针对单表自动生成mybatis执行所需要的代码(包括mapper.xml、mapper.java、po..)。一般在开发中,常用的逆向工程方式是通过数据库的表生成代码。
二,使用逆向工程
使用MyBatis的逆向工程,需要导入逆向工程的jar包,我用的是mybatis-generator-core-1.3.2.jar,下面开始总结一下MyBatis逆向工程的使用步骤。
三,创建项目
1,下载工程jar包
mybatis-generator-core.jar-MySQL工具类资源-CSDN下载
我们要新建一个java工程,这个工程专门用来使用逆向工程生成代码的。有些人可能会问,为什么要新建一个工程呢?直接在原来工程中你想生成不就可以了么?确实是这样,可以在原来的工程中生成,但是有风险,因为MyBatis是根据配置文件来生成的(下面会说到),如果生成的路径中有相同的文件,那么就会覆盖原来的文件,这样会有风险。所以开发中一般都会新建一个java工程来生成,然后将生成的文件拷贝到自己的工程中,这也不麻烦,而且很安全。如下
四,配置逆向工程的配置文件
MyBatis逆向工程生成代码需要一个配置文件,名字随便起。然后MyBatis会根据这 个配置文件中的配置,生成相应的代码。mybatis-generator-core-1.3.2.jar这个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>
<!-- 数据库驱动 -->
<classPathEntry location="lib/mysql-connector-java-5.1.20-bin.jar" />
<context id="testTables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true" />
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--数据库链接URL,用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost/ine" userId="root" password="123456">
</jdbcConnection>
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
NUMERIC 类型解析为java.math.BigDecimal 主要是针对oracle数据库-->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 生成模型的包名和位置 -->
<javaModelGenerator targetPackage="com.sxt.domain"
targetProject="src">
<property name="enableSubPackages" value="true" />
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 生成映射文件的包名和位置 -->
<sqlMapGenerator targetPackage="com.sxt.mapping"
targetProject="src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 生成DAO的包名和位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.sxt.dao" targetProject="src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名 -->
<table tableName="dept" domainObjectName="Dept"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false"></table>
<table tableName="emp" domainObjectName="Emp"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>
从上面的配置文件中可以看出,配置文件主要做的几件事是:
1,连接数据库,这是必须的,要不然怎么根据数据库的表生成代码呢?
2,指定要生成代码的位置,要生成的代码包括po类,mapper.xml和mapper.java
3,指定数据库中想要生成哪些表
五,执行逆向工程生成代码
配置文件搞好了,然后就执行以下程序即可生成代码了,生成的java程序,下载的逆向工程文档中都有示例,如下:
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();
}
}
}
运行一下即可,运行完了后刷新一下工程,就可以看到最新生成的代码了。
这里写图片描述
这里可以看出有个细节,每个po类多了一个东西,就是xxxExample.java,这个类是给用户自定义sql使用的,后面我会提到。到这里就生成好了,下面我们就把生成的代码拷贝到自己的工程使用了。