MyBatis(2)
1)注意mapper目錄是創建在resource文件夾下面的而不是static目錄
2)還要注意,你注入一個接口,還是使用@Autowried,然后@Mapper是修飾一個接口的,而不是諸如接口
?對于我們向數據庫中增加數據來說,程序應該返回兩種類型的數據,一種是受影響的行數,一種是我想要拿到添加成功的主鍵,一種是我添加數據后所影響的行數;
?編寫代碼:以向數據庫中添加數據為例,根據在瀏覽器上面輸入的username和password進行向數據庫中插入數據
package com.example.demo; import lombok.Getter; import lombok.Setter; import lombok.ToString; @Setter @Getter @ToString public class User {private int ClassID;private int userID;private String username;private String password; }第一種:我們想要拿到插入數據后的影響行數,我們還要注意我們在insert標簽里面是不用進行設置resultMap和resultType這樣的類型的,啥東西也不用設置
1)這是UserController里面的代碼:在里面要進行參數校驗
package com.example.demo.Controller;import com.example.demo.Service.UserService; import com.example.demo.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody;import java.util.List;@Controller public class UserController {@AutowiredUserService userService;@RequestMapping("/InsertAll")@ResponseBodypublic int InsertAll(User user){//我們需要在UserController進行參數校驗if(user==null||user.equals("")){return -1;}return userService.InsertAll(user);} }2)這是UserService里面的代碼:
package com.example.demo.Service; import com.example.demo.User; import com.example.demo.UserMapper.SpringBootMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserService {@Autowiredprivate SpringBootMapper mapper;public int InsertAll(User user) {return mapper.InsertAll(user);} }3)這是SpringBootMapper里面的代碼:
public int InsertAll(User user); //后面的XML字段屬性和User中的字段屬性名是一樣的,當方法名的參數是一個對象的時候4)這是xml文件里面的代碼:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD com.example.demo.Mapper1 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.demo.UserMapper.SpringBootMapper"><insert id="InsertAll">insert into User values(null,#{classID},#{username},#{password}) 這里面的字段名必須和對象的屬性名是相同的</insert> </mapper>我們在寫XML文件的時候,我們是希望在url輸入一個地址,里面的querystring就是user對象的值,但是我們在進行插入數據的時候
格式是#{Java代碼中類的字段名}(對應著數據庫的列名)
第二種:我們希望拿到插入數據之后的自增主鍵(返回自增id)
我們只需要更改xml文件里面的配置即可
下面我們對這里面添加的東西來做一個說明:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD com.example.demo.Mapper1 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.demo.UserMapper.SpringBootMapper"><insert id="InsertAll" useGeneratedKeys="true" keyProperty="userID" keyColumn="userID">insert into User values(null,#{classID},#{username},#{password})</insert> </mapper>1)useGeneratedKeys:這會令MyBatis使用JDBC的getGeneratedKeys方法來取出由數據庫內部生成的主鍵,像MYSQL和SQL Server這樣的數據庫管理系統的自動遞增字段,默認值是false
2)keyColumn:設置生成鍵值在數據庫表中的列名,自增主鍵在數據庫中的名字叫啥,當我們的數據庫字段名和對象中的屬性不一樣的時候,這個keyColumn是可以不用進行設置的,但是如果說數據庫的字段名是id,對象中的屬性名叫做UserID,這個時候就會進行自動設置的,自動把id的值賦值給User對象中的ID屬性,所以方法的返回值是可以是void,也可以設置為int
3)keyProperties:指定對象中的屬性,你返回的主鍵賦值到對象中的哪一個屬性
我們還要注意:
***調用數據庫完成添加操作,執行完添加之后會將自增的userID指定到user上面的userID屬性上面,因為我們進行添加操作的時候 我們是沒有進行指定UserID的,是我們的數據庫自己進行添加操作的,我們之前在XML里面進行設置的目的就是將我們進行成功插入之后 程序可以將我們的數據庫生成的自增主鍵賦值到我們的user對象中的userID上面,然后我們再userController層上面就可以user的 userID屬性了,如果我們的XML文件不進行設置的話,我們就此時返回的userID就不知道是啥值了 1)UserController里面的代碼: package com.example.demo.Controller; import com.example.demo.Service.UserService; import com.example.demo.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.util.List; @Controller public class UserController {@AutowiredUserService userService;@RequestMapping("/InsertAll")@ResponseBodypublic int InsertAll(User user){//我們需要在UserController進行參數校驗if(user==null||user.equals("")){return -1;}userService.InsertAll(user);return user.getUserID();} } 2)UserService里面的代碼: package com.example.demo.Service; import com.example.demo.User; import com.example.demo.UserMapper.SpringBootMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserService {@Autowiredprivate SpringBootMapper mapper;public List<User> GetAll() {return mapper.GetAll();}public int InsertAll(User user) {return mapper.InsertAll(user);} } 3)接口里面的代碼:public int InsertAll(User user);回顧單元測試:?
1)定義:是指對軟件中的最小可測試單元進行檢查和驗證的過程中就叫做單元測試,在咱們的SpringBoot中是測試某一個方法,可以非常簡單直觀,快速的測試某一項功能是否正確
單元測試是開發者編寫的一小段代碼,用于檢測被測試代碼一個很小的,很明確的代碼功能是否執行正確,就是為了證明某段代碼的執行是否符合我們的預期
2)單元測試的好處:
2.1)單元測試是不需要進行啟動Tomact的,還有就是說如果我們中途修改了代碼,打包的時候就會自動的去執行單元測試,單元測試后錯誤就會被發現,也就是說在我們打包之前所有的單元測試必須通過,否則不能打包成功
2.2)使用單元測試進行測試的時候,可以不污染連接的數據庫,也就是說在不對數據庫進行任何污染的情況下,測試功能
??創建SpringBoot單元測試:test文件夾是在src目錄下的
咱們的SpringBoot項目創建的時候會默認單元測試框架spring-boot-test,而這個單元測試框架是依靠另一個著名的測試框架Junit來進行實現的,這個依賴是SpringBoot項目自動添加的,所以說咱們的高版本的SpringBoot已經內置了一些框架,單元測試,JSON格式的處理
1)創建準備條件:測試框架(默認已經添加)添加測試的目錄,默認是已經被創建的;
2)在需要進行測試的類左邊進行右鍵雙擊Generate生成,選擇生成單元測試,生成測試類和測試的方法,點擊test
Testing library:單元測試框架,選擇默認即可
ClassName:生成單元測試的類名
Superclass:表示單元測試的父類,不需要直接設置,成空就可以了
Destination package:生成單元測試的目錄?
Generate:是否生成前置方法(執行測試方法之前的前一個方法)或者后置方法(不動)
這個前置方法和后置方法可以用來記錄一下程序執行時間
Member:測試方法列表
最后點擊OK就行了
3)要給當前類加上@SpringBootTest注解,聲明當前的類是在SpringBoot容器里面運行的,也就是說咱們要進行測試的類是一個SpringBoot
4)在方法中構建咱們的測試代碼:
@Transaction的好處就是執行完我們的單元測試代碼之后可以進行回滾操作,就不會進行污染業務數據,不會污染原有的數據庫,紅色表示單元測試執行失敗
package com.example.demo.Controller;import com.example.demo.User; 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 org.springframework.transaction.annotation.Transactional;import static org.junit.jupiter.api.Assertions.*; @SpringBootTest @Transactional class UserControllerTest {@AutowiredUserController userController;@Testvoid insertAll() {User user=new User();user.setClassID(2);user.setUsername("白雪");user.setPassword("我想吃飯");int len=userController.InsertAll(user);Assertions.assertEquals(len,1);//比較它們是否相等,如果相等,說明單元測試過了,否則是不會過的} }下面來我們來使用一下單元測試的方式來進行驗證一下插入操作返回的時候是主鍵ID
package com.example.demo.Controller; import com.example.demo.User; 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 org.springframework.transaction.annotation.Transactional; import static org.junit.jupiter.api.Assertions.*; @SpringBootTest @Transactional class UserControllerTest {@AutowiredUserController userController; //需要先注入UserController對象,調用里面的方法@Testvoid insertAll() {User user=new User();user.setClassID(3);user.setUsername("李佳鑫");user.setPassword("生活要戰斗");int len= userController.InsertAll(user);Assertions.assertEquals(len,user.getUserID());} }程序報錯---->程序直接爆紅
測試不通過--->顏色變成橙色
測試通過---->直接變成藍色
@Param里面的字段數據要和XML里面的數據要相同,一一對應,可以修改接口中的參數的別名,盡量寫@param,盡量寫@Param,會出現找不到參數的異常
@Transaction加上這個注解,就可以保證單元測試的數據不會污染到原來的數據
下面我們來實現以下修改操作:我想修改指定人名的密碼:
我們此時一定要注意,我們修改的屬性和名字和我們的數據庫中的名字和Java代碼中的類的名字都是不一致的
http://127.0.0.1:8080/update?name=李佳偉&studentPassword=778896 上面是我們在瀏覽器上面輸入的url一:這是UserController里面的代碼:
import java.util.List; @Controller public class UserController {@AutowiredUserService userService;@RequestMapping("/update")@ResponseBodypublic int UpDate(String name,String studentPassword){//我們首先要在Controller層進行參數校驗if(name==null||name.equals("")||studentPassword.equals("")||studentPassword==null){return -1;}return userService.UpDate(name,studentPassword);} }二:這是UserService里面的代碼:
package com.example.demo.Service; import com.example.demo.User; import com.example.demo.UserMapper.SpringBootMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserService {@Autowiredprivate SpringBootMapper mapper;public int UpDate(String name, String studentPassword) {return mapper.UpDate(name,studentPassword);} }三:這是XML文件和接口中的代碼:
int UpDate(String name, String studentPassword);<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD com.example.demo.Mapper1 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.demo.UserMapper.SpringBootMapper"><update id="UpDate">update user set password=#{studentPassword} where username=#{name}</update> </mapper>我們使用單元測式的方式來進行檢測:
package com.example.demo.Controller; import com.example.demo.User; 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 org.springframework.transaction.annotation.Transactional; import static org.junit.jupiter.api.Assertions.*; @SpringBootTest @Transactional class UserControllerTest {@AutowiredUserController userController;@Testvoid upDate() {int len= userController.UpDate("李佳偉","773377");Assertions.assertEquals(len,1);} }我們來實現一下刪除操作:我們設想是通過瀏覽器傳遞的UserID來進行刪除
?1)UserController里面的代碼:
package com.example.demo.Controller; import com.example.demo.Service.UserService; import com.example.demo.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.util.List; @Controller public class UserController {@AutowiredUserService userService;@RequestMapping("/Delete")@ResponseBodypublic int Delete(Integer userID){if(userID==null||userID.equals("")||userID<=0){return -1;}return userService.Delete(userID);} }2)UserService和Mapper里面的代碼:
package com.example.demo.Service; import com.example.demo.User; import com.example.demo.UserMapper.SpringBootMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserService {@Autowiredprivate SpringBootMapper mapper;public int Delete(Integer userID) {return mapper.Delete(userID);} }int Delete(Integer userID);3)我們實現的XML文件的代碼:
?
我們使用單元測試的方式來進行測試我們的刪除操作:
package com.example.demo.Controller; import com.example.demo.User; 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 org.springframework.transaction.annotation.Transactional; import static org.junit.jupiter.api.Assertions.*; @SpringBootTest @Transactional class UserControllerTest {@AutowiredUserController userController;@Testvoid delete() {int len= userController.Delete(1);} 1)在接口中可以寫成這樣:User run(@Param("userID") int userID);多加上一個注解2)在線演示地址,linux服務器項目地址(在線演示地址,提供用戶名和密碼),源碼地址(githup)(加上注釋),(然后重要的技能放前面,學校等不占優勢的放在后面)3)在進行查詢的時候,一定要加上resultMap或者resultType,否則就會報500這樣的錯誤,二者必選其一,但是update標簽只需要設置id就可以
總結
以上是生活随笔為你收集整理的MyBatis(2)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: torch.prob
- 下一篇: http隐藏服务器相关配置信息,apac