在上一篇文章 從零搭建springboot+mybatis逆向工程 中介紹了如何在IDEA+springboot下搭建mybatis逆向工程以及一個簡單的接口。本文主要總結一下mapper接口中方法的使用,和個人的一些理解。
一、mapper接口中的方法解析
mapper接口中的函數及方法
二、example實例解析
mybatis的逆向工程中會生成實例及實例對應的example,example用於添加條件,相當where后面的部分
xxxExample example = new xxxExample();
Criteria criteria = new Example().createCriteria();
三、單元測試中使用
創建單元測試類,在springboot中十分方便
1、selectByPrimaryKey
先注入userMapper,然后再測試方法
不要忘了在UserMapper接口上加上@Mapper注解,不然會報錯
package com.example.dao;
import com.example.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
@SpringBootTest
public class UserDaoTest {
@Test
public void index(){
System.out.println("我是SpringBoot的單元測試");
}
@Resource
private UserMapper userMapper;
/**
* 根據主鍵查找
*/
@Test
void selectByPrimaryKey() {
System.out.println(userMapper.selectByPrimaryKey(1));
}
}
2、selectByExample
/**
* 根據條件查詢
*/
@Test
void selectByExample(){
//通過criteria構造查詢條件
UserExample userExample = new UserExample();
// userExample.setOrderByClause("username asc"); //asc升序,desc降序排列
// userExample.setDistinct(false); //去除重復,true是選擇不重復記錄,false反之
UserExample.Criteria criteria = userExample.createCriteria(); //構造自定義查詢條件
criteria.andUsernameEqualTo("admin");
criteria.andPasswordEqualTo("admin");
//自定義查詢條件可能返回多條記錄,使用List接收
List<User> users = userMapper.selectByExample(userExample);
System.out.println(users);
}
3、insert
/**
* 插入
*/
@Test
void insert(){
User user = new User();
// user.setId(null);
user.setUsername("張三");
// user.setPassword();
System.out.println(userMapper.insert(user));
}
4、insertSelective
@Test
void insertSelective(){
User user = new User();
user.setUsername("zhangsan");
System.out.println(userMapper.insertSelective(user));
}
5、deleteByPrimaryKey
@Test
void deleteByPrimaryKey(){
System.out.println(userMapper.deleteByPrimaryKey(2));
}
6、deleteByExample
@Test
void deleteByExample(){
//通過criteria構造查詢條件
UserExample userExample = new UserExample();
// userExample.setOrderByClause("username asc"); //asc升序,desc降序排列
// userExample.setDistinct(false); //去除重復,true是選擇不重復記錄,false反之
UserExample.Criteria criteria = userExample.createCriteria(); //構造自定義查詢條件
criteria.andIdBetween(5,13);
//自定義查詢條件可能返回多條記錄,使用List接收
userMapper.deleteByExample(userExample);
System.out.println();
}
7、updateByPrimaryKey
@Test
void updateByPrimaryKey(){
User user = new User();
user.setId(4);
// user.setUsername("lisi");
userMapper.updateByPrimaryKey(user);
}
8、updateByPrimaryKeySelective
@Test
void updateByPrimaryKeySelective(){
User user = new User();
user.setId(4);
user.setUsername("lisi");
userMapper.updateByPrimaryKeySelective(user);
}
9、updateByExample
@Test
void updateByExample(){
//通過criteria構造查詢條件
UserExample userExample = new UserExample();
// userExample.setOrderByClause("username asc"); //asc升序,desc降序排列
// userExample.setDistinct(false); //去除重復,true是選擇不重復記錄,false反之
UserExample.Criteria criteria = userExample.createCriteria(); //構造自定義查詢條件
criteria.andIdEqualTo(3);
User user = new User();
user.setId(3);
user.setUsername("李四2");
// user.setPassword("123456");
userMapper.updateByExample(user,userExample);
}
10、updateByExampleSelective
@Test
void updateByExampleSelective(){
//通過criteria構造查詢條件
UserExample userExample = new UserExample();
// userExample.setOrderByClause("username asc"); //asc升序,desc降序排列
// userExample.setDistinct(false); //去除重復,true是選擇不重復記錄,false反之
UserExample.Criteria criteria = userExample.createCriteria(); //構造自定義查詢條件
criteria.andPasswordEqualTo("123456");
User user = new User();
user.setUsername("李四");
// user.setPassword("123456");
userMapper.updateByExampleSelective(user,userExample);
}
四、Example與Selective
1、Example
相當於WHERE,將一些限制條件拼接到SQL語句后面,具體的參照上面所說。這里舉一個簡單的例子:這里相當於一個登錄驗證的功能,通過用戶名和密碼來驗證用戶
//通過criteria構造查詢條件
UserExample userExample = new UserExample();
// userExample.setOrderByClause("username asc"); //asc升序,desc降序排列
// userExample.setDistinct(false); //去除重復,true是選擇不重復記錄,false反之
UserExample.Criteria criteria = userExample.createCriteria(); //構造自定義查詢條件
criteria.andUsernameEqualTo("admin");
criteria.andPasswordEqualTo("admin");
//自定義查詢條件可能返回多條記錄,使用List接收
List<User> users = userMapper.selectByExample(userExample);
System.out.println(users);
SQL語句如下
查詢結果如下
2、Selective
Selective會對為空的字段進行屏蔽
以更新為例
<update id="updateByPrimaryKeySelective" parameterType="com.example.pojo.User">
update t_user
<set>
<if test="username != null">
username = #{username,jdbcType=VARCHAR},
</if>
<if test="password != null">
password = #{password,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.example.pojo.User">
update t_user
set username = #{username,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR}
where id = #{id,jdbcType=INTEGER}
</update>
@Test
void updateByPrimaryKey(){
User user = new User();
user.setId(4);
/**
* updateByPrimaryKey會將主鍵為4的那條記錄更新為除了主鍵其他字段全為空的一條記錄
*/
userMapper.updateByPrimaryKey(user);
}
@Test
void updateByPrimaryKeySelective(){
User user = new User();
user.setId(3);
user.setUsername("lisi");
/**
* updateByPrimaryKeySelective會將主鍵為3那條記錄的username字段更新為“lisi”,其他字段不變
*/
userMapper.updateByPrimaryKeySelective(user);
}