mybatis08--关联查询多对一
生活随笔
收集整理的這篇文章主要介紹了
mybatis08--关联查询多对一
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
?根據(jù)省會的id查詢出省會和對應(yīng)國家的信息
01.多表的連接查詢
修改對應(yīng)的實體類信息
/***國家的實體類*/ 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 ;}}?
?
/*** *省會對應(yīng)的實體類*/ public class Provincial {private Integer pId; //省會的編號private String pName; //省會名稱//關(guān)聯(lián)的國家屬性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 + "]";}}?
修改對應(yīng)的dao和mapper
public interface ProvincialDao {/*** 根據(jù)省會的id查詢出省會和對應(yīng)國家的信息 */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在底層封裝的時候,是根據(jù)我們resultMap中寫的屬性來的 --><resultMap type="Provincial" id="provincialMap"><id property="pId" column="pid"/><result property="pName" column="pname"/><!-- 設(shè)置關(guān)聯(lián)的屬性 --><association property="country" javaType="Country"><id property="cId" column="cid"/><result property="cName" column="cname"/></association></resultMap><!-- 這是單表的關(guān)聯(lián)查詢 不經(jīng)常使用 因為 不能使用延遲加載 --><select id="selectProvincialById" resultMap="provincialMap">select cid,cname,pid,pname from country,provincialwhere cid=countryid and pid=#{xxx} <!-- #{xxx} 參數(shù)的占位符 --></select> </mapper>?
mybatis.xml文件管理mapper文件
<!-- 加載映射文件信息 --><mappers><mapper resource="cn/bdqn/dao/ProvincialMapper.xml" /></mappers>?
測試類代碼
public class ProvincialTest {ProvincialDao dao;SqlSession session;@Beforepublic void before() {// 因為需要關(guān)閉session 需要把session提取出去session = SessionUtil.getSession();dao = session.getMapper(ProvincialDao.class);}@Afterpublic void after() {if (session != null) {session.close();}}/*** 根據(jù)省會的id查詢出省會和對應(yīng)國家的信息 */@Testpublic void test1() {Provincial provincial = dao.selectProvincialById(1);System.out.println(provincial);}}?
?
02.使用單表的單獨查詢
只需要修改mapper文件內(nèi)容 其他代碼不變
<?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節(jié)點中的column屬性 --></select><resultMap type="Provincial" id="provincialMap"><id property="pId" column="pid"/><result property="pName" column="pname"/><!-- 設(shè)置關(guān)聯(lián)的屬性 select:關(guān)聯(lián)的查詢結(jié)果 --><association property="country" javaType="Country"select="selectCountryByProvincialId" column="countryid" /></resultMap><!-- 多表的單獨查詢 常用的方式 可以使用延遲加載策略 --><select id="selectProvincialById" resultMap="provincialMap">select pid,pname,countryid from provincialwhere pid=#{xxx} <!-- #{xxx} 用戶傳遞參數(shù)的占位符 --></select></mapper>轉(zhuǎn)載于:https://www.cnblogs.com/HHR-SUN/p/7201562.html
總結(jié)
以上是生活随笔為你收集整理的mybatis08--关联查询多对一的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android设备局域网中快速搜索之cl
- 下一篇: telnet命令的使用