Mybatis中的自带Mapper方法

也不知道该怎么介绍,直接上代码吧!
创建一个超类接口,继承mapper内部的所有方法,开发可以简便很多。当然,复杂的sql还是需要我们自己写的。

public interface BaseMapper<T> extends InsertSelectiveMapper<T>, UpdateByExampleSelectiveMapper<T>, UpdateByPrimaryKeySelectiveMapper<T>,
        SelectOneMapper<T>, SelectByPrimaryKeyMapper<T>, SelectMapper<T>, SelectByExampleMapper<T>, SelectByExampleRowBoundsMapper<T>,
        SelectCountByExampleMapper<T> {}

下面详细的介绍每一个方法的使用吧!
1、countByExample ===>根据条件查询数量

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

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

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

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

intdeleteByPrimaryKey(Integerid);
userDAO.deleteByPrimaryKey(10001);  
相当于:delete from user where id=10001;

4、insert===>插入数据

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

5、insertSelective===>插入数据

intinsertSelective(Accountrecord);

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

List<Account> selectByExample(AccountExample example); 
UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("joe");
criteria.andUsernameIsNull();
example.setOrderByClause("username asc,email desc");
List<T>list = 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===>根据主键查询数据

AccountselectByPrimaryKey(Integer id);
//相当于select * from user where id = {id};

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

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

9、updateByExampleSelective===>按条件更新

intupdateByExample(@Param("record")Accountrecord,@Param("example")AccountExampleexample);

10、updateByPrimaryKeySelective===>按条件更新

int updateByPrimaryKeySelective(Account record); 
//下面是一个完整的案例  
User user = new User();user.setId(10001);
user.setPassword("123456");
userDAO.updateByPrimaryKeySelective(user);
相当于:update user set password='123456' where id=10001

11、updateByPrimaryKey===>按主键更新

intupdateByPrimaryKey(Accountrecord); 
//下面是一个完整的案例
Useruser=newUser();
user.setId(10001);
user.setUsername("mrHan");
user.setPassword("123456");
user.setEmail("786***[email protected]");
userDAO.updateByPrimaryKey(user);
相当于:update user set username='mrHan',password='123456',email='786**[email protected]'where id=10001;
intupdateByPrimaryKeySelective(Accountrecord); 
//下面是一个完整的案例 
Useruser=newUser();
user.setId(10001);
user.setPassword("123456");
userDAO.updateByPrimaryKeySelective(user);
相当于:update user set password='123456'where id=10001;

补充
Mybatis自动生成的查询selectByExample(TExample example) 中like需要自己写通配符

TExample example = new TExample();  
TExample.Criteria criteria = example.createCriteria();  
if(StringUtils.isNotBlank(userName)){   
userName = "%" + userName + "%";  
}  
if(StringUtils.isNotBlank(userName)){   
criteria.andBuyerNameLike(userName);  
}  
dao.countByExample(example)  

猜你喜欢

转载自blog.csdn.net/i_hanjt/article/details/80090599