TkMybatis常用方法

小虾米 阅读:765 2021-04-01 11:25:36 评论:0

1、查询全部

    public List<SysUser> getUserList() {
   
     
 
        return sysUserMapper.selectAll(); 
    } 

2、保存

    public void save(SysUser user) {
   
     
     
        sysUserMapper.save(user); 
    } 

3、按主键查询

    public SysUser getUserById(Long id) {
   
     
     
        return  sysUserMapper.selectByPrimaryKey(id); 
    } 

4、按主键修改(只更新有值的字段)

    public void update(SysUser user) {
   
     
 
        sysUserMapper.updateByPrimaryKeySelective(user); 
    } 

5、按主键修改(更新所有字段)

    public void update(SysUser user) {
   
     
 
        sysUserMapper.updateByPrimaryKey(user); 
    } 

6、按主键删除

    public void delete(Long id) {
   
     
     
        sysUserMapper.deleteByPrimaryKey(id); 
    } 

7、按条件精确查询

    public List<SysUser> getUserList(String loginName) {
   
     
        Example example = new Example(SysUser.class); 
        if(StringUtils.isNotBlank(loginName)){
   
     
            example.createCriteria().andEqualTo("loginName", loginName); 
        } 
        return sysUserMapper.selectByExample(example); 
    } 

8、按条件模糊查询

    public List<SysUser> getUserList(String loginName) {
   
     
        Example example = new Example(SysUser.class); 
        if(StringUtils.isNotBlank(loginName)){
   
     
            example.createCriteria().andLike("loginName", "%" + loginName + "%"); 
        } 
        return sysUserMapper.selectByExample(example); 
    } 

9、按时间段查询并排序

    public List<SysUser> getUserList(String startDate, String endDate) {
   
     
        Example example = new Example(SysUser.class); 
        Example.Criteria criteria = example.createCriteria(); 
        if (StringUtils.isNotBlank(startDate)) {
   
     
            criteria.andGreaterThanOrEqualTo("createTime", startDate); 
        } 
        if (StringUtils.isNotBlank(endDate)) {
   
     
            criteria.andLessThanOrEqualTo("createTime", endDate); 
        } 
        example.setOrderByClause("create_time desc"); 
        return sysUserMapper.selectByExample(example); 
    } 

10、按条件修改(只更新有值的字段)

    public void update(SysUser user) {
   
     
        Example example = new Example(SysUser.class); 
        example.createCriteria().andEqualTo("loginName", user.getLoginName()); 
        sysUserMapper.updateByExampleSelective(user, example); 
    } 

11、按条件修改(更新所有字段)

    public void update(SysUser user) {
   
     
        Example example = new Example(SysUser.class); 
        example.createCriteria().andEqualTo("loginName", user.getLoginName()); 
        sysUserMapper.updateByExample(user, example); 
    } 

12、按条件删除

    public void delete(String loginName) {
   
     
        Example example = new Example(SysUser.class); 
        example.createCriteria().andEqualTo("loginName", loginName); 
        sysUserMapper.deleteByExample(example); 
    } 

13、复杂条件查询

    public List<SysUser> getUserList(String loginName, String password) {
   
     
        Example example = new Example(SysUser.class); 
        Example.Criteria criteria1 = example.createCriteria(); 
        criteria1.andEqualTo("loginName", loginName); 
        criteria1.andEqualTo("password", password); 
        Example.Criteria criteria2 = example.createCriteria(); 
        criteria2.andEqualTo("email", loginName); 
        criteria2.andEqualTo("password", password); 
        example.or(criteria2); 
        return sysUserMapper.selectByExample(example); 
    } 
SELECT * FROM sys_user WHERE ((login_name = ? AND password = ?) OR (email = ? AND password = ?))  
标签:MyBatis
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号