MyBatis框架

9.3.4 MyBatis配置

  • 全局文件的配置

<?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>

         <!--  Spring 整合 MyBatis 后, MyBatis中配置数据源,事务等一些配置都可以

         迁移到Spring的整合配置中。MyBatis配置文件中只需要配置与MyBatis相关

         的即可。

          -->

          <!-- settings: 包含很多重要的设置项      -->

            <settings>

                 <!-- 映射下划线到驼峰命名 -->

                 <setting name="mapUnderscoreToCamelCase" value="true"/>

                 <!-- 设置Mybatis对null值的默认处理 -->

                 <setting name="jdbcTypeForNull" value="NULL"/>

                 <!-- 开启延迟加载 -->

                 <setting name="lazyLoadingEnabled" value="true"/>

                 <!-- 设置加载的数据是按需还是全部 -->

                 <setting name="aggressiveLazyLoading" value="false"/>

                 <!-- 配置开启二级缓存 -->

                 <setting name="cacheEnabled" value="true"/>

          </settings>

</configuration>

 

  • SQL映射文件配置

<?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="com.atguigu.ssm.mapper.EmployeeMapper">

         <!-- public List<Employee> getAllEmps(); -->

         <select id="getAllEmps" resultMap="myEmpsAndDept" >

                   select e.id eid, e.last_name,e.email,e.gender, d.id did, d.dept_name

                   from tbl_employee e ,tbl_dept d

                   where e.d_id = d.id

         </select>

         <resultMap type="com.atguigu.ssm.beans.Employee" id="myEmpsAndDept">

                   <id column="eid" property="id"/>

                   <result column="last_name" property="lastName"/>

                   <result column="email" property="email"/>

                   <result column="gender" property="gender"/>

 

                   <association property="dept" javaType="com.atguigu.ssm.beans.Department">

                            <id column="did" property="id"/>

                            <result column="dept_name" property="departmentName"/>

                   </association>

         </resultMap>

</mapper>