Springboot+Mybatis-plus不使用SQL語句進行多表添加操作及問題小結(jié)
在Springboot+Mybatis-plus不使用SQL語句進行多表添加操作
我所遇到的問題準備工作在測試環(huán)境下模擬思維分解一下:創(chuàng)建出一個帶有參數(shù)的BrandDTO對象模擬對后臺傳遞參數(shù)
我所遇到的問題
我們都知道,在我們使用Mybatis-plus中進行多表操作是極其困難的,如果你不使用Mybatis-plus-join這一類的工具,你只能去配置對應(yīng)的Mapper.xml文件,配置又臭又長的ResultMap,然后再去寫對應(yīng)的sql語句,這種方法雖然看上去很麻煩,但具有很高的靈活性,可以讓我們更靈活的修改甲方需要的功能.
但是如果我將要做一個很普通的小項目,不需要什么靈活的變動,并且我不想去寫SQL語句,想直接用Mybatis-plus的功能來實現(xiàn)多表(一主多副)數(shù)據(jù)的添加,那我該怎么做呢?
觀看數(shù)據(jù)庫可以知道,我們有商品表,然而商品對于商品圖片,商品參數(shù)和商品類型都是一對多或者多對一的關(guān)系,但是我想要我們的前端可以直接提交一個表單就能完成多個表中數(shù)據(jù)的添加,多表操作是必然的了

