关于mybatis中Example类的详解

1,617 阅读1分钟

  说明一下,关于ssm框架,之前我没有深入接触过,直到最近因为项目需要,才做了一些研究,若有错误,还麻烦各位大佬能及时指出!

  首先mapper接口中的函数及方法

  按条件查询个数

  long countByExample(CandidateExample example);

  根据条件删除

  int deleteByExample(CandidateExample example);

  根据主键删除

  int deleteByPrimaryKey(Integer id);

  整条插入

  int insert(Candidate record);

  有选择性插入

  int insertSelective(Candidate record);

  按条件查询表,返回一个集合

  ListselectByExample(CandidateExample example);

  按主键查询,返回一个个体

  Candidate selectByPrimaryKey(Integer id);

  按条件有选择的进行更新

  int updateByExampleSelective(@Param(record) Candidate record,

  @Param(example) CandidateExample example);

  按条件进行更新

  int updateByExample(@Param(record) Candidate record,

  @Param(example) CandidateExample example);

  按主键有选择的进行更新

  int updateByPrimaryKeySelective(Candidate record);

  按主键更新

  int updateByPrimaryKey(Candidate record);

  对每一个方法进行具体查询

  整条插入

  candidateMapper.insert(new Candidate(根据有参构造器里的参数进行具体传值));

  有选择性插入

  candidateMapper.insertSelective(new Candidate());//根据具体类对应的配置文件进行传值

  根据主键删除

  candidateMapper.deleteByPrimaryKey(1);

  按主键查询,返回一个个体

  candidateMapper.selectByPrimaryKey(2);

  按主键更新

  candidateMapper.updateByPrimaryKey(new Candidate());//注意配置文件的相应规则

  按主键有选择的进行更新

  candidateMapper.updateByPrimaryKeySelective();

  以下是重点

  按条件查询个数

  CandidateExample example = new CandidateExample();

  CandidateExample.Criteria criteria = example.createCriteria();

  long count = candidateMapper.countByExample(example);

  System.out.println(count);

  根据条件删除

  criteria.andDstateGreaterThan(20);

  candidateMapper.deleteByExample(example);

  注意下面的这一步必须要给对应的类构造无参方法

  按条件查询表,返回一个集合

  example.setOrderByClause(did asc);//升降序

  example.setDistinct(false);//是否去重复

  criteria.andDstateEqualTo(3);

  ListcandidateList=candidateMapper.selectByExample(example);

  for(Candidate list:candidateList){

  System.out.println(list.toString());

  }

  按条件进行更新

  criteria.andDstateGreaterThan(10);

  candidateMapper.updateByExample(new Candidate(record),example);

  按条件有选择的进行更新

  criteria.andDidGreaterThan(90);

  candidateMapper.updateByExampleSelective(new Candidate(record),example ) ;