详解Mybatis中的select方法

时间:2022-12-08 15:37:41

selectbyid方法

根据id,查询记录

?
1
2
3
4
5
public void updaterecycleassaybusinessitemcharge(string id) {
  assaybusinessitemcharge assaybusinessitemcharge = assaybusinessitemchargeservice.selectbyid(id);
  assaybusinessitemcharge.setrecordstatus(recordstatusenum.valid.getvalue());
  assaybusinessitemchargeservice.update(assaybusinessitemcharge);
}

selectbyexample方法

根据实体字段,查询记录

?
1
2
3
4
5
6
7
8
9
10
public account findbyaccountname(string accountname) {
  accountexample accountexample = new accountexample();
  accountexample.criteria criteria = accountexample.createcriteria();
  criteria.andaccountnameequalto(accountname);
  list<account> accountlist = accountservice.selectbyexample(accountexample);
  if (accountlist == null || accountlist.size() != 1)
    return null;
  else
    return accountlist.get(0);
}

查询所有list

传一个空的实体,不要给赋字段值

?
1
2
3
4
5
6
7
8
9
public account findbyaccountname(string accountname) {
  accountexample accountexample = new accountexample();
  accountexample.criteria criteria = accountexample.createcriteria();
  list<account> accountlist = accountservice.selectbyexample(accountexample);
  if (accountlist == null || accountlist.size() != 1)
    return null;
  else
    return accountlist.get(0);
}

总结

以上所述是小编给大家介绍的mybatis中的select方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:https://blog.csdn.net/nangeali/article/details/81278822