一、MyBatis Generator:

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

官方工程给了maven依赖,以及jar下载

1.2 创建工程Mybatis_mbg(这里使用的是传统的java工程,没有使用maven创建),会生成一个src文件夹。

jdk包会引入,同时创建lib文件夹,存放jar包。

建立conf文件夹,用于放配置文件。

需要使用的jar包

1.3 创建项目需要的数据表tbl_employee和tbl_dept:(先创建数据库mybatis)

create table tbl_employee(
id int primary key auto_increment,
last_name varchar(64) default null,
email varchar(60) default null,
gender varchar(6) default null,
d_id int
)engine=innodb,default charset=utf8;create table tbl_dept(
id int primary key auto_increment,
dept_name varchar(64) default null
)engine=innodb,default charset=utf8;alter table tbl_employee add constraint FK2tbl_dept foreign key (d_id) references tbl_dept(id) on delete no action on update no action;

创建好之后的数据表:

1.3创建工程配置文件mbg.xml(该文件放在根目录下,不在conf文件夹下)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfigurationPUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN""http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration><!-- targetRuntime="MyBatis3Simple":生成简单版的CRUDMyBatis3:豪华版--><context id="DB2Tables" targetRuntime="MyBatis3"><!-- jdbcConnection:指定如何连接到目标数据库 --><jdbcConnection driverClass="com.mysql.jdbc.Driver"connectionURL="jdbc:mysql://localhost:3306/mybatis?allowMultiQueries=true"userId="root"password="123456"></jdbcConnection><javaTypeResolver ><!--property标签,可以配置的属性为forceBigDecimals,该属性可以控制是否强制将 DECIMAL 和 NUMERIC 类型的 JDBC字段转换为 Java 类型的java.math.BigDecimal,默认值为false  --><property name="forceBigDecimals" value="false" /></javaTypeResolver><!-- javaModelGenerator:指定javaBean的生成策略 targetPackage="test.model":目标包名targetProject="\MBGTestProject\src":目标工程--><javaModelGenerator targetPackage="com.xiaomifeng1010.mybatis.bean" targetProject=".\src"><property name="enableSubPackages" value="true" /><property name="trimStrings" value="true" /></javaModelGenerator><!-- sqlMapGenerator:sql映射生成策略: --><sqlMapGenerator targetPackage="com.xiaomifeng1010.mybatis.dao"  targetProject=".\conf"><property name="enableSubPackages" value="true" /></sqlMapGenerator><!-- javaClientGenerator:指定mapper接口所在的位置 --><javaClientGenerator type="XMLMAPPER" targetPackage="com.xiaomifeng1010.mybatis.dao"  targetProject=".\src"><property name="enableSubPackages" value="true" /></javaClientGenerator><!-- 指定要逆向分析哪些表:根据表要创建javaBean --><table tableName="tbl_dept" domainObjectName="Department"></table><table tableName="tbl_employee" domainObjectName="Employee"></table></context>
</generatorConfiguration>

说明:

1)jdbcConnection配置数据库连接信息
2)javaModelGenerator配置javaBean的生成策略
3)sqlMapGenerator配置sql映射文件生成策略
4)javaClientGenerator配置Mapper接口的生成策略
5)table配置要逆向解析的数据表
tableName:表名
domainObjectName:对应的javaBean名

注意:
Context标签
targetRuntime=“MyBatis3“可以生成带条件的增删改查
targetRuntime=“MyBatis3Simple“可以生成基本的增删改查
如果再次生成,建议将之前生成的数据删除,避免xml向后追加内容出现的问题。

1.4在conf文件夹下创建配置文件mybatis-config.xml,配置mybatis,创建log4j.xml配置日志,ehcache.xml配置缓存,dbconfig.properties配置数据库连接属性

1.4.1 dbconfig.properties(这里配置了mysql和Oracle数据源)

jdbc.driver=com.mysql.jdbc.Driver
#allowMultiQueries=true开启批量执行sql
jdbc.url=jdbc:mysql://localhost:3306/mybatis?allowMultiQueries=true
jdbc.username=root
jdbc.password=123456#如果是高版本的mysql需要对jdbc.url做一些额外设置
#jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&allowMultiQueries=true#使用Oracle数据库时的配置
orcl.driver=oracle.jdbc.OracleDriver
orcl.url=jdbc:oracle:thin:@localhost:1521:XE
orcl.username=system
orcl.password=oracle

1.4.2 mybatais-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><!--1、mybatis可以使用properties来引入外部properties配置文件的内容;resource:引入类路径下的资源url:引入网络路径或者磁盘路径下的资源--><properties resource="dbconfig.properties"></properties><!-- 2、settings包含很多重要的设置项setting:用来设置每一个设置项name:设置项名value:设置项取值--><settings><setting name="mapUnderscoreToCamelCase" value="true"/><setting name="jdbcTypeForNull" value="NULL"/><!--显式的指定每个我们需要更改的配置的值,即使他是默认的。防止版本更新带来的问题  --><setting name="cacheEnabled" value="true"/><setting name="lazyLoadingEnabled" value="true"/><setting name="aggressiveLazyLoading" value="false"/></settings><!-- 3、typeAliases:别名处理器:可以为我们的java类型起别名 别名不区分大小写--><typeAliases><!-- 1、typeAlias:为某个java类型起别名type:指定要起别名的类型全类名;默认别名就是类名小写;employeealias:指定新的别名--><!-- <typeAlias type="com.xiaomifeng1010.mybatis.bean.Employee" alias="emp"/> --><!-- 2、package:为某个包下的所有类批量起别名 name:指定包名(为当前包以及下面所有的后代包的每一个类都起一个默认别名(类名小写),使用别名时不再区分大小写)--><package name="com.xiaomifeng1010.mybatis.bean"/><!-- 3、批量起别名的情况下,使用@Alias注解为某个类型指定新的别名 --></typeAliases><!-- 4、environments:环境们,mybatis可以配置多种环境 ,default指定使用某种环境。可以达到快速切换环境。environment:配置一个具体的环境信息;必须有两个标签;id代表当前环境的唯一标识transactionManager:事务管理器;type:事务管理器的类型;JDBC(JdbcTransactionFactory)|MANAGED(ManagedTransactionFactory)自定义事务管理器:实现TransactionFactory接口.type指定为全类名dataSource:数据源;type:数据源类型;UNPOOLED(UnpooledDataSourceFactory)|POOLED(PooledDataSourceFactory)|JNDI(JndiDataSourceFactory)自定义数据源:实现DataSourceFactory接口,type是全类名--><environments default="dev_mysql"><environment id="dev_mysql"><transactionManager type="JDBC"></transactionManager><dataSource type="POOLED"><property name="driver" value="${jdbc.driver}" /><property name="url" value="${jdbc.url}" /><property name="username" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /></dataSource></environment><environment id="dev_oracle"><transactionManager type="JDBC" /><dataSource type="POOLED"><property name="driver" value="${orcl.driver}" /><property name="url" value="${orcl.url}" /><property name="username" value="${orcl.username}" /><property name="password" value="${orcl.password}" /></dataSource></environment></environments><!-- 5、databaseIdProvider:支持多数据库厂商的;type="DB_VENDOR":VendorDatabaseIdProvider作用就是得到数据库厂商的标识(驱动getDatabaseProductName()),mybatis就能根据数据库厂商标识来执行不同的sql;MySQL,Oracle,SQL Server,xxxx--><databaseIdProvider type="DB_VENDOR"><!-- 为不同的数据库厂商起别名 --><property name="MySQL" value="mysql"/><property name="Oracle" value="oracle"/><property name="SQL Server" value="sqlserver"/></databaseIdProvider><!-- 将我们写好的sql映射文件(EmployeeMapper.xml)一定要注册到全局配置文件(mybatis-config.xml)中 --><!-- 6、mappers:将sql映射注册到全局配置中 --><mappers><!-- mapper:注册一个sql映射 注册配置文件resource:引用类路径下的sql映射文件mybatis/mapper/EmployeeMapper.xmlurl:引用网路路径或者磁盘路径下的sql映射文件file:///var/mappers/AuthorMapper.xml注册接口class:引用(注册)接口,1、有sql映射文件,映射文件名必须和接口同名,并且放在与接口同一目录下;2、没有sql映射文件,所有的sql都是利用注解写在接口上;推荐:比较重要的,复杂的Dao接口我们来写sql映射文件不重要,简单的Dao接口为了开发快速可以使用注解;--><!-- <mapper resource="mybatis/mapper/EmployeeMapper.xml"/> --><!-- <mapper class="com.xiaomifeng1010.mybatis.dao.EmployeeMapperAnnotation"/> --><!-- 批量注册: --><package name="com.xiaomifeng1010.mybatis.dao"/></mappers>
</configuration>

