mybatis08--關聯查詢多對一
阿新 • • 發佈:2017-06-29
spa namespace 文件 pan encoding iat ide java integer
根據省會的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(); } @Override public 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(); } @Override public 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,provincial where 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; @Before public void before() { // 因為需要關閉session 需要把session提取出去 session = SessionUtil.getSession(); dao = session.getMapper(ProvincialDao.class); } @After public void after() { if (session != null) { session.close(); } } /** * 根據省會的id查詢出省會和對應國家的信息 */ @Test public 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 provincial where pid=#{xxx} <!-- #{xxx} 用戶傳遞參數的占位符 --> </select> </mapper>
mybatis08--關聯查詢多對一