MyBatis-Plus的CRUD简单使用
这里只针对select进行举例,其他的操作大家可以举一反三,难度不大,直接上代码。
@Test
public void contextLoads() {
System.out.println("-------list----------");
List<User> list = mapper.selectList(null);
Assert.assertEquals(5,list.size());
list.forEach(System.out::println);
System.out.println("-----------batch------------");
ArrayList<Integer> batch = new ArrayList<>();
batch.add(2);
batch.add(3);
List<User> users = mapper.selectBatchIds(batch);
users.forEach(System.out::println);
System.out.println("----------byid---------");
User user = mapper.selectById(3);
System.out.println(user);
System.out.println("----------bymap--------------");
HashMap<String, Object> map = new HashMap<>();
map.put("password","123");
List<User> users1 = mapper.selectByMap(map);
users1.forEach(System.out::println);
System.out.println("-----page,null------");
IPage<User> userIPage = mapper.selectPage(new Page<>(0, 5), null);
List<User> records = userIPage.getRecords();
records.forEach(System.out::println);
System.out.println("-------page,querywrapper----------");
QueryWrapper<User> quary = new QueryWrapper<>();
quary.allEq(map);
IPage<User> userIPage2 = mapper.selectPage(new Page<>(0, 5), quary);
userIPage2.getRecords().forEach(System.out::println);
IPage<Map<String, Object>> mapIPage = mapper.selectMapsPage(new Page<>(0, 5), quary);
List<Map<String, Object>> records1 = mapIPage.getRecords();
records1.forEach(System.out::println);
}
以上的测试代码只是为了方便学习,实际开发中一定要定义好每个对象。小编早上条件分页后错误地对userIPage进行输出,
浪费了大把时间调试,最后才发现是输出对象搞错了。论命名的重要性!!!
sf科技
@Service
public class SocialConnectionServiceImpl extends ServiceImpl<SocialConnectionMapper, SocialConnectionDO> implements SocialConnectionService {
@Override
public SocialBindParam check(SocialBindParam param) {
QueryWrapper<SocialConnectionDO> queryWrapper1 = new QueryWrapper<>();
queryWrapper1.eq("source_id",param.getSourceId());
queryWrapper1.eq("source_type",param.getSourceType());
queryWrapper1.eq("dest_type",param.getTargetType());
SocialConnectionDO socialConnectionDO = baseMapper.selectOne(queryWrapper1);
}
}
public interface SocialConnectionMapper extends BaseMapper<SocialConnectionDO> {
}
@Data
@Accessors(chain = true)
@TableName("social_connection")
public class SocialConnectionDO implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 源ID
*/
@TableField("source_id")
private String sourceId;
/**
* 源类型:meeting:会议;task:任务;group:群组
*/
@TableField("source_type")
private String sourceType;
/**
* 源标题
*/
@TableField("source_title")
private String sourceTitle;
/**
* 目标ID
*/
@TableField("dest_id")
private String destId;
/**
* 目标类型:
* meeting:会议
* task:任务
* group:群组
*/
@TableField("dest_type")
private String destType;
/**
* 目标标题
*/
@TableField("dest_title")
private String destTitle;
/**
* 公司ID
*/
@TableField("creator_company_id")
private Long creatorCompanyId;
/**
* 创建者ID
*/
@TableField("creator")
private Long creator;
/**
* 创建时间
*/
@TableField("create_time")
private Long createTime;
/**
* 更新者ID
*/
@TableField("updator")
private Long updator;
/**
* 更新时间
*/
@TableField("update_time")
private Long updateTime;
/**
* 更新者公司ID
*/
@TableField("updator_company_id")
private Long updatorCompanyId;
/**
* 扩展字段
*/
@TableField("ext")
private String ext;
}