Mybatis中的自带Mapper方法随笔记录一下

经常用是克服忘记的有效方式。
                                                   Mybatis中的自带Mapper方法


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***195@qq.com"); 
userDAO.insert(user); 
相当于:insert into user(ID,username,password,email) values(10001,'mrHan','123456','786***195@qq.com');
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===>按条件更新
int  updateByExample(@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===>按主键更新
int updateByPrimaryKey(Accountrecord); 
//下面是一个完整的案例
Useruser=newUser();
user.setId(10001);
user.setUsername("mrHan");
user.setPassword("123456");
user.setEmail("786***195@qq.com");
userDAO.updateByPrimaryKey(user);
相当于:update user set username='mrHan',password='123456',email='786**195@qq.com'where id=10001;

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

补充
1
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)  
2
//按条件查询(包括BLOB字段)。只有当数据表中的字段类型有为二进制的才会产生。
List<Items> selectByExampleWithBLOBs(ItemsExample example);
3
//和updateByExample相比此方法可以修改大字段类型,其余性质和updateByExample相同
 int updateByExampleWithBLOBs(@Param("record") Items record, @Param("example") ItemsExample example);
4,
//和updateByPrimaryKey相比此方法可以修改大字段类型,其余性质和updateByPrimaryKey相同
    int updateByPrimaryKeyWithBLOBs(Items record);

https://www.cnblogs.com/aaaazzzz/p/13173054.html
http://www.bubuko.com/infodetail-3603220.html
https://blog.csdn.net/i_hanjt/article/details/80090599

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值