SpringBoot整合Mybatis-Plus實現(xiàn)關(guān)聯(lián)查詢
一、搭建環(huán)境
1.創(chuàng)建Maven普通項目
2.添加依賴坐標(biāo)
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.2.RELEASE</version> </parent> <dependencies> <!--單元測試--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <!-- 數(shù)據(jù)庫驅(qū)動 --> <!--mysql驅(qū)動--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <!-- <version>8.0.19</version>--> </dependency> <!-- lombok 簡化set get toString --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <!-- mybatis-plus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.3.1</version> </dependency> </dependencies>
3.導(dǎo)入SpringBoot的相關(guān)插件
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin>
4.創(chuàng)建mapper包、pojo包和啟動類Starter
mapper包:
在該包下定接口繼承BaseMapper接口
pojo包:
在該包下定義實體類:客戶類、商品類、訂單類和訂單詳情類
Starter類:
用于SpringBoot項目啟動運行
import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication //掃描mapper接口所在的包 @MapperScan("com.hs.mapper") public class Starter { public static void main(String[] args) { SpringApplication.run(Starter.class); } }
5.在resources文件下配置yml文件(數(shù)據(jù)庫配置和日志配置)
#數(shù)據(jù)庫配置 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/ssm2?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=GMT%2B8 username: root password: 123456 #添加日志配置項 mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
6.在resources文件下創(chuàng)建mapper文件夾,在mapper下創(chuàng)建XML文件自定義數(shù)據(jù)庫操作功能實現(xiàn)
<?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" >
二、實現(xiàn)訂單表和用戶表關(guān)聯(lián)查詢(一對一)
需求:關(guān)聯(lián)查詢其相關(guān)用戶信息
訂單表->用戶表:一個訂單只由一個用戶創(chuàng)建,一對一關(guān)系
做法:1.在Orders類中加入Users屬性,Users屬性用于存儲關(guān)聯(lián)查詢的用戶信息。
import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor public class Orders { private Integer id; private String orderNumber; private Double totalPrice; private String status; private Integer userId; /* *多表查詢:一對一關(guān)系 * 訂單表關(guān)聯(lián)查詢用戶表 * */ private Users users; }
2.在OrderMapper接口中,自定義功能實現(xiàn)
@Mapper public interface OrderMapper extends BaseMapper<Orders> { public List<Orders> selectUserResultMap(); }
3.在resources文件下mapper文件夾里,書寫映射下XML文件UserMapper
resultMap:需要單獨定義resultMap,使用 resultMap可以完成將關(guān)聯(lián)查詢映射到實體類的屬性中。
association標(biāo)簽: 一對一關(guān)系映射描述。
- property: 關(guān)系屬性名稱。
- javaType: 關(guān)系屬性類型。
<resultMap id="selectUserResultMap" type="com.hs.pojo.Orders"> <id column="id" property="id"/> <id column="order_number" property="orderNumber"/> <id column="total_price" property="totalPrice"/> <id column="status" property="status"/> <association property="users" javaType="com.hs.pojo.Users"> <id column="user_id" property="id"/> <id column="username" property="username"/> <id column="password" property="password"/> <id column="realname" property="realname"/> </association> </resultMap> <select id="selectUserResultMap" resultType="com.hs.pojo.Orders"> select o.*,u.username,u.password,u.realname from orders o,users u where o.user_id=u.id </select>
4.在測試類中進(jìn)行測試
@RunWith(SpringRunner.class) @SpringBootTest(classes=Starter.class) public class Test1 { @Resource private OrderMapper orderMapper; //多表查詢:一對一關(guān)系 //訂單表關(guān)聯(lián)查詢用戶表 @Test public void selectOneByOne(){ List<Orders> orders = orderMapper.selectUserResultMap(); orders.forEach(System.out::println); } }
三、實現(xiàn)訂單表和訂單詳情表關(guān)聯(lián)查詢(一對多)
需求:
- 關(guān)聯(lián)查詢其相關(guān)用戶信息。
- 關(guān)聯(lián)查詢其相關(guān)訂單詳情信息。
訂單表->訂單詳情表:一對多
具體做法同上。
1.在Orders類中加入屬性ordersDetailList
/* * 一對多關(guān)系屬性:一個訂單包含多個訂單詳情 * */ private List<OrderDetails> ordersDetailList;
2.在OrderMapper接口中,自定義功能實現(xiàn)
/* * 一對多:查詢訂單信息, * 一個訂單對應(yīng)多條詳情信息,則是一對多 * */ public List<Orders> selectOrdersAndDetail();
3.在resources文件下mapper文件夾里,書寫映射下XML文件UserMapper
在Order類中加入ordersDetailList屬性,details屬性用于存儲關(guān)聯(lián)查詢的訂單詳情。
collection標(biāo)簽: 一對多關(guān)系映射描述。
- property: 關(guān)系屬性名稱。
- ofType: 關(guān)系屬性是一個List集合,集合中存放的元素類型。
<!-- collection:一對多 property:訂單實體類中的屬性 ofType:集合里面存儲的類型 --> <collection property="orderDetailList" ofType="com.hs.pojo.OrderDetails"> <id column="detail_id" property="id"/> <id column="amount" property="amount"/> <id column="orders_id" property="ordersId"/> <id column="goods_id" property="goodsId"/> </collection> </resultMap> <select id="selectOrdersAndDetail" resultMap="detailResultMap"> select o.*,u.username,u.password,u.realname,d.id detail_id,d.amount,d.orders_id,d.goods_id from orders o,users u,orders_detail d where o.user_id=u.id and o.id=d.orders_id </select>
四、實現(xiàn)訂單表和商品表關(guān)聯(lián)查詢(多對多)
中間表:訂單詳情表
需求:
- 關(guān)聯(lián)查詢其相關(guān)用戶信息。
- 關(guān)聯(lián)查詢其相關(guān)訂單詳情信息。
- 關(guān)聯(lián)查詢訂單詳情中的商品信息。
訂單表->訂單詳情表:一對多 訂單詳情表->訂單表:一對一
訂單詳情表->商品表:一對一 商品表->訂單詳情表:一對多
訂單表->商品表:一對多 商品表->訂單表:多對一
做法:
1.在OrderDetails類中加入屬性goods
2.在OrderMapper接口中,自定義功能實現(xiàn)
3.在resources文件下mapper文件夾里,書寫映射下XML文件UserMappe
<resultMap id="goodsResultMap" type="com.hs.pojo.Orders"> <id column="id" property="id"/> <id column="order_number" property="orderNumber"/> <id column="total_price" property="totalPrice"/> <id column="status" property="status"/> <!-- collection:一對多映射 property:訂單實體類中的屬性 ofType:集合里面存儲的類型 --> <collection property="orderDetailList" ofType="com.hs.pojo.OrderDetails"> <id column="detail_id" property="id"/> <id column="amount" property="amount"/> <id column="orders_id" property="ordersId"/> <id column="goods_id" property="goodsId"/> <association property="goods" javaType="com.hs.pojo.Goods"> <id column="goods_id" property="id"/> <id column="goods_name" property="goodsName"/> <id column="description" property="description"/> <id column="price" property="price"/> </association> </collection> </resultMap> <select id="selectOrdersAndGoods" resultMap="goodsResultMap"> select o.*,d.goods_id,g.goods_name,g.description,g.price from orders o,orders_detail d,goods g where o.id = d.orders_id and d.goods_id = g.id </select>
以上就是SpringBoot整合Mybatis-Plus實現(xiàn)關(guān)聯(lián)查詢的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot Mybatis-Plus關(guān)聯(lián)查詢的資料請關(guān)注腳本之家其它相關(guān)文章!
- SpringBoot整合Mybatis-plus實現(xiàn)多級評論功能
- SpringBoot3整合mybatis-plus的實現(xiàn)
- Springboot3整合Mybatis-plus3.5.3報錯問題解決
- SpringBoot整合mybatis-plus實現(xiàn)分頁查詢功能
- springboot3.2整合mybatis-plus詳細(xì)代碼示例
- SpringBoot3和mybatis-plus整合出現(xiàn)的問題解決辦法
- SpringBoot3.2.2整合MyBatis-Plus3.5.5依賴不兼容的問題解決
- 全網(wǎng)最新springboot整合mybatis-plus的過程
- SpringBoot3.3.X整合Mybatis-Plus的實現(xiàn)示例
相關(guān)文章
解決報java.lang.AssertionError錯誤的問題
這篇文章主要介紹了解決報java.lang.AssertionError錯誤的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05淺談SpringCloud feign的http請求組件優(yōu)化方案
這篇文章主要介紹了淺談SpringCloud feign的http請求組件優(yōu)化方案,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02SpringBoot手動開啟事務(wù):DataSourceTransactionManager問題
這篇文章主要介紹了SpringBoot手動開啟事務(wù):DataSourceTransactionManager問題,具有很好的價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07