mybatis是目前很流行的持久层框架,很多企业都在采用。但是其复杂繁琐的配置,重复性的实体类创建等等,消耗了程序员大量的精力,同时有些地方如果一个细小的疏忽,可能导致最终功能运行失败。例如:在几十个字段的表中,某一列的列名配置疏忽。
基于此,mybatis推出了一套jar包,可以依据我们设计好的数据库表,自动生成pojo、mapper以及mapper.xml。有了逆向工程,便大大缩减了我们的开发时间。本章节将介绍借助idea的方式实现mybatis的逆向工程。
1 环境搭建
1.1 配置依赖
创建mybatis_generator工程并导入坐标
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
</build>
1.1.2 添加配置文件
在resources目录下添加名为 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:数据库的 JDBC驱动的jar 包地址 -->
<classPathEntry
location="F:\\HeiMa\\MyFolder\\FrameWork\\MyBatis\\mybatis_generator\\mysql-connector-java-5.1.32.jar" />
<context id="caigouTables" targetRuntime="MyBatis3">
<!-- 配置生成pojo的序列化的插件,mybatis支持很多插件,这些插件都在 org.mybatis.generator.plugins包下 -->
<plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/saas-export" userId="root"
password="root">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- targetProject:生成 POJO 类的位置 -->
<javaModelGenerator targetPackage="cn.itcast.domain.cargo" targetProject="src/main/java">
<!-- enableSubPackages:是否让schema 作为包的后缀 -->
<property name="enableSubPackages" value="true" />
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 生成 XML文件 -->
<sqlMapGenerator targetPackage="cn.itcast.dao.cargo" targetProject="./src/main/resources">
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!--生成接口-->
<javaClientGenerator type="XMLMAPPER"
targetPackage="cn.itcast.dao.cargo" targetProject="./src/main/java">
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 指定数据库表 -->
<table tableName="co_contract" domainObjectName="Contract" mapperName="ContractDao"
enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" enableUpdateByExample="false"/>
<table tableName="co_contract_product" domainObjectName="ContractProduct" mapperName="ContractProductDao"
enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" enableUpdateByExample="false"/>
<table tableName="co_ext_cproduct" domainObjectName="ExtCproduct" mapperName="ExtCproductDao"
enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" enableUpdateByExample="false"/>
<table tableName="co_factory" domainObjectName="Factory" mapperName="FactoryDao"
enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" enableUpdateByExample="false"/>
</context>
</generatorConfiguration>
1.1.3 使用配置的插件生成代码
1.2 逆向工程的使用
通过逆向工程生成的代码如下: