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

 使用Example 过程。

github :https://github.com/foolchild/ibatis-example

1.添加依赖

<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>



2.解决mapper.xml文件不能随便放的问题。
1.添加build 属性
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>

2.添加配置properties
mybatis.mapper-locations=classpath*:com/test/test/mapper/*.xml

3.启动类添加配置
@MapperScan("com.ibatis.example.mapper")

##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()  

 1 UserExample example = new UserExample();  
 2 
 3 Criteria criteria = example.createCriteria();  
 4 
 5 criteria.andUsernameEqualTo("joe");  
 6 
 7 criteria.andUsernameIsNull();  
 8 
 9 example.setOrderByClause("username asc,email desc");  
10 
11 List<?>list = ##Mapper.selectByExample(example);  
12 
13 相当于:select * from user where username = 'joe' and username is null order  by username asc,email desc  
View Code

注:在iBator 生成的文件UserExample.java中包含一个static 的内部类 Criteria ,  

在Criteria中有很多方法,主要是定义SQL 语句where后的查询条件。  

③ insert()  

 1 User user = new User();  
 2 
 3 user.setId(101);  
 4 
 5 user.setUsername("test");  
 6 
 7 user.setPassword("123")  
 8 
 9 user.setEmail("joe@163.com");  
10 
11 ##Mapper.insert(user);  
12 
13 相当于:insert into user(ID,username,password,email) values  (101,'test','123','joe@163.com');  
View Code

 ④ updateByPrimaryKey() 和 updateByPrimaryKeySelective()  

 1 User user =new User();  
 2 
 3 user.setId(101);  
 4 
 5 user.setUsername("joe");  
 6 
 7 user.setPassword("joe");  
 8 
 9 user.setEmail("joe@163.com");  
10 
11 ##Mapper.updateByPrimaryKey(user);  
12 
13 相当于:update user set username='joe',password='joe',email='joe@163.com'  where id=101  
14 
15 User user = new User();  
16 
17 user.setId(101);  
18 
19 user.setPassword("joe");  
20 
21 ##Mapper.updateByPrimaryKeySelective(user);  
22 
23 相当于:update user set password='joe' where id=101 
View Code

⑤ updateByExample() 和 updateByExampleSelective()  

 1 UserExample example = new UserExample();  
 2 
 3 Criteria criteria = example.createCriteria();  
 4 
 5 criteria.andUsernameEqualTo("joe");  
 6 
 7 User user = new User();  
 8 
 9 user.setPassword("123");  
10 
11 ##Mapper.updateByPrimaryKeySelective(user,example);  
12 
13 相当于:update user set password='123' where username='joe'  
View Code

⑥ deleteByPrimaryKey()  

##Mapper.deleteByPrimaryKey(101);  相当于:delete from user where id=101  

⑦ deleteByExample() 

1 UserExample example = new UserExample();  
2 
3 Criteria criteria = example.createCriteria();  
4 
5 criteria.andUsernameEqualTo("joe");  
6 
7 ##Mapper.deleteByExample(example);  
8 
9 相当于:delete from user where username='joe'  
View Code

⑧ countByExample()  

1 UserExample example = new UserExample();  
2 
3 Criteria criteria = example.createCriteria();  
4 
5 criteria.andUsernameEqualTo("joe");  
6 
7 int count = ##Mapper.countByExample(example);  
8 
9 相当于:select count(*) from user where username='joe'  
View Code
原文地址:https://www.cnblogs.com/zhanghao1799/p/8317069.html