您好,登錄后才能下訂單哦!
今天給大家介紹一下springboot怎樣使用mybatis一對多的關(guān)聯(lián)查詢問題。文章的內(nèi)容小編覺得不錯,現(xiàn)在給大家分享一下,覺得有需要的朋友可以了解一下,希望對大家有所幫助,下面跟著小編的思路一起來閱讀吧。
由于剛開始寫java不久,對sql語句的熟悉度還是不夠熟練,雖然現(xiàn)在使用的mybatisPlus比較多,但我始終覺得sql不能忘也不能不用,剛好最近有個需求需要做到關(guān)聯(lián)的查詢,時間也算充足,所以用sql來寫,于是踩了很久坑,終于跳出來了,小小記錄一下。
# 我這里是一對多查詢,一張主表兩張副表,最后還要有一張VO表(就是做關(guān)聯(lián)映射用的),主表和副表的實體我就不貼了,以下是VO實體
這是我的controller
@RequestMapping(value = "/queryChartAll", method = RequestMethod.GET) public R<?> queryChartAll(){ List<StatementEnteringVO> statementEnteringVO = statementEnteringMapper.queruMapperPage(); if(statementEnteringVO != null){ return R.data(statementEnteringVO); }else{ return R.fail(ResultCode.ERROR); } }
mapper
public interface StatementEnteringMapper extends BaseMapper<StatementEntering> { List<StatementEnteringVO> queruMapperPage(); }
mapper.xml(注意了,最難受也是最坑的)
為了展示方便,我貼少點的字段,不然老長的代碼看球不明白,再注一下:一對多使用collection
,一對一使用association
,文章主講一對多的。所以也不對association
做解釋了,感興趣的朋友可以自己去了解一下,用法是一樣的。
<?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.ld.ldstat.mapper.StatementEnteringMapper"> <resultMap id="queryChartAllTwo" type="com.ld.ldstat.vo.StatementEnteringVO"> <id property="id" jdbcType="BIGINT" column="sid"></id> <result property="theDate" jdbcType="DATE" column="the_date"></result> <result property="deptName" jdbcType="VARCHAR" column="dept_name"></result> <result property="deptId" jdbcType="BIGINT" column="dept_id"></result> <!--這里是對應(yīng)vo實體的表1實體--> <collection property="cashCar" javaType="java.util.List" ofType="com.ld.ldstat.entity.CashCar"> <id property="id" jdbcType="BIGINT" column="cid"></id> <result property="parentId" jdbcType="BIGINT" column="c_parent_id"/> <result property="projectName" jdbcType="VARCHAR" column="c_project_name"/> <result property="ninetyWithin" jdbcType="INTEGER" column="ninety_within"/> <result property="ninetyExcept" jdbcType="INTEGER" column="ninety_except"/> <result property="cashCar" jdbcType="INTEGER" column="cash_car"/> </collection> <!--這里是對應(yīng)vo實體的表2實體--> <collection property="subtotalAll" ofType="com.ld.ldstat.entity.SubtotalAll"> <id property="id" jdbcType="BIGINT" column="aid"></id> <result property="parentId" jdbcType="BIGINT" column="a_parent_id"/> <result property="projectName" jdbcType="VARCHAR" column="a_project_name"/> <result property="subtotalType" jdbcType="INTEGER" column="subtotal_type"/> <result property="task" jdbcType="INTEGER" column="task"/> <result property="theDay" jdbcType="INTEGER" column="the_day"/> </collection> </resultMap> <select id="queruMapperPage" resultMap="queryChartAllTwo"> SELECT se.id sid,se.the_date,se.dept_name, ca.id cid,ca.project_name c_project_name,ca.parent_id c_parent_id,ca.ninety_Within,ca.ninety_except,ca.cash_car, sa.id aid,sa.project_name a_project_name,sa.parent_id a_parent_id,sa.task,sa.the_day FROM statement_entering se LEFT JOIN cash_car ca on se.id = ca.parent_id LEFT JOIN subtotal_all sa on se.id = sa.parent_id <!-- 條件可根據(jù)自己的需求增加啦--> </select> </mapper>
以下是需要注意的點(我就是在這里踩了好久的坑)
以下sql用的是左連接語句LEFT JOIN
,具體的sql語句我也不解釋了,因為要了解的太多了,【尷尬】
這里的這些字段必須要使用別名,為啥?因為幾張表的字段相同所以會出現(xiàn)覆蓋的問題,比如我副表1和副表2同時存在一個相同字段project_name
,如果不給其聲明別名,副表2該字段的數(shù)據(jù)會被副表1的該字段覆蓋掉,原理我也解釋不清楚,哈哈?。?/p>
對應(yīng)的映射字段要使用別名,上圖
線劃的丑了些,將就吧!
看下最終獲取到的數(shù)據(jù)結(jié)構(gòu)
完美,理想中的效果。。。
至于最終效果圖為啥這么多null
,不用懷疑,這些是我沒有寫對應(yīng)的映射
springboot一種全新的編程規(guī)范,其設(shè)計目的是用來簡化新Spring應(yīng)用的初始搭建以及開發(fā)過程,SpringBoot也是一個服務(wù)于框架的框架,服務(wù)范圍是簡化配置文件。
以上就是springboot怎樣使用mybatis一對多的關(guān)聯(lián)查詢問題的全部內(nèi)容了,更多與springboot怎樣使用mybatis一對多的關(guān)聯(lián)查詢問題相關(guān)的內(nèi)容可以搜索億速云之前的文章或者瀏覽下面的文章進(jìn)行學(xué)習(xí)哈!相信小編會給大家增添更多知識,希望大家能夠支持一下億速云!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。