1.4.3 log4j.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"><log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"><appender name="STDOUT" class="org.apache.log4j.ConsoleAppender"><param name="Encoding" value="UTF-8" /><layout class="org.apache.log4j.PatternLayout"><param name="ConversionPattern" value="%-5p %d{MM-dd HH:mm:ss,SSS} %m  (%F:%L) \n" /></layout></appender><logger name="java.sql"><level value="debug" /></logger><logger name="org.apache.ibatis"><level value="info" /></logger><root><level value="debug" /><appender-ref ref="STDOUT" /></root>
</log4j:configuration>

 1.4.4 ehcache.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"><log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"><appender name="STDOUT" class="org.apache.log4j.ConsoleAppender"><param name="Encoding" value="UTF-8" /><layout class="org.apache.log4j.PatternLayout"><param name="ConversionPattern" value="%-5p %d{MM-dd HH:mm:ss,SSS} %m  (%F:%L) \n" /></layout></appender><logger name="java.sql"><level value="debug" /></logger><logger name="org.apache.ibatis"><level value="info" /></logger><root><level value="debug" /><appender-ref ref="STDOUT" /></root>
</log4j:configuration>

1.5 在src文件夹下创建包

com.xiaomifeng1010.mybatis.bean和com.xiaomifeng1010.dao以及com.xiaomifeng1010.test

同时在conf文件下创建com.xiaomifeng1010.mybatis.dao用于存放生成器生成的mapper映射xml文件。

在test包下创建测试类和测试方法用于测试生成器。

package com.xiaomifeng1010.mybatis.test;import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;public class MyBatisTest {public SqlSessionFactory getSqlSessionFactory() throws IOException {String resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);return new SqlSessionFactoryBuilder().build(inputStream);}@Testpublic void testMbg() throws Exception {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);}
}

1.6 此时项目的结构:

此时,bean包下边是空的,没有实体类,dao包下没有对应的mapper接口,conf文件下的dao包下也是空的,没有对应mapper的映射xml文件

1.7 执行测试类中的测试方法:

1.8 此时刷新项目:

发现bean包下产生Employee和Department实体类,每个实体类还多产生了对应的example类。同时产生了对应的dao接口,和dao的配置xml文件

1.9查看生成的类:

1.9.1 Employee类和Department类

