CodingTechWork
在上一家公司一直使用Spring Data JPA
,它是对基于JPA的数据访问层的增强支持,底层使用Hibernate
框架,支持使用原生SQ
L或JPQL
查询语言。JPA
是基于ORM
的,它把代码和DB
分离,即在代码和DB
之间增加了新的一层,从而间接操作DB
,大家可能觉得这个学习成本比较高,但其实也还好,都用不到xml
文件等。
而在新的公司,使用的是Mybatis
,Mybatis
面向DB
基于SQL
的模式,复杂的SQL
需要通过mapper
写xml
文件来操作DB
可能更直观友好,当Mybatis
结合Mybatis-Plus
就原地起飞了。本文主要总结使用Mybatis-Plus
的一些实践经验及源码分析。
MyBatis-Plus
是Mybatis
的增强工具,可以简化开发,提高我们的开发效率。
Mybatis
的增强工具,不做侵入式改变。Lambda
表达式编写各类查询。Mapper
和IService
接口和各种操作方法。描述:表名注解,标识实体类。
描述:主键注解
值 | 说明 |
---|---|
AUTO | 数据库ID自增 |
NONE | 默认,无状态,该类型为未设置主键类型(注解里等于跟随全局,全局里约等于 INPUT) |
INPUT | insert 前自行 set 主键值 |
ASSIGN_ID | 分配 ID(主键类型为 Number(Long 和 Integer)或 String)(since 3.3.0),使用接口IdentifierGenerator的方法nextId(默认实现类为DefaultIdentifierGenerator雪花算法) |
ASSIGN_UUID | 分配 UUID,主键类型为 String(since 3.3.0),使用接口IdentifierGenerator的方法nextUUID(默认 default 方法) |
ID_WORKER | 分布式全局唯一 ID 长整型类型(please use ASSIGN_ID) |
UUID | 32 位 UUID 字符串(please use ASSIGN_UUID) |
ID_WORKER_STR | 分布式全局唯一 ID 字符串类型(please use ASSIGN_ID) |
描述:字段注解(非主键)
常用属性:
属性 | 类型 必须指定 默认值 描述 |
---|---|
value | String 否 “” 数据库字段名 |
exist | boolean 否 true 是否为数据库表字段 |
// 插入一条记录(选择字段,策略插入)
boolean save(T entity);
// 插入(批量)
boolean saveBatch(Collection<T> entityList);
// 根据插入批次数量,插入(批量)
boolean saveBatch(Collection<T> entityList, int batchSize);
// TableId 注解存在更新记录,是否插入一条记录或者更新记录
boolean saveOrUpdate(T entity);
// 根据updateWrapper尝试更新,否继续执行saveOrUpdate(T)方法
boolean saveOrUpdate(T entity, Wrapper<T> updateWrapper);
// 批量修改插入
boolean saveOrUpdateBatch(Collection<T> entityList);
// 根据插入批次数量,批量修改插入
boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize);
// 根据 entity 条件,删除记录
boolean remove(Wrapper<T> queryWrapper);
// 根据 ID 删除
boolean removeById(Serializable id);
// 根据 columnMap 条件,删除记录
boolean removeByMap(Map<String, Object> columnMap);
// 删除(根据ID 批量删除)
boolean removeByIds(Collection<? extends Serializable> idList);
// 根据 UpdateWrapper 条件,更新记录 需要设置sqlset
boolean update(Wrapper<T> updateWrapper);
// 根据 whereWrapper 条件,更新记录
boolean update(T updateEntity, Wrapper<T> whereWrapper);
// 根据 ID 选择修改
boolean updateById(T entity);
// 根据ID 批量更新
boolean updateBatchById(Collection<T> entityList);
// 根据插入批次数量,根据ID 批量更新
boolean updateBatchById(Collection<T> entityList, int batchSize);
// 根据 ID 查询
T getById(Serializable id);
// 根据 Wrapper,查询一条记录。结果集,如果是多个会抛出异常,随机取一条加上限制条件 wrapper.last("LIMIT 1")
T getOne(Wrapper<T> queryWrapper);
// 根据 Wrapper,查询一条记录
T getOne(Wrapper<T> queryWrapper, boolean throwEx);
// 根据 Wrapper,查询一条记录
Map<String, Object> getMap(Wrapper<T> queryWrapper);
// 根据 Wrapper,查询一条记录
<V> V getObj(Wrapper<T> queryWrapper, Function<? super Object, V> mapper);
// 查询所有
List<T> list();
// 查询列表
List<T> list(Wrapper<T> queryWrapper);
// 查询(根据ID 批量查询)
Collection<T> listByIds(Collection<? extends Serializable> idList);
// 查询(根据 columnMap 条件)
Collection<T> listByMap(Map<String, Object> columnMap);
// 查询所有列表
List<Map<String, Object>> listMaps();
// 查询列表
List<Map<String, Object>> listMaps(Wrapper<T> queryWrapper);
// 查询全部记录
List<Object> listObjs();
// 查询全部记录
<V> List<V> listObjs(Function<? super Object, V> mapper);
// 根据 Wrapper 条件,查询全部记录
List<Object> listObjs(Wrapper<T> queryWrapper);
// 根据 Wrapper 条件,查询全部记录
<V> List<V> listObjs(Wrapper<T> queryWrapper, Function<? super Object, V> mapper);
// 无条件分页查询
IPage<T> page(IPage<T> page);
// 条件分页查询
IPage<T> page(IPage<T> page, Wrapper<T> queryWrapper);
// 无条件分页查询
IPage<Map<String, Object>> pageMaps(IPage<T> page);
// 条件分页查询
IPage<Map<String, Object>> pageMaps(IPage<T> page, Wrapper<T> queryWrapper);
int insert(T entity);
int deleteById(Serializable id);
int deleteById(T entity);
int deleteByMap(@Param("cm") Map<String, Object> columnMap);
int delete(@Param("ew") Wrapper<T> queryWrapper);
int deleteBatchIds(@Param("coll") Collection<? extends Serializable> idList);
int updateById(@Param("et") T entity);
int update(@Param("et") T entity, @Param("ew") Wrapper<T> updateWrapper);
T selectById(Serializable id);
List<T> selectBatchIds(@Param("coll") Collection<? extends Serializable> idList);
List<T> selectByMap(@Param("cm") Map<String, Object> columnMap);
default T selectOne(@Param("ew") Wrapper<T> queryWrapper) {
List<T> ts = this.selectList(queryWrapper);
if (CollectionUtils.isNotEmpty(ts)) {
if (ts.size() != 1) {
throw ExceptionUtils.mpe("One record is expected, but the query result is multiple records", new Object[0]);
} else {
return ts.get(0);
}
} else {
return null;
}
}
Long selectCount(@Param("ew") Wrapper<T> queryWrapper);
List<T> selectList(@Param("ew") Wrapper<T> queryWrapper);
List<Map<String, Object>> selectMaps(@Param("ew") Wrapper<T> queryWrapper);
List<Object> selectObjs(@Param("ew") Wrapper<T> queryWrapper);
<P extends IPage<T>> P selectPage(P page, @Param("ew") Wrapper<T> queryWrapper);
<P extends IPage<Map<String, Object>>> P selectMapsPage(P page, @Param("ew") Wrapper<T> queryWrapper);
PaginationInnerInterceptor
对所有查询请求进行拦截,beforeQuery
查询时,首先判断参数中是否包含IPage
类型参数。IPage
类型参数,则根据传参值进行分页查询,否则,不分页。位置:mybatis-plus-extension-3.4.3.4.jar#PaginationInnerInterceptor
public class PaginationInnerInterceptor implements InnerInterceptor {
public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
//判断并寻找IPage参数
IPage<?> page = (IPage)ParameterUtils.findPage(parameter).orElse((Object)null);
//参数中包含IPage类型参数,则执行分页
if (null != page) {
boolean addOrdered = false;
String buildSql = boundSql.getSql();
List<OrderItem> orders = page.orders();
if (CollectionUtils.isNotEmpty(orders)) {
addOrdered = true;
buildSql = this.concatOrderBy(buildSql, orders);
}
//根据page参数,组装分页查询sql
Long _limit = page.maxLimit() != null ? page.maxLimit() : this.maxLimit;
//无需分页
if (page.getSize() < 0L && null == _limit) {
if (addOrdered) {
PluginUtils.mpBoundSql(boundSql).sql(buildSql);
}
} else {
this.handlerLimit(page, _limit);
IDialect dialect = this.findIDialect(executor);
Configuration configuration = ms.getConfiguration();
//构建分页sql
DialectModel model = dialect.buildPaginationSql(buildSql, page.offset(), page.getSize());
MPBoundSql mpBoundSql = PluginUtils.mpBoundSql(boundSql);
List<ParameterMapping> mappings = mpBoundSql.parameterMappings();
Map<String, Object> additionalParameter = mpBoundSql.additionalParameters();
model.consumers(mappings, configuration, additionalParameter);
mpBoundSql.sql(model.getDialectSql());
mpBoundSql.parameterMappings(mappings);
}
}
}
}
位置:mybatis-plus-core-3.4.3.4.jar#ParameterUtils
public class ParameterUtils {
private ParameterUtils() {
}
public static Optional<IPage> findPage(Object parameterObject) {
if (parameterObject != null) {
//若多个参数,转为map对象,任意一个value包含IPage类型对象,则返回IPage对象
if (parameterObject instanceof Map) {
Map<?, ?> parameterMap = (Map)parameterObject;
Iterator var2 = parameterMap.entrySet().iterator();
while(var2.hasNext()) {
Entry entry = (Entry)var2.next();
if (entry.getValue() != null && entry.getValue() instanceof IPage) {
return Optional.of((IPage)entry.getValue());
}
}
} else if (parameterObject instanceof IPage) {
//单个参数,若类型为IPage,则返回IPage对象
return Optional.of((IPage)parameterObject);
}
}
//参数中无IPage类型参数,则返回空
return Optional.empty();
}
}
public class MySqlDialect implements IDialect {
public MySqlDialect() {
}
public DialectModel buildPaginationSql(String originalSql, long offset, long limit) {
//拼接构建分页sql
StringBuilder sql = (new StringBuilder(originalSql)).append(" LIMIT ").append("?");
if (offset != 0L) {
sql.append(",").append("?");
return (new DialectModel(sql.toString(), offset, limit)).setConsumerChain();
} else {
return (new DialectModel(sql.toString(), limit)).setConsumer(true);
}
}
}
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package com.baomidou.mybatisplus.core.conditions.interfaces;
import java.io.Serializable;
import java.util.Map;
import java.util.function.BiPredicate;
public interface Compare<Children, R> extends Serializable {
default <V> Children allEq(Map<R, V> params) {
return this.allEq(params, true);
}
default <V> Children allEq(Map<R, V> params, boolean null2IsNull) {
return this.allEq(true, params, null2IsNull);
}
<V> Children allEq(boolean condition, Map<R, V> params, boolean null2IsNull);
default <V> Children allEq(BiPredicate<R, V> filter, Map<R, V> params) {
return this.allEq(filter, params, true);
}
default <V> Children allEq(BiPredicate<R, V> filter, Map<R, V> params, boolean null2IsNull) {
return this.allEq(true, filter, params, null2IsNull);
}
<V> Children allEq(boolean condition, BiPredicate<R, V> filter, Map<R, V> params, boolean null2IsNull);
default Children eq(R column, Object val) {
return this.eq(true, column, val);
}
Children eq(boolean condition, R column, Object val);
default Children ne(R column, Object val) {
return this.ne(true, column, val);
}
Children ne(boolean condition, R column, Object val);
default Children gt(R column, Object val) {
return this.gt(true, column, val);
}
Children gt(boolean condition, R column, Object val);
default Children ge(R column, Object val) {
return this.ge(true, column, val);
}
Children ge(boolean condition, R column, Object val);
default Children lt(R column, Object val) {
return this.lt(true, column, val);
}
Children lt(boolean condition, R column, Object val);
default Children le(R column, Object val) {
return this.le(true, column, val);
}
Children le(boolean condition, R column, Object val);
default Children between(R column, Object val1, Object val2) {
return this.between(true, column, val1, val2);
}
Children between(boolean condition, R column, Object val1, Object val2);
default Children notBetween(R column, Object val1, Object val2) {
return this.notBetween(true, column, val1, val2);
}
Children notBetween(boolean condition, R column, Object val1, Object val2);
default Children like(R column, Object val) {
return this.like(true, column, val);
}
Children like(boolean condition, R column, Object val);
default Children notLike(R column, Object val) {
return this.notLike(true, column, val);
}
Children notLike(boolean condition, R column, Object val);
default Children likeLeft(R column, Object val) {
return this.likeLeft(true, column, val);
}
Children likeLeft(boolean condition, R column, Object val);
default Children likeRight(R column, Object val) {
return this.likeRight(true, column, val);
}
Children likeRight(boolean condition, R column, Object val);
}
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package com.baomidou.mybatisplus.core.conditions.interfaces;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.function.Consumer;
public interface Func<Children, R> extends Serializable {
default Children isNull(R column) {
return this.isNull(true, column);
}
Children isNull(boolean condition, R column);
default Children isNotNull(R column) {
return this.isNotNull(true, column);
}
Children isNotNull(boolean condition, R column);
default Children in(R column, Collection<?> coll) {
return this.in(true, column, coll);
}
Children in(boolean condition, R column, Collection<?> coll);
default Children in(R column, Object... values) {
return this.in(true, column, values);
}
Children in(boolean condition, R column, Object... values);
default Children notIn(R column, Collection<?> coll) {
return this.notIn(true, column, coll);
}
Children notIn(boolean condition, R column, Collection<?> coll);
default Children notIn(R column, Object... value) {
return this.notIn(true, column, value);
}
Children notIn(boolean condition, R column, Object... values);
default Children inSql(R column, String inValue) {
return this.inSql(true, column, inValue);
}
Children inSql(boolean condition, R column, String inValue);
Children gtSql(boolean condition, R column, String inValue);
default Children gtSql(R column, String inValue) {
return this.gtSql(true, column, inValue);
}
Children geSql(boolean condition, R column, String inValue);
default Children geSql(R column, String inValue) {
return this.geSql(true, column, inValue);
}
Children ltSql(boolean condition, R column, String inValue);
default Children ltSql(R column, String inValue) {
return this.ltSql(true, column, inValue);
}
Children leSql(boolean condition, R column, String inValue);
default Children leSql(R column, String inValue) {
return this.leSql(true, column, inValue);
}
default Children notInSql(R column, String inValue) {
return this.notInSql(true, column, inValue);
}
Children notInSql(boolean condition, R column, String inValue);
Children groupBy(boolean condition, R column);
default Children groupBy(R column) {
return this.groupBy(true, column);
}
Children groupBy(boolean condition, List<R> columns);
default Children groupBy(List<R> columns) {
return this.groupBy(true, columns);
}
/** @deprecated */
@Deprecated
default Children groupBy(R column, R... columns) {
return this.groupBy(true, column, columns);
}
/** @deprecated */
@Deprecated
Children groupBy(boolean condition, R column, R... columns);
default Children orderByAsc(boolean condition, R column) {
return this.orderBy(condition, true, column);
}
default Children orderByAsc(R column) {
return this.orderByAsc(true, column);
}
default Children orderByAsc(boolean condition, List<R> columns) {
return this.orderBy(condition, true, columns);
}
default Children orderByAsc(List<R> columns) {
return this.orderByAsc(true, columns);
}
/** @deprecated */
@Deprecated
default Children orderByAsc(R column, R... columns) {
return this.orderByAsc(true, column, columns);
}
/** @deprecated */
@Deprecated
default Children orderByAsc(boolean condition, R column, R... columns) {
return this.orderBy(condition, true, column, columns);
}
default Children orderByDesc(boolean condition, R column) {
return this.orderBy(condition, false, column);
}
default Children orderByDesc(R column) {
return this.orderByDesc(true, column);
}
default Children orderByDesc(boolean condition, List<R> columns) {
return this.orderBy(condition, false, columns);
}
default Children orderByDesc(List<R> columns) {
return this.orderByDesc(true, columns);
}
/** @deprecated */
@Deprecated
default Children orderByDesc(R column, R... columns) {
return this.orderByDesc(true, column, columns);
}
/** @deprecated */
@Deprecated
default Children orderByDesc(boolean condition, R column, R... columns) {
return this.orderBy(condition, false, column, columns);
}
Children orderBy(boolean condition, boolean isAsc, R column);
Children orderBy(boolean condition, boolean isAsc, List<R> columns);
/** @deprecated */
@Deprecated
Children orderBy(boolean condition, boolean isAsc, R column, R... columns);
default Children having(String sqlHaving, Object... params) {
return this.having(true, sqlHaving, params);
}
Children having(boolean condition, String sqlHaving, Object... params);
default Children func(Consumer<Children> consumer) {
return this.func(true, consumer);
}
Children func(boolean condition, Consumer<Children> consumer);
}
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package com.baomidou.mybatisplus.core.conditions.interfaces;
import java.io.Serializable;
public interface Join<Children> extends Serializable {
default Children or() {
return this.or(true);
}
Children or(boolean condition);
default Children apply(String applySql, Object... values) {
return this.apply(true, applySql, values);
}
Children apply(boolean condition, String applySql, Object... values);
default Children last(String lastSql) {
return this.last(true, lastSql);
}
Children last(boolean condition, String lastSql);
default Children comment(String comment) {
return this.comment(true, comment);
}
Children comment(boolean condition, String comment);
default Children first(String firstSql) {
return this.first(true, firstSql);
}
Children first(boolean condition, String firstSql);
default Children exists(String existsSql, Object... values) {
return this.exists(true, existsSql, values);
}
Children exists(boolean condition, String existsSql, Object... values);
default Children notExists(String existsSql, Object... values) {
return this.notExists(true, existsSql, values);
}
Children notExists(boolean condition, String existsSql, Object... values);
}
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package com.baomidou.mybatisplus.core.conditions.interfaces;
import java.io.Serializable;
import java.util.function.Consumer;
public interface Nested<Param, Children> extends Serializable {
default Children and(Consumer<Param> consumer) {
return this.and(true, consumer);
}
Children and(boolean condition, Consumer<Param> consumer);
default Children or(Consumer<Param> consumer) {
return this.or(true, consumer);
}
Children or(boolean condition, Consumer<Param> consumer);
default Children nested(Consumer<Param> consumer) {
return this.nested(true, consumer);
}
Children nested(boolean condition, Consumer<Param> consumer);
default Children not(Consumer<Param> consumer) {
return this.not(true, consumer);
}
Children not(boolean condition, Consumer<Param> consumer);
}
函数名 | 说明 | 示例与对应sql |
---|---|---|
eq |
等于= | eq(“id”, 1) 即 id = 1 |
ne |
不等于<> | ne(“id”, 1) 即 id <> 1 |
gt |
大于> | gt(“id”, 1) 即 id > 1 |
ge |
大于等于>= | ge(“id”,1) 即 id >= 1 |
lt |
小于< | lt(“id”,1) 即 id < 1 |
le |
小于等于<= | le("id,1) 即 id <= 1 |
between |
BETWEEN 值1 AND 值2 | between(“id”, 1, 3) 即 id BETWEEN 1 AND 3 |
notBetween |
NOT BETWEEN 值1 AND 值2 | notBetween(“id”, 1, 3) 即 id NOT BETWEEN 1 AND 3 |
like |
LIKE ‘%值%’ | like(“name”, "小”) 即 name LIKE ‘%小%’ |
notLike |
NOT LIKE ‘%值%’ | notLike(“name”, "小”) 即 name NOT LIKE ‘%小%’ |
likeLeft |
LIKE ‘%值’ | likeLeft(“name”, "小”) 即 name LIKE ‘%小’ |
likeRight |
LIKE ‘值%’ | likeRight(“name”, "小”) 即 name LIKE ‘小%’ |
isNull |
字段 IS NULL | isNull(“name”) 即 name IS NULL |
isNotNull |
字段 IS NOT NULL | isNotNull(“name”) 即 name IS NOT NULL |
in |
字段 IN (值1, 值2, … …) | in(“id”, {1,2,3,4,5}) 即 id IN (1,2,3,4,5) |
notIn |
字段 NOT IN (值1, 值2, … …) | notIn(“id”, {1,2,3,4,5}) 即 id NOT IN (1,2,3,4,5) |
inSql |
字段 IN (sql语句) | inSql(“id”, “select id from tab_01 where id > 10”) 即 id IN (select id from tab_01 where id > 10) |
notInSql |
字段 NOT IN (sql语句) | notInSql(“id”, “select id from tab_01 where id > 10”) 即 id NOT IN (select id from tab_01 where id > 10) |
groupBy |
GROUP BY 字段1, 字段2, … … | groupBy(“id”, “name”) 即 GROUP BY id, name |
orderBy |
ORDER BY 字段1, 字段2, … … | orderBy(true, false, “id”, “name”) 即 ORDER BY id ASC, name DESC |
orderByAsc |
ORDER BY 字段1, 字段2, … … ASC | orderByAsc(“id”, “name”) 即 ORDER BY id ASC, name ASC |
orderByDesc |
ORDER BY 字段1, 字段2, … … DESC | orderByDesc(“id”, “name”) 即 ORDER BY id DESC, name DESC |
having |
HAVING(sql语句) | having(“count(number) > {0}”, 20) 即 HAVING COUNT(number) > 20 |
or |
OR | eq(“id”, 2).or().eq(“name”, “小红”) 即 id = 2 OR name = “小红” |
and |
AND | and(query -> query.eq(“id”, 2).ne(“name”, "小红”)) 即 AND (id = 2 AND name <> “小红” |
apply |
拼接sql语句 | apply("date_format(df, ‘%Y-%m-%d’) = {0}, “2023-04-25”) 即 date_format(df, ‘%Y-%m-%d’) = ‘2023-04-25’) |
last |
拼接sql到最后 | last(“limit”, 10) 即 xxx limit 10 |
exists |
EXISTS (sql语句) | exists("select id from tab_01 where name = “小红”) 即 EXISTS ("select id from tab_01 where name = “小红”) |
notExists |
NOT EXISTS (sql语句) | notExists("select id from tab_01 where name = “小红”) 即 NOT EXISTS ("select id from tab_01 where name = “小红”) |
nested |
不带AND或OR的嵌套 | nested(query -> query.eq(“id”, 2).ne(“name”, "小红”)) 即 (id = 2 AND name <> “小红” |
com.baomidou
mybatis-plus-boot-starter
@Configuration
public class MybatisPlusConfig {
/**
* mybatis-plus 拦截器
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//新增内置分页拦截器,并设置数据库类型为mysql
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
@Data
@TableName("demo_entity")
@ApiModel(value = "demo表")
public class DemoEntity {
/**
* id
*/
@TableId(type = IdType.ASSIGN_ID)
private Long id;
/**
* 名称
*/
private String name;
/**
* 编码
*/
private String code;
/**
* 是否删除
*/
private Integer deleted;
}
@Mapper
public interface DemoEntityMapper extends BaseMapper<DemoEntity> {
... ...
}
public interface DemoEntityService extends IService<DemoEntity> {
... ...
}
@Slf4j
@Service
public class DemoEntityServiceImpl extends ServiceImpl<DemoEntityMapper, DemoEntity> implements DemoEntityService{
/**
* mapper
*/
@Autowired
private DemoEntityMapper demoEntityMapper;
... ...
}
... ...
Long id = 1000L;
List<Long> userId = new ArrayList<>();
userId.add(100000L);
userId.add(100001L);
LambdaQueryWrapper<DemoEntity> lambdaQueryWrapper = new LambdaQueryWrapper();
lambdaQueryWrapper.eq(DemoEntity::getId, id)
.in(DemoEntity::getUserId, userId);
List<DemoEntity> entityList = demoEntityMapper.selectList(lambdaQueryWrapper);
... ...
... ...
Long id = 1000L;
List<Long> userId = new ArrayList<>();
userId.add(100000L);
userId.add(100001L);
QueryWrapper<DemoEntity> queryWrapper = Wrappers.query();
queryWrapper.lambda()
//and筛选
.and(wrapper -> wrapper.in(DemoEntity::getUserId, userId)
//or条件
.or()
.eq(DemoEntity::getId, id);
... ...
... ...
Long id = 1000L;
this.lambdaUpdate()
.eq(DemoEntity::getId, id)
.set(DemoEntity::getUserId, 10000002L)
//更新
.update();
... ...
@Mapper
public interface DemoEntityMapper extends BaseMapper<DemoEntity> {
... ...
/**
* 根据id进行查询
* @param id
* @return
*/
public DemoVO getById(int id);
... ...
}
<?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.ljy.demo.mapper.DemoEntityMapper">
<select id="getById" parameterType="int" resultType="com.ljy.demo.vo.DemoVO">
SELECT * FROM demo_entity WHERE id=#{id}
</select>
</mapper>
@Mapper
public interface DemoEntityMapper extends BaseMapper<DemoEntity> {
... ...
/**
* 根据name和code进行查询
* @param name
* @param code
* @return
*/
public List<DemoVO> getByNameOrCode(String name, String code);
... ...
}
<?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.ljy.demo.mapper.DemoEntityMapper">
<select id="getByNameOrCode" resultType="com.ljy.demo.vo.DemoVO">
SELECT * FROM demo_entity WHERE name=#{0} OR code=#{1}
</select>
</mapper>
@Mapper
public interface DemoEntityMapper extends BaseMapper<DemoEntity> {
... ...
/**
* 根据dto的不同入参进行查询
* @param demoDTO
* @return
*/
public List<DemoVO> listDemo(@Param("dto") DemoDTO demoDTO);
... ...
}
<?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.ljy.demo.mapper.DemoEntityMapper">
<sql id="List_demo">
select de.id,
de.name,
de.code
from demo_entity de
</sql>
<select id="listDemo" resultType="com.ljy.demo.vo.DemoVO">
<include refid="List_demo"/>
<where>
<if test="dto.id != null" >
<!--id不为空,则精准查询-->
and de.id = #{dto.id}
</if>
<if test="dto.name != null and dto.name != ''">
<!--名称不为空,则模糊查询-->
and de.name like concat('%',#{dto.name},'%')
</if>
<!--未删除-->
and de.deleted = 0
</where>
group by de.id
order by de.name desc
</select>
</mapper>
@Mapper
public interface DemoEntityMapper extends BaseMapper<DemoEntity> {
... ...
/**
* 分页查询
* @param page
* @param demoDTO
* @return
*/
IPage<DemoVO> pageListDemo(@Param("page") IPage<DemoVO> page, @Param("dto") DemoDTO demoDTO);
... ...
}
<?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.ljy.demo.mapper.DemoEntityMapper">
<sql id="List_demo">
select de.id,
de.name,
de.code
from demo_entity de
</sql>
<!--名称保持一致-->
<select id="pageListDemo" resultType="com.ljy.demo.vo.DemoVO">
<include refid="List_demo"/>
<where>
<if test="dto.name != null and dto.name != ''">
<!--名称不为空,则模糊查询-->
and de.name like concat('%',#{dto.name},'%')
</if>
<!--未删除-->
and de.deleted = 0
</where>
group by de.id
order by de.name desc
</select>
</mapper>
...
//构造分页
Page<DemoVO> page = new Page<>(2, 10);
//page 查询
IPage<DemoVO> iPage = demoEntityMapper.pageListDemo(page, demoDTO);
...