mybatis实现延迟加载多对一
1、数据库表
CREATE TABLE `country` ( `cid` int(4) NOT NULL AUTO_INCREMENT COMMENT '国家id', `cname` varchar(20) COLLATE utf8_unicode_ci NOT NULL, PRIMARY KEY (`cid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; CREATE TABLE `provincial` ( `pid` int(4) NOT NULL AUTO_INCREMENT, `pname` varchar(20) COLLATE utf8_unicode_ci NOT NULL, `countryid` int(4) NOT NULL, PRIMARY KEY (`pid`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
2、实体类
package cn.bdqn.bean; /** *国家的实体类 */ 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; } }
package cn.bdqn.bean; /** * *省会对应的实体类 */ 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 + "]"; }*/ }
3、mybatis.xml配置文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 通过properties文件配置连接数据库的四要素 --> <properties resource="jdbc.properties" /> <settings> <!-- 打开延迟加载的开关 --> <setting name="lazyLoadingEnabled" value="true" /> <!-- 将积极加载改为消息加载即按需加载 --> <setting name="aggressiveLazyLoading" value="false"/> </settings> <!-- 设置别名 两种方式 --> <typeAliases> <!-- 01.这种方式别名可以随意取 但是如果有多个 类 就需要配置多个typeAlias <typeAlias type="cn.bdqn.bean.Student" alias="student"/> --> <package name="cn.bdqn.bean"/><!-- 02.这种方式要求别名必须使用简写的类名 --> </typeAliases> <!-- 配置运行环境 可以有多个 environment --> <environments default="mysql"><!-- 默认采用的环境 --> <environment id="mysql"> <!-- 环境的名称 --> <!--配置事务管理 采用jdbc默认的事务管理 之后整合的时候 肯定是交给了 spring来处理了 --> <transactionManager type="JDBC" /> <!-- 数据源采用的是连接池技术 POOLED:mybatis自带的数据源,JNDI:基于tomcat的数据源 --> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </dataSource> </environment> </environments> <!-- 加载映射文件信息 --> <mappers> <mapper resource="cn/bdqn/dao/StduentMapper.xml" /> <!-- <mapper resource="cn/bdqn/dao/CountryMapper.xml" /> --> <mapper resource="cn/bdqn/dao/ProvincialMapper.xml" /> </mappers> </configuration>
4、Dao层接口
package cn.bdqn.dao; import cn.bdqn.bean.Provincial; public interface ProvincialDao { /** * 根据省会的id查询出省会和对应国家的信息 */ Provincial selectProvincialById(Integer pId); }
5、Mapper.xml
<?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 = #{countryid} </select> <resultMap type="provincial" id="provincialMap"> <id column="pid" property="pId"/> <result column="pname" property="pName"/> <association property="country" javaType="Country" select="selectCountryByProvincialId" column="countryid" > </association> </resultMap> <select id="selectProvincialById" resultMap="provincialMap"> select pid,pname,countryid from provincial where pid = #{pid} </select> </mapper>
6、测试类
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.Provincial; import cn.bdqn.dao.ProvincialDao; import cn.bdqn.util.MybatisUtil; public class Test1 { private Logger logger = Logger.getLogger(Test1.class); SqlSession session; ProvincialDao dao; @Before public void before() { // 因为需要关闭session 需要把session提取出去 session = MybatisUtil.getSqlSession(); dao = session.getMapper(ProvincialDao.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());*/ } }