MyBatis学习第三天

    科技2024-05-29  179

    ---------------------------------- 自定义结果映射规则 ------------------------------

    resultMap:自定义结果集。

    1.全局setting设置 ----autoMappingBehavior默认是PARTIAL,开启自动映射的功能。唯一的要求是列名和javaBean属性名一置。 -----如果autoMappingBehavior设置为null会取消自动映射 ------数据库字段命名规则,POJO属性符合驼峰命名法,如aColumn,我们则可以开启驼峰命名规范则映射功能,mapUndersourceToCamelCase=true。 2.自定义resultMap:实现高级结果映射。

    ---------- 映射文件 select—resultmap—关联查询—环境搭建 ----------------

    方式1:

    <!--resultMap:自定义结果集映射规则, type:自定义的规则java类型 id:方便引用 --> <resultMap id="MyEmp" type="cn.itcast.mybatis.bean.Employee"> <!--指定主键列封装规则 id定义主键会在底层有优化 column:指定那一列 property:指定对应的javaBean属性--> <id column="id" property="id"/> <!--定义普通封装规则--> <result column="lastname" property="lastName"/> <!--其他不指定的列会自动封装,我们只要写resultMap就把全列的映射规则都写上, 为了方便进行检查--> <result column="email" property="email"/> <result column="gender" property="gender"/> </resultMap> <!--public Employee getEmpById(Integer id);--> <select id="getEmpById" resultMap="MyEmp"> select * from tbl_employee where id=#{id} </select> <!-- 联合查询,级联属性封装结果集--> <!-- public Employee getEmpAndDept(Integer id);--> <resultMap id="MyEmpPlus" type="cn.itcast.mybatis.bean.Employee"> <id column="id" property="id"/> <result column="lastname" property="lastName"/> <result column="email" property="email"/> <result column="gender" property="gender"/> <result column="did" property="dept.id"/> <result column="dept_name" property="dept.departmentName"/> </resultMap> <select id="getEmpAndDept" resultMap="MyEmpPlus"> SELECT e.id id, e.lastname lastname,e.gender gender,e.d_id d_id,d.id did,d.dept_name dept_name from tbl_employee e,tbl_dept d WHERE e.d_id=d.id and e.id=#{id} </select>

    方式2:

    <!--使用association定义关联单个对象的封装规则:--> <resultMap id="MyEmpPlus2" type="cn.itcast.mybatis.bean.Employee"> <id column="id" property="id"/> <result column="lastname" property="lastName"/> <result column="email" property="email"/> <result column="gender" property="gender"/> <!--association可以指定联合的javaBean对象 property=dept:指定那个属性是联合对象 javaType:指定这个属性对象的类型 【不能省略】--> <association property="dept" javaType="cn.itcast.mybatis.bean.Department"> <id column="did" property="id"/> <result column="dept_name" property="departmentName"/> </association> </resultMap> <select id="getEmpAndDept" resultMap="MyEmpPlus2"> SELECT e.id id, e.lastname lastname,e.gender gender,e.d_id d_id,d.id did,d.dept_name dept_name from tbl_employee e,tbl_dept d WHERE e.d_id=d.id and e.id=#{id} </select> <!--使用association进行分布查询--> <!-- 场景一: 查询employee的同时查出员工对应的部门 Employee====Department 一个员工有与之对应的部门信息--> <!--public Employee getEmpByIdStep(Integer id);--> <!---分布查询 1.先按照员工id查询员工信息 2.根据查询员工信息中的d_id值,去部门表查出部门信息 3.将查出的部门设置到员工里面--> <resultMap id="myEmpByStep" type="cn.itcast.mybatis.bean.Employee"> <id column="id" property="id"/> <result column="lastname" property="lastName"/> <result column="email" property="email"/> <result column="gender" property="gender"/> <!--定义关联对象封装规则 select:表明当前定义的属性是调用select指定的方法查出的。 column:指定将那一列的值传进去d_id 使用select指定的方法,传入column指定的这列参数值,查出对象,并封装给property属性 --> <association property="dept" select="cn.itcast.mybatis.dao.DepartmentMapper.getDeptById" column="d_id" > </association> </resultMap> <select id="getEmpByIdStep" resultMap="myEmpByStep"> select * from tbl_employee where id=#{id} </select>

    ----- 映射文件 select—resultmap—关联查询—分布查询&延迟加载 -------

    <!-- 分布查询的好处:可以使用延迟加载 Employee===》Dept 我们每次查询Employee对象的时候,都将一起查询出来 部门信息在我们使用的时候再去查询。 分段查询的基础上加上两个配置 --> <setting name="lazyLoadingEnabled" value="true"/> <setting name="aggressiveLazyLoading" value="false"/>

    — 映射文件 select—resultmap—collection定义关联集合封装规则 --------

    <!--public Department getDeptByIdPlus(Integer id);--> <!--collection嵌套结果集的方式,定义关联的集合类型元素封装规则--> <resultMap id="myDept" type="cn.itcast.mybatis.bean.Department"> <id column="did" property="id"></id> <result column="dept_name" property="departmentName"/> <!--collection 定义关联集合类型的属性的封装规则 ofType:指定集合里面元素的类型--> <collection property="emps" ofType="cn.itcast.mybatis.bean.Employee"> <!--定义集合中元素的封装规则--> <id column="eid" property="id"/> <result column="lastname" property="lastName"/> <result column="email" property="email"/> <result column="gender" property="gender"/> </collection> </resultMap> <select id="getDeptByIdPlus" resultMap="myDept"> SELECT d.id did, d.dept_name dept_name, e.id eid,e.lastname lastname ,e.email email ,e.gender gender FROM tbl_dept d LEFT JOIN tbl_employee e ON d.id=e.d_id WHERE d.id=#{id} </select>

    -------映射文件 select—resultmap—collection分布查询&延迟加载 ---------

    <resultMap id="myDeptStep" type="cn.itcast.mybatis.bean.Department"> <id column="id" property="id"/> <id column="dept_name" property="departmentName"/> <collection property="emps" select="cn.itcast.mybatis.dao.EmployeeMapperPlus.getEmpsByDeptId" column="id"></collection> </resultMap> <!-- public Department getDeptByIdPlusStep(Integer id);--> <select id="getDeptByIdPlusStep" resultMap="myDeptStep"> select id, dept_name departmentName from tbl_dept where id=#{id} </select>

    ------ 映射文件 select—resultmap—分布查询传递多列值&fetchType -----

    <!--传递多列的值, 将多列的值封装map column="{key1=column1,key2=column2}" fetchType="lazy" 表示使用延迟加载 eager——立即-->

    ------------- 映射文件 select—resultmap—discriminator鉴别器 --------------

    <!--<discriminator javaType=""> <case value=""></case> </discriminator> 鉴别器:myBatis可以使用discriminator判断某列的值,然后根据某列的值改变封装行为 封装Employee 如果查出的是女生,就把部门信息查询出来。 如果是男生,把lastname这一列的值赋值给email; --> <resultMap id="myEmpDiscriminator" type="cn.itcast.mybatis.bean.Employee"> <id column="id" property="id"/> <result column="lastname" property="lastName"/> <result column="email" property="email"/> <result column="gender" property="gender"/> <!--column:指定判断那一列 javaType:列值对应的java类型 --> <discriminator javaType="string" column="gender"> <case value="0" resultType="cn.itcast.mybatis.bean.Employee"> <association property="dept" select="cn.itcast.mybatis.dao.DepartmentMapper.getDeptById" column="d_id" > </association> </case> <case value="1" resultType="cn.itcast.mybatis.bean.Employee"> <id column="id" property="id"/> <result column="lastname" property="lastName"/> <result column="lastname" property="email"/> <result column="gender" property="gender"/> </case> </discriminator> </resultMap>
    Processed: 0.015, SQL: 8