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());*/ } }