mybatis反向生成插件
一:反向生成插件的作用:自动生成实体类、mapper、实体和mapper的自动映射,并且比自己写的还要详细。
二:实现步骤
第一步:在pom.xml中build的plugins中添加配置文件:
<!--反向生成插件-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
<!--配置文件的路径-->
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
</configuration>
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
</dependencies>
</plugin>
第二步:在配置文件的路径下建立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>
<!--1、数据库驱动jar:添加自己的jar路径 -->
<classPathEntry
location="D:\repository\mysql\mysql-connector-java\8.0.23\mysql-connector-java-8.0.23.jar"/>
<context id="MyBatis" targetRuntime="MyBatis3">
<!--去除注释 -->
<commentGenerator>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--2、数据库连接 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/spring?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT"
userId="root"
password="zhangjing1234">
</jdbcConnection>
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer;
为 true时把JDBC DECIMAL和NUMERIC类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!--3、生成实体类 指定包名 以及生成的地址 (可以自定义地址,但是路径不存在不会自动创建
使用Maven生成在target目录下,会自动创建) -->
<javaModelGenerator targetPackage="com.kkb.pojo"
targetProject="src\main\java">
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--4、生成SQLmapper.xml文件 -->
<sqlMapGenerator targetPackage="com.kkb.mapper"
targetProject="src\main\resources">
</sqlMapGenerator>
<!--5、生成Dao(Mapper)文件,生成接口 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.kkb.mapper"
targetProject="src\main\java">
</javaClientGenerator>
<!--6、要生成哪些表(更改tableName和domainObjectName就可以) -->
<!-- tableName:要生成的表名
enableCountByExample:Count语句中加入where条件查询,默认为true开启
enableUpdateByExample:Update语句中加入where条件查询,默认为true开启
enableDeleteByExample:Delete语句中加入where条件查询,默认为true开启
enableSelectByExample:Select多条语句中加入where条件查询,默认为true开启
selectByExampleQueryId:Select单个对象语句中加入where条件查询,默认为true开启
-->
<!--下面的配置是让生成的实体类的属性名字符合Java的驼峰命名法,数据库中带下划线的不用配置-->
<table tableName="Team">//将上面这些enableCountByExample设置为false后,将会减少很多的多条件查询方法,所以我们不禁用。
<property name="useActualColumnNames" value="true"/>
</table>
<table tableName="Player">
<property name="useActualColumnNames" value="true"/>
</table>
<table tableName="game">
<property name="useActualColumnNames" value="true"/>
</table>
<table tableName="GameType">
<property name="useActualColumnNames" value="true"/>
</table>
<table tableName="Admins">
<property name="useActualColumnNames" value="true"/>
</table>
<table tableName="AdminRole">
<property name="useActualColumnNames" value="true"/>
</table>
</context>
</generatorConfiguration>
第三步:
三:解释
1、pojo中,生成的对应的类,就是只有我们的属性,和get set 方法,例如:
package com.kkb.pojo;
public class Adminrole {
private Integer roleId;
private String roleName;
public Integer getRoleId() {
return roleId;
}
public void setRoleId(Integer roleId) {
this.roleId = roleId;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName == null ? null : roleName.trim();
}
}
生成的类Example类,它里面是多条件操作要拼接的属性,同时里面还有很多多条件的方法例如:
package com.kkb.pojo;
import java.util.ArrayList;
import java.util.List;
public class AdminroleExample {
protected String orderByClause;
protected boolean distinct;
protected List<Criteria> oredCriteria;
public AdminroleExample() {
oredCriteria = new ArrayList<Criteria>();
}
public void setOrderByClause(String orderByClause) {
this.orderByClause = orderByClause;
}
public String getOrderByClause() {
return orderByClause;
}
public void setDistinct(boolean distinct) {
this.distinct = distinct;
}
public boolean isDistinct() {
return distinct;
}
public List<Criteria> getOredCriteria() {
return oredCriteria;
}
public void or(Criteria criteria) {
oredCriteria.add(criteria);
}
public Criteria or() {
Criteria criteria = createCriteriaInternal();
oredCriteria.add(criteria);
return criteria;
}
public Criteria createCriteria() {
Criteria criteria = createCriteriaInternal();
if (oredCriteria.size() == 0) {
oredCriteria.add(criteria);
}
return criteria;
}
protected Criteria createCriteriaInternal() {
Criteria criteria = new Criteria();
return criteria;
}
public void clear() {
oredCriteria.clear();
orderByClause = null;
distinct = false;
}
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 andRoleIdIsNull() {
addCriterion("roleId is null");
return (Criteria) this;
}
public Criteria andRoleIdIsNotNull() {
addCriterion("roleId is not null");
return (Criteria) this;
}
public Criteria andRoleIdEqualTo(Integer value) {
addCriterion("roleId =", value, "roleId");
return (Criteria) this;
}
public Criteria andRoleIdNotEqualTo(Integer value) {
addCriterion("roleId <>", value, "roleId");
return (Criteria) this;
}
public Criteria andRoleIdGreaterThan(Integer value) {
addCriterion("roleId >", value, "roleId");
return (Criteria) this;
}
public Criteria andRoleIdGreaterThanOrEqualTo(Integer value) {
addCriterion("roleId >=", value, "roleId");
return (Criteria) this;
}
public Criteria andRoleIdLessThan(Integer value) {
addCriterion("roleId <", value, "roleId");
return (Criteria) this;
}
public Criteria andRoleIdLessThanOrEqualTo(Integer value) {
addCriterion("roleId <=", value, "roleId");
return (Criteria) this;
}
public Criteria andRoleIdIn(List<Integer> values) {
addCriterion("roleId in", values, "roleId");
return (Criteria) this;
}
public Criteria andRoleIdNotIn(List<Integer> values) {
addCriterion("roleId not in", values, "roleId");
return (Criteria) this;
}
public Criteria andRoleIdBetween(Integer value1, Integer value2) {
addCriterion("roleId between", value1, value2, "roleId");
return (Criteria) this;
}
public Criteria andRoleIdNotBetween(Integer value1, Integer value2) {
addCriterion("roleId not between", value1, value2, "roleId");
return (Criteria) this;
}
public Criteria andRoleNameIsNull() {
addCriterion("roleName is null");
return (Criteria) this;
}
public Criteria andRoleNameIsNotNull() {
addCriterion("roleName is not null");
return (Criteria) this;
}
public Criteria andRoleNameEqualTo(String value) {
addCriterion("roleName =", value, "roleName");
return (Criteria) this;
}
public Criteria andRoleNameNotEqualTo(String value) {
addCriterion("roleName <>", value, "roleName");
return (Criteria) this;
}
public Criteria andRoleNameGreaterThan(String value) {
addCriterion("roleName >", value, "roleName");
return (Criteria) this;
}
public Criteria andRoleNameGreaterThanOrEqualTo(String value) {
addCriterion("roleName >=", value, "roleName");
return (Criteria) this;
}
public Criteria andRoleNameLessThan(String value) {
addCriterion("roleName <", value, "roleName");
return (Criteria) this;
}
public Criteria andRoleNameLessThanOrEqualTo(String value) {
addCriterion("roleName <=", value, "roleName");
return (Criteria) this;
}
public Criteria andRoleNameLike(String value) {
addCriterion("roleName like", value, "roleName");
return (Criteria) this;
}
public Criteria andRoleNameNotLike(String value) {
addCriterion("roleName not like", value, "roleName");
return (Criteria) this;
}
public Criteria andRoleNameIn(List<String> values) {
addCriterion("roleName in", values, "roleName");
return (Criteria) this;
}
public Criteria andRoleNameNotIn(List<String> values) {
addCriterion("roleName not in", values, "roleName");
return (Criteria) this;
}
public Criteria andRoleNameBetween(String value1, String value2) {
addCriterion("roleName between", value1, value2, "roleName");
return (Criteria) this;
}
public Criteria andRoleNameNotBetween(String value1, String value2) {
addCriterion("roleName not between", value1, value2, "roleName");
return (Criteria) this;
}
}
public static class Criteria extends GeneratedCriteria {
protected Criteria() {
super();
}
}
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);
}
}
}
2、mapper包下的类
这个包下的类是生成的一些单表操作的方法的接口
3、resource下的 .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.kkb.mapper.AdminroleMapper">
<resultMap id="BaseResultMap" type="com.kkb.pojo.Adminrole">
<id column="roleId" jdbcType="INTEGER" property="roleId" />
<result column="roleName" jdbcType="VARCHAR" property="roleName" />
</resultMap>
<sql id="Example_Where_Clause">
<where>
<foreach collection="oredCriteria" item="criteria" separator="or">
<if test="criteria.valid">
<trim prefix="(" prefixOverrides="and" suffix=")">
<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 close=")" collection="criterion.value" item="listItem" open="(" separator=",">
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Update_By_Example_Where_Clause">
<where>
<foreach collection="example.oredCriteria" item="criteria" separator="or">
<if test="criteria.valid">
<trim prefix="(" prefixOverrides="and" suffix=")">
<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 close=")" collection="criterion.value" item="listItem" open="(" separator=",">
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Base_Column_List">
roleId, roleName
</sql>
<select id="selectByExample" parameterType="com.kkb.pojo.AdminroleExample" resultMap="BaseResultMap">
select
<if test="distinct">
distinct
</if>
<include refid="Base_Column_List" />
from adminrole
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
<if test="orderByClause != null">
order by ${orderByClause}
</if>
</select>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from adminrole
where roleId = #{roleId,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from adminrole
where roleId = #{roleId,jdbcType=INTEGER}
</delete>
<delete id="deleteByExample" parameterType="com.kkb.pojo.AdminroleExample">
delete from adminrole
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
</delete>
<insert id="insert" parameterType="com.kkb.pojo.Adminrole">
insert into adminrole (roleId, roleName)
values (#{roleId,jdbcType=INTEGER}, #{roleName,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" parameterType="com.kkb.pojo.Adminrole">
insert into adminrole
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="roleId != null">
roleId,
</if>
<if test="roleName != null">
roleName,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="roleId != null">
#{roleId,jdbcType=INTEGER},
</if>
<if test="roleName != null">
#{roleName,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<select id="countByExample" parameterType="com.kkb.pojo.AdminroleExample" resultType="java.lang.Long">
select count(*) from adminrole
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
</select>
<update id="updateByExampleSelective" parameterType="map">
update adminrole
<set>
<if test="record.roleId != null">
roleId = #{record.roleId,jdbcType=INTEGER},
</if>
<if test="record.roleName != null">
roleName = #{record.roleName,jdbcType=VARCHAR},
</if>
</set>
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="updateByExample" parameterType="map">
update adminrole
set roleId = #{record.roleId,jdbcType=INTEGER},
roleName = #{record.roleName,jdbcType=VARCHAR}
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="updateByPrimaryKeySelective" parameterType="com.kkb.pojo.Adminrole">
update adminrole
<set>
<if test="roleName != null">
roleName = #{roleName,jdbcType=VARCHAR},
</if>
</set>
where roleId = #{roleId,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.kkb.pojo.Adminrole">
update adminrole
set roleName = #{roleName,jdbcType=VARCHAR}
where roleId = #{roleId,jdbcType=INTEGER}
</update>
<resultMap id="BaseResultMap" type="com.kkb.pojo.Adminrole">
<id column="roleId" jdbcType="INTEGER" property="roleId" />
<result column="roleName" jdbcType="VARCHAR" property="roleName" />
</resultMap>
<sql id="Example_Where_Clause">
<where>
<foreach collection="oredCriteria" item="criteria" separator="or">
<if test="criteria.valid">
<trim prefix="(" prefixOverrides="and" suffix=")">
<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 close=")" collection="criterion.value" item="listItem" open="(" separator=",">
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Update_By_Example_Where_Clause">
<where>
<foreach collection="example.oredCriteria" item="criteria" separator="or">
<if test="criteria.valid">
<trim prefix="(" prefixOverrides="and" suffix=")">
<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 close=")" collection="criterion.value" item="listItem" open="(" separator=",">
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Base_Column_List">
roleId, roleName
</sql>
<select id="selectByExample" parameterType="com.kkb.pojo.AdminroleExample" resultMap="BaseResultMap">
select
<if test="distinct">
distinct
</if>
<include refid="Base_Column_List" />
from adminrole
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
<if test="orderByClause != null">
order by ${orderByClause}
</if>
</select>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from adminrole
where roleId = #{roleId,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from adminrole
where roleId = #{roleId,jdbcType=INTEGER}
</delete>
<delete id="deleteByExample" parameterType="com.kkb.pojo.AdminroleExample">
delete from adminrole
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
</delete>
<insert id="insert" parameterType="com.kkb.pojo.Adminrole">
insert into adminrole (roleId, roleName)
values (#{roleId,jdbcType=INTEGER}, #{roleName,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" parameterType="com.kkb.pojo.Adminrole">
insert into adminrole
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="roleId != null">
roleId,
</if>
<if test="roleName != null">
roleName,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="roleId != null">
#{roleId,jdbcType=INTEGER},
</if>
<if test="roleName != null">
#{roleName,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<select id="countByExample" parameterType="com.kkb.pojo.AdminroleExample" resultType="java.lang.Long">
select count(*) from adminrole
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
</select>
<update id="updateByExampleSelective" parameterType="map">
update adminrole
<set>
<if test="record.roleId != null">
roleId = #{record.roleId,jdbcType=INTEGER},
</if>
<if test="record.roleName != null">
roleName = #{record.roleName,jdbcType=VARCHAR},
</if>
</set>
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="updateByExample" parameterType="map">
update adminrole
set roleId = #{record.roleId,jdbcType=INTEGER},
roleName = #{record.roleName,jdbcType=VARCHAR}
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="updateByPrimaryKeySelective" parameterType="com.kkb.pojo.Adminrole">
update adminrole
<set>
<if test="roleName != null">
roleName = #{roleName,jdbcType=VARCHAR},
</if>
</set>
where roleId = #{roleId,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.kkb.pojo.Adminrole">
update adminrole
set roleName = #{roleName,jdbcType=VARCHAR}
where roleId = #{roleId,jdbcType=INTEGER}
</update>
</mapper>

类里面的就是一些操作数据库时需要用到的一些sql语句。
四:用法
1、记得在mybatis的配置文件中注册我们生成的pojo 和 mapper 类
2、测试类:
private TeamMapper mapper = Mybatis.getSqlSessions().getMapper(TeamMapper.class); //这里的mapper就相当于以前的dao层了。
在测试中我们直接使用mapper. 方法名() 调方法就可以了。
这里采用了自动生成的dao的代理方式。
3、多条件操作:
private TeamMapper mapper = Mybatis.getSqlSessions().getMapper(TeamMapper.class); //这里的mapper就相当于以前的dao层了。
//TeamExample是一个为多条件、排序等服务的一个类 TeamExample example = new TeamExample(); //创建一个用于放多条件的一个容器 TeamExample.Criteria criteria = example.createCriteria(); //向容器中添加条件 criteria.addTeamLike(”人“); criteria.addTeamIdBetween(1001,1100); //调用方法,将我们的example放进入 List<Team> teams = mapper.selectByExample(example); ......
在这里大家可能有个疑虑,就是我们的mapper接口没有实现类,在这里解释一下,这里的实现类是spring框架在运行时自动实现的,可以理解为动态代理

浙公网安备 33010602011771号