延时加载
一对一,一对多,多对一,多对多
一对一:身份证 - 身份证信息
一对多:班级 - 学生
如果不采用延时加载(立即加载),查询时会将一和多都查询,即班级信息,班级中所有学生的信息
如果想只查询班级信息,而学生信息先不查询 而是在需要的时候再去查询 即为延时加载
使用需要在conf.xml配置
<!--开启延迟加载-->
<setting name="lazyLoadingEnabled" value="true"/>
<!--关闭立即加载-->
<setting name="aggressiveLazyLoading" value="false"/>
mapper.xml
对于需要延迟加载的属性,不再resultMap中写映射,而是在外部写查询的sql,在resultMap中配置select和column属性
采用延时加载:
在查询班级时,并不立即加载学生信息
colume 关联的外键
通过select指定外部sql,在需要时加载学生信息
<!--一对一延迟加载-->
<select id="lazyloadone" resultMap="cardMap1" parameterType="int">
SELECT * FROM idcard WHERE id = #{id}
</select>
<resultMap id="cardMap1" type="idcard">
<id property="id" column="id"></id>
<result property="name" column="name"></result>
<result property="cardid" column="cardid"></result>
<association property="cardinfo" javaType="CardInfo" select="lazyloadSql1" column="cardid">
</association>
</resultMap>
<!--延时加载 一对多-->
<select id="lazyloadsql3" resultMap="classMap1" parameterType="int">
SELECT * FROM class WHERE classid = #{id}
</select>
<!--类与表的映射关系-->
<resultMap id="classMap1" type="StudentClass">
<id property="classid" column="classid"></id>
<result property="classname" column="classname"></result>
<!--
采用延时加载:在查询学生时,并不立即加载学生信息
colume 关联的外键
通过select指定外部sql,在需要时加载学生信息
-->
<collection property="students" ofType="Students" select="lazyloadSql2" column="classid">
<!-- 立即加载配置
<id property="id" column="id"></id>
<result property="name" column="name"></result>
<result property="classid" column="classid"></result>
-->
</collection>
</resultMap>
<!--懒加载sql 一对多-->
<select id="lazyloadSql2" parameterType="int" resultType="Students">
SELECT * FROM students WHERE classid = #{id}
</select>
<!--懒加载sql 一对一-->
<!--禁用二级缓存
<!--清理缓存-->
<select id="lazyloadSql1" parameterType="int" resultType="CardInfo" useCache="false" flushCache="true">
SELECT * FROM cardinfo WHERE cardid = #{id}
</select>
• MyBatis Generator:
• 简称MBG,是一个专门为MyBatis框架使用者定 制的代码生成器,可以快速的根据表生成对应的 映射文件,接口,以及bean类。支持基本的增删 改查,以及QBC风格的条件查询。但是表连接、 存储过程等这些复杂sql的定义需要我们手工编写
• 官方文档地址
• 官方工程地址
• 使用步骤:
导入jarbao:mybatis-generator-core.jar
– 1)编写MBG的配置文件(重要几处配置) generator.xml
1)jdbcConnection配置数据库连接信息
2)javaModelGenerator配置javaBean的生成策略
3)sqlMapGenerator 配置sql映射文件生成策略
4)javaClientGenerator配置Mapper接口的生成策略
5)table 配置要逆向解析的数据表 tableName:表名 domainObjectName:对应的javaBean名
– 2)运行代码生成器生成代码
• 注意: Context标签 targetRuntime=“MyBatis3“可以生成带条件的增删改查
targetRuntime=“MyBatis3Simple“可以生成基本的增删改查
如果再次生成,建议将之前生成的数据删除,避免xml向后追加内容出现的问 题。
generator.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<!--
suppressAllComments属性值:
true:自动生成实体类、SQL映射文件时没有注释
true:自动生成实体类、SQL映射文件,并附有注释
-->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- 数据库连接信息 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/person"
userId="root" password="root">
</jdbcConnection>
<!--
forceBigDecimals属性值:
true:把数据表中的DECIMAL和NUMERIC类型,
解析为JAVA代码中的java.math.BigDecimal类型
false(默认):把数据表中的DECIMAL和NUMERIC类型,
解析为解析为JAVA代码中的Integer类型
-->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!--
targetProject属性值:实体类的生成位置
targetPackage属性值:实体类所在包的路径
-->
<javaModelGenerator targetPackage="entity"
targetProject=".\src">
<!-- trimStrings属性值:
true:对数据库的查询结果进行trim操作(去除前后空格)
false(默认):不进行trim操作
-->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!--
targetProject属性值:SQL映射文件的生成位置
targetPackage属性值:SQL映射文件所在包的路径
-->
<sqlMapGenerator targetPackage="mapper"
targetProject=".\src">
</sqlMapGenerator>
<!-- 生成动态代理的接口 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="mapper" targetProject=".\src">
</javaClientGenerator>
<!-- 指定数据库表 -->
<table tableName="person"> </table>
<table tableName="idcard"> </table>
<table tableName="cardinfo"> </table>
</context>
</generatorConfiguration>
java
@Test
public void test() throws Exception {
//配置文件
File file = new File("src/generator.xml");
//集合保存异常信息
ArrayList<String> warning = new ArrayList<>();
//
ConfigurationParser configurationParser = new ConfigurationParser(warning);
Configuration config = configurationParser.parseConfiguration(file);
DefaultShellCallback callback = new DefaultShellCallback(true);
//逆向工程的核心类
MyBatisGenerator generator = new MyBatisGenerator(config, callback, warning);
generator.generate(null);
}