mybatis下的单表查询demo

demo1登录时的核对用户名及密码

    @Autowired
UsersMapper usersMapper;

@Override
public boolean login(Users users) {
    UsersExample usersExample=new UsersExample();
    usersExample.createCriteria().andUsernameEqualTo(users.getUsername());
    List<Users> users1=usersMapper.selectByExample(usersExample);
    if(users1.isEmpty()){
        //没有这个用户名
        return false;
    }
    return users1.get(0).getPsw().equals(users.getPsw());
}

demo2全部查询

(附基于dto的分页)

    @Autowired
private MainPostsAndUsersMapper mainPostsAndUsersMapper;
@Autowired
private MainPostsAndUsers mainPostsAndUsers;


@Override
public PageInfo<MainPostsAndUsers> sgetMainPostsAndUsers(Integer nowPage,Integer count){
    PageHelper.startPage(nowPage,count);
    List<MainPostsAndUsers> list=mainPostsAndUsersMapper.selectAllInfo(mainPostsAndUsers);
    PageInfo<MainPostsAndUsers> pageInfo=new PageInfo<MainPostsAndUsers>(list);

    return pageInfo;
}

通过某个字段排序

String orderByClause = "id DESC";
UserExample ex = new UserExample();
ex.setOrderByClause(orderByClause);
List<User> userList = userDao.selectByExample(ex);

条件搜索,不确定条件的个数

UserExample ex = new UserExample();
Criteria criteria = ex.createCriteria();
if(StringUtils.isNotBlank(user.getAddress())){
criteria.andAddressEqualTo(user.getAddress());
}    
if(StringUtils.isNotBlank(user.getName())){
criteria.andNameEqualTo(user.getName());
}
List<User> userList = userDao.selectByExample(ex);