SpringBoot+Mybatis-Plus
中文官网:https://mp.baomidou.com/
Mapper CRUD 接口
/** * <p> * 插入一条记录 * </p> * * @param entity 实体对象 * @return 插入成功记录数 */ int insert(T entity); //示例 @Test public void testInsert(){ System.out.println(("----- insert method test ------")); User user = new User(); user.setName("留言"); user.setAge(30); user.setEmail("1515@163.com"); int cout = userMapper.insert(user); System.out.println(cout); }
/** * <p> * 根据 ID 删除 * </p> * * @param id 主键ID * @return 删除成功记录数 */ int deleteById(Serializable id); //示例 @Test public void deleteById(){ userMapper.deleteById(2); }
/** * <p> * 根据 columnMap 条件,删除记录 * </p> * * @param columnMap 表字段 map 对象 * @return 删除成功记录数 */ int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap); //示例 @Test public void deleteByMap(){ Map<String,Object> usreMap = new HashMap<>(); usreMap.put("age","18"); int cout = userMapper.deleteByMap(usreMap); log.info(""+cout); }
/** * <p> * 根据 entity 条件,删除记录 * </p> * * @param wrapper 实体对象封装操作类(可以为 null) * @return 删除成功记录数 */ int delete(@Param(Constants.WRAPPER) Wrapper<T> wrapper);
示例:https://blog.csdn.net/m0_37034294/article/details/82917234
/** * <p> * 删除(根据ID 批量删除) * </p> * * @param idList 主键ID列表(不能为 null 以及 empty) * @return 删除成功记录数 */ int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList); //示例 @Test public void deleteBatchIds(){ List<Integer> list = new ArrayList<>(); list.add(3); list.add(4); int cout = userMapper.deleteBatchIds(list); log.info(""+cout); }
/** * <p> * 根据 ID 修改 * </p> * * @param entity 实体对象 * @return 修改成功记录数 */ int updateById(@Param(Constants.ENTITY) T entity); //示例 @Test public void updateById(){ User user = new User(); user.setId(7L); user.setName("留言"); user.setAge(30); user.setEmail("1515@163.com"); int cout = userMapper.updateById(user); log.info(""+cout); }
/** * <p> * 根据 whereEntity 条件,更新记录 * </p> * * @param entity 实体对象 (set 条件值,可为 null) * @param updateWrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句) * @return 修改成功记录数 */ int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper<T> updateWrapper);
示例:https://blog.csdn.net/m0_37034294/article/details/82917234
/** * <p> * 根据 ID 查询 * </p> * * @param id 主键ID * @return 实体 */ T selectById(Serializable id); //示例 @Test public void selectById(){ User user = userMapper.selectById(6); log.info(""+user.toString()); }
/** * <p> * 查询(根据ID 批量查询) * </p> * * @param idList 主键ID列表(不能为 null 以及 empty) * @return 实体集合 */ List<T> selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList); //示例 @Test public void selectBatchIds(){ List<Integer> list = new ArrayList<>(); list.add(5); list.add(6); List<User> users = userMapper.selectBatchIds(list); for (User user:users) { log.info(""+user.toString()); } }
/** * <p> * 根据 entity 条件,查询一条记录 * </p> * * @param queryWrapper 实体对象 * @return 实体 */ T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper); //示例 @Test public void selectOne(){ QueryWrapper<User> ueryWrapper = new QueryWrapper<>(); ueryWrapper.eq("id","6"); User user = userMapper.selectOne(ueryWrapper); log.info(""+user.toString()); }
示例:https://blog.csdn.net/m0_37034294/article/details/82917234
/** * <p> * 根据 Wrapper 条件,查询总记录数 * </p> * * @param queryWrapper 实体对象 * @return 满足条件记录数 */ Integer selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper); //示例 @Test public void selectCount(){ QueryWrapper<User> ueryWrapper = new QueryWrapper<>(); ueryWrapper.like("age","%3%"); int count = userMapper.selectCount(ueryWrapper); log.info(""+count); }
/** * <p> * 根据 entity 条件,查询全部记录 * </p> * * @param queryWrapper 实体对象封装操作类(可以为 null) * @return 实体集合 */ List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper); //示例 @Test public void selectList(){ QueryWrapper<User> ueryWrapper = new QueryWrapper<>(); ueryWrapper.like("age","%3%"); List<User> users = userMapper.selectList(ueryWrapper); for (User user:users) { log.info(""+user.toString()); } }
示例:https://blog.csdn.net/m0_37034294/article/details/82917234
/** * <p> * 根据 Wrapper 条件,查询全部记录 * </p> * * @param queryWrapper 实体对象封装操作类(可以为 null) * @return 字段映射对象 Map 集合 */ List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper); //示例 @Test public void selectMaps(){ QueryWrapper<User> ueryWrapper = new QueryWrapper<>(); List<Map<String, Object>> users = userMapper.selectMaps(ueryWrapper); for (Map<String, Object> map:users){ System.out.println(map); } }
示例:https://blog.csdn.net/m0_37034294/article/details/82917234
/** * <p> * 根据 entity 条件,查询全部记录(并翻页) * </p> * * @param page 分页查询条件(可以为 RowBounds.DEFAULT) * @param queryWrapper 实体对象封装操作类(可以为 null) * @return 实体分页对象 */ IPage<T> selectPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
/** * <p> * 根据 Wrapper 条件,查询全部记录(并翻页) * </p> * * @param page 分页查询条件 * @param queryWrapper 实体对象封装操作类 * @return 字段映射对象 Map 分页对象 */ IPage<Map<String, Object>> selectMapsPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.2.0</version> </dependency>
//实体类 package com.example.mybatisplusdemo.pojo; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; @Data @TableName(value = "user") public class User { @TableId(value = "id",type = IdType.AUTO) private Long id; @TableField(value = "name") private String name; @TableField(value = "age") private Integer age; @TableField(value = "email") private String email; @TableField(value = "time",exist = false) private String time; }
//接口类 package com.example.mybatisplusdemo.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.example.mybatisplusdemo.pojo.User; public interface UserMapper extends BaseMapper<User> {}
//测试类 package com.example.mybatisplusdemo.test; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.example.mybatisplusdemo.mapper.UserMapper; import com.example.mybatisplusdemo.pojo.User; import lombok.extern.java.Log; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @Log @RunWith(SpringRunner.class) @SpringBootTest public class SampleTest { @Autowired private UserMapper userMapper; @Test public void testSelect(){ System.out.println(("----- selectAll method test ------")); List<User> userList = userMapper.selectList(null); for (User user:userList) { log.info(user.toString()); } } @Test public void testInsert(){ System.out.println(("----- insert method test ------")); User user = new User(); user.setName("留言"); user.setAge(30); user.setEmail("1515@163.com"); int cout = userMapper.insert(user); System.out.println(cout); } @Test public void deleteById(){ userMapper.deleteById(2); } @Test public void deleteByMap(){ Map<String,Object> usreMap = new HashMap<>(); usreMap.put("age","18"); int cout = userMapper.deleteByMap(usreMap); } @Test public void delete(){ QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper.like("name","%言%"); int cout = userMapper.delete(queryWrapper); log.info(""+cout); } @Test public void deleteBatchIds(){ List<Integer> list = new ArrayList<>(); list.add(3); list.add(4); int cout = userMapper.deleteBatchIds(list); log.info(""+cout); } @Test public void updateById(){ User user = new User(); user.setId(7L); user.setName("留言"); user.setAge(30); user.setEmail("1515@163.com"); int cout = userMapper.updateById(user); log.info(""+cout); } @Test public void update(){ User user = new User(); user.setName("留言"); user.setAge(30); user.setEmail("1515@163.com"); UpdateWrapper<User> updateWrapper = new UpdateWrapper<>(); updateWrapper.eq("id","6"); int cout = userMapper.update(user,updateWrapper); log.info(""+cout); } @Test public void selectById(){ User user = userMapper.selectById(6); log.info(""+user.toString()); } @Test public void selectBatchIds(){ List<Integer> list = new ArrayList<>(); list.add(5); list.add(6); List<User> users = userMapper.selectBatchIds(list); for (User user:users) { log.info(""+user.toString()); } } @Test public void selectOne(){ QueryWrapper<User> ueryWrapper = new QueryWrapper<>(); ueryWrapper.eq("id","6"); User user = userMapper.selectOne(ueryWrapper); log.info(""+user.toString()); } @Test public void selectCount(){ QueryWrapper<User> ueryWrapper = new QueryWrapper<>(); ueryWrapper.like("age","%3%"); int count = userMapper.selectCount(ueryWrapper); log.info(""+count); } @Test public void selectList(){ QueryWrapper<User> ueryWrapper = new QueryWrapper<>(); ueryWrapper.like("age","%3%"); List<User> users = userMapper.selectList(ueryWrapper); for (User user:users) { log.info(""+user.toString()); } } @Test public void selectMaps(){ QueryWrapper<User> ueryWrapper = new QueryWrapper<>(); List<Map<String, Object>> users = userMapper.selectMaps(ueryWrapper); for (Map<String, Object> map:users){ System.out.println(map); } } @Test public void selectObjs(){ QueryWrapper<User> ueryWrapper = new QueryWrapper<>(); List<Object> users = userMapper.selectObjs(ueryWrapper); for (Object user:users){ log.info(user.toString()); } } @Test public void selectPage(){ QueryWrapper<User> ueryWrapper = new QueryWrapper<>(); Page<User> userPage= new Page<>(1,1); List<User> users = userMapper.selectPage(userPage,ueryWrapper).getRecords(); for (User user:users){ log.info(user.toString()); } } }