準備工作
因為此操作之前我已經(jīng)使用了mybatis-plus-join的多表查詢操作,所以我已經(jīng)生成了一個DTO的實體類
@Data
public class BrandDTO {
private Integer id;
//類型表
private String type;
//商品表
private String brandName;
private String companyName;
private String description;
//圖片鏈接表
private List<Img> imgUrlList;
//參數(shù)表
private List<Parameter> parameterList;
}在這個類中你會疑惑 : 為什么我不直接封裝一個Brand實體對象進來呢?
因為我之前使用了這個類來進行連表查詢,把各個實體類的參數(shù)分開放進來(竟然沒有重名的 hhhh),并且這個類需要做展示,所以我把Brand類的屬性原原本本加入進來,而tpye對應(yīng)Brand應(yīng)該是多(type)對一(Brand),所以在這里我也只封裝了一個,但由于Brand對于Img和Parameter是一對多的關(guān)系,所以我把他們封裝成為了一個list<對象>,就這樣我們得到了一個類似于中間類的東西
在測試環(huán)境下模擬
我們不妨想一下,有了這樣的一個類,我們只需要把參數(shù)分開添加到各個表中,我們需要想象我們得到一個封裝有數(shù)據(jù)的BrandDTO的對象,然后拆解開來使用各自的mapper接口的方法來進行插入表格行為
(首先接口要繼承對應(yīng)的BaseMapper<>,才可以進行快速操作,當然如果你在接口有對應(yīng)的添加方法也可以,但是既然我們用了mybatis-plus,為什么還要拐回去自己寫添加方法?)
于是,經(jīng)過幾次反復實驗,我得到了如下測試方法:
@Test
public void addBrand(){
Brand brand = new Brand();
Type type = new Type();
Img img = new Img();
Parameter parameter = new Parameter();
BrandDTO brandDTO = new BrandDTO();
brandDTO.setBrandName("測試商品3");
brandDTO.setCompanyName("廠家3");
brandDTO.setDescription("這是第二個個測試");
brandDTO.setType("第Ⅱ型");
List<Img> imgs =new ArrayList<>();
imgs.add(new Img("w/daw/daw/daww"));
imgs.add(new Img("xxwdAWd/dawd/wx"));
brandDTO.setImgUrlList(imgs);
List<Parameter> parameters = new ArrayList<>();
parameters.add(new Parameter("110","270*860*270",30,450));
parameters.add(new Parameter("120","170*4350*720",990,5530));
brandDTO.setParameterList(parameters);
List<Img> imgUrlList = brandDTO.getImgUrlList();
List<Parameter> parameterList = brandDTO.getParameterList();
brand.setBrandName(brandDTO.getBrandName());
brand.setCompanyName(brandDTO.getCompanyName());
brand.setDescription(brandDTO.getDescription());
brandMapper.insert(brand);
Integer id = brand.getId();
type.setBType(brandDTO.getType());
type.setBId(id);
typeMapper.insert(type);
for (Parameter parameterl : parameterList) {
parameter.setBModel(parameterl.getBModel());
parameter.setBOutput(parameterl.getBOutput());
parameter.setBSize(parameterl.getBSize());
parameter.setBId(id);
parameterMapper.insert(parameter);
}
for (Img imgl : imgUrlList) {
img.setImgUrl(imgl.getImgUrl());
img.setBrandId(id);
imgMapper.insert(img);
}
System.out.println(id);
}
思維分解一下:
接下來我會對方法體的各個部分進行分解表達
創(chuàng)建出一個帶有參數(shù)的BrandDTO對象
首先我們模擬了一個封裝有各個參數(shù)的BrandDTO對象:
Type type = new Type();
Img img = new Img();
Parameter parameter = new Parameter();
BrandDTO brandDTO = new BrandDTO();
brandDTO.setBrandName("測試商品3");
brandDTO.setCompanyName("廠家3");
brandDTO.setDescription("這是第二個個測試");
brandDTO.setType("第Ⅱ型");
List<Img> imgs =new ArrayList<>();
//此操作能成功是因為我在對應(yīng)的對象中生成了除了id屬性和外鍵屬性的有參構(gòu)造
imgs.add(new Img("w/daw/daw/daww"));
imgs.add(new Img("xxwdAWd/dawd/wx"));
brandDTO.setImgUrlList(imgs);
List<Parameter> parameters = new ArrayList<>();
//此操作能成功是因為我在對應(yīng)的對象中生成了除了id屬性和外鍵屬性的有參構(gòu)造
parameters.add(new Parameter("110","270*860*270",30,450));
parameters.add(new Parameter("120","170*4350*720",990,5530));
brandDTO.setParameterList(parameters);這一部分主要是對參數(shù)的封裝,是前端的工作,讓我們后臺服務(wù)器收到一個帶有參數(shù)的BrandDTO對象
模擬對后臺傳遞參數(shù)
取出各個表中所對應(yīng)的各個參數(shù)
//取出ImgUrlList和ParameterList()
List<Img> imgUrlList = brandDTO.getImgUrlList();
List<Parameter> parameterList = brandDTO.getParameterList();
//單獨封裝brand對象
brand.setBrandName(brandDTO.getBrandName());
brand.setCompanyName(brandDTO.getCompanyName());
brand.setDescription(brandDTO.getDescription());
//調(diào)用對應(yīng)Mapper接口的insert方法(或者你自己寫的添加方法)
brandMapper.insert(brand);
//使用主鍵返回(要確保mybatis中設(shè)置了主鍵自增并且在各個實體類中聲明了主鍵屬性)
Integer id = brand.getId();經(jīng)過以上以上操作我們向Brand表中添加了一行信息,并且將主鍵返回了過來.
于是我們的其他表知曉了對應(yīng)的商品的id,就可以利用此id來進行表中外鍵id的定義:
(請注意,在這個測試類中我把所需要用的各個實體類的Mapper接口都注入了,所以我才能調(diào)用insert方法)
//向Type表中添加數(shù)據(jù)并指定外鍵(BrandID)的id
type.setBType(brandDTO.getType());
type.setBId(id);
typeMapper.insert(type);
//向Paramater表中添加數(shù)據(jù)并指定外鍵(BrandID)的id
for (Parameter parameterl : parameterList) {
parameter.setBModel(parameterl.getBModel());
parameter.setBOutput(parameterl.getBOutput());
parameter.setBSize(parameterl.getBSize());
parameter.setBId(id);
parameterMapper.insert(parameter);
}
//向Img表中添加數(shù)據(jù)并指定外鍵(BrandID)的id
for (Img imgl : imgUrlList) {
img.setImgUrl(imgl.getImgUrl());
img.setBrandId(id);
imgMapper.insert(img);
}采用循環(huán)添加,我們可以將對象中的數(shù)據(jù)逐個添加到各個表中,接下來我們需要通過控制臺來得到我們添加商品對應(yīng)的主鍵id:
System.out.println(id);
在這之后我們運行,我這里得到的數(shù)據(jù)是3,然后我們?nèi)フ{(diào)用通過id查詢商品的方法:
我這里用的是Apifox:

可以看出來我們的信息已經(jīng)插入了表格.返回值部分為null是因為我寫的多表查詢有一些小bug,但是在數(shù)據(jù)庫仍然是有數(shù)據(jù)的,由此可見,這個測試是成功了,接下來只要將代碼CV到對應(yīng)的service,在controller層模擬傳入一個Json對象就可以檢驗是否可行!
到此這篇關(guān)于在Springboot+Mybatis-plus不使用SQL語句進行多表添加操作的文章就介紹到這了,更多相關(guān)Springboot Mybatis-plus多表添加內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解如何在Spring Boot啟動后執(zhí)行指定代碼
這篇文章主要介紹了在Spring Boot啟動后執(zhí)行指定代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06
IDEA 連接數(shù)據(jù)庫的實現(xiàn)方法
這篇文章主要介紹了IDEA 連接數(shù)據(jù)庫的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-10-10
Spring動態(tài)注冊多數(shù)據(jù)源的實現(xiàn)方法
這篇文章主要介紹了Spring動態(tài)注冊多數(shù)據(jù)源的實現(xiàn)方法,小編覺的挺不錯的,現(xiàn)分享到腳本之家平臺,需要的朋友可以參考下2018-01-01
java連接池Druid獲取連接getConnection示例詳解
這篇文章主要為大家介紹了java連接池Druid獲取連接getConnection示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-09-09
springboot-2.3.x最新版源碼閱讀環(huán)境搭建(基于gradle構(gòu)建)
這篇文章主要介紹了springboot-2.3.x最新版源碼閱讀環(huán)境搭建(基于gradle構(gòu)建),需要的朋友可以參考下2020-08-08

