经常用是克服忘记的有效方式。
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