MyBatis - 7.MyBatis逆向 Generator

2023-06-12,,

MyBatis Generator:

简称MBG,是一个专门为MyBatis框架使用者定制的代码生成器,可以快速的根据表生成对应的映射文件,接口,以及bean类。支持基本的增删改查,以及QBC风格的条件查询。但是表连接、存储过程等这些复杂sql的定义需要我们手工编写
官方文档地址

http://www.mybatis.org/generator/
官方工程地址

https://github.com/mybatis/generator/releases

1.mbg配置文件编写

<?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">
<!--如和连接数据库-->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:4040/db_mybatis?serverTimezone = GMT"
userId="root"
password="123">
</jdbcConnection> <javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver> <!--
javaModelGenerator:指定javaBean生成策略
- targetPackage="test.model": 目标包名
- targetProject="\MBGTestProject\src" 目标工程
-->
<javaModelGenerator targetPackage="com.tangge.bean" targetProject=".\src">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator> <!--
sqlMapGenerator: sql映射XML生成策略
- targetPackage="test.xml":
- targetProject="\MBGTestProject\src":
-->
<sqlMapGenerator targetPackage="tangge.mapper" targetProject=".\src">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator> <!--
javaClientGenerator:指定的mapper接口生成策略(Java客户端生成器的属性,Java客户端生成器构建Java接口和类。)
- type="XMLMAPPER":
- XMLMAPPER 生成的对象将是MyBatis 3.x映射器基础结构的Java接口。接口将依赖于生成的XML映射器文件。
- MIXEDMAPPER 接口将基于注释和XML的混合。将使用注释将在简单注释工作的地方使用。
此客户端不会生成和Sql Provider,因此所有复杂的动态SQL都将以XML格式生成。
- ANNOTATEDMAPPER 接口将基于注释和MyBatis 3.x SqlProviders。不会生成XML映射器文件。
- targetPackage="test.dao": 目标包名
- targetProject=".\src": 目标工程
-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.tangge.dao"
targetProject=".\src">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator> <!--<table schema="db_mybatis" tableName="tbl_employee" 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>-->
<!--
table:表生成策略
- tableName(必须):表名
- domainObjectName(选填):生成类的名字
-->
<table schema="db_mybatis" tableName="tbl_employee" domainObjectName="Employee">
</table>
<table schema="db_mybatis" tableName="tbl_dept" domainObjectName="Department">
</table> </context>
</generatorConfiguration>

1.1 如果不想生成Example类文件

table里面就关闭掉 enable** = "false"。

 <table tableName="tbl_employee" domainObjectName="Employee" enableCountByExample="false"
enableDeleteByExample="false" enableSelectByExample="false" enableSelectByPrimaryKey="true"
enableUpdateByPrimaryKey="true" enableDeleteByPrimaryKey="true" selectByExampleQueryId="false"
enableUpdateByExample="false">
</table>
<table tableName="tbl_dept" domainObjectName="Department" enableCountByExample="false"
enableDeleteByExample="false" enableUpdateByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false" enableSelectByPrimaryKey="true" enableUpdateByPrimaryKey="true"
enableDeleteByPrimaryKey="true">
</table>

2.mbg逆向生成

 @Test
public void test()
throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("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);
}

下面看看 生成的mapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.tangge.dao.EmployeeMapper">
<resultMap id="BaseResultMap" type="com.tangge.bean.Employee">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Mon Sep 03 10:51:01 CST 2018.
-->
<id column="id" jdbcType="INTEGER" property="id" />
<result column="last_name" jdbcType="VARCHAR" property="lastName" />
<result column="gender" jdbcType="CHAR" property="gender" />
<result column="email" jdbcType="VARCHAR" property="email" />
<result column="dept_id" jdbcType="INTEGER" property="deptId" />
</resultMap>
<sql id="Base_Column_List">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Mon Sep 03 10:51:01 CST 2018.
-->
id, last_name, gender, email, dept_id
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Mon Sep 03 10:51:01 CST 2018.
-->
select
<include refid="Base_Column_List" />
from tbl_employee
where id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Mon Sep 03 10:51:01 CST 2018.
-->
delete from tbl_employee
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.tangge.bean.Employee">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Mon Sep 03 10:51:01 CST 2018.
-->
insert into tbl_employee (id, last_name, gender,
email, dept_id)
values (#{id,jdbcType=INTEGER}, #{lastName,jdbcType=VARCHAR}, #{gender,jdbcType=CHAR},
#{email,jdbcType=VARCHAR}, #{deptId,jdbcType=INTEGER})
</insert>
<insert id="insertSelective" parameterType="com.tangge.bean.Employee">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Mon Sep 03 10:51:01 CST 2018.
-->
insert into tbl_employee
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="lastName != null">
last_name,
</if>
<if test="gender != null">
gender,
</if>
<if test="email != null">
email,
</if>
<if test="deptId != null">
dept_id,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=INTEGER},
</if>
<if test="lastName != null">
#{lastName,jdbcType=VARCHAR},
</if>
<if test="gender != null">
#{gender,jdbcType=CHAR},
</if>
<if test="email != null">
#{email,jdbcType=VARCHAR},
</if>
<if test="deptId != null">
#{deptId,jdbcType=INTEGER},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.tangge.bean.Employee">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Mon Sep 03 10:51:01 CST 2018.
-->
update tbl_employee
<set>
<if test="lastName != null">
last_name = #{lastName,jdbcType=VARCHAR},
</if>
<if test="gender != null">
gender = #{gender,jdbcType=CHAR},
</if>
<if test="email != null">
email = #{email,jdbcType=VARCHAR},
</if>
<if test="deptId != null">
dept_id = #{deptId,jdbcType=INTEGER},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.tangge.bean.Employee">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Mon Sep 03 10:51:01 CST 2018.
-->
update tbl_employee
set last_name = #{lastName,jdbcType=VARCHAR},
gender = #{gender,jdbcType=CHAR},
email = #{email,jdbcType=VARCHAR},
dept_id = #{deptId,jdbcType=INTEGER}
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>

3.Example的使用

QBC风格的带条件查询

@Test
public void test01(){
SqlSession openSession = build.openSession();
DeptMapper mapper = openSession.getMapper(DeptMapper.class);
DeptExample example = new DeptExample();
//所有的条件都在example中封装
Criteria criteria = example.createCriteria();
//select id, deptName, locAdd from tbl_dept WHERE
//( deptName like ? and id > ? )
criteria.andDeptnameLike("%部%");
criteria.andIdGreaterThan(2);
List<Dept> list = mapper.selectByExample(example);
for (Dept dept : list) {
System.out.println(dept);
}
}

MyBatis - 7.MyBatis逆向 Generator的相关教程结束。

《MyBatis - 7.MyBatis逆向 Generator.doc》

下载本文的Word格式文档,以方便收藏与打印。