MyBatis(2)
1)注意mapper目錄是創(chuàng)建在resource文件夾下面的而不是static目錄
2)還要注意,你注入一個(gè)接口,還是使用@Autowried,然后@Mapper是修飾一個(gè)接口的,而不是諸如接口
?對(duì)于我們向數(shù)據(jù)庫(kù)中增加數(shù)據(jù)來(lái)說(shuō),程序應(yīng)該返回兩種類型的數(shù)據(jù),一種是受影響的行數(shù),一種是我想要拿到添加成功的主鍵,一種是我添加數(shù)據(jù)后所影響的行數(shù);
?編寫(xiě)代碼:以向數(shù)據(jù)庫(kù)中添加數(shù)據(jù)為例,根據(jù)在瀏覽器上面輸入的username和password進(jìn)行向數(shù)據(jù)庫(kù)中插入數(shù)據(jù)
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; }第一種:我們想要拿到插入數(shù)據(jù)后的影響行數(shù),我們還要注意我們?cè)趇nsert標(biāo)簽里面是不用進(jìn)行設(shè)置resultMap和resultType這樣的類型的,啥東西也不用設(shè)置
1)這是UserController里面的代碼:在里面要進(jìn)行參數(shù)校驗(yàn)
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進(jìn)行參數(shù)校驗(yàn)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中的字段屬性名是一樣的,當(dāng)方法名的參數(shù)是一個(gè)對(duì)象的時(shí)候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}) 這里面的字段名必須和對(duì)象的屬性名是相同的</insert> </mapper>我們?cè)趯?xiě)XML文件的時(shí)候,我們是希望在url輸入一個(gè)地址,里面的querystring就是user對(duì)象的值,但是我們?cè)谶M(jìn)行插入數(shù)據(jù)的時(shí)候
格式是#{Java代碼中類的字段名}(對(duì)應(yīng)著數(shù)據(jù)庫(kù)的列名)
第二種:我們希望拿到插入數(shù)據(jù)之后的自增主鍵(返回自增id)
我們只需要更改xml文件里面的配置即可
下面我們對(duì)這里面添加的東西來(lái)做一個(gè)說(shuō)明:
<?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:這會(huì)令MyBatis使用JDBC的getGeneratedKeys方法來(lái)取出由數(shù)據(jù)庫(kù)內(nèi)部生成的主鍵,像MYSQL和SQL Server這樣的數(shù)據(jù)庫(kù)管理系統(tǒng)的自動(dòng)遞增字段,默認(rèn)值是false
2)keyColumn:設(shè)置生成鍵值在數(shù)據(jù)庫(kù)表中的列名,自增主鍵在數(shù)據(jù)庫(kù)中的名字叫啥,當(dāng)我們的數(shù)據(jù)庫(kù)字段名和對(duì)象中的屬性不一樣的時(shí)候,這個(gè)keyColumn是可以不用進(jìn)行設(shè)置的,但是如果說(shuō)數(shù)據(jù)庫(kù)的字段名是id,對(duì)象中的屬性名叫做UserID,這個(gè)時(shí)候就會(huì)進(jìn)行自動(dòng)設(shè)置的,自動(dòng)把id的值賦值給User對(duì)象中的ID屬性,所以方法的返回值是可以是void,也可以設(shè)置為int
3)keyProperties:指定對(duì)象中的屬性,你返回的主鍵賦值到對(duì)象中的哪一個(gè)屬性
我們還要注意:
***調(diào)用數(shù)據(jù)庫(kù)完成添加操作,執(zhí)行完添加之后會(huì)將自增的userID指定到user上面的userID屬性上面,因?yàn)槲覀冞M(jìn)行添加操作的時(shí)候 我們是沒(méi)有進(jìn)行指定UserID的,是我們的數(shù)據(jù)庫(kù)自己進(jìn)行添加操作的,我們之前在XML里面進(jìn)行設(shè)置的目的就是將我們進(jìn)行成功插入之后 程序可以將我們的數(shù)據(jù)庫(kù)生成的自增主鍵賦值到我們的user對(duì)象中的userID上面,然后我們?cè)賣serController層上面就可以u(píng)ser的 userID屬性了,如果我們的XML文件不進(jìn)行設(shè)置的話,我們就此時(shí)返回的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進(jìn)行參數(shù)校驗(yàn)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);回顧單元測(cè)試:?
1)定義:是指對(duì)軟件中的最小可測(cè)試單元進(jìn)行檢查和驗(yàn)證的過(guò)程中就叫做單元測(cè)試,在咱們的SpringBoot中是測(cè)試某一個(gè)方法,可以非常簡(jiǎn)單直觀,快速的測(cè)試某一項(xiàng)功能是否正確
單元測(cè)試是開(kāi)發(fā)者編寫(xiě)的一小段代碼,用于檢測(cè)被測(cè)試代碼一個(gè)很小的,很明確的代碼功能是否執(zhí)行正確,就是為了證明某段代碼的執(zhí)行是否符合我們的預(yù)期
2)單元測(cè)試的好處:
2.1)單元測(cè)試是不需要進(jìn)行啟動(dòng)Tomact的,還有就是說(shuō)如果我們中途修改了代碼,打包的時(shí)候就會(huì)自動(dòng)的去執(zhí)行單元測(cè)試,單元測(cè)試后錯(cuò)誤就會(huì)被發(fā)現(xiàn),也就是說(shuō)在我們打包之前所有的單元測(cè)試必須通過(guò),否則不能打包成功
2.2)使用單元測(cè)試進(jìn)行測(cè)試的時(shí)候,可以不污染連接的數(shù)據(jù)庫(kù),也就是說(shuō)在不對(duì)數(shù)據(jù)庫(kù)進(jìn)行任何污染的情況下,測(cè)試功能
??創(chuàng)建SpringBoot單元測(cè)試:test文件夾是在src目錄下的
咱們的SpringBoot項(xiàng)目創(chuàng)建的時(shí)候會(huì)默認(rèn)單元測(cè)試框架spring-boot-test,而這個(gè)單元測(cè)試框架是依靠另一個(gè)著名的測(cè)試框架Junit來(lái)進(jìn)行實(shí)現(xiàn)的,這個(gè)依賴是SpringBoot項(xiàng)目自動(dòng)添加的,所以說(shuō)咱們的高版本的SpringBoot已經(jīng)內(nèi)置了一些框架,單元測(cè)試,JSON格式的處理
1)創(chuàng)建準(zhǔn)備條件:測(cè)試框架(默認(rèn)已經(jīng)添加)添加測(cè)試的目錄,默認(rèn)是已經(jīng)被創(chuàng)建的;
2)在需要進(jìn)行測(cè)試的類左邊進(jìn)行右鍵雙擊Generate生成,選擇生成單元測(cè)試,生成測(cè)試類和測(cè)試的方法,點(diǎn)擊test
Testing library:單元測(cè)試框架,選擇默認(rèn)即可
ClassName:生成單元測(cè)試的類名
Superclass:表示單元測(cè)試的父類,不需要直接設(shè)置,成空就可以了
Destination package:生成單元測(cè)試的目錄?
Generate:是否生成前置方法(執(zhí)行測(cè)試方法之前的前一個(gè)方法)或者后置方法(不動(dòng))
這個(gè)前置方法和后置方法可以用來(lái)記錄一下程序執(zhí)行時(shí)間
Member:測(cè)試方法列表
最后點(diǎn)擊OK就行了
3)要給當(dāng)前類加上@SpringBootTest注解,聲明當(dāng)前的類是在SpringBoot容器里面運(yùn)行的,也就是說(shuō)咱們要進(jìn)行測(cè)試的類是一個(gè)SpringBoot
4)在方法中構(gòu)建咱們的測(cè)試代碼:
@Transaction的好處就是執(zhí)行完我們的單元測(cè)試代碼之后可以進(jìn)行回滾操作,就不會(huì)進(jìn)行污染業(yè)務(wù)數(shù)據(jù),不會(huì)污染原有的數(shù)據(jù)庫(kù),紅色表示單元測(cè)試執(zhí)行失敗
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);//比較它們是否相等,如果相等,說(shuō)明單元測(cè)試過(guò)了,否則是不會(huì)過(guò)的} }下面來(lái)我們來(lái)使用一下單元測(cè)試的方式來(lái)進(jìn)行驗(yàn)證一下插入操作返回的時(shí)候是主鍵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; //需要先注入U(xiǎn)serController對(duì)象,調(diào)用里面的方法@Testvoid insertAll() {User user=new User();user.setClassID(3);user.setUsername("李佳鑫");user.setPassword("生活要戰(zhàn)斗");int len= userController.InsertAll(user);Assertions.assertEquals(len,user.getUserID());} }程序報(bào)錯(cuò)---->程序直接爆紅
測(cè)試不通過(guò)--->顏色變成橙色
測(cè)試通過(guò)---->直接變成藍(lán)色
@Param里面的字段數(shù)據(jù)要和XML里面的數(shù)據(jù)要相同,一一對(duì)應(yīng),可以修改接口中的參數(shù)的別名,盡量寫(xiě)@param,盡量寫(xiě)@Param,會(huì)出現(xiàn)找不到參數(shù)的異常
@Transaction加上這個(gè)注解,就可以保證單元測(cè)試的數(shù)據(jù)不會(huì)污染到原來(lái)的數(shù)據(jù)
下面我們來(lái)實(shí)現(xiàn)以下修改操作:我想修改指定人名的密碼:
我們此時(shí)一定要注意,我們修改的屬性和名字和我們的數(shù)據(jù)庫(kù)中的名字和Java代碼中的類的名字都是不一致的
http://127.0.0.1:8080/update?name=李佳偉&studentPassword=778896 上面是我們?cè)跒g覽器上面輸入的url一:這是UserController里面的代碼:
import java.util.List; @Controller public class UserController {@AutowiredUserService userService;@RequestMapping("/update")@ResponseBodypublic int UpDate(String name,String studentPassword){//我們首先要在Controller層進(jìn)行參數(shù)校驗(yàn)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>我們使用單元測(cè)式的方式來(lái)進(jìn)行檢測(cè):
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);} }我們來(lái)實(shí)現(xiàn)一下刪除操作:我們?cè)O(shè)想是通過(guò)瀏覽器傳遞的UserID來(lái)進(jìn)行刪除
?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)我們實(shí)現(xiàn)的XML文件的代碼:
?
我們使用單元測(cè)試的方式來(lái)進(jìn)行測(cè)試我們的刪除操作:
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)在接口中可以寫(xiě)成這樣:User run(@Param("userID") int userID);多加上一個(gè)注解2)在線演示地址,linux服務(wù)器項(xiàng)目地址(在線演示地址,提供用戶名和密碼),源碼地址(githup)(加上注釋),(然后重要的技能放前面,學(xué)校等不占優(yōu)勢(shì)的放在后面)3)在進(jìn)行查詢的時(shí)候,一定要加上resultMap或者resultType,否則就會(huì)報(bào)500這樣的錯(cuò)誤,二者必選其一,但是update標(biāo)簽只需要設(shè)置id就可以
總結(jié)
以上是生活随笔為你收集整理的MyBatis(2)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: torch.prob
- 下一篇: http隐藏服务器相关配置信息,apac