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="dao.ICountryDao"> <!-- resultMap 能解决字段和属性不一样的问题 --> <!-- 以后用得比较多 ,是因为可以使用延迟加载--> <!-- 嵌套查询 --> <select resultType="Minister"> select mid,mname from minister where countryId=#{ooo} </select> <resultMap type="Country"> <id column="cid" property="cid"/> <result column="cname" property="cname"/> <!-- country中有一个成员变量是ministers,它的泛型是Minister --> <collection property="ministers" ofType="Minister" select="selectMinisterByCountry" column="cid"> </collection> </resultMap> <select resultMap="countryMapper"> select cid,cname from country where cid=#{cid} </select> </mapper>与之对应的sql接口:
public interface ICountryDao { Country selectCountryById(int cid); }使用到的工具类:
public class MyBatisUtils { private static SqlSessionFactory sqlSessionFactory; public static SqlSession getSqlSession() { InputStream is; try { is = Resources.getResourceAsStream("mybatis.xml"); if (sqlSessionFactory == null) { sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); } return sqlSessionFactory.openSession(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } } 直接加载查询关于懒加载的配置,我们只需要在mybatis.xml文件里面使用<settings></settings>就可以了,懒加载有一个总开关,lazyloadingEnabled,只要置为false就可以将延迟加载关掉,那就是直接加载查询了。配置在<properties></properties>与<typeAliases></typeAliases>之间。
<properties resource="jdbc_mysql.properties"> </properties> <settings> <setting value="flase"/> </settings> <!-- 别名,对数据对象操作全名太长,需要使用别名 --> <typeAliases> <!--<typeAlias type="bean.Student" alias="Student"/>--> <!--直接使用类名即可,对于整个包的路径配置(别名),简单快捷 --> <package/> </typeAliases>当单元测试是直接査country对象的时候:
@Test public void TestselectCountryById(){ Country country=dao.selectCountryById(1); }结果是,我们可以看到两条sql,除了查询country之外,连同minister关联对象也一起查询了,这就是直接加载,简单粗暴,不管是否使用,都会先将关联查询加载:
[service] 2018-07-17 09:59:00,796 - dao.ICountryDao.selectCountryById -491 [main] DEBUG dao.ICountryDao.selectCountryById - ==> Preparing: select cid,cname from country where cid=? [service] 2018-07-17 09:59:00,823 - dao.ICountryDao.selectCountryById -518 [main] DEBUG dao.ICountryDao.selectCountryById - ==> Parameters: 1(Integer) [service] 2018-07-17 09:59:00,838 - dao.ICountryDao.selectMinisterByCountry -533 [main] DEBUG dao.ICountryDao.selectMinisterByCountry - ====> Preparing: select mid,mname from minister where countryId=? [service] 2018-07-17 09:59:00,838 - dao.ICountryDao.selectMinisterByCountry -533 [main] DEBUG dao.ICountryDao.selectMinisterByCountry - ====> Parameters: 1(Integer) [service] 2018-07-17 09:59:00,849 - dao.ICountryDao.selectMinisterByCountry -544 [main] DEBUG dao.ICountryDao.selectMinisterByCountry - <==== Total: 2 [service] 2018-07-17 09:59:00,850 - dao.ICountryDao.selectCountryById -545 [main] DEBUG dao.ICountryDao.selectCountryById - <== Total: 1 侵入式延迟加载需要将延迟加载开关开启(true),同时也需要将侵入式加载开关开启(true)
<settings> <setting value="true"/> <setting value="true"/> </settings>1.当我们只查询country的时候,只会执行country的查询,不会执行关联查询minister:
@Test public void TestselectCountryById(){ Country country=dao.selectCountryById(1); }结果如下,只有一条sql:
[service] 2018-07-17 14:30:55,471 - dao.ICountryDao.selectCountryById -902 [main] DEBUG dao.ICountryDao.selectCountryById - ==> Preparing: select cid,cname from country where cid=? [service] 2018-07-17 14:30:55,494 - dao.ICountryDao.selectCountryById -925 [main] DEBUG dao.ICountryDao.selectCountryById - ==> Parameters: 1(Integer) [service] 2018-07-17 14:30:55,590 - dao.ICountryDao.selectCountryById -1021 [main] DEBUG dao.ICountryDao.selectCountryById - <== Total: 1