记录分为两个部分,第一部分主要关注selectByExample类的实例函数的实现;第二部分讨论Mybatis框架下基本的实例函数。

(一)selectByExample类的实例函数的实现

mybatis中的mapper接口文件以及selectByExample类的实例函数详解-LMLPHP

当你启动项目,并且打算查询相应的数据库中的相应的表时:

mybatis中的mapper接口文件以及selectByExample类的实例函数详解-LMLPHP

接着跟踪进去:

mybatis中的mapper接口文件以及selectByExample类的实例函数详解-LMLPHP

mybatis中的mapper接口文件以及selectByExample类的实例函数详解-LMLPHP

然后查询过程就交给了Mybatis框架处理了,那么还有一个问题,我们知道selectByExample实例函数的参数SQL语句的条件值,那么这个是怎么实现的呢?一般在entity层包含一个static 的内部类 Criteria ,在Criteria中有很多方法,主要是定义SQL 语句where后的查询条件。

mybatis中的mapper接口文件以及selectByExample类的实例函数详解-LMLPHP

mybatis中的mapper接口文件以及selectByExample类的实例函数详解-LMLPHP

这样大概就能使用Mybatis的接口了,当然后续还得深入的学习Mybatis框架。

(二)Mybatis框架下基本的实例函数

这个部分是参考的网上的Mapper接口的其他的example实例函数,可以看一下,对于Mybatis下的基本服务接口能有一个大概的脉络。

    ##Example example = new ##Example();
example.setOrderByClause("字段名 ASC"); //升序排列,desc为降序排列。
example.setDistinct(false)//去除重复,boolean型,true为选择不重复的记录。
Criteria criteria = new Example().createCriteria();
is null;is not null;
equal to(value);not equal to(value);
GreaterThan(value);GreaterThanOrEqualTo(value);
LessThan(value); LessThanOrEqualTo(value);
in(item,item,item,...);not in(item,item,item,...);
like("%"+value+"%");not like("%"+value+"%");
Between(value1,value2);not between(value1,value2) mybatis中mapper的实例函数:
int countByExample(UserExample example) thorws SQLException:按条件计数。
int deleteByPrimaryKey(Integer id) thorws SQLException:按主键删除。
int deleteByExample(UserExample example) thorws SQLException:按条件删除。
String/Integer insert(User record) thorws SQLException:插入(返回值为id值)
User selectByPrimaryKey(Integer id) thorws SQLException:按主键查询。
List<?>selectByExample(UserExample example) thorws SQLException:按条件查询
List<?>selectByExampleWithBLOGs(UserExample example) thorws SQLException:按 条件查询(包括BLOB字段)。只有当数据表中的字段类型有为二进制的才会产生。
int updateByPrimaryKey(User record) thorws SQLException:按主键更新
int updateByPrimaryKeySelective(User record) thorws SQLException:按主键更新 值不为null的字段
int updateByExample(User record, UserExample example) thorws SQLException: 按条件更新
int updateByExampleSelective(User record, UserExample example) thorws SQLException:按条件更新值不为null的字段 mybatis中mapper的实例函数详解:
① selectByPrimaryKey() User user = ##Mapper.selectByPrimaryKey(100); 相当于select * from user where id = 100 ② selectByExample() 和 selectByExampleWithBLOGs() UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("joe");
criteria.andUsernameIsNull();
example.setOrderByClause("username asc,email desc");
List<?>list = ##Mapper.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后的查询条件。 ③ insert() User user = new User();
user.setId(101);
user.setUsername("test");
user.setPassword("123")
user.setEmail("[email protected]");
##Mapper.insert(user);
相当于:insert into user(ID,username,password,email) values (101,'test','123','[email protected]'); ④ updateByPrimaryKey() 和 updateByPrimaryKeySelective() User user =new User();
user.setId(101);
user.setUsername("joe");
user.setPassword("joe");
user.setEmail("[email protected]");
##Mapper.updateByPrimaryKey(user);
相当于:update user set username='joe',password='joe',email='[email protected]' where id=101 User user = new User();
user.setId(101);
user.setPassword("joe");
##Mapper.updateByPrimaryKeySelective(user);
相当于:update user set password='joe' where id=101 ⑤ updateByExample() 和 updateByExampleSelective() UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("joe");
User user = new User();
user.setPassword("123");
##Mapper.updateByPrimaryKeySelective(user,example);
相当于:update user set password='123' where username='joe' ⑥ deleteByPrimaryKey() ##Mapper.deleteByPrimaryKey(101); 相当于:delete from user where id=101 ⑦ deleteByExample() UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("joe");
##Mapper.deleteByExample(example);
相当于:delete from user where username='joe' ⑧ countByExample() UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("joe");
int count = ##Mapper.countByExample(example);
相当于:select count(*) from user where username='joe'
04-23 07:56