MyBatis lazy loading (lazy loading)

MyBatis lazy loading (lazy loading)

The so-called lazy loading is a relatively immediate loaded statement. Lazy loading: only to use the data (the object), will query the data from the database.
 benefits: avoid frequent access to the database, reducing the pressure on the database and save resources.
 Cons: Because only in the use of the data, will be sent to query the database SQL, if there are large quantities of data need to query, because the query itself will consume time, causing the user to wait too long to experience decreased.

To demonstrate through code

All information by querying the inquiry staff to demonstrate lazy loading
to create an entity class employees and departments, and the establishment of many to one relationship

Employees mapping file EmployyeeMapper.xml

In the main configuration file

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<!--namespace:命名空间,用于指定当前mapper文件中操作的SQL属于哪一个接口类下面的方法-->
<mapper namespace="com.hp.mybatis.mapper.EmployeeMapper">

    <resultMap id="base_map" type="employee">
        <id property="id" column="id"></id>
        <result property="name" column="name"></result>
        
        <!--建立和部门关系-->
        <association property="dept" javaType="dept" column="dept_id"
                     select="com.hp.mybatis.mapper.DeptMapper.findById"></association>
    </resultMap>
    <!--根据id查询员工,级联查询部门
        不使用左外链接,因为左外链接不会出现延迟加载情况
    -->
    <select id="findById" parameterType="int" resultMap="base_map">
        select id,dept_id,name from employee where id = #{emId}
    </select>

</mapper>

Sector mapping file DeptMapper.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<!--namespace:命名空间,用于指定当前mapper文件中操作的SQL属于哪一个接口类下面的方法-->
<mapper namespace="com.hp.mybatis.mapper.DeptMapper">

    <select id="findById" parameterType="int" resultType="dept">
        select * from dept where id = #{id}
    </select>
</mapper>

Process: In the test class: Call findById interface methods EmployeeMapper, execute select query to find EmployeeMapper.xml by sqlMapConfig.xml file, but the result set containing the information required resultMap department to resolve the result set, parsing association references employee DeptMapper.xml department id to select query in the sector information. Connected to the same results as left
(when we do not need to query the information department staff, that there has been delay in loading, lazy loading is disabled by default, will check out all the information that the employee does not include sector information).

sqlMapConfig.xml Configuration

<?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="com/db.properties"></properties>//数据库配置
    <!--配置开启延迟加载-->
    <settings>
        <setting name="lazyLoadingEnabled" value="true"/>
        <setting name="lazyLoadTriggerMethods" value="clone"/>
    </settings>
    
    <!--别名配置-->
    <typeAliases>
        <!--指定包名,指定后,别名默认就是包下的类名-->
        <package name="com.hp.mybatis.bean"/>
    </typeAliases>

    <!--配置环境-->
    <environments default="abc">
        <!--环境中定义了,要访问的数据库连接池,事务管理类型-->
        <environment id="abc">
            <transactionManager type="JDBC"></transactionManager>
            <!--数据库连接池-->
            <dataSource type="POOLED">
                <property name="driver" value="${db.driverName}"/>
                <property name="url" value="${db.url}"/>
                <property name="username" value="${db.username}"/>
                <property name="password" value="${db.password}"/>
            </dataSource>
        </environment>
    </environments>

    <mappers>
        <!--当前包需要和mapper接口类所在包同名,并且当前包需要一级一级创建-->
        <package name="com.hp.mybatis.mapper"/>
    </mappers>

</configuration>

The first look may be a bit confused, to see more yellow font text can be simple to understand.

Published 68 original articles · won praise 7 · views 2527

Guess you like

Origin blog.csdn.net/Cui6023056/article/details/104414278