MyBatis - 7.MyBatis逆向 Generator
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 注解开发+逆向(Generator)
注解开发 最初设计时,MyBatis 是一个 XML 驱动的框架.配置信息是基于 XML 的,而且映射语句也是定义在 XML 中的.随着技术的更新发展,对于开发效率要求也原来越高,特别是一些小型项目; ...
- Hello Mybatis 02 mybatis generator
接着上一篇文章通过Mybatis完成了一个User的CRUD的功能之后,这篇开始还需要建立一个Blog类,这样就可以模拟一个简单的微博平台的数据库了. 数据库准备 首先我们,还是需要在数据库中新建一个 ...
- springboot集成mybatis及mybatis generator工具使用
原文链接 前言mybatis是一个半自动化的orm框架,所谓半自动化就是mybaitis只支持数据库查出的数据映射到pojo类上,而实体到数据库的映射需要自己编写sql语句实现,相较于hibernat ...
- springboot入门(三)-- springboot集成mybatis及mybatis generator工具使用
前言 mybatis是一个半自动化的orm框架,所谓半自动化就是mybaitis只支持数据库查出的数据映射到pojo类上,而实体到数据库的映射需要自己编写sql语句实现,相较于hibernate这种完 ...
- spring boot集成mybatis(3) - mybatis generator 配置
Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...
- 【Mybatis】MyBatis之动态SQL(六)
MyBatis 的强大特性之一便是它的动态 SQL,本章介绍动态 SQL 查看本章,请先阅读[Mybatis]MyBatis对表执行CRUD操作(三). 本例表结构 CREATE TABLE `emp ...
- 【Mybatis】MyBatis之Sql配置文件的使用(四)
上一章[Mybatis]MyBatis对表执行CRUD操作(三),已经讲了基本操作,本章介绍Sql配置文件中常用功能 1.插入返回主键 2.参数值的获取方式 3.resultMap使用 插入返回主键 ...
- MyBatis笔记----MyBatis 入门经典的两个例子: XML 定义与注解定义
----致敬MyBatis官方开放文档让大家翻译,不用看书直接看文档就行了,mybatis的中文文档还需要完备的地方 简介 什么是 MyBatis ? MyBatis 是支持定制化 SQL.存储过程以 ...
- 【Mybatis】MyBatis对表执行CRUD操作(三)
本例在[Mybatis]MyBatis配置文件的使用(二)基础上继续学习对表执行CRUD操作 使用MyBatis对表执行CRUD操作 1.定义sql映射xml文件(EmployeeMapper.xml ...
- 【Mybatis】MyBatis配置文件的使用(二)
本例在[Mybatis]MyBatis快速入门(一)基础上继续学习XML映射配置文件 MyBatis 的配置文件包含了会深深影响 MyBatis 行为的设置(settings)和属性(properti ...
随机推荐
- MII、RMII、GMII接口的详细介绍【转】
转自:https://www.cnblogs.com/geekite/p/5204512.html 概述: MII (Media Independent Interface(介质无关接口)或称为媒体独 ...
- Java文件复制
主要是工作代码,无解释. /** * 将文件或文件夹source复制到dest * <br>目标文件检测: * <br> a.当文件不存在时:需要创建文件 * <br&g ...
- Vue 介绍,优点,实例
一 认识vue 1.什么是vue 以数据驱动的web渐进式框架 三大主流框架: Angular React Vue 设计模式:MVVM 2.vue优点 - 以数据驱动,不直接操作DOM,效率高- 单页 ...
- 从MySQL中导入数据到MongoDB中
从sql中导出需要的数据为csv格式的数据 select field1,field2,...,fieldn from TABLE into outfile '/test.csv' fields ter ...
- mybatis:延迟加载时不要在get/set方法上面添加final关键字(原创)
1.mybatis-config.xml: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ...
- es2015箭头函数的this
摘自https://www.cnblogs.com/chenxygx/p/6509564.html,谢谢博主的分享!
- django配置发送邮箱
该邮箱配置后台发送邮箱验证使用 settings内配置 # 服务器地址 EMAIL_HOST = 'smtp.163.com' # 端口,邮箱默认动态端口 25 EMAIL_PORT = 25 # 邮 ...
- Netty学习4—NIO服务端报错:远程主机强迫关闭了一个现有的连接
1 发现问题 NIO编程中服务端会出现报错 Exception in thread "main" java.io.IOException: 远程主机强迫关闭了一个现有的连接. at ...
- Confluence 6 管理协同编辑
协同编辑能够让项目小组中的协同合作达到下一个高度.这个页面对相关协同编辑中的问题进行了讨论,能够提供给你所有希望了解的内容. 进入 Collaborative editing 页面来获得项目小组是如何 ...
- web的分页方法
web分页的三种方式,闲来无事总结一下. 1.使用前端表格插件进行分页 例如用bootstrap的拓展table组件,注意设置其分页属性时设置为"client", 即是 sideP ...