第一章 什么是Mybatis Generator?
MyBatis Generator Core – Introduction to MyBatis Generator
MyBatis生成器(MBG)是MyBatis框架的代码生成工具。它支持为所有版本的MyBatis生成代码,通过解析数据库表(或多个表)结构,自动生成用于访问这些表的相关组件。这有效减轻了手动配置对象和配置文件以实现数据库表交互的初始繁琐工作。MBG主要致力于简化大量基础性的数据库操作——即常见的增删改查(CRUD)操作。但对于涉及联合查询或存储过程等复杂场景,仍需开发者手动编写SQL和对象。
尽管限制新版本的MBG可以生成带注解版本(不包含xml)的Java代码,但是注解实现复杂场景(表join查询)时不是太方便,所以一般推荐使用传统版本的xml配置文件进行映射。
第二章 Mybatis Generator 各版本的功能?
我们主要使用的两个版本:
MyBatis3DynamicSQL:可生成不带xml配置的Java注解版代码。
Mybatis3:生成传统的使用XML进行映射的访问数据库代码。
Runtime | MyBatis Generator Version | MyBatis Version | MyBatis Dynamic SQL Version |
---|---|---|---|
MyBatis3, MyBatis3Simple | Any | 3.0+ | N/A |
MyBatis3DynamicSQL | 1.3.6 - 1.3.7 | 3.4.2+ | 1.1.0 - 1.2.1 |
MyBatis3DynamicSQL, MyBatis3Kotlin | 1.4.0 | 3.4.2+ | 1.1.3+ |
MyBatis3DynamicSQL | 1.4.1+ | 3.4.2+ | 1.3.1+ |
MyBatis3Kotlin | 1.4.1+ | 3.4.2+ | 1.4.0+ |
通常我们使用maven插件来进行代码的生成,多次运行代码Mybatis Generator的处理方式。
注意:当Mapper.java或者Mapper.xml有改动时,默认情况下时自动合并xml文件(保留已经修改的内容),但是Mapper.java会生成一个新的带版本的java文件(比如Mapper.java.1),需要手动合并。
比较推荐的做法是:使用原生的生成文件不做任何改动,需要修改时使用MapperExt.java, MapperExt.xml进行修改。这样省去了合并代码的工作。
第三章 怎样使用Mybatis Genrator?
3.1.引入依赖
<dependencies>
<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
<version>2.1.1</version>
<scope>provided</scope>
</dependency>
<!-- MyBatis 核心依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.13</version>
</dependency>
<!-- MySQL 连接器依赖 -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.4.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.11.4</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.4.2</version>
<configuration>
<configurationFile>src/main/resources/mybatis-generator-config.xml</configurationFile>
</configuration>
<dependencies>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.4.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
此处数据库默认使用的MySQL,所以引入mysql的driver. 同时使用maven运行mybatis generator,加入maven mybatis-generator-maven-plugin.
3.2 加入配置文件
配置文件名称: mybatis-generator-config.xml
<!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="mybatis3" targetRuntime="MyBatis3">
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/test_db"
userId="user_name"
password="my_password"/>
<javaModelGenerator targetPackage="com.my.model" targetProject="src/main/java"/>
<sqlMapGenerator targetPackage="com.my.mapper" targetProject="src/main/resources"/>
<javaClientGenerator type="XMLMAPPER" targetPackage="com.my.mapper" targetProject="src/main/java"/>
<table tableName="t_user" domainObjectName="User" />
<table tableName="t_teacher" domainObjectName="Teacher" />
<table tableName="t_student" domainObjectName="Student" />
</context>
</generatorConfiguration>
根据配置文件中的内容,修改自己的数据库连接信息,需要放置的包名称,需要生成的表名称。
3.运行命令
mvn mybatis-generator:generate
然后代码就自动生成了。就可以完美的进行单表操作了。
第四章 生成代码的使用?
生成的Mapper.xml就不看了,写得很棒很专业。我们需要看看Mapper.java 和Example类,这样可以知道生成的类怎样使用?
4.1 UserExample类
下面以User的Example类进行说明,该类的含义和使用。
package com.my.model;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class UserExample {
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database table user
*
* @mbg.generated Sat Mar 08 07:25:49 CST 2025
*/
protected String orderByClause;
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database table user
*
* @mbg.generated Sat Mar 08 07:25:49 CST 2025
*/
protected boolean distinct;
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database table user
*
* @mbg.generated Sat Mar 08 07:25:49 CST 2025
*/
protected List<Criteria> oredCriteria;
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table user
*
* @mbg.generated Sat Mar 08 07:25:49 CST 2025
*/
public UserExample() {
oredCriteria = new ArrayList<>();
}
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table user
*
* @mbg.generated Sat Mar 08 07:25:49 CST 2025
*/
public void setOrderByClause(String orderByClause) {
this.orderByClause = orderByClause;
}
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table user
*
* @mbg.generated Sat Mar 08 07:25:49 CST 2025
*/
public String getOrderByClause() {
return orderByClause;
}
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table user
*
* @mbg.generated Sat Mar 08 07:25:49 CST 2025
*/
public void setDistinct(boolean distinct) {
this.distinct = distinct;
}
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table user
*
* @mbg.generated Sat Mar 08 07:25:49 CST 2025
*/
public boolean isDistinct() {
return distinct;
}
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table user
*
* @mbg.generated Sat Mar 08 07:25:49 CST 2025
*/
public List<Criteria> getOredCriteria() {
return oredCriteria;
}
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table user
*
* @mbg.generated Sat Mar 08 07:25:49 CST 2025
*/
public void or(Criteria criteria) {
oredCriteria.add(criteria);
}
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table user
*
* @mbg.generated Sat Mar 08 07:25:49 CST 2025
*/
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 user
*
* @mbg.generated Sat Mar 08 07:25:49 CST 2025
*/
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 user
*
* @mbg.generated Sat Mar 08 07:25:49 CST 2025
*/
protected Criteria createCriteriaInternal() {
Criteria criteria = new Criteria();
return criteria;
}
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table user
*
* @mbg.generated Sat Mar 08 07:25:49 CST 2025
*/
public void clear() {
oredCriteria.clear();
orderByClause = null;
distinct = false;
}
/**
* This class was generated by MyBatis Generator.
* This class corresponds to the database table user
*
* @mbg.generated Sat Mar 08 07:25:49 CST 2025
*/
protected abstract static class GeneratedCriteria {
protected List<Criterion> criteria;
protected GeneratedCriteria() {
super();
criteria = new ArrayList<>();
}
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 andUserIdIsNull() {
addCriterion("USER_ID is null");
return (Criteria) this;
}
public Criteria andUserIdIsNotNull() {
addCriterion("USER_ID is not null");
return (Criteria) this;
}
public Criteria andUserIdEqualTo(Integer value) {
addCriterion("USER_ID =", value, "userId");
return (Criteria) this;
}
public Criteria andUserIdNotEqualTo(Integer value) {
addCriterion("USER_ID <>", value, "userId");
return (Criteria) this;
}
public Criteria andUserIdGreaterThan(Integer value) {
addCriterion("USER_ID >", value, "userId");
return (Criteria) this;
}
public Criteria andUserIdGreaterThanOrEqualTo(Integer value) {
addCriterion("USER_ID >=", value, "userId");
return (Criteria) this;
}
public Criteria andUserIdLessThan(Integer value) {
addCriterion("USER_ID <", value, "userId");
return (Criteria) this;
}
public Criteria andUserIdLessThanOrEqualTo(Integer value) {
addCriterion("USER_ID <=", value, "userId");
return (Criteria) this;
}
public Criteria andUserIdIn(List<Integer> values) {
addCriterion("USER_ID in", values, "userId");
return (Criteria) this;
}
public Criteria andUserIdNotIn(List<Integer> values) {
addCriterion("USER_ID not in", values, "userId");
return (Criteria) this;
}
public Criteria andUserIdBetween(Integer value1, Integer value2) {
addCriterion("USER_ID between", value1, value2, "userId");
return (Criteria) this;
}
public Criteria andUserIdNotBetween(Integer value1, Integer value2) {
addCriterion("USER_ID not between", value1, value2, "userId");
return (Criteria) this;
}
public Criteria andUserNameIsNull() {
addCriterion("USER_NAME is null");
return (Criteria) this;
}
public Criteria andUserNameIsNotNull() {
addCriterion("USER_NAME is not null");
return (Criteria) this;
}
public Criteria andUserNameEqualTo(String value) {
addCriterion("USER_NAME =", value, "userName");
return (Criteria) this;
}
public Criteria andUserNameNotEqualTo(String value) {
addCriterion("USER_NAME <>", value, "userName");
return (Criteria) this;
}
public Criteria andUserNameGreaterThan(String value) {
addCriterion("USER_NAME >", value, "userName");
return (Criteria) this;
}
public Criteria andUserNameGreaterThanOrEqualTo(String value) {
addCriterion("USER_NAME >=", value, "userName");
return (Criteria) this;
}
public Criteria andUserNameLessThan(String value) {
addCriterion("USER_NAME <", value, "userName");
return (Criteria) this;
}
public Criteria andUserNameLessThanOrEqualTo(String value) {
addCriterion("USER_NAME <=", value, "userName");
return (Criteria) this;
}
public Criteria andUserNameLike(String value) {
addCriterion("USER_NAME like", value, "userName");
return (Criteria) this;
}
public Criteria andUserNameNotLike(String value) {
addCriterion("USER_NAME not like", value, "userName");
return (Criteria) this;
}
public Criteria andUserNameIn(List<String> values) {
addCriterion("USER_NAME in", values, "userName");
return (Criteria) this;
}
public Criteria andUserNameNotIn(List<String> values) {
addCriterion("USER_NAME not in", values, "userName");
return (Criteria) this;
}
public Criteria andUserNameBetween(String value1, String value2) {
addCriterion("USER_NAME between", value1, value2, "userName");
return (Criteria) this;
}
public Criteria andUserNameNotBetween(String value1, String value2) {
addCriterion("USER_NAME not between", value1, value2, "userName");
return (Criteria) this;
}
public Criteria andAgeIsNull() {
addCriterion("AGE is null");
return (Criteria) this;
}
public Criteria andAgeIsNotNull() {
addCriterion("AGE is not null");
return (Criteria) this;
}
public Criteria andAgeEqualTo(Integer value) {
addCriterion("AGE =", value, "age");
return (Criteria) this;
}
public Criteria andAgeNotEqualTo(Integer value) {
addCriterion("AGE <>", value, "age");
return (Criteria) this;
}
public Criteria andAgeGreaterThan(Integer value) {
addCriterion("AGE >", value, "age");
return (Criteria) this;
}
public Criteria andAgeGreaterThanOrEqualTo(Integer value) {
addCriterion("AGE >=", value, "age");
return (Criteria) this;
}
public Criteria andAgeLessThan(Integer value) {
addCriterion("AGE <", value, "age");
return (Criteria) this;
}
public Criteria andAgeLessThanOrEqualTo(Integer value) {
addCriterion("AGE <=", value, "age");
return (Criteria) this;
}
public Criteria andAgeIn(List<Integer> values) {
addCriterion("AGE in", values, "age");
return (Criteria) this;
}
public Criteria andAgeNotIn(List<Integer> values) {
addCriterion("AGE not in", values, "age");
return (Criteria) this;
}
public Criteria andAgeBetween(Integer value1, Integer value2) {
addCriterion("AGE between", value1, value2, "age");
return (Criteria) this;
}
public Criteria andAgeNotBetween(Integer value1, Integer value2) {
addCriterion("AGE not between", value1, value2, "age");
return (Criteria) this;
}
public Criteria andCreatedTimeIsNull() {
addCriterion("CREATED_TIME is null");
return (Criteria) this;
}
public Criteria andCreatedTimeIsNotNull() {
addCriterion("CREATED_TIME is not null");
return (Criteria) this;
}
public Criteria andCreatedTimeEqualTo(Date value) {
addCriterion("CREATED_TIME =", value, "createdTime");
return (Criteria) this;
}
public Criteria andCreatedTimeNotEqualTo(Date value) {
addCriterion("CREATED_TIME <>", value, "createdTime");
return (Criteria) this;
}
public Criteria andCreatedTimeGreaterThan(Date value) {
addCriterion("CREATED_TIME >", value, "createdTime");
return (Criteria) this;
}
public Criteria andCreatedTimeGreaterThanOrEqualTo(Date value) {
addCriterion("CREATED_TIME >=", value, "createdTime");
return (Criteria) this;
}
public Criteria andCreatedTimeLessThan(Date value) {
addCriterion("CREATED_TIME <", value, "createdTime");
return (Criteria) this;
}
public Criteria andCreatedTimeLessThanOrEqualTo(Date value) {
addCriterion("CREATED_TIME <=", value, "createdTime");
return (Criteria) this;
}
public Criteria andCreatedTimeIn(List<Date> values) {
addCriterion("CREATED_TIME in", values, "createdTime");
return (Criteria) this;
}
public Criteria andCreatedTimeNotIn(List<Date> values) {
addCriterion("CREATED_TIME not in", values, "createdTime");
return (Criteria) this;
}
public Criteria andCreatedTimeBetween(Date value1, Date value2) {
addCriterion("CREATED_TIME between", value1, value2, "createdTime");
return (Criteria) this;
}
public Criteria andCreatedTimeNotBetween(Date value1, Date value2) {
addCriterion("CREATED_TIME not between", value1, value2, "createdTime");
return (Criteria) this;
}
public Criteria andCreatedByIsNull() {
addCriterion("CREATED_BY is null");
return (Criteria) this;
}
public Criteria andCreatedByIsNotNull() {
addCriterion("CREATED_BY is not null");
return (Criteria) this;
}
public Criteria andCreatedByEqualTo(String value) {
addCriterion("CREATED_BY =", value, "createdBy");
return (Criteria) this;
}
public Criteria andCreatedByNotEqualTo(String value) {
addCriterion("CREATED_BY <>", value, "createdBy");
return (Criteria) this;
}
public Criteria andCreatedByGreaterThan(String value) {
addCriterion("CREATED_BY >", value, "createdBy");
return (Criteria) this;
}
public Criteria andCreatedByGreaterThanOrEqualTo(String value) {
addCriterion("CREATED_BY >=", value, "createdBy");
return (Criteria) this;
}
public Criteria andCreatedByLessThan(String value) {
addCriterion("CREATED_BY <", value, "createdBy");
return (Criteria) this;
}
public Criteria andCreatedByLessThanOrEqualTo(String value) {
addCriterion("CREATED_BY <=", value, "createdBy");
return (Criteria) this;
}
public Criteria andCreatedByLike(String value) {
addCriterion("CREATED_BY like", value, "createdBy");
return (Criteria) this;
}
public Criteria andCreatedByNotLike(String value) {
addCriterion("CREATED_BY not like", value, "createdBy");
return (Criteria) this;
}
public Criteria andCreatedByIn(List<String> values) {
addCriterion("CREATED_BY in", values, "createdBy");
return (Criteria) this;
}
public Criteria andCreatedByNotIn(List<String> values) {
addCriterion("CREATED_BY not in", values, "createdBy");
return (Criteria) this;
}
public Criteria andCreatedByBetween(String value1, String value2) {
addCriterion("CREATED_BY between", value1, value2, "createdBy");
return (Criteria) this;
}
public Criteria andCreatedByNotBetween(String value1, String value2) {
addCriterion("CREATED_BY not between", value1, value2, "createdBy");
return (Criteria) this;
}
public Criteria andUpdatedTimeIsNull() {
addCriterion("UPDATED_TIME is null");
return (Criteria) this;
}
public Criteria andUpdatedTimeIsNotNull() {
addCriterion("UPDATED_TIME is not null");
return (Criteria) this;
}
public Criteria andUpdatedTimeEqualTo(Date value) {
addCriterion("UPDATED_TIME =", value, "updatedTime");
return (Criteria) this;
}
public Criteria andUpdatedTimeNotEqualTo(Date value) {
addCriterion("UPDATED_TIME <>", value, "updatedTime");
return (Criteria) this;
}
public Criteria andUpdatedTimeGreaterThan(Date value) {
addCriterion("UPDATED_TIME >", value, "updatedTime");
return (Criteria) this;
}
public Criteria andUpdatedTimeGreaterThanOrEqualTo(Date value) {
addCriterion("UPDATED_TIME >=", value, "updatedTime");
return (Criteria) this;
}
public Criteria andUpdatedTimeLessThan(Date value) {
addCriterion("UPDATED_TIME <", value, "updatedTime");
return (Criteria) this;
}
public Criteria andUpdatedTimeLessThanOrEqualTo(Date value) {
addCriterion("UPDATED_TIME <=", value, "updatedTime");
return (Criteria) this;
}
public Criteria andUpdatedTimeIn(List<Date> values) {
addCriterion("UPDATED_TIME in", values, "updatedTime");
return (Criteria) this;
}
public Criteria andUpdatedTimeNotIn(List<Date> values) {
addCriterion("UPDATED_TIME not in", values, "updatedTime");
return (Criteria) this;
}
public Criteria andUpdatedTimeBetween(Date value1, Date value2) {
addCriterion("UPDATED_TIME between", value1, value2, "updatedTime");
return (Criteria) this;
}
public Criteria andUpdatedTimeNotBetween(Date value1, Date value2) {
addCriterion("UPDATED_TIME not between", value1, value2, "updatedTime");
return (Criteria) this;
}
public Criteria andUpdatedByIsNull() {
addCriterion("UPDATED_BY is null");
return (Criteria) this;
}
public Criteria andUpdatedByIsNotNull() {
addCriterion("UPDATED_BY is not null");
return (Criteria) this;
}
public Criteria andUpdatedByEqualTo(String value) {
addCriterion("UPDATED_BY =", value, "updatedBy");
return (Criteria) this;
}
public Criteria andUpdatedByNotEqualTo(String value) {
addCriterion("UPDATED_BY <>", value, "updatedBy");
return (Criteria) this;
}
public Criteria andUpdatedByGreaterThan(String value) {
addCriterion("UPDATED_BY >", value, "updatedBy");
return (Criteria) this;
}
public Criteria andUpdatedByGreaterThanOrEqualTo(String value) {
addCriterion("UPDATED_BY >=", value, "updatedBy");
return (Criteria) this;
}
public Criteria andUpdatedByLessThan(String value) {
addCriterion("UPDATED_BY <", value, "updatedBy");
return (Criteria) this;
}
public Criteria andUpdatedByLessThanOrEqualTo(String value) {
addCriterion("UPDATED_BY <=", value, "updatedBy");
return (Criteria) this;
}
public Criteria andUpdatedByLike(String value) {
addCriterion("UPDATED_BY like", value, "updatedBy");
return (Criteria) this;
}
public Criteria andUpdatedByNotLike(String value) {
addCriterion("UPDATED_BY not like", value, "updatedBy");
return (Criteria) this;
}
public Criteria andUpdatedByIn(List<String> values) {
addCriterion("UPDATED_BY in", values, "updatedBy");
return (Criteria) this;
}
public Criteria andUpdatedByNotIn(List<String> values) {
addCriterion("UPDATED_BY not in", values, "updatedBy");
return (Criteria) this;
}
public Criteria andUpdatedByBetween(String value1, String value2) {
addCriterion("UPDATED_BY between", value1, value2, "updatedBy");
return (Criteria) this;
}
public Criteria andUpdatedByNotBetween(String value1, String value2) {
addCriterion("UPDATED_BY not between", value1, value2, "updatedBy");
return (Criteria) this;
}
}
/**
* This class was generated by MyBatis Generator.
* This class corresponds to the database table user
*
* @mbg.generated do_not_delete_during_merge Sat Mar 08 07:25:49 CST 2025
*/
public static class Criteria extends GeneratedCriteria {
protected Criteria() {
super();
}
}
/**
* This class was generated by MyBatis Generator.
* This class corresponds to the database table user
*
* @mbg.generated Sat Mar 08 07:25:49 CST 2025
*/
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);
}
}
}
这个Example类,主要是使用Critiertion(单个的查询条件)和Critiera(多个查询条件)的封装类。
- 查询条件Critera --> List<Criterition>
- setDistinct 设置是否去重
- setOrderByClause(**) 设置排序的字段
使用它的方式如下:
UserExample example = new UserExample();
example.createCretiar().andCreatedByEqualTo("jack.zhang"); // 构建第一个and条件连接的查询链
example.or().anUserNameIsEqualTo("name"); // 构建第二个包含or里的 ( and 条件链)
example.setDistinct(true) // 设置去重
example.setOrderByClause("USER_NAME DESC"); // 设置排序字段
生成的SQL语句如下:
select
distinct USER_ID, USER_NAME, AGE, CREATED_TIME, CREATED_BY
from user
WHERE ( CREATED_BY = ? ) or ( USER_NAME = ? )
order by USER_ID ASC
4.2 UserMapper.java类
package com.keyrus.mapper;
import com.keyrus.model.User;
import com.keyrus.model.UserExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;
public interface UserMapper {
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table user
*
* @mbg.generated Sat Mar 08 07:25:49 CST 2025
*/
long countByExample(UserExample example);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table user
*
* @mbg.generated Sat Mar 08 07:25:49 CST 2025
*/
int deleteByExample(UserExample example);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table user
*
* @mbg.generated Sat Mar 08 07:25:49 CST 2025
*/
int deleteByPrimaryKey(Integer userId);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table user
*
* @mbg.generated Sat Mar 08 07:25:49 CST 2025
*/
int insert(User row);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table user
*
* @mbg.generated Sat Mar 08 07:25:49 CST 2025
*/
int insertSelective(User row);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table user
*
* @mbg.generated Sat Mar 08 07:25:49 CST 2025
*/
List<User> selectByExample(UserExample example);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table user
*
* @mbg.generated Sat Mar 08 07:25:49 CST 2025
*/
User selectByPrimaryKey(Integer userId);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table user
*
* @mbg.generated Sat Mar 08 07:25:49 CST 2025
*/
int updateByExampleSelective(@Param("row") User row, @Param("example") UserExample example);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table user
*
* @mbg.generated Sat Mar 08 07:25:49 CST 2025
*/
int updateByExample(@Param("row") User row, @Param("example") UserExample example);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table user
*
* @mbg.generated Sat Mar 08 07:25:49 CST 2025
*/
int updateByPrimaryKeySelective(User row);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table user
*
* @mbg.generated Sat Mar 08 07:25:49 CST 2025
*/
int updateByPrimaryKey(User row);
}
按照新增/修改/查询/删除的顺序,方法归纳如下:
insert(User user) 插入整个User对象;
insertSelective(User user) 插入部分字段;
updateByPrimaryKey(User user) 根据主键更新User;
updateByPrimaryKeySelective (User user) 根据主键部分更新User的字段;
updateByExampleSelective(User user, UserExample example) 根据example条件筛选出记录进行部分user字段的更新。
selectByPrimaryKey(id) 根据主键查询User;
selectByExample(UserExample exapmle) 根据example条件筛选查询User;
countByExample(UserExample example) 根据example条件查询统计记录数目;
deleteByPrimaryKey(id) 根据主键进行删除单条记录;
deleteByExample(UserExample example) 根据example条件筛选出多条记录进行删除;
基本上上面的四类方法,包含了日常单表的所有操作。
有复杂的表与表直接的操作,需要自己手写去完成。