java学习知识积累-Mybatis中Mapper内置方法细解
2017-08-04 11:23
375 查看
MVC的设计模式解释:
model层就是实体类,对应数据库的表。controller层是Servlet,主要是负责业务模块流程的控制,调用service接口的方法,在struts2就是Action。Service层主要做逻辑判断,Dao层是数据访问层,与数据库进行对接。至于Mapper是mybtis框架的映射用到,mapper映射文件在dao层用。
下面是介绍一下Mapper的内置方法:
1、countByExample ===>根据条件查询数量
2、deleteByExample ===>根据条件删除多条
3、deleteByPrimaryKey===>根据条件删除单条
4、insert===>插入数据
5、insertSelective===>插入数据
6、selectByExample===>根据条件查询数据
7、selectByPrimaryKey===>根据主键查询数据
8、updateByExampleSelective===>按条件更新值不为null的字段
9、updateByExampleSelective===>按条件更新
10、updateByPrimaryKeySelective===>按条件更新
11、updateByPrimaryKey===>按主键更新
insert,返回值是:新插入行的主键(primary key);需要包含<selectKey>语句,才会返回主键,否则返回值为null。
update/delete,返回值是:更新或删除的行数;无需指明resultClass;但如果有约束异常而删除失败,只能去捕捉异常。
queryForObject,返回的是:一个实例对象或null;需要包含<select>语句,并且指明resultMap;
queryForList,返回的是:实例对象的列表;需要包含<select>语句,并且指明resultMap;
model层就是实体类,对应数据库的表。controller层是Servlet,主要是负责业务模块流程的控制,调用service接口的方法,在struts2就是Action。Service层主要做逻辑判断,Dao层是数据访问层,与数据库进行对接。至于Mapper是mybtis框架的映射用到,mapper映射文件在dao层用。
下面是介绍一下Mapper的内置方法:
1、countByExample ===>根据条件查询数量
12345678 | 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 | int deleteByExample(AccountExample example); //下面是一个完整的案例 UserExample example=new UserExample(); Criteriacriteria=example.createCriteria(); criteria.andUsernameEqualTo("joe"); userDAO.deleteByExample(example); 相当于:delete from user where username='joe' |
12 | int deleteByPrimaryKey(Integer id);userDAO.deleteByPrimaryKey(101); 相当于:delete from user where id=101 |
1 2 3 4 5 6 7 8 9 10 | 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 | int insertSelective(Account record); |
1 2 3 4 5 6 7 8 9 10 11 12 | List<Account>selectByExample(AccountExample example); //下面是一个完整的案例 UserExample example=new UserExample(); Criteriacriteria=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,emaildesc //注:在iBator 生成的文件UserExample.java中包含一个static 的内部类 Criteria ,在Criteria中有很多方法,主要是定义SQL 语句where后的查询条件。 |
1 | Account selectByPrimaryKey(Integer id);//相当于select * from user where id = 变量id |
1 2 3 4 5 6 7 8 9 10 | 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 | int updateByExample(@Param("record") Account record, @Param("example") AccountExample example); |
1 2 3 4 5 6 7 8 9 | int updateByPrimaryKeySelective(Accountrecord); //下面是一个完整的案例 User user=new User(); user.setId(101); user.setPassword("joe"); userDAO.updateByPrimaryKeySelective(user); 相当于:update user set password = 'joe' whereid = 101 |
1 2 3 4 5 6 7 8 9 10 | intupdateByPrimaryKey(Accountrecord); //下面是一个完整的案例 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 |
update/delete,返回值是:更新或删除的行数;无需指明resultClass;但如果有约束异常而删除失败,只能去捕捉异常。
queryForObject,返回的是:一个实例对象或null;需要包含<select>语句,并且指明resultMap;
queryForList,返回的是:实例对象的列表;需要包含<select>语句,并且指明resultMap;
相关文章推荐
- Mybatis中Mapper内置方法细解
- Mybatis中Mapper内置方法细解
- Mybatis中Mapper内置方法细解
- Java程序猿的JavaScript学习笔记(5——prototype和Object内置方法)
- java基础知识回顾之java Thread类学习(十一)--join方法的理解
- MyBatis由浅入深学习总结之二:MyBatis解决Java实体类和数据库表字段不一致方法总结
- 【MyBatis学习04】mapper代理方法开发dao
- 【MyBatis学习04】mapper代理方法开发dao
- MyBatis由浅入深学习总结之二:MyBatis解决Java实体类和数据库表字段不一致方法总结
- MyBatis由浅入深学习总结之二:MyBatis解决Java实体类和数据库表字段不一致方法总结
- Java学习之容器上(Collection接口常用方法,Iterator接口,使用foreach循环遍历Collection集合元素,Set集合通用知识(Hashset类,hashcode()与LinkedHashSet类))
- Mybatis中model内置方法细解
- 【Python】Java程序员学习Python(四)— 内置方法和内置变量
- 【Java学习笔记】基础知识学习6【类的基础、方法,对象基础、包】
- mybatis学习笔记,mapper代理方法的使用
- Mybatis学习(05)-mapper代理方法开发dao && 输入映射和输出映射
- Java程序员的JavaScript学习笔记(5——prototype和Object内置方法)
- 【MyBatis学习04】mapper代理方法开发dao
- [原]Java程序员的JavaScript学习笔记(5——prototype和Object内置方法)