package com.xiaomifeng1010.mybatis.bean;public class Employee {/*** This field was generated by MyBatis Generator.* This field corresponds to the database column tbl_employee.id** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/private Integer id;/*** This field was generated by MyBatis Generator.* This field corresponds to the database column tbl_employee.last_name** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/private String lastName;/*** This field was generated by MyBatis Generator.* This field corresponds to the database column tbl_employee.email** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/private String email;/*** This field was generated by MyBatis Generator.* This field corresponds to the database column tbl_employee.gender** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/private String gender;/*** This field was generated by MyBatis Generator.* This field corresponds to the database column tbl_employee.d_id** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/private Integer dId;/*** This method was generated by MyBatis Generator.* This method returns the value of the database column tbl_employee.id** @return the value of tbl_employee.id** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/public Integer getId() {return id;}/*** This method was generated by MyBatis Generator.* This method sets the value of the database column tbl_employee.id** @param id the value for tbl_employee.id** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/public void setId(Integer id) {this.id = id;}/*** This method was generated by MyBatis Generator.* This method returns the value of the database column tbl_employee.last_name** @return the value of tbl_employee.last_name** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/public String getLastName() {return lastName;}/*** This method was generated by MyBatis Generator.* This method sets the value of the database column tbl_employee.last_name** @param lastName the value for tbl_employee.last_name** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/public void setLastName(String lastName) {this.lastName = lastName == null ? null : lastName.trim();}/*** This method was generated by MyBatis Generator.* This method returns the value of the database column tbl_employee.email** @return the value of tbl_employee.email** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/public String getEmail() {return email;}/*** This method was generated by MyBatis Generator.* This method sets the value of the database column tbl_employee.email** @param email the value for tbl_employee.email** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/public void setEmail(String email) {this.email = email == null ? null : email.trim();}/*** This method was generated by MyBatis Generator.* This method returns the value of the database column tbl_employee.gender** @return the value of tbl_employee.gender** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/public String getGender() {return gender;}/*** This method was generated by MyBatis Generator.* This method sets the value of the database column tbl_employee.gender** @param gender the value for tbl_employee.gender** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/public void setGender(String gender) {this.gender = gender == null ? null : gender.trim();}/*** This method was generated by MyBatis Generator.* This method returns the value of the database column tbl_employee.d_id** @return the value of tbl_employee.d_id** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/public Integer getdId() {return dId;}/*** This method was generated by MyBatis Generator.* This method sets the value of the database column tbl_employee.d_id** @param dId the value for tbl_employee.d_id** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/public void setdId(Integer dId) {this.dId = dId;}
}
package com.xiaomifeng1010.mybatis.bean;public class Department {/*** This field was generated by MyBatis Generator.* This field corresponds to the database column tbl_dept.id** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/private Integer id;/*** This field was generated by MyBatis Generator.* This field corresponds to the database column tbl_dept.dept_name** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/private String deptName;/*** This method was generated by MyBatis Generator.* This method returns the value of the database column tbl_dept.id** @return the value of tbl_dept.id** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/public Integer getId() {return id;}/*** This method was generated by MyBatis Generator.* This method sets the value of the database column tbl_dept.id** @param id the value for tbl_dept.id** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/public void setId(Integer id) {this.id = id;}/*** This method was generated by MyBatis Generator.* This method returns the value of the database column tbl_dept.dept_name** @return the value of tbl_dept.dept_name** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/public String getDeptName() {return deptName;}/*** This method was generated by MyBatis Generator.* This method sets the value of the database column tbl_dept.dept_name** @param deptName the value for tbl_dept.dept_name** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/public void setDeptName(String deptName) {this.deptName = deptName == null ? null : deptName.trim();}
}

可以看到生成的类中的方法中都有生成的说明信息。

1.9.2 查看生成的example类

package com.xiaomifeng1010.mybatis.bean;import java.util.ArrayList;
import java.util.List;public class EmployeeExample {/*** This field was generated by MyBatis Generator.* This field corresponds to the database table tbl_employee** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/protected String orderByClause;/*** This field was generated by MyBatis Generator.* This field corresponds to the database table tbl_employee** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/protected boolean distinct;/*** This field was generated by MyBatis Generator.* This field corresponds to the database table tbl_employee** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/protected List<Criteria> oredCriteria;/*** This method was generated by MyBatis Generator.* This method corresponds to the database table tbl_employee** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/public EmployeeExample() {oredCriteria = new ArrayList<Criteria>();}/*** This method was generated by MyBatis Generator.* This method corresponds to the database table tbl_employee** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/public void setOrderByClause(String orderByClause) {this.orderByClause = orderByClause;}/*** This method was generated by MyBatis Generator.* This method corresponds to the database table tbl_employee** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/public String getOrderByClause() {return orderByClause;}/*** This method was generated by MyBatis Generator.* This method corresponds to the database table tbl_employee** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/public void setDistinct(boolean distinct) {this.distinct = distinct;}/*** This method was generated by MyBatis Generator.* This method corresponds to the database table tbl_employee** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/public boolean isDistinct() {return distinct;}/*** This method was generated by MyBatis Generator.* This method corresponds to the database table tbl_employee** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/public List<Criteria> getOredCriteria() {return oredCriteria;}/*** This method was generated by MyBatis Generator.* This method corresponds to the database table tbl_employee** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/public void or(Criteria criteria) {oredCriteria.add(criteria);}/*** This method was generated by MyBatis Generator.* This method corresponds to the database table tbl_employee** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/public Criteria or() {Criteria criteria = createCriteriaInternal();oredCriteria.add(criteria);return criteria;}/*** This method was generated by MyBatis Generator.* This method corresponds to the database table tbl_employee** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/public Criteria createCriteria() {Criteria criteria = createCriteriaInternal();if (oredCriteria.size() == 0) {oredCriteria.add(criteria);}return criteria;}/*** This method was generated by MyBatis Generator.* This method corresponds to the database table tbl_employee** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/protected Criteria createCriteriaInternal() {Criteria criteria = new Criteria();return criteria;}/*** This method was generated by MyBatis Generator.* This method corresponds to the database table tbl_employee** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/public void clear() {oredCriteria.clear();orderByClause = null;distinct = false;}/*** This class was generated by MyBatis Generator.* This class corresponds to the database table tbl_employee** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/protected abstract static class GeneratedCriteria {protected List<Criterion> criteria;protected GeneratedCriteria() {super();criteria = new ArrayList<Criterion>();}public boolean isValid() {return criteria.size() > 0;}public List<Criterion> getAllCriteria() {return criteria;}public List<Criterion> getCriteria() {return criteria;}protected void addCriterion(String condition) {if (condition == null) {throw new RuntimeException("Value for condition cannot be null");}criteria.add(new Criterion(condition));}protected void addCriterion(String condition, Object value, String property) {if (value == null) {throw new RuntimeException("Value for " + property + " cannot be null");}criteria.add(new Criterion(condition, value));}protected void addCriterion(String condition, Object value1, Object value2, String property) {if (value1 == null || value2 == null) {throw new RuntimeException("Between values for " + property + " cannot be null");}criteria.add(new Criterion(condition, value1, value2));}public Criteria andIdIsNull() {addCriterion("id is null");return (Criteria) this;}public Criteria andIdIsNotNull() {addCriterion("id is not null");return (Criteria) this;}public Criteria andIdEqualTo(Integer value) {addCriterion("id =", value, "id");return (Criteria) this;}public Criteria andIdNotEqualTo(Integer value) {addCriterion("id <>", value, "id");return (Criteria) this;}public Criteria andIdGreaterThan(Integer value) {addCriterion("id >", value, "id");return (Criteria) this;}public Criteria andIdGreaterThanOrEqualTo(Integer value) {addCriterion("id >=", value, "id");return (Criteria) this;}public Criteria andIdLessThan(Integer value) {addCriterion("id <", value, "id");return (Criteria) this;}public Criteria andIdLessThanOrEqualTo(Integer value) {addCriterion("id <=", value, "id");return (Criteria) this;}public Criteria andIdIn(List<Integer> values) {addCriterion("id in", values, "id");return (Criteria) this;}public Criteria andIdNotIn(List<Integer> values) {addCriterion("id not in", values, "id");return (Criteria) this;}public Criteria andIdBetween(Integer value1, Integer value2) {addCriterion("id between", value1, value2, "id");return (Criteria) this;}public Criteria andIdNotBetween(Integer value1, Integer value2) {addCriterion("id not between", value1, value2, "id");return (Criteria) this;}public Criteria andLastNameIsNull() {addCriterion("last_name is null");return (Criteria) this;}public Criteria andLastNameIsNotNull() {addCriterion("last_name is not null");return (Criteria) this;}public Criteria andLastNameEqualTo(String value) {addCriterion("last_name =", value, "lastName");return (Criteria) this;}public Criteria andLastNameNotEqualTo(String value) {addCriterion("last_name <>", value, "lastName");return (Criteria) this;}public Criteria andLastNameGreaterThan(String value) {addCriterion("last_name >", value, "lastName");return (Criteria) this;}public Criteria andLastNameGreaterThanOrEqualTo(String value) {addCriterion("last_name >=", value, "lastName");return (Criteria) this;}public Criteria andLastNameLessThan(String value) {addCriterion("last_name <", value, "lastName");return (Criteria) this;}public Criteria andLastNameLessThanOrEqualTo(String value) {addCriterion("last_name <=", value, "lastName");return (Criteria) this;}public Criteria andLastNameLike(String value) {addCriterion("last_name like", value, "lastName");return (Criteria) this;}public Criteria andLastNameNotLike(String value) {addCriterion("last_name not like", value, "lastName");return (Criteria) this;}public Criteria andLastNameIn(List<String> values) {addCriterion("last_name in", values, "lastName");return (Criteria) this;}public Criteria andLastNameNotIn(List<String> values) {addCriterion("last_name not in", values, "lastName");return (Criteria) this;}public Criteria andLastNameBetween(String value1, String value2) {addCriterion("last_name between", value1, value2, "lastName");return (Criteria) this;}public Criteria andLastNameNotBetween(String value1, String value2) {addCriterion("last_name not between", value1, value2, "lastName");return (Criteria) this;}public Criteria andEmailIsNull() {addCriterion("email is null");return (Criteria) this;}public Criteria andEmailIsNotNull() {addCriterion("email is not null");return (Criteria) this;}public Criteria andEmailEqualTo(String value) {addCriterion("email =", value, "email");return (Criteria) this;}public Criteria andEmailNotEqualTo(String value) {addCriterion("email <>", value, "email");return (Criteria) this;}public Criteria andEmailGreaterThan(String value) {addCriterion("email >", value, "email");return (Criteria) this;}public Criteria andEmailGreaterThanOrEqualTo(String value) {addCriterion("email >=", value, "email");return (Criteria) this;}public Criteria andEmailLessThan(String value) {addCriterion("email <", value, "email");return (Criteria) this;}public Criteria andEmailLessThanOrEqualTo(String value) {addCriterion("email <=", value, "email");return (Criteria) this;}public Criteria andEmailLike(String value) {addCriterion("email like", value, "email");return (Criteria) this;}public Criteria andEmailNotLike(String value) {addCriterion("email not like", value, "email");return (Criteria) this;}public Criteria andEmailIn(List<String> values) {addCriterion("email in", values, "email");return (Criteria) this;}public Criteria andEmailNotIn(List<String> values) {addCriterion("email not in", values, "email");return (Criteria) this;}public Criteria andEmailBetween(String value1, String value2) {addCriterion("email between", value1, value2, "email");return (Criteria) this;}public Criteria andEmailNotBetween(String value1, String value2) {addCriterion("email not between", value1, value2, "email");return (Criteria) this;}public Criteria andGenderIsNull() {addCriterion("gender is null");return (Criteria) this;}public Criteria andGenderIsNotNull() {addCriterion("gender is not null");return (Criteria) this;}public Criteria andGenderEqualTo(String value) {addCriterion("gender =", value, "gender");return (Criteria) this;}public Criteria andGenderNotEqualTo(String value) {addCriterion("gender <>", value, "gender");return (Criteria) this;}public Criteria andGenderGreaterThan(String value) {addCriterion("gender >", value, "gender");return (Criteria) this;}public Criteria andGenderGreaterThanOrEqualTo(String value) {addCriterion("gender >=", value, "gender");return (Criteria) this;}public Criteria andGenderLessThan(String value) {addCriterion("gender <", value, "gender");return (Criteria) this;}public Criteria andGenderLessThanOrEqualTo(String value) {addCriterion("gender <=", value, "gender");return (Criteria) this;}public Criteria andGenderLike(String value) {addCriterion("gender like", value, "gender");return (Criteria) this;}public Criteria andGenderNotLike(String value) {addCriterion("gender not like", value, "gender");return (Criteria) this;}public Criteria andGenderIn(List<String> values) {addCriterion("gender in", values, "gender");return (Criteria) this;}public Criteria andGenderNotIn(List<String> values) {addCriterion("gender not in", values, "gender");return (Criteria) this;}public Criteria andGenderBetween(String value1, String value2) {addCriterion("gender between", value1, value2, "gender");return (Criteria) this;}public Criteria andGenderNotBetween(String value1, String value2) {addCriterion("gender not between", value1, value2, "gender");return (Criteria) this;}public Criteria andDIdIsNull() {addCriterion("d_id is null");return (Criteria) this;}public Criteria andDIdIsNotNull() {addCriterion("d_id is not null");return (Criteria) this;}public Criteria andDIdEqualTo(Integer value) {addCriterion("d_id =", value, "dId");return (Criteria) this;}public Criteria andDIdNotEqualTo(Integer value) {addCriterion("d_id <>", value, "dId");return (Criteria) this;}public Criteria andDIdGreaterThan(Integer value) {addCriterion("d_id >", value, "dId");return (Criteria) this;}public Criteria andDIdGreaterThanOrEqualTo(Integer value) {addCriterion("d_id >=", value, "dId");return (Criteria) this;}public Criteria andDIdLessThan(Integer value) {addCriterion("d_id <", value, "dId");return (Criteria) this;}public Criteria andDIdLessThanOrEqualTo(Integer value) {addCriterion("d_id <=", value, "dId");return (Criteria) this;}public Criteria andDIdIn(List<Integer> values) {addCriterion("d_id in", values, "dId");return (Criteria) this;}public Criteria andDIdNotIn(List<Integer> values) {addCriterion("d_id not in", values, "dId");return (Criteria) this;}public Criteria andDIdBetween(Integer value1, Integer value2) {addCriterion("d_id between", value1, value2, "dId");return (Criteria) this;}public Criteria andDIdNotBetween(Integer value1, Integer value2) {addCriterion("d_id not between", value1, value2, "dId");return (Criteria) this;}}/*** This class was generated by MyBatis Generator.* This class corresponds to the database table tbl_employee** @mbggenerated do_not_delete_during_merge Fri Oct 11 22:03:26 CST 2019*/public static class Criteria extends GeneratedCriteria {protected Criteria() {super();}}/*** This class was generated by MyBatis Generator.* This class corresponds to the database table tbl_employee** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/public static class Criterion {private String condition;private Object value;private Object secondValue;private boolean noValue;private boolean singleValue;private boolean betweenValue;private boolean listValue;private String typeHandler;public String getCondition() {return condition;}public Object getValue() {return value;}public Object getSecondValue() {return secondValue;}public boolean isNoValue() {return noValue;}public boolean isSingleValue() {return singleValue;}public boolean isBetweenValue() {return betweenValue;}public boolean isListValue() {return listValue;}public String getTypeHandler() {return typeHandler;}protected Criterion(String condition) {super();this.condition = condition;this.typeHandler = null;this.noValue = true;}protected Criterion(String condition, Object value, String typeHandler) {super();this.condition = condition;this.value = value;this.typeHandler = typeHandler;if (value instanceof List<?>) {this.listValue = true;} else {this.singleValue = true;}}protected Criterion(String condition, Object value) {this(condition, value, null);}protected Criterion(String condition, Object value, Object secondValue, String typeHandler) {super();this.condition = condition;this.value = value;this.secondValue = secondValue;this.typeHandler = typeHandler;this.betweenValue = true;}protected Criterion(String condition, Object value, Object secondValue) {this(condition, value, secondValue, null);}}
}
package com.xiaomifeng1010.mybatis.bean;import java.util.ArrayList;
import java.util.List;public class DepartmentExample {/*** This field was generated by MyBatis Generator.* This field corresponds to the database table tbl_dept** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/protected String orderByClause;/*** This field was generated by MyBatis Generator.* This field corresponds to the database table tbl_dept** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/protected boolean distinct;/*** This field was generated by MyBatis Generator.* This field corresponds to the database table tbl_dept** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/protected List<Criteria> oredCriteria;/*** This method was generated by MyBatis Generator.* This method corresponds to the database table tbl_dept** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/public DepartmentExample() {oredCriteria = new ArrayList<Criteria>();}/*** This method was generated by MyBatis Generator.* This method corresponds to the database table tbl_dept** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/public void setOrderByClause(String orderByClause) {this.orderByClause = orderByClause;}/*** This method was generated by MyBatis Generator.* This method corresponds to the database table tbl_dept** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/public String getOrderByClause() {return orderByClause;}/*** This method was generated by MyBatis Generator.* This method corresponds to the database table tbl_dept** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/public void setDistinct(boolean distinct) {this.distinct = distinct;}/*** This method was generated by MyBatis Generator.* This method corresponds to the database table tbl_dept** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/public boolean isDistinct() {return distinct;}/*** This method was generated by MyBatis Generator.* This method corresponds to the database table tbl_dept** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/public List<Criteria> getOredCriteria() {return oredCriteria;}/*** This method was generated by MyBatis Generator.* This method corresponds to the database table tbl_dept** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/public void or(Criteria criteria) {oredCriteria.add(criteria);}/*** This method was generated by MyBatis Generator.* This method corresponds to the database table tbl_dept** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/public Criteria or() {Criteria criteria = createCriteriaInternal();oredCriteria.add(criteria);return criteria;}/*** This method was generated by MyBatis Generator.* This method corresponds to the database table tbl_dept** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/public Criteria createCriteria() {Criteria criteria = createCriteriaInternal();if (oredCriteria.size() == 0) {oredCriteria.add(criteria);}return criteria;}/*** This method was generated by MyBatis Generator.* This method corresponds to the database table tbl_dept** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/protected Criteria createCriteriaInternal() {Criteria criteria = new Criteria();return criteria;}/*** This method was generated by MyBatis Generator.* This method corresponds to the database table tbl_dept** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/public void clear() {oredCriteria.clear();orderByClause = null;distinct = false;}/*** This class was generated by MyBatis Generator.* This class corresponds to the database table tbl_dept** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/protected abstract static class GeneratedCriteria {protected List<Criterion> criteria;protected GeneratedCriteria() {super();criteria = new ArrayList<Criterion>();}public boolean isValid() {return criteria.size() > 0;}public List<Criterion> getAllCriteria() {return criteria;}public List<Criterion> getCriteria() {return criteria;}protected void addCriterion(String condition) {if (condition == null) {throw new RuntimeException("Value for condition cannot be null");}criteria.add(new Criterion(condition));}protected void addCriterion(String condition, Object value, String property) {if (value == null) {throw new RuntimeException("Value for " + property + " cannot be null");}criteria.add(new Criterion(condition, value));}protected void addCriterion(String condition, Object value1, Object value2, String property) {if (value1 == null || value2 == null) {throw new RuntimeException("Between values for " + property + " cannot be null");}criteria.add(new Criterion(condition, value1, value2));}public Criteria andIdIsNull() {addCriterion("id is null");return (Criteria) this;}public Criteria andIdIsNotNull() {addCriterion("id is not null");return (Criteria) this;}public Criteria andIdEqualTo(Integer value) {addCriterion("id =", value, "id");return (Criteria) this;}public Criteria andIdNotEqualTo(Integer value) {addCriterion("id <>", value, "id");return (Criteria) this;}public Criteria andIdGreaterThan(Integer value) {addCriterion("id >", value, "id");return (Criteria) this;}public Criteria andIdGreaterThanOrEqualTo(Integer value) {addCriterion("id >=", value, "id");return (Criteria) this;}public Criteria andIdLessThan(Integer value) {addCriterion("id <", value, "id");return (Criteria) this;}public Criteria andIdLessThanOrEqualTo(Integer value) {addCriterion("id <=", value, "id");return (Criteria) this;}public Criteria andIdIn(List<Integer> values) {addCriterion("id in", values, "id");return (Criteria) this;}public Criteria andIdNotIn(List<Integer> values) {addCriterion("id not in", values, "id");return (Criteria) this;}public Criteria andIdBetween(Integer value1, Integer value2) {addCriterion("id between", value1, value2, "id");return (Criteria) this;}public Criteria andIdNotBetween(Integer value1, Integer value2) {addCriterion("id not between", value1, value2, "id");return (Criteria) this;}public Criteria andDeptNameIsNull() {addCriterion("dept_name is null");return (Criteria) this;}public Criteria andDeptNameIsNotNull() {addCriterion("dept_name is not null");return (Criteria) this;}public Criteria andDeptNameEqualTo(String value) {addCriterion("dept_name =", value, "deptName");return (Criteria) this;}public Criteria andDeptNameNotEqualTo(String value) {addCriterion("dept_name <>", value, "deptName");return (Criteria) this;}public Criteria andDeptNameGreaterThan(String value) {addCriterion("dept_name >", value, "deptName");return (Criteria) this;}public Criteria andDeptNameGreaterThanOrEqualTo(String value) {addCriterion("dept_name >=", value, "deptName");return (Criteria) this;}public Criteria andDeptNameLessThan(String value) {addCriterion("dept_name <", value, "deptName");return (Criteria) this;}public Criteria andDeptNameLessThanOrEqualTo(String value) {addCriterion("dept_name <=", value, "deptName");return (Criteria) this;}public Criteria andDeptNameLike(String value) {addCriterion("dept_name like", value, "deptName");return (Criteria) this;}public Criteria andDeptNameNotLike(String value) {addCriterion("dept_name not like", value, "deptName");return (Criteria) this;}public Criteria andDeptNameIn(List<String> values) {addCriterion("dept_name in", values, "deptName");return (Criteria) this;}public Criteria andDeptNameNotIn(List<String> values) {addCriterion("dept_name not in", values, "deptName");return (Criteria) this;}public Criteria andDeptNameBetween(String value1, String value2) {addCriterion("dept_name between", value1, value2, "deptName");return (Criteria) this;}public Criteria andDeptNameNotBetween(String value1, String value2) {addCriterion("dept_name not between", value1, value2, "deptName");return (Criteria) this;}}/*** This class was generated by MyBatis Generator.* This class corresponds to the database table tbl_dept** @mbggenerated do_not_delete_during_merge Fri Oct 11 22:03:26 CST 2019*/public static class Criteria extends GeneratedCriteria {protected Criteria() {super();}}/*** This class was generated by MyBatis Generator.* This class corresponds to the database table tbl_dept** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/public static class Criterion {private String condition;private Object value;private Object secondValue;private boolean noValue;private boolean singleValue;private boolean betweenValue;private boolean listValue;private String typeHandler;public String getCondition() {return condition;}public Object getValue() {return value;}public Object getSecondValue() {return secondValue;}public boolean isNoValue() {return noValue;}public boolean isSingleValue() {return singleValue;}public boolean isBetweenValue() {return betweenValue;}public boolean isListValue() {return listValue;}public String getTypeHandler() {return typeHandler;}protected Criterion(String condition) {super();this.condition = condition;this.typeHandler = null;this.noValue = true;}protected Criterion(String condition, Object value, String typeHandler) {super();this.condition = condition;this.value = value;this.typeHandler = typeHandler;if (value instanceof List<?>) {this.listValue = true;} else {this.singleValue = true;}}protected Criterion(String condition, Object value) {this(condition, value, null);}protected Criterion(String condition, Object value, Object secondValue, String typeHandler) {super();this.condition = condition;this.value = value;this.secondValue = secondValue;this.typeHandler = typeHandler;this.betweenValue = true;}protected Criterion(String condition, Object value, Object secondValue) {this(condition, value, secondValue, null);}}
}

1.9.3 查看生成的dao接口(mybatis中称为mapper接口)

package com.xiaomifeng1010.mybatis.dao;import com.xiaomifeng1010.mybatis.bean.Employee;
import com.xiaomifeng1010.mybatis.bean.EmployeeExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;public interface EmployeeMapper {/*** This method was generated by MyBatis Generator.* This method corresponds to the database table tbl_employee** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/int countByExample(EmployeeExample example);/*** This method was generated by MyBatis Generator.* This method corresponds to the database table tbl_employee** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/int deleteByExample(EmployeeExample example);/*** This method was generated by MyBatis Generator.* This method corresponds to the database table tbl_employee** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/int deleteByPrimaryKey(Integer id);/*** This method was generated by MyBatis Generator.* This method corresponds to the database table tbl_employee** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/int insert(Employee record);/*** This method was generated by MyBatis Generator.* This method corresponds to the database table tbl_employee** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/int insertSelective(Employee record);/*** This method was generated by MyBatis Generator.* This method corresponds to the database table tbl_employee** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/List<Employee> selectByExample(EmployeeExample example);/*** This method was generated by MyBatis Generator.* This method corresponds to the database table tbl_employee** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/Employee selectByPrimaryKey(Integer id);/*** This method was generated by MyBatis Generator.* This method corresponds to the database table tbl_employee** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/int updateByExampleSelective(@Param("record") Employee record, @Param("example") EmployeeExample example);/*** This method was generated by MyBatis Generator.* This method corresponds to the database table tbl_employee** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/int updateByExample(@Param("record") Employee record, @Param("example") EmployeeExample example);/*** This method was generated by MyBatis Generator.* This method corresponds to the database table tbl_employee** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/int updateByPrimaryKeySelective(Employee record);/*** This method was generated by MyBatis Generator.* This method corresponds to the database table tbl_employee** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/int updateByPrimaryKey(Employee record);
}
package com.xiaomifeng1010.mybatis.dao;import com.xiaomifeng1010.mybatis.bean.Department;
import com.xiaomifeng1010.mybatis.bean.DepartmentExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;public interface DepartmentMapper {/*** This method was generated by MyBatis Generator.* This method corresponds to the database table tbl_dept** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/int countByExample(DepartmentExample example);/*** This method was generated by MyBatis Generator.* This method corresponds to the database table tbl_dept** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/int deleteByExample(DepartmentExample example);/*** This method was generated by MyBatis Generator.* This method corresponds to the database table tbl_dept** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/int deleteByPrimaryKey(Integer id);/*** This method was generated by MyBatis Generator.* This method corresponds to the database table tbl_dept** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/int insert(Department record);/*** This method was generated by MyBatis Generator.* This method corresponds to the database table tbl_dept** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/int insertSelective(Department record);/*** This method was generated by MyBatis Generator.* This method corresponds to the database table tbl_dept** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/List<Department> selectByExample(DepartmentExample example);/*** This method was generated by MyBatis Generator.* This method corresponds to the database table tbl_dept** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/Department selectByPrimaryKey(Integer id);/*** This method was generated by MyBatis Generator.* This method corresponds to the database table tbl_dept** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/int updateByExampleSelective(@Param("record") Department record, @Param("example") DepartmentExample example);/*** This method was generated by MyBatis Generator.* This method corresponds to the database table tbl_dept** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/int updateByExample(@Param("record") Department record, @Param("example") DepartmentExample example);/*** This method was generated by MyBatis Generator.* This method corresponds to the database table tbl_dept** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/int updateByPrimaryKeySelective(Department record);/*** This method was generated by MyBatis Generator.* This method corresponds to the database table tbl_dept** @mbggenerated Fri Oct 11 22:03:26 CST 2019*/int updateByPrimaryKey(Department record);
}

