//实体类package com.sk.skkill.entity;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 import com.baomidou.mybatisplus.annotation.TableField;import com.baomidou.mybatisplus.annotation.TableName;import lombok.Data;import java.io.Serializable;import java.util.Date;import java.util.List;@TableName("orders") @Data public class Order implements Serializable { public static final long serialVersionUID =1L ; private String id; private String orderName; private Date createTime; private Date updateTime; private String userID; @TableField(exist = false) private List<Users> listUsers; public Order () { } public Order (String id, String orderName) { this .id = id; this .orderName = orderName; } } package com.sk.skkill.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;import com.baomidou.mybatisplus.extension.plugins.pagination.Page;import com.sk.skkill.entity.Order;import com.sun.javafx.collections.MappingChange;import org.apache.ibatis.annotations.Select;import java.util.List;import java.util.Map;public interface OrderMapper extends BaseMapper <Order > {List<Order> selectOrder () ;int addOrder (Order order) ;@Select("select t1.*,t2.user_name,t2.nick_name from orders t1 LEFT JOIN users t2 ON t1.user_id =t2.id WHERE t1.user_id= #{id}") List<Map<String,Object>> orderUserList(Page<Map<String,Object>> page,String id); } package com.sk.skkill.service;import com.baomidou.mybatisplus.extension.plugins.pagination.Page;import com.baomidou.mybatisplus.extension.service.IService;import com.sk.skkill.entity.Order;import java.util.List;import java.util.Map;public interface OrderService extends IService <Order > { List<Order> selectOrder () ; int addOrder (Order order) ; Page<Map<String,Object>> selectListPage(int current,int number,String id); } package com.sk.skkill.service.impl;import com.baomidou.mybatisplus.extension.plugins.pagination.Page;import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;import com.sk.skkill.entity.Order;import com.sk.skkill.mapper.OrderMapper;import com.sk.skkill.service.OrderService;import org.springframework.stereotype.Service;import java.util.List;import java.util.Map;@Service public class OrderServiceImpl extends ServiceImpl <OrderMapper , Order > implements OrderService { @Override public List<Order> selectOrder () { return baseMapper.selectList(null ); } @Override public int addOrder (Order order) { return baseMapper.insert(order); } @Override public Page<Map<String, Object>> selectListPage(int current, int number,String id) { Page<Map<String,Object>> page =new Page<Map<String,Object>>(current,number); return page.setRecords(this .baseMapper.orderUserList(page,id)); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 package com.sk.skkill.controller;import com.baomidou.mybatisplus.extension.plugins.pagination.Page;import com.sk.skkill.entity.Order;import com.sk.skkill.service.impl.OrderServiceImpl;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.List;import java.util.Map;@RestController @RequestMapping("order") public class OrderController { @Autowired private OrderServiceImpl service; @RequestMapping("selectOrder") public List<Order> selectOrder () { return service.selectOrder(); } @RequestMapping("addOrder") public int addOrder (Order order) { order=new Order("FGGG" ,"蒙牛MILK" ); return service.addOrder(order); } @RequestMapping("selectListPage") public List<Map<String,Object>> selectListPage(String id) { Page<Map<String, Object>> page = service.selectListPage(1 , 2 ,id); return page.getRecords(); } }
MapperScan配置问题 在Application中著名包名,例 1 @MapperScan("com.InterviewPreparation.learning.MyBatisPlus.mapper")
写一个配置类,例 1 2 3 4 5 6 7 8 9 10 11 12 13 import org.mybatis.spring.annotation.MapperScan;import org.springframework.context.annotation.Configuration;@Configuration @MapperScan(value = {"com.InterviewPreparation.learning.MyBatisPlus.mapper*"}) public class MybatisPlusConfig {}
配置文件中表示 1 mapper-locations: classpath*:org/jeecg/modules/**/xml/*Mapper.xml
代码生成器使用 CRUD 接口 # Service CRUD 接口说明:
通用 Service CRUD 封装IService (opens new window) 接口,进一步封装 CRUD 采用 get 查询单行
remove 删除
list 查询集合
page 分页
前缀命名方式区分 Mapper
层避免混淆,
泛型 T
为任意实体对象
建议如果存在自定义通用 Service 方法的可能,请创建自己的 IBaseService
继承 Mybatis-Plus
提供的基类
对象 Wrapper
为 条件构造器
# Save1 2 3 4 5 6 boolean save (T entity) ;boolean saveBatch (Collection<T> entityList) ;boolean saveBatch (Collection<T> entityList, int batchSize) ;
# 参数说明
类型
参数名
描述
T
entity
实体对象
Collection
entityList
实体对象集合
int
batchSize
插入批次数量
# SaveOrUpdate1 2 3 4 5 6 7 8 boolean saveOrUpdate (T entity) ;boolean saveOrUpdate (T entity, Wrapper<T> updateWrapper) ;boolean saveOrUpdateBatch (Collection<T> entityList) ;boolean saveOrUpdateBatch (Collection<T> entityList, int batchSize) ;
# 参数说明
类型
参数名
描述
T
entity
实体对象
Wrapper
updateWrapper
实体对象封装操作类 UpdateWrapper
Collection
entityList
实体对象集合
int
batchSize
插入批次数量
# Remove1 2 3 4 5 6 7 8 boolean remove (Wrapper<T> queryWrapper) ;boolean removeById (Serializable id) ;boolean removeByMap (Map<String, Object> columnMap) ;boolean removeByIds (Collection<? extends Serializable> idList) ;
# 参数说明
类型
参数名
描述
Wrapper
queryWrapper
实体包装类 QueryWrapper
Serializable
id
主键ID
Map<String, Object>
columnMap
表字段 map 对象
Collection<? extends Serializable>
idList
主键ID列表
# Update1 2 3 4 5 6 7 8 9 10 boolean update (Wrapper<T> updateWrapper) ;boolean update (T updateEntity, Wrapper<T> whereWrapper) ;boolean updateById (T entity) ;boolean updateBatchById (Collection<T> entityList) ;boolean updateBatchById (Collection<T> entityList, int batchSize) ;
# 参数说明
类型
参数名
描述
Wrapper
updateWrapper
实体对象封装操作类 UpdateWrapper
T
entity
实体对象
Collection
entityList
实体对象集合
int
batchSize
更新批次数量
# Get1 2 3 4 5 6 7 8 9 10 T getById (Serializable id) ;T getOne (Wrapper<T> queryWrapper) ;T getOne (Wrapper<T> queryWrapper, boolean throwEx) ;Map<String, Object> getMap (Wrapper<T> queryWrapper) ;<V> V getObj (Wrapper<T> queryWrapper, Function<? super Object, V> mapper) ;
# 参数说明
类型
参数名
描述
Serializable
id
主键ID
Wrapper
queryWrapper
实体对象封装操作类 QueryWrapper
boolean
throwEx
有多个 result 是否抛出异常
T
entity
实体对象
Function<? super Object, V>
mapper
转换函数
# List1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 List<T> list () ;List<T> list (Wrapper<T> queryWrapper) ;Collection<T> listByIds (Collection<? extends Serializable> idList) ;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) ; List<Object> listObjs (Wrapper<T> queryWrapper) ;<V> List<V> listObjs (Wrapper<T> queryWrapper, Function<? super Object, V> mapper) ;
# 参数说明
类型
参数名
描述
Wrapper
queryWrapper
实体对象封装操作类 QueryWrapper
Collection<? extends Serializable>
idList
主键ID列表
Map<?String, Object>
columnMap
表字段 map 对象
Function<? super Object, V>
mapper
转换函数
# Page1 2 3 4 5 6 7 8 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);
# 参数说明
类型
参数名
描述
IPage
page
翻页对象
Wrapper
queryWrapper
实体对象封装操作类 QueryWrapper
# Count1 2 3 4 int count () ;int count (Wrapper<T> queryWrapper) ;
# 参数说明
类型
参数名
描述
Wrapper
queryWrapper
实体对象封装操作类 QueryWrapper
# Chain# query1 2 3 4 5 6 7 8 QueryChainWrapper<T> query () ;LambdaQueryChainWrapper<T> lambdaQuery () ; query().eq("column" , value).one(); lambdaQuery().eq(Entity::getId, value).list();
# update1 2 3 4 5 6 7 8 UpdateChainWrapper<T> update () ;LambdaUpdateChainWrapper<T> lambdaUpdate () ;update().eq("column" , value).remove(); lambdaUpdate().eq(Entity::getId, value).update(entity);
# Mapper CRUD 接口说明:
通用 CRUD 封装BaseMapper (opens new window) 接口,为 Mybatis-Plus
启动时自动解析实体表关系映射转换为 Mybatis
内部对象注入容器
泛型 T
为任意实体对象
参数 Serializable
为任意类型主键 Mybatis-Plus
不推荐使用复合主键约定每一张表都有自己的唯一 id
主键
对象 Wrapper
为 条件构造器
# Insert
# 参数说明
# Delete1 2 3 4 5 6 7 8 int delete (@Param(Constants.WRAPPER) Wrapper<T> wrapper) ;int deleteBatchIds (@Param(Constants.COLLECTION) Collection<? extends Serializable> idList) ;int deleteById (Serializable id) ;int deleteByMap (@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap) ;
# 参数说明
类型
参数名
描述
Wrapper
wrapper
实体对象封装操作类(可以为 null)
Collection<? extends Serializable>
idList
主键ID列表(不能为 null 以及 empty)
Serializable
id
主键ID
Map<String, Object>
columnMap
表字段 map 对象
# Update1 2 3 4 int update (@Param(Constants.ENTITY) T updateEntity, @Param(Constants.WRAPPER) Wrapper<T> whereWrapper) ;int updateById (@Param(Constants.ENTITY) T entity) ;
# 参数说明
类型
参数名
描述
T
entity
实体对象 (set 条件值,可为 null)
Wrapper
updateWrapper
实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句)
# Select1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 T selectById (Serializable id) ;T selectOne (@Param(Constants.WRAPPER) Wrapper<T> queryWrapper) ;List<T> selectBatchIds (@Param(Constants.COLLECTION) Collection<? extends Serializable> idList) ;List<T> selectList (@Param(Constants.WRAPPER) Wrapper<T> queryWrapper) ;List<T> selectByMap (@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap) ;List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper); List<Object> selectObjs (@Param(Constants.WRAPPER) Wrapper<T> queryWrapper) ;IPage<T> selectPage (IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper) ;IPage<Map<String, Object>> selectMapsPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper); Integer selectCount (@Param(Constants.WRAPPER) Wrapper<T> queryWrapper) ;
# 参数说明
类型
参数名
描述
Serializable
id
主键ID
Wrapper
queryWrapper
实体对象封装操作类(可以为 null)
Collection<? extends Serializable>
idList
主键ID列表(不能为 null 以及 empty)
Map<String, Object>
columnMap
表字段 map 对象
IPage
page
分页查询条件(可以为 RowBounds.DEFAULT)
# mapper 层 选装件说明:
选装件位于 com.baomidou.mybatisplus.extension.injector.methods
包下 需要配合Sql 注入器 使用,案例(opens new window) 使用详细见源码注释(opens new window)
1 int alwaysUpdateSomeColumnById (T entity) ;
1 int insertBatchSomeColumn (List<T> entityList) ;
1 int deleteByIdWithFill (T entity) ;
# 条件构造器说明:
以下出现的第一个入参boolean condition
表示该条件是否 加入最后生成的sql中,例如:query.like(StringUtils.isNotBlank(name), Entity::getName, name) .eq(age!=null && age >= 0, Entity::getAge, age)
以下代码块内的多个方法均为从上往下补全个别boolean
类型的入参,默认为true
以下出现的泛型Param
均为Wrapper
的子类实例(均具有AbstractWrapper
的所有方法)
以下方法在入参中出现的R
为泛型,在普通wrapper中是String
,在LambdaWrapper中是函数 (例:Entity::getId
,Entity
为实体类,getId
为字段id
的getMethod )
以下方法入参中的R column
均表示数据库字段,当R
具体类型为String
时则为数据库字段名(字段名是数据库关键字的自己用转义符包裹! )!而不是实体类数据字段名!!!,另当R
具体类型为SFunction
时项目runtime不支持eclipse自家的编译器!!!
以下举例均为使用普通wrapper,入参为Map
和List
的均以json
形式表现!
使用中如果入参的Map
或者List
为空 ,则不会加入最后生成的sql中!!!
有任何疑问就点开源码看,看不懂函数 的点击我学习新知识(opens new window)
警告:
不支持以及不赞成在 RPC 调用中把 Wrapper 进行传输
wrapper 很重
传输 wrapper 可以类比为你的 controller 用 map 接收值(开发一时爽,维护火葬场)
正确的 RPC 调用姿势是写一个 DTO 进行传输,被调用方再根据 DTO 执行相应的操作
我们拒绝接受任何关于 RPC 传输 Wrapper 报错相关的 issue 甚至 pr
# AbstractWrapper说明:
QueryWrapper(LambdaQueryWrapper) 和 UpdateWrapper(LambdaUpdateWrapper) 的父类 用于生成 sql 的 where 条件, entity 属性也用于生成 sql 的 where 条件 注意: entity 生成的 where 条件与 使用各个 api 生成的 where 条件没有任何关联行为
# allEq1 2 3 allEq(Map<R, V> params) allEq(Map<R, V> params, boolean null2IsNull) allEq(boolean condition, Map<R, V> params, boolean null2IsNull)
个别参数说明:
params
: key
为数据库字段名,value
为字段值null2IsNull
: 为true
则在map
的value
为null
时调用 isNull 方法,为false
时则忽略value
为null
的
例1: allEq({id:1,name:"老王",age:null})
—>id = 1 and name = '老王' and age is null
例2: allEq({id:1,name:"老王",age:null}, false)
—>id = 1 and name = '老王'
1 2 3 allEq(BiPredicate<R, V> filter, Map<R, V> params) allEq(BiPredicate<R, V> filter, Map<R, V> params, boolean null2IsNull) allEq(boolean condition, BiPredicate<R, V> filter, Map<R, V> params, boolean null2IsNull)
个别参数说明:
filter
: 过滤函数,是否允许字段传入比对条件中params
与 null2IsNull
: 同上
例1: allEq((k,v) -> k.indexOf("a") >= 0, {id:1,name:"老王",age:null})
—>name = '老王' and age is null
例2: allEq((k,v) -> k.indexOf("a") >= 0, {id:1,name:"老王",age:null}, false)
—>name = '老王'
1 2 eq(R column, Object val) eq(boolean condition, R column, Object val)
等于 =
例: eq("name", "老王")
—>name = '老王'
1 2 ne(R column, Object val) ne(boolean condition, R column, Object val)
不等于 <>
例: ne("name", "老王")
—>name <> '老王'
1 2 gt(R column, Object val) gt(boolean condition, R column, Object val)
大于 >
例: gt("age", 18)
—>age > 18
1 2 ge(R column, Object val) ge(boolean condition, R column, Object val)
大于等于 >=
例: ge("age", 18)
—>age >= 18
1 2 lt(R column, Object val) lt(boolean condition, R column, Object val)
小于 <
例: lt("age", 18)
—>age < 18
1 2 le(R column, Object val) le(boolean condition, R column, Object val)
小于等于 <=
例: le("age", 18)
—>age <= 18
# between1 2 between(R column, Object val1, Object val2) between(boolean condition, R column, Object val1, Object val2)
BETWEEN 值1 AND 值2
例: between("age", 18, 30)
—>age between 18 and 30
# notBetween1 2 notBetween(R column, Object val1, Object val2) notBetween(boolean condition, R column, Object val1, Object val2)
NOT BETWEEN 值1 AND 值2
例: notBetween("age", 18, 30)
—>age not between 18 and 30
# like1 2 like(R column, Object val) like(boolean condition, R column, Object val)
LIKE ‘%值%’
例: like("name", "王")
—>name like '%王%'
# notLike1 2 notLike(R column, Object val) notLike(boolean condition, R column, Object val)
NOT LIKE ‘%值%’
例: notLike("name", "王")
—>name not like '%王%'
# likeLeft1 2 likeLeft(R column, Object val) likeLeft(boolean condition, R column, Object val)
LIKE ‘%值’
例: likeLeft("name", "王")
—>name like '%王'
# likeRight1 2 likeRight(R column, Object val) likeRight(boolean condition, R column, Object val)
LIKE ‘值%’
例: likeRight("name", "王")
—>name like '王%'
# isNull1 2 isNull(R column) isNull(boolean condition, R column)
字段 IS NULL
例: isNull("name")
—>name is null
# isNotNull1 2 isNotNull(R column) isNotNull(boolean condition, R column)
字段 IS NOT NULL
例: isNotNull("name")
—>name is not null
1 2 in(R column, Collection<?> value) in(boolean condition, R column, Collection<?> value)
字段 IN (value.get(0), value.get(1), …)
例: in("age",{1,2,3})
—>age in (1,2,3)
1 2 in(R column, Object... values) in(boolean condition, R column, Object... values)
字段 IN (v0, v1, …)
例: in("age", 1, 2, 3)
—>age in (1,2,3)
# notIn1 2 notIn(R column, Collection<?> value) notIn(boolean condition, R column, Collection<?> value)
字段 NOT IN (value.get(0), value.get(1), …)
例: notIn("age",{1,2,3})
—>age not in (1,2,3)
1 2 notIn(R column, Object... values) notIn(boolean condition, R column, Object... values)
字段 NOT IN (v0, v1, …)
例: notIn("age", 1, 2, 3)
—>age not in (1,2,3)
# inSql1 2 inSql(R column, String inValue) inSql(boolean condition, R column, String inValue)
字段 IN ( sql语句 )
例: inSql("age", "1,2,3,4,5,6")
—>age in (1,2,3,4,5,6)
例: inSql("id", "select id from table where id < 3")
—>id in (select id from table where id < 3)
# notInSql1 2 notInSql(R column, String inValue) notInSql(boolean condition, R column, String inValue)
字段 NOT IN ( sql语句 )
例: notInSql("age", "1,2,3,4,5,6")
—>age not in (1,2,3,4,5,6)
例: notInSql("id", "select id from table where id < 3")
—>id not in (select id from table where id < 3)
# groupBy1 2 groupBy(R... columns) groupBy(boolean condition, R... columns)
分组:GROUP BY 字段, …
例: groupBy("id", "name")
—>group by id,name
# orderByAsc1 2 orderByAsc(R... columns) orderByAsc(boolean condition, R... columns)
排序:ORDER BY 字段, … ASC
例: orderByAsc("id", "name")
—>order by id ASC,name ASC
# orderByDesc1 2 orderByDesc(R... columns) orderByDesc(boolean condition, R... columns)
排序:ORDER BY 字段, … DESC
例: orderByDesc("id", "name")
—>order by id DESC,name DESC
# orderBy1 orderBy(boolean condition, boolean isAsc, R... columns)
排序:ORDER BY 字段, …
例: orderBy(true, true, "id", "name")
—>order by id ASC,name ASC
# having1 2 having(String sqlHaving, Object... params) having(boolean condition, String sqlHaving, Object... params)
HAVING ( sql语句 )
例: having("sum(age) > 10")
—>having sum(age) > 10
例: having("sum(age) > {0}", 11)
—>having sum(age) > 11
# func1 2 func(Consumer<Children> consumer) func(boolean condition, Consumer<Children> consumer)
func 方法(主要方便在出现if…else下调用不同方法能不断链)
例: func(i -> if(true) {i.eq("id", 1)} else {i.ne("id", 1)})
1 2 or() or(boolean condition)
注意事项:
主动调用or
表示紧接着下一个方法 不是用and
连接!(不调用or
则默认为使用and
连接)
例: eq("id",1).or().eq("name","老王")
—>id = 1 or name = '老王'
1 2 or(Consumer<Param> consumer) or(boolean condition, Consumer<Param> consumer)
OR 嵌套
例: or(i -> i.eq("name", "李白").ne("status", "活着"))
—>or (name = '李白' and status <> '活着')
# and1 2 and(Consumer<Param> consumer) and(boolean condition, Consumer<Param> consumer)
AND 嵌套
例: and(i -> i.eq("name", "李白").ne("status", "活着"))
—>and (name = '李白' and status <> '活着')
# nested1 2 nested(Consumer<Param> consumer) nested(boolean condition, Consumer<Param> consumer)
正常嵌套 不带 AND 或者 OR
例: nested(i -> i.eq("name", "李白").ne("status", "活着"))
—>(name = '李白' and status <> '活着')
# apply1 2 apply(String applySql, Object... params) apply(boolean condition, String applySql, Object... params)
注意事项:
该方法可用于数据库函数 动态入参的params
对应前面applySql
内部的{index}
部分.这样是不会有sql注入风险的,反之会有!
例: apply("id = 1")
—>id = 1
例: apply("date_format(dateColumn,'%Y-%m-%d') = '2008-08-08'")
—>date_format(dateColumn,'%Y-%m-%d') = '2008-08-08'")
例: apply("date_format(dateColumn,'%Y-%m-%d') = {0}", "2008-08-08")
—>date_format(dateColumn,'%Y-%m-%d') = '2008-08-08'")
# last1 2 last(String lastSql) last(boolean condition, String lastSql)
注意事项:
只能调用一次,多次调用以最后一次为准 有sql注入的风险,请谨慎使用
# exists1 2 exists(String existsSql) exists(boolean condition, String existsSql)
拼接 EXISTS ( sql语句 )
例: exists("select id from table where age = 1")
—>exists (select id from table where age = 1)
# notExists1 2 notExists(String notExistsSql) notExists(boolean condition, String notExistsSql)
拼接 NOT EXISTS ( sql语句 )
例: notExists("select id from table where age = 1")
—>not exists (select id from table where age = 1)
# QueryWrapper说明:
继承自 AbstractWrapper ,自身的内部属性 entity 也用于生成 where 条件 及 LambdaQueryWrapper, 可以通过 new QueryWrapper().lambda() 方法获取
# select1 2 3 select(String... sqlSelect) select(Predicate<TableFieldInfo> predicate) select(Class<T> entityClass, Predicate<TableFieldInfo> predicate)
说明:
以上方法分为两类. 第二类方法为:过滤查询字段(主键除外),入参不包含 class 的调用前需要wrapper
内的entity
属性有值! 这两类方法重复调用以最后一次为准
例: select("id", "name", "age")
例: select(i -> i.getProperty().startsWith("test"))
# UpdateWrapper说明:
继承自 AbstractWrapper
,自身的内部属性 entity
也用于生成 where 条件 及 LambdaUpdateWrapper
, 可以通过 new UpdateWrapper().lambda()
方法获取!
# set1 2 set(String column, Object val) set(boolean condition, String column, Object val)
SQL SET 字段
例: set("name", "老李头")
例: set("name", "")
—>数据库字段值变为空字符串
例: set("name", null)
—>数据库字段值变为null
# setSql
设置 SET 部分 SQL
例: setSql("name = '老李头'")
# lambda
获取 LambdaWrapper
在QueryWrapper
中是获取LambdaQueryWrapper
在UpdateWrapper
中是获取LambdaUpdateWrapper
# 使用 Wrapper 自定义SQL注意事项:
需要mybatis-plus
版本 >= 3.0.7
param 参数名要么叫ew
,要么加上注解@Param(Constants.WRAPPER)
使用${ew.customSqlSegment}
不支持 Wrapper
内的entity生成where语句
# 用注解1 2 @Select("select * from mysql_data ${ew.customSqlSegment}") List<MysqlData> getAll (@Param(Constants.WRAPPER) Wrapper wrapper) ;
# 用XML1 2 3 4 List<MysqlData> getAll (Wrapper ew) ;<select id="getAll" resultType="MysqlData" > SELECT * FROM mysql_data ${ew.customSqlSegment} </select>
# kotlin使用wrapper
kotlin 可以使用 QueryWrapper
和 UpdateWrapper
但无法使用 LambdaQueryWrapper
和 LambdaUpdateWrapper
如果想使用 lambda 方式的 wrapper 请使用 KtQueryWrapper
和 KtUpdateWrapper
# 链式调用 lambda 式1 2 3 4 5 6 7 8 9 10 11 12 13 UpdateChainWrapper<T> update () ;LambdaUpdateChainWrapper<T> lambdaUpdate () ;query().eq("id" , value).one(); lambdaQuery().eq(Entity::getId, value).one(); update().eq("id" , value).remove(); lambdaUpdate().eq(Entity::getId, value).remove();