目錄
前言??
一、增刪查改操作??
1、查??
Ⅰ、mapper接口:??
Ⅱ、UserMapper.xml 查詢所有用戶的具體實現(xiàn) SQL:??
Ⅲ、進行單元測試??
2、增、刪、改操作??
Ⅰ、增??
添加用戶??
添加用戶并且返回自增 id??
Ⅱ、改??
根據(jù)id修改用戶名??
開啟 MyBatis sql 日志打印??
Ⅲ、刪??
二、在單元測試時不污染數(shù)據(jù)庫???
前言??
??????SSM專欄更新中,各位大佬覺得寫得不錯,支持一下,感謝了!??????
Spring + Spring MVC + MyBatis_冷兮雪的博客-CSDN博客
上篇我們寫了一個簡單的根據(jù)id進行查詢,知道了如何去進行查詢,下面來仔細講講增刪改查操作。
一、增刪查改操作??
下面操作會使用到Spring Boot單元測試,可以先看:
Spring Boot單元測試_spring boot 單元測試_冷兮雪的博客-CSDN博客
1、查??
查詢所有的用戶:
Ⅰ、mapper接口:??
package com.example.ssmdemo1.mapper;
import com.example.ssmdemo1.entity.Userinfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@Mapper//需要添加 @Mapper 注解
public interface UserMapper {
/**
* 根據(jù)用戶id查詢用戶信息
* @param id
* @return
*/
Userinfo getUserById(@Param("id") Integer id);
//查詢所有的用戶
List<Userinfo> getAll();
}
Ⅱ、UserMapper.xml 查詢所有用戶的具體實現(xiàn) SQL:??
使用$進行傳遞參數(shù)可能會SQL注入,所以大部分情況下是使用#的?
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.ssmdemo1.mapper.UserMapper">
<select id="getUserById" resultType="com.example.ssmdemo1.entity.Userinfo">
select * from userinfo where id=${id}
</select>
<select id="getAll" resultType="com.example.ssmdemo1.entity.Userinfo">
select * from userinfo
</select>
</mapper>
Ⅲ、進行單元測試??
在UserMapperTest中就有了getAll的測試代碼:
添加單元測試業(yè)務邏輯?
package com.example.ssmdemo1.mapper;
import com.example.ssmdemo1.entity.Userinfo;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest//1、表明當前單元測試是運行在Spring Boot環(huán)境中的
class UserMapperTest {
//2、注入測試對象
@Autowired
private UserMapper userMapper;
@Test
void getUserById() {
//3、添加單元測試的業(yè)務代碼
Userinfo userinfo=userMapper.getUserById(1);
System.out.println(userinfo);
//判斷1是否等于2 簡單斷言
Assertions.assertEquals("admin",userinfo.getUsername());
}
@Test
void getAll() {
List<Userinfo> list=userMapper.getAll();
Assertions.assertEquals(1,list.size());//斷言判斷l(xiāng)ist集合里面是否只有一條數(shù)據(jù),即查詢數(shù)據(jù)庫中只有一個用戶
}
}
?測試正確:
?如果我們沒有設置resultType,則 MyBatis無法自動將查詢結果映射到Java對象,這就會導致單元測試失敗并報錯。
2、增、刪、改操作??
與查詢操作都是一樣的,只是使用的標簽不一樣:
- <insert>標簽:插入語句
- <update>標簽:修改語句
- <delete>標簽:刪除語句
Ⅰ、增??
添加用戶??
①、在接口(UserMapper)中聲明方法
Integer add(Userinfo user);
②、UserService
public Integer getAdd(Userinfo user) {return userMapper.add(user);}
③、在xml中提供實現(xiàn)
<insert id="add">
insert into userinfo(username,password) values(#{username},#{password})
</insert>
#{} 進行賦值使用的是對象中的屬性。
注意:對應的不是數(shù)據(jù)庫的字段,而是程序類中的屬性。
④、controller 實現(xiàn)代碼:
@RequestMapping(value = "/add",method = RequestMethod.POST)
public Integer add(@RequestBody Userinfo user){
return userService.getAdd(user);
}
⑤、單元測試
@Test
void add() {
//偽代碼,構建對象并設置對應的值
//本來是前端傳值過來
Userinfo userinfo=new Userinfo();
userinfo.setUsername("張三");
userinfo.setPassword("123456");
//調(diào)用mybatis 添加方法執(zhí)行添加操作
int result=userMapper.add(userinfo);
Assertions.assertEquals(1,result);
}
?單元測試成功,可以去看數(shù)據(jù)庫也添加了一條數(shù)據(jù)
但是如果我們?nèi)カ@取這個用戶的id,就會報空指針異常:
添加用戶并且返回自增 id??
?如果我們想在添加用戶的時候同時去獲取他的id,具體實現(xiàn)如下:
①、在接口(UserMapper)中聲明方法
Integer addGetid(Userinfo user);
②、UserService
public Integer addGetid(Userinfo user) {return userMapper.addGetid(user);}
③、在xml中提供實現(xiàn)
<insert id="addGetid" useGeneratedKeys="true" keyProperty="id">
insert into userinfo(username,password) values(#{username},#{password})
</insert>
- useGeneratedKeys:這會令 MyBatis 使用?JDBC 的 getGeneratedKeys 方法來取出由數(shù)據(jù)庫內(nèi)部生成的主鍵(比如:像 MySQL 和 SQL Server 這樣的關系型數(shù)據(jù)庫管理系統(tǒng)的自動遞增字段),默認值:false。
- keyColumn:設置生成鍵值在表中的列名,在某些數(shù)據(jù)庫(像 PostgreSQL)中,當主鍵列 不是表中的第?列的時候,是必須設置的。如果?成列不止?個,可以用逗號分隔多個屬性 名稱。
- keyProperty:指定能夠唯?識別對象的屬性,MyBatis會使用?getGeneratedKeys 的返回值或 insert 語句的 selectKey 子元素設置它的值,默認值:未設置(unset)。如果生成列不止?個,可以?逗號分隔多個屬性名稱。
④、controller 實現(xiàn)代碼:
@RequestMapping(value = "/add2", method = RequestMethod.POST)
public Integer add2(@RequestBody Userinfo user) {
userService.addGetid(user);
return user.getId();
}
⑤、單元測試
@Test
void addGetid() {
//偽代碼,構建對象并設置對應的值
//本來是前端傳值過來
Userinfo userinfo=new Userinfo();
userinfo.setUsername("張三");
userinfo.setPassword("123456");
//調(diào)用MyBatis 添加方法執(zhí)行添加操作 返回受影響的行數(shù)
int result=userMapper.addGetid(userinfo);
int id=userinfo.getId();//得到自增id(MyBatis自動賦值)
System.out.println("id:"+id);
Assertions.assertEquals(1,result);
}
?一樣的代碼,就一行代碼不同
Ⅱ、改??
根據(jù)id修改用戶名??
①、在接口(UserMapper)中聲明方法
/**
* 修改用戶
* @param userinfo
* @return
*/
Integer upUserName(Userinfo userinfo);
②、在xml中提供實現(xiàn)
<update id="upUserName">
update userinfo set username=#{username} where id=#{id}
</update>
③、單元測試
將之前添加的張三修改成為李四
@Test
void upUserName() {
// 構建測試數(shù)據(jù)
Userinfo userinfo = new Userinfo();
userinfo.setId(9);
userinfo.setUsername("李四");
int result = userMapper.upUserName(userinfo);
System.out.println("修改:" + result);
Assertions.assertEquals(1, result);
}
修改成功:?
如果我們需要在控制臺看到SQL語句,需要去配置文件進行配置,因為這默認是隱藏MyBatic生成SQL語句的細節(jié)
開啟 MyBatis sql 日志打印??
①、添加配置文件
# 開啟 mybatis sql 日志打印
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
logging.level.com.example.demo=debug
②、重新運行單元測試
@Test
void upUserName() {
// 構建測試數(shù)據(jù)
Userinfo userinfo = new Userinfo();
userinfo.setId(9);
userinfo.setUsername("張三");
int result = userMapper.upUserName(userinfo);
System.out.println("修改:" + result);
Assertions.assertEquals(1, result);
}
?執(zhí)行成功:
結論: 可以看到這個就是jdbc鏈接,這個SQL寫法也是jdbc的寫法,也就是說mybatis是基于jdbc的,MyBatis使用了JDBC來與數(shù)據(jù)庫進行交互,它也簡化了JDBC的使用,提供了更方便的數(shù)據(jù)庫訪問方式。。
建議大家在開發(fā)階段是開啟SQL的打印,可以更好的幫助你找到錯誤。
Ⅲ、刪??
①、在接口(UserMapper)中聲明方法
int delById(@Param("id") Integer id);
②、在xml中提供實現(xiàn)
<delete id="delById">
delete from userinfo where id=#{id}
</delete>
?③、單元測試
將id為9的用戶刪除:
@Test
void delById() {
int id=9;
int result=userMapper.delById(id);
System.out.println("刪除:"+result);
Assertions.assertEquals(1,result);
}
刪除成功:?
二、在單元測試時不污染數(shù)據(jù)庫???
在需要的地方添加注解:@Transactional(可以修飾方法也可以修飾類)
如下面,去修飾UserMapper整個類:
文章來源:http://www.zghlxwxcb.cn/news/detail-602858.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-602858.html
到了這里,關于MyBatis查詢數(shù)據(jù)庫(2)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!