MybatisPlus-通用Mapper

敢说敢作敢为, 无怨无恨无悔。这篇文章主要讲述MybatisPlus-通用Mapper相关的知识,希望能为你提供帮助。
一、基本操作
1.新建UserMapper 文件

public interface UserMapper extends BaseMapper< User> { }

2.新建实体对象User
@Data public class User { private Long id; private String name; private Integer age; private String email; }

3.配置MapperScan注解
@SpringBootApplication @MapperScan("com.xl.baomidouTest.mapper") public class BaomidouTestApplication {public static void main(String[] args) { SpringApplication.run(BaomidouTestApplication.class, args); } }

二、操作方法
1.Insert
// 插入一条记录 int insert(T entity);

@Test void insert(){ User user = new User(); user.setName("123"); user.setAge(12); user.setEmail("xl@qq.com"); int rows = userMapper.insert(user); System.out.println("受影响行数"+rows); }

2.Delete
// 根据 entity 条件,删除记录 int delete(@Param(Constants.WRAPPER) Wrapper< T> wrapper); // 删除(根据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);

@Test void delete(){ int row = userMapper.deleteById(1279329436131975170L); System.out.println(row); Map< String,Object> dataMap = new HashMap< > (); dataMap.put("id",1279329782896082946L); row = userMapper.deleteByMap(dataMap); System.out.println(row); LambdaQueryWrapper< User> userLambdaQueryWrapper = Wrappers.< User> lambdaQuery(); userLambdaQueryWrapper.eq(User::getName,"xiangming"); int rows = userMapper.delete(userLambdaQueryWrapper); System.out.println(rows); }

3.Update
// 根据 whereEntity 条件,更新记录 int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper< T> updateWrapper); // 根据 ID 修改 int updateById(@Param(Constants.ENTITY) T entity);

@Test void update(){ User user = new User(); user.setId(1l); user.setName("xiangli"); int row = userMapper.updateById(user); System.out.println("影响记录数:"+row); UpdateWrapper< User> userUpdateWrapper = new UpdateWrapper< > (); userUpdateWrapper.eq("name","xiangli"); User user1 = new User(); user1.setName("xiangming"); System.out.println("受影响行数:"+userMapper.update(user1,userUpdateWrapper)); UpdateWrapper< User> userUpdateWrappers = new UpdateWrapper< > (); userUpdateWrappers.eq("id",1).set("name","Jack"); System.out.println("受影响行数:"+ userMapper.update(null,userUpdateWrappers); LambdaUpdateWrapper< User> lambdaUpdateWrapper = Wrappers.< User> lambdaUpdate(); lambdaUpdateWrapper.eq(User::getId,2).set(User::getName,"Jim"); System.out.println("受影响行数:"+ userMapper.update(null,lambdaUpdateWrapper); boolean flag = new LambdaUpdateChainWrapper< User> (userMapper).eq(User::getId,3).set(User::getName,"Alice").update(); System.out.println(flag); }

4.Select
// 根据 ID 查询 T selectById(Serializable id); // 根据 entity 条件,查询一条记录 T selectOne(@Param(Constants.WRAPPER) Wrapper< T> queryWrapper); // 查询(根据ID 批量查询) List< T> selectBatchIds(@Param(Constants.COLLECTION) Collection< ? extends Serializable> idList); // 根据 entity 条件,查询全部记录 List< T> selectList(@Param(Constants.WRAPPER) Wrapper< T> queryWrapper); // 查询(根据 columnMap 条件) List< T> selectByMap(@Param(Constants.COLUMN_MAP) Map< String, Object> columnMap); // 根据 Wrapper 条件,查询全部记录 List< Map< String, Object> > selectMaps(@Param(Constants.WRAPPER) Wrapper< T> queryWrapper); // 根据 Wrapper 条件,查询全部记录。注意: 只返回第一个字段的值 List< Object> selectObjs(@Param(Constants.WRAPPER) Wrapper< T> queryWrapper); // 根据 entity 条件,查询全部记录(并翻页) IPage< T> selectPage(IPage< T> page, @Param(Constants.WRAPPER) Wrapper< T> queryWrapper); // 根据 Wrapper 条件,查询全部记录(并翻页) IPage< Map< String, Object> > selectMapsPage(IPage< T> page, @Param(Constants.WRAPPER) Wrapper< T> queryWrapper); // 根据 Wrapper 条件,查询总记录数 Integer selectCount(@Param(Constants.WRAPPER) Wrapper< T> queryWrapper);

List< User> userList = userMapper.selectList(null);
Assert.assertEquals(5, userList.size());
userList.forEach(System.out::println);

System.out.println(userMapper.selectById(1));

List< User> userList1 = userMapper.selectBatchIds(Arrays.asList(1,2,3));
userList1.forEach(System.out::println);

Map< String,Object>   map = new HashMap< > ();
map.put("age","12");
userMapper.selectByMap(map).forEach(System.out::println);


QueryWrapper< User> queryWrapper = new QueryWrapper< > ();
queryWrapper.like("name","j").lt("age","30");
userMapper.selectList(queryWrapper).forEach(System.out::println);

QueryWrapper< User> queryWrapper8 = new QueryWrapper< > ();
Map< String,Object> map2 = new HashMap< > ();
map2.put("name","123");
//map.put("age",null);
queryWrapper8.allEq(map2);
//queryWrapper.allEq((k,y)-> !k.equals("age"),map);
userMapper.selectMaps(queryWrapper8).forEach(System.out::println);

QueryWrapper< User> queryWrapper9 = new QueryWrapper< > ();
Map< String,Object> map3 = new HashMap< > ();
map3.put("name","123");
queryWrapper9.allEq(map3);
userMapper.selectObjs(queryWrapper9).forEach(System.out::println);

QueryWrapper< User> queryWrapper10 = new QueryWrapper< > ();
Map< String,Object> map4 = new HashMap< > ();
map4.put("name","123");
queryWrapper10.allEq(map4);
System.out.println(userMapper.selectCount(queryWrapper)); 【MybatisPlus-通用Mapper】 

    推荐阅读