Mybatis中Mapper内置方法

1.countByExample ===>根据条件查询数量

//下面是一个完整的案列
 UserExample example = new UserExample();
 Criteria criteria = example.createCriteria();
 criteria.andUsernameEqualTo("joe");
 int count = userDAO.countByExample(example);
 相当于:select count(*) from user where username='joe'

2、deleteByExample ===>根据条件删除多条

//下面是一个完整的案例
 UserExample example = new UserExample();
 Criteria criteria = example.createCriteria();
 criteria.andUsernameEqualTo("joe");
 userDAO.deleteByExample(example);
 相当于:delete from user where username='joe'

3、deleteByPrimaryKey===>根据主键条件删除单条

int deleteByPrimaryKey(Integer id);
userDAO.deleteByPrimaryKey(101);  相当于:delete from user where id=101

4.insert===>插入数据

//下面是完整的案例
  User user = new User();
 //user.setId(101);
 user.setUsername("test");
 user.setPassword("123456")
 user.setEmail("[email protected]");
 userDAO.insert(user);
 相当于:insert into user(ID,username,password,email) values(101,'test','123456','[email protected]');

5.insertSelective===>插入数据(使用不为null的属性作为字段使用)

int insertSelective(Account record);

6.selectByExample===>根据条件查询数据

//下面是一个完整的案例
UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("joe");
criteria.andUsernameIsNull();
example.setOrderByClause("username asc,email desc");
Listlist = userDAO.selectByExample(example);
相当于:select * from user where username = 'joe' and username is null order by username asc,email desc

//注:在iBator 生成的文件UserExample.java中包含一个static 的内部类 Criteria ,在Criteria中有很多方法,主要是定义SQL 语句where后的查询条件。

7.selectByPrimaryKey===>根据主键查询数据

Account selectByPrimaryKey(Integer id);//相当于select * from user where id = 变量id

8.updateByExampleSelective===>按条件更新值不为null的字段

//下面是一个完整的案列
 UserExample example = new UserExample();
 Criteria criteria = example.createCriteria();
 criteria.andUsernameEqualTo("joe");
 User user = new User();
 user.setPassword("123");
 userDAO.updateByPrimaryKeySelective(user,example);
 相当于:update user set password='123' where username='joe'

9.updateByPrimaryKeySelective===>按条件更新

//下面是一个完整的案例

 User user = new User();
user.setId(101);
user.setPassword("joe");
userDAO.updateByPrimaryKeySelective(user);
相当于:update user set password='joe' where id=101

10.updateByPrimaryKey===>按主键更新

//下面是一个完整的案例
 User user =new User();
 user.setId(101);
 user.setUsername("joe");
 user.setPassword("joe");
 user.setEmail("[email protected]");
 userDAO.updateByPrimaryKey(user);
 相当于:update user set username='joe',password='joe',email='[email protected]' where id=101

你可能感兴趣的:(MyBatis解读)