1.9.4 查看生成的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.xiaomifeng1010.mybatis.dao.EmployeeMapper" ><resultMap id="BaseResultMap" type="com.xiaomifeng1010.mybatis.bean.Employee" ><!--WARNING - @mbggeneratedThis element is automatically generated by MyBatis Generator, do not modify.This element was generated on Fri Oct 11 22:03:26 CST 2019.--><id column="id" property="id" jdbcType="INTEGER" /><result column="last_name" property="lastName" jdbcType="VARCHAR" /><result column="email" property="email" jdbcType="VARCHAR" /><result column="gender" property="gender" jdbcType="VARCHAR" /><result column="d_id" property="dId" jdbcType="INTEGER" /></resultMap><sql id="Example_Where_Clause" ><!--WARNING - @mbggeneratedThis element is automatically generated by MyBatis Generator, do not modify.This element was generated on Fri Oct 11 22:03:26 CST 2019.--><where ><foreach collection="oredCriteria" item="criteria" separator="or" ><if test="criteria.valid" ><trim prefix="(" suffix=")" prefixOverrides="and" ><foreach collection="criteria.criteria" item="criterion" ><choose ><when test="criterion.noValue" >and ${criterion.condition}</when><when test="criterion.singleValue" >and ${criterion.condition} #{criterion.value}</when><when test="criterion.betweenValue" >and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}</when><when test="criterion.listValue" >and ${criterion.condition}<foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >#{listItem}</foreach></when></choose></foreach></trim></if></foreach></where></sql><sql id="Update_By_Example_Where_Clause" ><!--WARNING - @mbggeneratedThis element is automatically generated by MyBatis Generator, do not modify.This element was generated on Fri Oct 11 22:03:26 CST 2019.--><where ><foreach collection="example.oredCriteria" item="criteria" separator="or" ><if test="criteria.valid" ><trim prefix="(" suffix=")" prefixOverrides="and" ><foreach collection="criteria.criteria" item="criterion" ><choose ><when test="criterion.noValue" >and ${criterion.condition}</when><when test="criterion.singleValue" >and ${criterion.condition} #{criterion.value}</when><when test="criterion.betweenValue" >and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}</when><when test="criterion.listValue" >and ${criterion.condition}<foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >#{listItem}</foreach></when></choose></foreach></trim></if></foreach></where></sql><sql id="Base_Column_List" ><!--WARNING - @mbggeneratedThis element is automatically generated by MyBatis Generator, do not modify.This element was generated on Fri Oct 11 22:03:26 CST 2019.-->id, last_name, email, gender, d_id</sql><select id="selectByExample" resultMap="BaseResultMap" parameterType="com.xiaomifeng1010.mybatis.bean.EmployeeExample" ><!--WARNING - @mbggeneratedThis element is automatically generated by MyBatis Generator, do not modify.This element was generated on Fri Oct 11 22:03:26 CST 2019.-->select<if test="distinct" >distinct</if><include refid="Base_Column_List" />from tbl_employee<if test="_parameter != null" ><include refid="Example_Where_Clause" /></if><if test="orderByClause != null" >order by ${orderByClause}</if></select><select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" ><!--WARNING - @mbggeneratedThis element is automatically generated by MyBatis Generator, do not modify.This element was generated on Fri Oct 11 22:03:26 CST 2019.-->select <include refid="Base_Column_List" />from tbl_employeewhere id = #{id,jdbcType=INTEGER}</select><delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" ><!--WARNING - @mbggeneratedThis element is automatically generated by MyBatis Generator, do not modify.This element was generated on Fri Oct 11 22:03:26 CST 2019.-->delete from tbl_employeewhere id = #{id,jdbcType=INTEGER}</delete><delete id="deleteByExample" parameterType="com.xiaomifeng1010.mybatis.bean.EmployeeExample" ><!--WARNING - @mbggeneratedThis element is automatically generated by MyBatis Generator, do not modify.This element was generated on Fri Oct 11 22:03:26 CST 2019.-->delete from tbl_employee<if test="_parameter != null" ><include refid="Example_Where_Clause" /></if></delete><insert id="insert" parameterType="com.xiaomifeng1010.mybatis.bean.Employee" ><!--WARNING - @mbggeneratedThis element is automatically generated by MyBatis Generator, do not modify.This element was generated on Fri Oct 11 22:03:26 CST 2019.-->insert into tbl_employee (id, last_name, email, gender, d_id)values (#{id,jdbcType=INTEGER}, #{lastName,jdbcType=VARCHAR}, #{email,jdbcType=VARCHAR}, #{gender,jdbcType=VARCHAR}, #{dId,jdbcType=INTEGER})</insert><insert id="insertSelective" parameterType="com.xiaomifeng1010.mybatis.bean.Employee" ><!--WARNING - @mbggeneratedThis element is automatically generated by MyBatis Generator, do not modify.This element was generated on Fri Oct 11 22:03:26 CST 2019.-->insert into tbl_employee<trim prefix="(" suffix=")" suffixOverrides="," ><if test="id != null" >id,</if><if test="lastName != null" >last_name,</if><if test="email != null" >email,</if><if test="gender != null" >gender,</if><if test="dId != null" >d_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="email != null" >#{email,jdbcType=VARCHAR},</if><if test="gender != null" >#{gender,jdbcType=VARCHAR},</if><if test="dId != null" >#{dId,jdbcType=INTEGER},</if></trim></insert><select id="countByExample" parameterType="com.xiaomifeng1010.mybatis.bean.EmployeeExample" resultType="java.lang.Integer" ><!--WARNING - @mbggeneratedThis element is automatically generated by MyBatis Generator, do not modify.This element was generated on Fri Oct 11 22:03:26 CST 2019.-->select count(*) from tbl_employee<if test="_parameter != null" ><include refid="Example_Where_Clause" /></if></select><update id="updateByExampleSelective" parameterType="map" ><!--WARNING - @mbggeneratedThis element is automatically generated by MyBatis Generator, do not modify.This element was generated on Fri Oct 11 22:03:26 CST 2019.-->update tbl_employee<set ><if test="record.id != null" >id = #{record.id,jdbcType=INTEGER},</if><if test="record.lastName != null" >last_name = #{record.lastName,jdbcType=VARCHAR},</if><if test="record.email != null" >email = #{record.email,jdbcType=VARCHAR},</if><if test="record.gender != null" >gender = #{record.gender,jdbcType=VARCHAR},</if><if test="record.dId != null" >d_id = #{record.dId,jdbcType=INTEGER},</if></set><if test="_parameter != null" ><include refid="Update_By_Example_Where_Clause" /></if></update><update id="updateByExample" parameterType="map" ><!--WARNING - @mbggeneratedThis element is automatically generated by MyBatis Generator, do not modify.This element was generated on Fri Oct 11 22:03:26 CST 2019.-->update tbl_employeeset id = #{record.id,jdbcType=INTEGER},last_name = #{record.lastName,jdbcType=VARCHAR},email = #{record.email,jdbcType=VARCHAR},gender = #{record.gender,jdbcType=VARCHAR},d_id = #{record.dId,jdbcType=INTEGER}<if test="_parameter != null" ><include refid="Update_By_Example_Where_Clause" /></if></update><update id="updateByPrimaryKeySelective" parameterType="com.xiaomifeng1010.mybatis.bean.Employee" ><!--WARNING - @mbggeneratedThis element is automatically generated by MyBatis Generator, do not modify.This element was generated on Fri Oct 11 22:03:26 CST 2019.-->update tbl_employee<set ><if test="lastName != null" >last_name = #{lastName,jdbcType=VARCHAR},</if><if test="email != null" >email = #{email,jdbcType=VARCHAR},</if><if test="gender != null" >gender = #{gender,jdbcType=VARCHAR},</if><if test="dId != null" >d_id = #{dId,jdbcType=INTEGER},</if></set>where id = #{id,jdbcType=INTEGER}</update><update id="updateByPrimaryKey" parameterType="com.xiaomifeng1010.mybatis.bean.Employee" ><!--WARNING - @mbggeneratedThis element is automatically generated by MyBatis Generator, do not modify.This element was generated on Fri Oct 11 22:03:26 CST 2019.-->update tbl_employeeset last_name = #{lastName,jdbcType=VARCHAR},email = #{email,jdbcType=VARCHAR},gender = #{gender,jdbcType=VARCHAR},d_id = #{dId,jdbcType=INTEGER}where id = #{id,jdbcType=INTEGER}</update>
</mapper>
<?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.xiaomifeng1010.mybatis.dao.DepartmentMapper" ><resultMap id="BaseResultMap" type="com.xiaomifeng1010.mybatis.bean.Department" ><!--WARNING - @mbggeneratedThis element is automatically generated by MyBatis Generator, do not modify.This element was generated on Fri Oct 11 22:03:26 CST 2019.--><id column="id" property="id" jdbcType="INTEGER" /><result column="dept_name" property="deptName" jdbcType="VARCHAR" /></resultMap><sql id="Example_Where_Clause" ><!--WARNING - @mbggeneratedThis element is automatically generated by MyBatis Generator, do not modify.This element was generated on Fri Oct 11 22:03:26 CST 2019.--><where ><foreach collection="oredCriteria" item="criteria" separator="or" ><if test="criteria.valid" ><trim prefix="(" suffix=")" prefixOverrides="and" ><foreach collection="criteria.criteria" item="criterion" ><choose ><when test="criterion.noValue" >and ${criterion.condition}</when><when test="criterion.singleValue" >and ${criterion.condition} #{criterion.value}</when><when test="criterion.betweenValue" >and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}</when><when test="criterion.listValue" >and ${criterion.condition}<foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >#{listItem}</foreach></when></choose></foreach></trim></if></foreach></where></sql><sql id="Update_By_Example_Where_Clause" ><!--WARNING - @mbggeneratedThis element is automatically generated by MyBatis Generator, do not modify.This element was generated on Fri Oct 11 22:03:26 CST 2019.--><where ><foreach collection="example.oredCriteria" item="criteria" separator="or" ><if test="criteria.valid" ><trim prefix="(" suffix=")" prefixOverrides="and" ><foreach collection="criteria.criteria" item="criterion" ><choose ><when test="criterion.noValue" >and ${criterion.condition}</when><when test="criterion.singleValue" >and ${criterion.condition} #{criterion.value}</when><when test="criterion.betweenValue" >and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}</when><when test="criterion.listValue" >and ${criterion.condition}<foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >#{listItem}</foreach></when></choose></foreach></trim></if></foreach></where></sql><sql id="Base_Column_List" ><!--WARNING - @mbggeneratedThis element is automatically generated by MyBatis Generator, do not modify.This element was generated on Fri Oct 11 22:03:26 CST 2019.-->id, dept_name</sql><select id="selectByExample" resultMap="BaseResultMap" parameterType="com.xiaomifeng1010.mybatis.bean.DepartmentExample" ><!--WARNING - @mbggeneratedThis element is automatically generated by MyBatis Generator, do not modify.This element was generated on Fri Oct 11 22:03:26 CST 2019.-->select<if test="distinct" >distinct</if><include refid="Base_Column_List" />from tbl_dept<if test="_parameter != null" ><include refid="Example_Where_Clause" /></if><if test="orderByClause != null" >order by ${orderByClause}</if></select><select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" ><!--WARNING - @mbggeneratedThis element is automatically generated by MyBatis Generator, do not modify.This element was generated on Fri Oct 11 22:03:26 CST 2019.-->select <include refid="Base_Column_List" />from tbl_deptwhere id = #{id,jdbcType=INTEGER}</select><delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" ><!--WARNING - @mbggeneratedThis element is automatically generated by MyBatis Generator, do not modify.This element was generated on Fri Oct 11 22:03:26 CST 2019.-->delete from tbl_deptwhere id = #{id,jdbcType=INTEGER}</delete><delete id="deleteByExample" parameterType="com.xiaomifeng1010.mybatis.bean.DepartmentExample" ><!--WARNING - @mbggeneratedThis element is automatically generated by MyBatis Generator, do not modify.This element was generated on Fri Oct 11 22:03:26 CST 2019.-->delete from tbl_dept<if test="_parameter != null" ><include refid="Example_Where_Clause" /></if></delete><insert id="insert" parameterType="com.xiaomifeng1010.mybatis.bean.Department" ><!--WARNING - @mbggeneratedThis element is automatically generated by MyBatis Generator, do not modify.This element was generated on Fri Oct 11 22:03:26 CST 2019.-->insert into tbl_dept (id, dept_name)values (#{id,jdbcType=INTEGER}, #{deptName,jdbcType=VARCHAR})</insert><insert id="insertSelective" parameterType="com.xiaomifeng1010.mybatis.bean.Department" ><!--WARNING - @mbggeneratedThis element is automatically generated by MyBatis Generator, do not modify.This element was generated on Fri Oct 11 22:03:26 CST 2019.-->insert into tbl_dept<trim prefix="(" suffix=")" suffixOverrides="," ><if test="id != null" >id,</if><if test="deptName != null" >dept_name,</if></trim><trim prefix="values (" suffix=")" suffixOverrides="," ><if test="id != null" >#{id,jdbcType=INTEGER},</if><if test="deptName != null" >#{deptName,jdbcType=VARCHAR},</if></trim></insert><select id="countByExample" parameterType="com.xiaomifeng1010.mybatis.bean.DepartmentExample" resultType="java.lang.Integer" ><!--WARNING - @mbggeneratedThis element is automatically generated by MyBatis Generator, do not modify.This element was generated on Fri Oct 11 22:03:26 CST 2019.-->select count(*) from tbl_dept<if test="_parameter != null" ><include refid="Example_Where_Clause" /></if></select><update id="updateByExampleSelective" parameterType="map" ><!--WARNING - @mbggeneratedThis element is automatically generated by MyBatis Generator, do not modify.This element was generated on Fri Oct 11 22:03:26 CST 2019.-->update tbl_dept<set ><if test="record.id != null" >id = #{record.id,jdbcType=INTEGER},</if><if test="record.deptName != null" >dept_name = #{record.deptName,jdbcType=VARCHAR},</if></set><if test="_parameter != null" ><include refid="Update_By_Example_Where_Clause" /></if></update><update id="updateByExample" parameterType="map" ><!--WARNING - @mbggeneratedThis element is automatically generated by MyBatis Generator, do not modify.This element was generated on Fri Oct 11 22:03:26 CST 2019.-->update tbl_deptset id = #{record.id,jdbcType=INTEGER},dept_name = #{record.deptName,jdbcType=VARCHAR}<if test="_parameter != null" ><include refid="Update_By_Example_Where_Clause" /></if></update><update id="updateByPrimaryKeySelective" parameterType="com.xiaomifeng1010.mybatis.bean.Department" ><!--WARNING - @mbggeneratedThis element is automatically generated by MyBatis Generator, do not modify.This element was generated on Fri Oct 11 22:03:26 CST 2019.-->update tbl_dept<set ><if test="deptName != null" >dept_name = #{deptName,jdbcType=VARCHAR},</if></set>where id = #{id,jdbcType=INTEGER}</update><update id="updateByPrimaryKey" parameterType="com.xiaomifeng1010.mybatis.bean.Department" ><!--WARNING - @mbggeneratedThis element is automatically generated by MyBatis Generator, do not modify.This element was generated on Fri Oct 11 22:03:26 CST 2019.-->update tbl_deptset dept_name = #{deptName,jdbcType=VARCHAR}where id = #{id,jdbcType=INTEGER}</update>
</mapper>

