日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

mybatis08--关联查询多对一

發布時間:2025/6/17 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mybatis08--关联查询多对一 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?根據省會的id查詢出省會和對應國家的信息

01.多表的連接查詢

修改對應的實體類信息

/***國家的實體類*/ public class Country {private Integer cId; //國家的編號private String cName; //國家的名稱public Integer getcId() {return cId;}public void setcId(Integer cId) {this.cId = cId;}public String getcName() {return cName;}public void setcName(String cName) {this.cName = cName;}public Country(Integer cId, String cName) {super();this.cId = cId;this.cName = cName;}public Country() {super();}@Overridepublic String toString() {return "Country [cId=" + cId + ", cName=" + cName ;}}

?

?

/*** *省會對應的實體類*/ public class Provincial {private Integer pId; //省會的編號private String pName; //省會名稱//關聯的國家屬性private Country country;public Country getCountry() {return country;}public void setCountry(Country country) {this.country = country;}public Integer getpId() {return pId;}public void setpId(Integer pId) {this.pId = pId;}public String getpName() {return pName;}public void setpName(String pName) {this.pName = pName;}public Provincial(Integer pId, String pName) {super();this.pId = pId;this.pName = pName;}public Provincial() {super();}@Overridepublic String toString() {return "Provincial [pId=" + pId + ", pName=" + pName + ", country="+ country + "]";}}

?

修改對應的dao和mapper

public interface ProvincialDao {/*** 根據省會的id查詢出省會和對應國家的信息 */Provincial selectProvincialById(Integer pId); }

?

<?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="cn.bdqn.dao.ProvincialDao"><!-- 這里的resultMap和之前使用的不一樣,哪怕屬性和字段一致 也要書寫因為mybatis在底層封裝的時候,是根據我們resultMap中寫的屬性來的 --><resultMap type="Provincial" id="provincialMap"><id property="pId" column="pid"/><result property="pName" column="pname"/><!-- 設置關聯的屬性 --><association property="country" javaType="Country"><id property="cId" column="cid"/><result property="cName" column="cname"/></association></resultMap><!-- 這是單表的關聯查詢 不經常使用 因為 不能使用延遲加載 --><select id="selectProvincialById" resultMap="provincialMap">select cid,cname,pid,pname from country,provincialwhere cid=countryid and pid=#{xxx} <!-- #{xxx} 參數的占位符 --></select> </mapper>

?

mybatis.xml文件管理mapper文件

<!-- 加載映射文件信息 --><mappers><mapper resource="cn/bdqn/dao/ProvincialMapper.xml" /></mappers>

?

測試類代碼

public class ProvincialTest {ProvincialDao dao;SqlSession session;@Beforepublic void before() {// 因為需要關閉session 需要把session提取出去session = SessionUtil.getSession();dao = session.getMapper(ProvincialDao.class);}@Afterpublic void after() {if (session != null) {session.close();}}/*** 根據省會的id查詢出省會和對應國家的信息 */@Testpublic void test1() {Provincial provincial = dao.selectProvincialById(1);System.out.println(provincial);}}

?

?

02.使用單表的單獨查詢

只需要修改mapper文件內容 其他代碼不變

<?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="cn.bdqn.dao.ProvincialDao"><select id="selectCountryByProvincialId" resultType="Country">select cid,cname from country where cid=#{xxx}<!--#{xxx}就是resultMap 中 association節點中的column屬性 --></select><resultMap type="Provincial" id="provincialMap"><id property="pId" column="pid"/><result property="pName" column="pname"/><!-- 設置關聯的屬性 select:關聯的查詢結果 --><association property="country" javaType="Country"select="selectCountryByProvincialId" column="countryid" /></resultMap><!-- 多表的單獨查詢 常用的方式 可以使用延遲加載策略 --><select id="selectProvincialById" resultMap="provincialMap">select pid,pname,countryid from provincialwhere pid=#{xxx} <!-- #{xxx} 用戶傳遞參數的占位符 --></select></mapper>

轉載于:https://www.cnblogs.com/HHR-SUN/p/7201562.html

總結

以上是生活随笔為你收集整理的mybatis08--关联查询多对一的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。