缺点:
1、MySQL数据库中有2个以上数据类型为text的字段,pojo、dao、mapper中会多生成一个名为~WithBLOBs的pojo,其中字段为数据库中类型为text的字段,Java中为String。
2、数据类型为tinyint,会生成Boolean型的属性。如表示type、status,最好用int(1)。
优点:
1、方便,根据数据库自动生成相应的pojo、dao、mapper,如果改变数据库,需删除mapper.xml,否则xml文件中会append,而不是重新生成。
2、利用example、Criteria执行各种查询方便,同时也存在缺点,代码量多,不如直接写相应的方法和mybatis的sql。
生成用法:
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>
<context id="mysql" targetRuntime="MyBatis3">
<plugin type="org.mybatis.plugin.MySQLLimitPlugin"></plugin>
<commentGenerator>
<property name="suppressDate" value="true" />
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--数据库链接URL,用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/数据库" userId="用户名"
password="密码">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"></property>
</javaTypeResolver>
<!-- 生成模型的包名和位置 -->
<javaModelGenerator targetPackage="pojo包名,如com.wj.pojo"
targetProject="位置,如src/main/java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 生成映射文件的包名和位置 -->
<sqlMapGenerator targetPackage="mapper映射文件包名,如com.wj.mapper"
targetProject="位置,如src/main/resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 生成DAO的包名与位置 -->
<javaClientGenerator targetPackage="dao包名,如com.wj.dao"
type="XMLMAPPER" targetProject="位置,如src/main/java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 要生成的表名或视图名,domainObjectName是实体类名 -->
<!-- 优惠管理 -->
<table tableName="数据库表,如table_user" domainObjectName="pojo文件,如TableUser"></table>
</context>
</generatorConfiguration>
MybatisGeneratorMain.java
package org.mybatis.generator;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
public class MybatisGeneratorMain {
public static void main(String[] args) throws IOException, XMLParserException, SQLException,
InterruptedException, InvalidConfigurationException {
List<String> warnings = new ArrayList<>();
boolean overwrite = true;
File configFile = new File(MyBatisGenerator.class.getResource("/generatorConfig.xml").getFile());
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);
System.out.print("生成完成");
}
}
Example.java,举例SchoolExample.java
package com.edu.pojo;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class SchoolExample {
protected String orderByClause;
protected boolean distinct;
protected List<Criteria> oredCriteria;
private Integer limit;
private Integer offset;
public SchoolExample() {
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;
}
public void setLimit(Integer limit) {
this.limit = limit;
}
public Integer getLimit() {
return limit;
}
public void setOffset(Integer offset) {
this.offset = offset;
}
public Integer getOffset() {
return offset;
}
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 andRegionIsNull() {
addCriterion("region is null");
return (Criteria) this;
}
public Criteria andRegionIsNotNull() {
addCriterion("region is not null");
return (Criteria) this;
}
public Criteria andRegionEqualTo(String value) {
addCriterion("region =", value, "region");
return (Criteria) this;
}
public Criteria andRegionNotEqualTo(String value) {
addCriterion("region <>", value, "region");
return (Criteria) this;
}
public Criteria andRegionGreaterThan(String value) {
addCriterion("region >", value, "region");
return (Criteria) this;
}
public Criteria andRegionGreaterThanOrEqualTo(String value) {
addCriterion("region >=", value, "region");
return (Criteria) this;
}
public Criteria andRegionLessThan(String value) {
addCriterion("region <", value, "region");
return (Criteria) this;
}
public Criteria andRegionLessThanOrEqualTo(String value) {
addCriterion("region <=", value, "region");
return (Criteria) this;
}
public Criteria andRegionLike(String value) {
addCriterion("region like", value, "region");
return (Criteria) this;
}
public Criteria andRegionNotLike(String value) {
addCriterion("region not like", value, "region");
return (Criteria) this;
}
public Criteria andRegionIn(List<String> values) {
addCriterion("region in", values, "region");
return (Criteria) this;
}
public Criteria andRegionNotIn(List<String> values) {
addCriterion("region not in", values, "region");
return (Criteria) this;
}
public Criteria andRegionBetween(String value1, String value2) {
addCriterion("region between", value1, value2, "region");
return (Criteria) this;
}
public Criteria andRegionNotBetween(String value1, String value2) {
addCriterion("region not between", value1, value2, "region");
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);
}
}
}
SchoolMapper.java
package com.edu.dao;
import com.edu.pojo.School;
import com.edu.pojo.SchoolExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;
public interface SchoolMapper {
int countByExample(SchoolExample example);
int deleteByExample(SchoolExample example);
int deleteByPrimaryKey(Integer id);
int insert(School record);
int insertSelective(School record);
List<School> selectByExample(SchoolExample example);
School selectByPrimaryKey(Integer id);
int updateByExampleSelective(@Param("record") School record, @Param("example") SchoolExample example);
int updateByExample(@Param("record") School record, @Param("example") SchoolExample example);
int updateByPrimaryKeySelective(School record);
int updateByPrimaryKey(School record);
}
<?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.edu.dao.SchoolMapper">
<resultMap id="BaseResultMap" type="com.edu.pojo.School">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="region" jdbcType="VARCHAR" property="region" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="period" jdbcType="VARCHAR" property="period" />
<result column="school_address" jdbcType="VARCHAR" property="schoolAddress" />
<result column="contact_number" jdbcType="VARCHAR" property="contactNumber" />
<result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
<result column="status" jdbcType="INTEGER" property="status" />
<result column="crm_id" jdbcType="VARCHAR" property="crmId" />
<result column="hr_id" jdbcType="VARCHAR" property="hrId" />
</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">
id, region, name, period, school_address, contact_number, create_time, status, crm_id,
hr_id
</sql>
<select id="selectByExample" parameterType="com.edu.pojo.SchoolExample" resultMap="BaseResultMap">
select
<if test="distinct">
distinct
</if>
<include refid="Base_Column_List" />
from school
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
<if test="orderByClause != null">
order by ${orderByClause}
</if>
<if test="limit != null">
<if test="offset != null">
limit ${offset}, ${limit}
</if>
<if test="offset == null">
limit ${limit}
</if>
</if>
</select>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from school
where id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from school
where id = #{id,jdbcType=INTEGER}
</delete>
<delete id="deleteByExample" parameterType="com.edu.pojo.SchoolExample">
delete from school
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
</delete>
<insert id="insert" parameterType="com.edu.pojo.School">
insert into school (id, region, name,
period, school_address, contact_number,
create_time, status, crm_id,
hr_id)
values (#{id,jdbcType=INTEGER}, #{region,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR},
#{period,jdbcType=VARCHAR}, #{schoolAddress,jdbcType=VARCHAR}, #{contactNumber,jdbcType=VARCHAR},
#{createTime,jdbcType=TIMESTAMP}, #{status,jdbcType=INTEGER}, #{crmId,jdbcType=VARCHAR},
#{hrId,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" parameterType="com.edu.pojo.School">
insert into school
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="region != null">
region,
</if>
<if test="name != null">
name,
</if>
<if test="period != null">
period,
</if>
<if test="schoolAddress != null">
school_address,
</if>
<if test="contactNumber != null">
contact_number,
</if>
<if test="createTime != null">
create_time,
</if>
<if test="status != null">
status,
</if>
<if test="crmId != null">
crm_id,
</if>
<if test="hrId != null">
hr_id,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=INTEGER},
</if>
<if test="region != null">
#{region,jdbcType=VARCHAR},
</if>
<if test="name != null">
#{name,jdbcType=VARCHAR},
</if>
<if test="period != null">
#{period,jdbcType=VARCHAR},
</if>
<if test="schoolAddress != null">
#{schoolAddress,jdbcType=VARCHAR},
</if>
<if test="contactNumber != null">
#{contactNumber,jdbcType=VARCHAR},
</if>
<if test="createTime != null">
#{createTime,jdbcType=TIMESTAMP},
</if>
<if test="status != null">
#{status,jdbcType=INTEGER},
</if>
<if test="crmId != null">
#{crmId,jdbcType=VARCHAR},
</if>
<if test="hrId != null">
#{hrId,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<select id="countByExample" parameterType="com.edu.pojo.SchoolExample" resultType="java.lang.Integer">
select count(*) from school
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
</select>
<update id="updateByExampleSelective" parameterType="map">
update school
<set>
<if test="record.id != null">
id = #{record.id,jdbcType=INTEGER},
</if>
<if test="record.region != null">
region = #{record.region,jdbcType=VARCHAR},
</if>
<if test="record.name != null">
name = #{record.name,jdbcType=VARCHAR},
</if>
<if test="record.period != null">
period = #{record.period,jdbcType=VARCHAR},
</if>
<if test="record.schoolAddress != null">
school_address = #{record.schoolAddress,jdbcType=VARCHAR},
</if>
<if test="record.contactNumber != null">
contact_number = #{record.contactNumber,jdbcType=VARCHAR},
</if>
<if test="record.createTime != null">
create_time = #{record.createTime,jdbcType=TIMESTAMP},
</if>
<if test="record.status != null">
status = #{record.status,jdbcType=INTEGER},
</if>
<if test="record.crmId != null">
crm_id = #{record.crmId,jdbcType=VARCHAR},
</if>
<if test="record.hrId != null">
hr_id = #{record.hrId,jdbcType=VARCHAR},
</if>
</set>
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="updateByExample" parameterType="map">
update school
set id = #{record.id,jdbcType=INTEGER},
region = #{record.region,jdbcType=VARCHAR},
name = #{record.name,jdbcType=VARCHAR},
period = #{record.period,jdbcType=VARCHAR},
school_address = #{record.schoolAddress,jdbcType=VARCHAR},
contact_number = #{record.contactNumber,jdbcType=VARCHAR},
create_time = #{record.createTime,jdbcType=TIMESTAMP},
status = #{record.status,jdbcType=INTEGER},
crm_id = #{record.crmId,jdbcType=VARCHAR},
hr_id = #{record.hrId,jdbcType=VARCHAR}
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="updateByPrimaryKeySelective" parameterType="com.edu.pojo.School">
update school
<set>
<if test="region != null">
region = #{region,jdbcType=VARCHAR},
</if>
<if test="name != null">
name = #{name,jdbcType=VARCHAR},
</if>
<if test="period != null">
period = #{period,jdbcType=VARCHAR},
</if>
<if test="schoolAddress != null">
school_address = #{schoolAddress,jdbcType=VARCHAR},
</if>
<if test="contactNumber != null">
contact_number = #{contactNumber,jdbcType=VARCHAR},
</if>
<if test="createTime != null">
create_time = #{createTime,jdbcType=TIMESTAMP},
</if>
<if test="status != null">
status = #{status,jdbcType=INTEGER},
</if>
<if test="crmId != null">
crm_id = #{crmId,jdbcType=VARCHAR},
</if>
<if test="hrId != null">
hr_id = #{hrId,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.edu.pojo.School">
update school
set region = #{region,jdbcType=VARCHAR},
name = #{name,jdbcType=VARCHAR},
period = #{period,jdbcType=VARCHAR},
school_address = #{schoolAddress,jdbcType=VARCHAR},
contact_number = #{contactNumber,jdbcType=VARCHAR},
create_time = #{createTime,jdbcType=TIMESTAMP},
status = #{status,jdbcType=INTEGER},
crm_id = #{crmId,jdbcType=VARCHAR},
hr_id = #{hrId,jdbcType=VARCHAR}
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>
Example使用用法CURD
A、查
List<StudentCourseCart> selectByExample(StudentCourseCartExample example);
StudentCourseCart selectByPrimaryKey(Integer id);
1、主键查询
selectByPrimaryKey(主键)
2、模糊查询
1)
StudentExample example = new StudentExample();
example.createCriteria().andSnoEqualTo(sno);
//example.setOrderByClause("idx ASC");
List<Student> students = studentMapper.selectByExample(example);
2)
StudentCourseCartExample example = new StudentCourseCartExample();
com.edu.pojo.StudentCourseCartExample.Criteria criteria = example.createCriteria();
criteria.andSnoEqualTo(sno);
criteria.andStatusEqualTo(0);
List<StudentCourseCart> cart= studentCourseCartMapper.selectByExample(example);
B、增
int insert(StudentCourseCart record);
int insertSelective(StudentCourseCart record);
C、删
int deleteByExample(StudentCourseCartExample example);
int deleteByPrimaryKey(Integer id);
D、改
int updateByExampleSelective(@Param("record") StudentCourseCart record, @Param("example") StudentCourseCartExample example);
int updateByExample(@Param("record") StudentCourseCart record, @Param("example") StudentCourseCartExample example);
int updateByPrimaryKeySelective(StudentCourseCart record);
int updateByPrimaryKey(StudentCourseCart record);