1 Service CRUD 接口
- 通用 Service CRUD 封装IService (opens new window)接口,进一步封装 CRUD 采用
get 查询单行 remove 删除 list 查询集合 page 分页 前缀命名方式区分 Mapper 层避免混淆。 - 泛型 T 为任意实体对象
1.1 Save
// 插入一条记录(选择字段,策略插入) boolean save(T entity);
User afanda = new User(100L, "afanda", 25, "123456789@qq.com");
boolean res = userService.save(afanda);
// 插入(批量) boolean saveBatch(Collection entityList);
User afanda = new User(102L, "afanda", 25, "123456789@qq.com");
User dafenqi = new User(103L, "dafenqi", 25, "123456789@qq.com");
User lining = new User(104L, "lining", 25, "123456789@qq.com");
User alidasi = new User(105L, "alidasi", 25, "123456789@qq.com");
List<User> users = Arrays.asList(afanda, dafenqi, lining, alidasi);
// 插入(批量) boolean saveBatch(Collection entityList, int batchSize);
User afanda = new User(102L, "afanda", 25, "123456789@qq.com");
User dafenqi = new User(103L, "dafenqi", 25, "123456789@qq.com");
User lining = new User(104L, "lining", 25, "123456789@qq.com");
User alidasi = new User(105L, "alidasi", 25, "123456789@qq.com");
List<User> users = Arrays.asList(afanda, dafenqi, lining, alidasi);
boolean res = userService.saveBatch(users, 2);
1.2 SaveOrUpdate
// TableId 注解存在更新记录,否插入一条记录 boolean saveOrUpdate(T entity);
User afanda = new User(102L, "Lenin", 25, "123456789@qq.com");
boolean res = userService.saveOrUpdate(afanda);
// 根据updateWrapper尝试更新,否继续执行saveOrUpdate(T)方法 boolean saveOrUpdate(T entity, Wrapper updateWrapper);
UpdateWrapper<User> wrapper = new UpdateWrapper<User>().eq("name", "Stalin");
User stalin = new User(106L, "Stalin", 25, "stalin@qq.com");
boolean res = userService.saveOrUpdate(stalin, wrapper);
// 批量修改插入 boolean saveOrUpdateBatch(Collection entityList); // 批量修改插入 boolean saveOrUpdateBatch(Collection entityList, int batchSize);
1.3 Remove
// 根据 entity 条件,删除记录 boolean remove(Wrapper queryWrapper);
QueryWrapper<User> queryWrapper = new QueryWrapper<User>().eq("name", "Jone");
boolean res = userService.remove(queryWrapper);
// 根据 ID 删除 boolean removeById(Serializable id); // 根据 columnMap 条件,删除记录 boolean removeByMap(Map<String, Object> columnMap);
Map<String, Object> map = new HashMap<>();
map.put("name", "Jack");
boolean res = userService.removeByMap(map);
// 删除(根据ID 批量删除) boolean removeByIds(Collection<? extends Serializable> idList);
1.4 Update
// 根据 UpdateWrapper 条件,更新记录 需要设置sqlset boolean update(Wrapper updateWrapper);
boolean res = userService.update(new UpdateWrapper<User>().set("name", "Zhukov").eq("id", 3));
// 根据 whereWrapper 条件,更新记录 boolean update(T updateEntity, Wrapper whereWrapper);
User user = new User(106L, "Mark Huafei", 25, "Mark Huafei@qq.com");
UpdateWrapper<User> wrapper = new UpdateWrapper<User>().eq("id", 106L);
// 根据 ID 选择修改 boolean updateById(T entity);
User user = new User(106L, "Jack Jones", 25, "Jack Jones@qq.com");
boolean res = userService.updateById(user);
// 根据ID 批量更新 boolean updateBatchById(Collection entityList);
// 根据ID 批量更新 boolean updateBatchById(Collection entityList, int batchSize);
1.5 Get
// 根据 ID 查询 T getById(Serializable id);
User user = userService.getById(106L);
// 根据 Wrapper,查询一条记录。结果集,如果是多个会抛出异常,随机取一条加上限制条件 wrapper.last(“LIMIT 1”) T getOne(Wrapper queryWrapper);
User user = userService.getOne(new QueryWrapper<User>().eq("email", "123456789@qq.com"));
报错信息:Expected one result (or null) to be returned by selectOne(), but found: 4
// 根据 Wrapper,查询一条记录 T getOne(Wrapper queryWrapper, boolean throwEx);
// 根据 Wrapper,查询一条记录 Map<String, Object> getMap(Wrapper queryWrapper);
Map<String, Object> userMap = userService.getMap(new QueryWrapper<User>().eq("email", "123456789@qq.com"));
// 根据 Wrapper,查询一条记录 V getObj(Wrapper queryWrapper, Function<? super Object, V> mapper);
1.6 List
// 查询所有 List list();
List<User> users = userService.list();
// 查询列表 List list(Wrapper queryWrapper);
List<User> users = userService.list(new QueryWrapper<User>().eq("email", "123456789@qq.com"));
// 查询(根据ID 批量查询) Collection listByIds(Collection<? extends Serializable> idList);
List<User> users = userService.listByIds(Arrays.asList(3, 4, 5));
// 查询(根据 columnMap 条件) Collection listByMap(Map<String, Object> columnMap);
// 查询所有列表 List<Map<String, Object>> listMaps();
// 查询列表 List<Map<String, Object>> listMaps(Wrapper queryWrapper);
// 查询全部记录 List listObjs();
// 查询全部记录 List listObjs(Function<? super Object, V> mapper);
// 根据 Wrapper 条件,查询全部记录 List listObjs(Wrapper queryWrapper);
// 根据 Wrapper 条件,查询全部记录 List listObjs(Wrapper queryWrapper, Function<? super Object, V> mapper);
1.7 Page
package com.zs.config;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@EnableTransactionManagement
@Configuration
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
return paginationInterceptor;
}
}
// 无条件分页查询 IPage page(IPage page);
Page<User> page = new Page<>(2, 2);
Page<User> userPage = userService.page(page);
System.out.println("getRecords :" + userPage.getRecords());
System.out.println("getCurrent :" + userPage.getCurrent());
System.out.println("getOrders :" + userPage.getOrders());
System.out.println("getSize :" + userPage.getSize());
System.out.println("getTotal :" + userPage.getTotal());
System.out.println("getPages :" + userPage.getPages());
// 条件分页查询 IPage page(IPage page, Wrapper queryWrapper);
// 无条件分页查询 IPage<Map<String, Object>> pageMaps(IPage page);
// 条件分页查询 IPage<Map<String, Object>> pageMaps(IPage page, Wrapper queryWrapper);
1.8 Count
// 查询总记录数 int count();
int res = userService.count();
// 根据 Wrapper 条件,查询总记录数 int count(Wrapper queryWrapper);
int res = userService.count(new QueryWrapper<User>().eq("email", "123456789@qq.com"));
1.9 Chain
1.9.1 query
// 链式查询 普通 QueryChainWrapper query();
Integer res = new QueryChainWrapper<User>(userMapper)
.eq("email", "123456789@qq.com")
.count();
System.out.println(res);
List<User> res = new QueryChainWrapper<User>(userMapper)
.eq("email", "123456789@qq.com")
.list();
System.out.println(res);
// 链式查询 lambda 式。 LambdaQueryChainWrapper lambdaQuery();
List<User> res = new LambdaQueryChainWrapper<User>(userMapper)
.eq(User::getAge, 25)
.list();
System.out.println(res);
2 Mapper CRUD 接口
2.1 Insert
// 插入一条记录 int insert(T entity);
User user = new User(9L, "Martyn Davies", 25, "Martyn Davies@qq.com");
int res = userMapper.insert(user);
2.2 Delete
// 根据 entity 条件,删除记录 int delete(@Param(Constants.WRAPPER) Wrapper wrapper);
int res = userMapper.delete(new QueryWrapper<User>().eq("id", 9L));
// 删除(根据ID 批量删除) int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
// 根据 ID 删除 int deleteById(Serializable id);
// 根据 columnMap 条件,删除记录 int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
2.3 Update
// 根据 whereWrapper 条件,更新记录 int update(@Param(Constants.ENTITY) T updateEntity, @Param(Constants.WRAPPER) Wrapper whereWrapper);
User user = new User(1L, "Maria cangai", 25, "Maria cangai@qq.com");
int res = userMapper.update(user, new UpdateWrapper<User>().eq("id", 1L));
// 根据 ID 修改 int updateById(@Param(Constants.ENTITY) T entity);
2.4 Select
// 根据 ID 查询 T selectById(Serializable id);
// 根据 entity 条件,查询一条记录 T selectOne(@Param(Constants.WRAPPER) Wrapper queryWrapper);
User user = userMapper.selectOne(new QueryWrapper<User>().eq("email", "cangai@qq.com"));
// 查询(根据ID 批量查询) List selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
// 根据 entity 条件,查询全部记录 List selectList(@Param(Constants.WRAPPER) Wrapper queryWrapper);
// 查询(根据 columnMap 条件) List selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
// 根据 Wrapper 条件,查询全部记录 List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper queryWrapper);
// 根据 Wrapper 条件,查询全部记录。注意: 只返回第一个字段的值 List selectObjs(@Param(Constants.WRAPPER) Wrapper queryWrapper);
List<Object> users = userMapper.selectObjs(new QueryWrapper<User>().eq("email", "123456789@qq.com"));
// 根据 entity 条件,查询全部记录(并翻页) IPage selectPage(IPage page, @Param(Constants.WRAPPER) Wrapper queryWrapper);
Page<User> page = userMapper.selectPage(new Page<User>(1, 2),
new QueryWrapper<User>().eq("email", "123456789@qq.com"));
// 根据 Wrapper 条件,查询全部记录(并翻页) IPage<Map<String, Object>> selectMapsPage(IPage page, @Param(Constants.WRAPPER) Wrapper queryWrapper); // 根据 Wrapper 条件,查询总记录数 Integer selectCount(@Param(Constants.WRAPPER) Wrapper queryWrapper);
|