mybatis-8-逆向工程
MyBatis Generator简称MBG
流程:
导入依赖:
<!--MBG逆向工程-->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
再resource下创建MBG配置文件mbg.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>
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<!-- 是否去除自动生成的注释 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!-- Mysql数据库连接的信息:驱动类、连接地址、用户名、密码 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mybatis"
userId="root"
password="123456">
</jdbcConnection>
<!-- Oracle数据库
<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:生成POJO类的位置 -->
<javaModelGenerator targetPackage="com.wang.entity" targetProject=".\src\main\java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- targetProject:mapper映射文件生成的位置 -->
<sqlMapGenerator targetPackage="com.wang.dao.xml" targetProject=".\src\main\java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- targetProject:mapper接口生成的的位置 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.wang.dao" targetProject=".\src\main\java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 指定数据表 -->
<table schema="" tableName="tbl_employee" domainObjectName="Employee"></table>
<table schema="" tableName="tbl-department" domaninObjectName="Department"></table>
<!-- <table schema="" tableName="tb_content_category"></table>-->
<!-- <table schema="" tableName="tb_item"></table>-->
<!-- <table schema="" tableName="tb_item_cat"></table>-->
<!-- <table schema="" tableName="tb_item_desc"></table>-->
<!-- <table schema="" tableName="tb_item_param"></table>-->
<!-- <table schema="" tableName="tb_item_param_item"></table>-->
<!-- <table schema="" tableName="tb_order"></table>-->
<!-- <table schema="" tableName="tb_order_item"></table>-->
<!-- <table schema="" tableName="tb_order_shipping"></table>-->
<!-- <table schema="" tableName="tb_user"></table>-->
<!-- 有些表的字段需要指定java类型
<table schema="DB2ADMIN" tableName="ALLTYPES" domainObjectName="Customer" >
<property name="useActualColumnNames" value="true"/>
<generatedKey column="ID" sqlStatement="DB2" identity="true" />
<columnOverride column="DATE_FIELD" property="startDate" />
<ignoreColumn column="FRED" />
<columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />
</table> -->
</context>
</generatorConfiguration>
生成的代码
@org.junit.Test
public void test1(){
try{
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
// 指定配置文件,这里最好是使用绝对路径
File configFile = new File("D:\\Coding\\IntellijProjects\\mybatis-MBG\\src\\main\\resources\\mbg.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);
}catch (Exception e){
e.printStackTrace();
}
}
补充:
自动生成的代码是不会包含有关联查询的,所以需要手动补充
如:
<!--=================补充:带部门的查询=====================-->
<resultMap id="withDepartmentResultMap" type="employee">
<id column="employee_id" property="employeeId"/>
<result column="employee_name" property="employeeName"/>
<result column="gender" property="gender"/>
<result column="email" property="email"/>
<result column="department_id" property="departmentId"/>
<!--分步查询-->
<!--调用departmentmapper中的selectByPrimaryKey来级联合成-->
<association property="department"
select="com.wang.dao.DepartmentMapper.selectByPrimaryKey"
column="department_id"/>
</resultMap>
<sql id="withDepartment_Column_List">
e.employee_id,e.employee_name,e.gender,e.email,e.department_id,d.department_name
</sql>
<!-- List<Employee> selectByExampleWithDepartment(EmployeeExample example);-->
<select id="selectByExampleWithDepartment" parameterType="com.wang.pojo.EmployeeExample">
select
<if test="distinct">
distinct
</if>
<include refid="withDepartment_Column_List" />
FROM tbl_employee e
LEFT JOIN tbl_department d
ON e.`department_id` = d.`department_id`
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
<if test="orderByClause != null">
order by ${orderByClause}
</if>
</select>
<!-- Employee selectByPrimaryKeyWithDepartment(Integer employeeId);-->
<select id="selectByPrimaryKeyWithDepartment" parameterType="com.wang.pojo.EmployeeExample">
select
<include refid="withDepartment_Column_List" />
FROM tbl_employee e
LEFT JOIN tbl_department d
ON e.`department_id` = d.`department_id`
where employee_id = #{employeeId,jdbcType=INTEGER}
</select>
常出现的问题一般的mapper.xml文件出问题
这个时候一般是mapper.xml文件中出现了重复的部分,删除掉即可