可以看到mapper映射配置文件中,由生成器已经自动生成的增删改查语句。

二、测试增删改查

2.1 可以在数据表中插入一些数据,进行测试查询:

package com.xiaomifeng1010.mybatis.test;import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;import com.xiaomifeng1010.mybatis.bean.Employee;
import com.xiaomifeng1010.mybatis.bean.EmployeeExample;
import com.xiaomifeng1010.mybatis.bean.EmployeeExample.Criteria;
import com.xiaomifeng1010.mybatis.dao.EmployeeMapper;public class MyBatisTest {public SqlSessionFactory getSqlSessionFactory() throws IOException {String resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);return new SqlSessionFactoryBuilder().build(inputStream);}@Testpublic void testMbg() throws Exception {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);}@Testpublic void testMyBatis3Simple() throws IOException{SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();SqlSession openSession = sqlSessionFactory.openSession();try{EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);List<Employee> list = mapper.selectByExample(null);for (Employee employee : list) {System.out.println(employee.getId());}}finally{openSession.close();}}@Testpublic void testMyBatis3() throws IOException{SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();SqlSession openSession = sqlSessionFactory.openSession();try{EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);//xxxExample就是封装查询条件的//1、查询所有//List<Employee> emps = mapper.selectByExample(null);//2、查询员工名字中有e字母的,和员工性别是1的//封装员工查询条件的exampleEmployeeExample example = new EmployeeExample();//通过example对象创建Criteria对象,这个Criteria就是拼装查询条件//select id, last_name, email, gender, d_id from tbl_employee //WHERE ( last_name like ? and gender = ? ) or email like "%e%"Criteria criteria = example.createCriteria();criteria.andLastNameLike("%e%");criteria.andGenderEqualTo("1");Criteria criteria2 = example.createCriteria();criteria2.andEmailLike("%e%");//使用OR 关键词组装两个Criteria 对象example.or(criteria2);List<Employee> list = mapper.selectByExample(example);for (Employee employee : list) {System.out.println(employee.getId());}}finally{openSession.close();}}}

ORM框架之Mybatis(四)MyBatis生成器,逆向工程生成实体类和SQL相关推荐

  1. 实体类dao接口mysql_利用MyBatis生成器自动生成实体类、DAO接口和Mapping映射文件...

    解决问题: 可利用MyBatis生成器自动生成实体类.DAO接口和Mapping映射文件. 测试环境准备: 新建一个mysql数据库,例如mungerzTest. 生成一张主键为自增ID的学生表: C ...

  2. 你还在手写sql吗? MyBatis 逆向工程使用 使用逆向工程生成实体类,超级好用的生成实体类与mapper

    众所周知项目上的rbac在入门以后,熟悉掌握了以后,sql这些自己手动写起来是非常繁琐且无聊,那大家有没有想过用工具,只要创建表,然后使用工具告诉他一些实体类的名字跟其他信息让他自动帮我们生成这些sq ...

  3. Mybatis逆向工程(生成实体类)开发指南

    2018/11/12 9:46:47 添加依赖 <dependency><groupId>org.mybatis.generator</groupId><ar ...

  4. 如何用MyBatis-Generator自动创建代码(映射生成实体类、DAO接口和Mapping映射文件)

    如何用MyBatis自动生成实体类.DAO接口和Mapping映射文件 引言: 什么是 MyBatis ? MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBa ...

  5. idea 集成mybatis,利用MyBatis Generator自动生成实体类、mapper文件

    最近一个老项目集成mybatis,利用 generator自动生成实体类.mapper的时候折腾了一小时,记录一下,避免以后再折腾 很简单的三步 https://gitee.com/shunangua ...

  6. 第一章:ORM框架发展历程和MyBatis的核心应用

    ORM框架的发展历史与MyBatis的高级应用 一.ORM框架的发展历程 1. JDBC操作 1.1 JDBC操作的特点 最初的时候我们肯定是直接通过jdbc来直接操作数据库的,本地数据库我们有一张t ...

  7. 【转】MyBatis学习总结(四)——解决字段名与实体类属性名不相同的冲突

    [转]MyBatis学习总结(四)--解决字段名与实体类属性名不相同的冲突 在平时的开发中,我们表中的字段名和表对应实体类的属性名称不一定都是完全相同的,下面来演示一下这种情况下的如何解决字段名与实体 ...

  8. mybatis逆向工程的使用,自动生成实体类与基础SQL

    目录 简介 mybatis逆向工程的使用 导入依赖 编写逆向工程配置文件 编写执行方法 查看结果 简介 来分享一个比较使用的玩意,mybatis的逆向工程,可以自动生成实体类.基础SQL映射文件.Ma ...

  9. SpringBoot中使用Mybatis逆向工程(实体类含数据库注释)

    Mybatis逆向工程:根据创建好的数据库表,生成对应的实体类.DAO.映射文件 文章目录 开发环境 1.新建SpringBoot应用 2.添加逆向工程插件依赖 3.执行逆向生成 开发环境 开发工具: ...

最新文章

  1. 熬夜与不熬夜,10年后差距到底有多大?
  2. 越心虚越藏着掖着,越藏越掖越脱节
  3. react native一键分享功能实现amp;原理和注意点(支持微信、qq、新浪微博等)
  4. 为什么豌豆荚可以在应用安装完成界面打广告?
  5. mybatis一个怪异的问题: Invalid bound statement (not found)
  6. 抽象函数和虚函数有什么区别?
  7. 常用模块之hashlib,subprocess,logging,re,collections
  8. Vensim建模--基于系统动力学的私人小汽车出行特征建模分析
  9. vfp中写入文本文件_VFP中操作多种文件
  10. 峨眉山徒步休闲三日游攻略内附详细时间
  11. JavaScript-ES6新特性详解
  12. 2021年数据泄露成本报告解读
  13. 获取空气质量站点数据 城市数据 日数据 时数据
  14. 克罗内克函数Kronecker Delta【OI Pharos 6.2.1】
  15. 微信小程序开发基础(02模板与配置)
  16. python里写中文出现 “SyntaxError: Non-ASCII character... but no encoding declared”解决方法
  17. sql查询当天 当月 当年
  18. exynos4412 祼机LED闪灯 - 一闪一闪亮晶晶
  19. 多旋翼飞行器设计与控制(二)—— 基本组成
  20. element datetimepicker

热门文章

  1. 子集数据帧中的丢包因子级别
  2. 通过按Enter阻止用户提交表单
  3. 函数式编程会取代GoF设计模式吗?
  4. 我如何开始使用Node.js [关闭]
  5. c++ 传入字符串 带返回值_python的数据类型(三):字符串
  6. windows操作系统_国产Linux操作系统体验:易用性不比Windows差,但输在这点上
  7. 普林斯顿大学计算机科学研究生条件,普林斯顿大学之计算机科学系
  8. 怎样通过ip查看linux密码,怎么根据linux根据ip地址查主机名
  9. python读取word element_Python:通过解析word将文本从docx提取到txt/文档.xm
  10. exfat最佳单元大小_2020年Window系统重装最佳方式