mybatis延遲加載一對多
阿新 • • 發佈:2017-08-11
property doctype ibatis 根據 lda .cn mybatis 查詢語句 測試類
1、實體類
package cn.bdqn.bean; import java.util.Set; /** *國家的實體類 */ public class Country { private Integer cId; // 國家的編號 private String cName; // 國家的名稱 private Set<Provincial> provincials; 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(); } public Set<Provincial> getProvincials() { return provincials; } public void setProvincials(Set<Provincial> provincials) { this.provincials = provincials; } @Override public String toString() { return "Country [cId=" + cId + ", cName=" + cName; } }
2、dao層
package cn.bdqn.dao; import cn.bdqn.bean.Country; public interface CountryDao { /** * 根據國家的id查詢出國家的信息 以及國家下面的省會信息 */ Country selectCountryById(Integer cId); }
3、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.CountryDao"> <select id="selectProvincialByCountryId" resultType="Provincial"> select pid,pname from provincial where countryid=#{cid} </select> <resultMap type="Country" id="countryMap"> <id property="cId" column="cid"/> <result property="cName" column="cname"/> <!--設置關聯的集合屬性 select:需要關聯的查詢語句 column: select關聯語句中需要的參數 --> <collection property="provincials" ofType="Provincial" select="selectProvincialByCountryId" column="cid"/> </resultMap> <!-- 多表的查詢 經常使用 可以使用延遲加載策略 --> <select id="selectCountryById" resultMap="countryMap"> select cid,cname from country where cid=#{cid} </select> </mapper>
4、測試類
package cn.bdqn.test; import org.apache.ibatis.session.SqlSession; import org.apache.log4j.Logger; import org.junit.After; import org.junit.Before; import org.junit.Test; import cn.bdqn.bean.Country; import cn.bdqn.bean.Provincial; import cn.bdqn.dao.CountryDao; import cn.bdqn.dao.ProvincialDao; import cn.bdqn.util.MybatisUtil; public class Test1 { private Logger logger = Logger.getLogger(Test1.class); SqlSession session; ProvincialDao dao; CountryDao dao2; @Before public void before() { // 因為需要關閉session 需要把session提取出去 session = MybatisUtil.getSqlSession(); dao = session.getMapper(ProvincialDao.class); dao2 = session.getMapper(CountryDao.class); } @After public void after() { if (session != null) { session.close(); } } @Test public void test1() { Provincial provincial = dao.selectProvincialById(1); /*logger.debug("provincialId=1======>provincial:" + provincial);*/ /*logger.debug("provincialId=1======>country:" + provincial.getCountry());*/ } @Test public void test2() { Country country = dao2.selectCountryById(1); /*Set<Provincial> provincials = country.getProvincials(); for (Provincial provincial : provincials) { logger.debug(provincial); }*/ /*logger.debug("provincialId=1======>provincial:" + provincial);*/ /*logger.debug("provincialId=1======>country:" + provincial.getCountry());*/ } }
mybatis延遲加載一對多