其他分享
首页 > 其他分享> > TkMybatis常用方法

TkMybatis常用方法

作者:互联网

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 = ?)) 

标签:sysUserMapper,常用,loginName,TkMybatis,Example,public,SysUser,方法,example
来源: https://blog.csdn.net/Qfoom/article/details/113675059