java学习知识积累-Mybatis中Mapper内置方法细解
MVC的设计模式解释:
model层就是实体类,对应数据库的表。controller层是Servlet,主要是负责业务模块流程的控制,调用service接口的方法,在struts2就是Action。Service层主要做逻辑判断,Dao层是数据访问层,与数据库进行对接。至于Mapper是mybtis框架的映射用到,mapper映射文件在dao层用。
下面是介绍一下Mapper的内置方法:
1、countByExample ===>根据条件查询数量
int countByExample(UserExample example);
//下面是一个完整的案列
UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("joe");
int count = userDAO.countByExample(example);
相当于:select count(*) from user where username='joe'
1
2
3
4
5
6
7
8
intcountByExample(UserExampleexample);
//下面是一个完整的案列
UserExampleexample=newUserExample();
Criteriacriteria=example.createCriteria();
criteria.andUsernameEqualTo("joe");
intcount=userDAO.countByExample(example);
相当于:selectcount(*)fromuserwhereusername='joe'
2、deleteByExample ===>根据条件删除多条
int deleteByExample(AccountExample example);
//下面是一个完整的案例
UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("joe");
userDAO.deleteByExample(example);
相当于:delete from user where username='joe'
1
2
3
4
5
6
7
8
intdeleteByExample(AccountExampleexample);
//下面是一个完整的案例
UserExampleexample=newUserExample();
Criteriacriteria=example.createCriteria();
criteria.andUsernameEqualTo("joe");
userDAO.deleteByExample(example);
相当于:deletefromuserwhereusername='joe'
3、deleteByPrimaryKey===>根据条件删除单条
int deleteByPrimaryKey(Integer id);
userDAO.deleteByPrimaryKey(101); 相当于:delete from user where id=101
1
2
intdeleteByPrimaryKey(Integerid);
userDAO.deleteByPrimaryKey(101);相当于:deletefromuserwhereid=101
4、insert===>插入数据
int insert(Account record);
//下面是完整的案例
User user = new User();
//user.setId(101);
user.setUsername("test");
user.setPassword("123456")
user.setEmail("674531003@qq.com");
userDAO.insert(user);
相当于:insert into user(ID,username,password,email) values(101,'test','123456','674531003@qq.com');
1
2
3
4
5
6
7
8
9
10
intinsert(Accountrecord);
//下面是完整的案例
Useruser=newUser();
//user.setId(101);
user.setUsername("test");
user.setPassword("123456")
user.setEmail("674531003@qq.com");
userDAO.insert(user);
相当于:insertintouser(ID,username,password,email)values(101,'test','123456','674531003@qq.com');
5、insertSelective===>插入数据
int insertSelective(Account record);
1
intinsertSelective(Accountrecord);
6、selectByExample===>根据条件查询数据
List selectByExample(AccountExample example);
//下面是一个完整的案例
UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("joe");
criteria.andUsernameIsNull();
example.setOrderByClause("username asc,email desc");
List>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后的查询条件。
1
2
3
4
5
6
7
8
9
10
11
12
ListselectByExample(AccountExampleexample);
//下面是一个完整的案例
UserExampleexample=newUserExample();
Criteriacriteria=example.createCriteria();
criteria.andUsernameEqualTo("joe");
criteria.andUsernameIsNull();
example.setOrderByClause("username asc,email desc");
List>list=userDAO.selectByExample(example);
相当于:select *fromuserwhereusername='joe'andusernameisnullorderbyusernameasc,emaildesc
//注:在iBator 生成的文件UserExample.java中包含一个static 的内部类 Criteria ,在Criteria中有很多方法,主要是定义SQL 语句where后的查询条件。
7、selectByPrimaryKey===>根据主键查询数据
Account selectByPrimaryKey(Integer id);//相当于select * from user where id = 变量id
1
AccountselectByPrimaryKey(Integerid);//相当于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("123");
userDAO.updateByPrimaryKeySelective(user,example);
相当于:update user set password='123' where username='joe'
1
2
3
4
5
6
7
8
9
10
intupdateByExampleSelective(@Param("record")Accountrecord,@Param("example")AccountExampleexample);
//下面是一个完整的案列
UserExampleexample=newUserExample();
Criteriacriteria=example.createCriteria();
criteria.andUsernameEqualTo("joe");
Useruser=newUser();
user.setPassword("123");
userDAO.updateByPrimaryKeySelective(user,example);
相当于:updateusersetpassword='123'whereusername='joe'
9、updateByExampleSelective===>按条件更新
int updateByExample(@Param("record") Account record, @Param("example") AccountExample example);
1
intupdateByExample(@Param("record")Accountrecord,@Param("example")AccountExampleexample);
10、updateByPrimaryKeySelective===>按条件更新
int updateByPrimaryKeySelective(Account record);
//下面是一个完整的案例
User user = new User();
user.setId(101);
user.setPassword("joe");
userDAO.updateByPrimaryKeySelective(user);
相当于:update user set password='joe' where id=101
1
2
3
4
5
6
7
8
9
intupdateByPrimaryKeySelective(Accountrecord);
//下面是一个完整的案例
Useruser=newUser();
user.setId(101);
user.setPassword("joe");
userDAO.updateByPrimaryKeySelective(user);
相当于:updateusersetpassword='joe'whereid=101
11、updateByPrimaryKey===>按主键更新
int updateByPrimaryKey(Account record);
//下面是一个完整的案例
User user =new User();
user.setId(101);
user.setUsername("joe");
user.setPassword("joe");
user.setEmail("joe@163.com");
userDAO.updateByPrimaryKey(user);
相当于:update user set username='joe',password='joe',email='joe@163.com' where id=101
1
2
3
4
5
6
7
8
9
10
intupdateByPrimaryKey(Accountrecord);
//下面是一个完整的案例
Useruser=newUser();
user.setId(101);
user.setUsername("joe");
user.setPassword("joe");
user.setEmail("joe@163.com");
userDAO.updateByPrimaryKey(user);
相当于:updateusersetusername='joe',password='joe',email='joe@163.com'whereid=101
请尊重我们的辛苦付出,未经允许,请不要转载 本站 的文章,鄙视各种无耻的采集行为!