笔记要点
出错分析与总结
在全局配置中,映射dao包下的全部: <mapper> <package name="com.dao"/> </mappers>
工程组织
1.定义接口
package com.dao; import com.bean.*; public interface EmployeeMapperPlus { public Employee getEmpAndDept(Integer id);//关联查询 public Employee getEmpAndDept2(Integer id); //关联查询 ,使用association public Employee getEmpById(Integer id); }
2.定义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="com.dao.EmployeeMapperPlus"> <!--ResultMap ;自定义结果集映射规则; type: 自定义规则的Java类型;id: 唯一的标识,方便引用--> <resultMap id="MySimpleEmp" type="com.bean.Employee"> <!--指定主键列的封装规则,id定义主键,底层会有优化规则; column : 指定结果集的具体的那一列; property:指定的JavaBean对应的属性--> <id column="id" property="id"/> <!--定义普通列的封装规则--> <result column="last_name" property="lastName"/> <!--,其他不指定的列会自动封装;但是, 我们只要写ResultMap,就把剩下的映射全部都写上--> <result column="email" property="email"/> <result column="gender" property="gender"/> </resultMap> <!--public Employee getEmpById(Integer id); 注意进行更改为resultMap--> <select id="getEmpById" resultMap="MySimpleEmp"> select * from tbl_employee where id=#{id} </select> <!--场景1 ,方法1:使用级联属性的方式 查询Employee的同时查询员工对应的部门Employee.dept=Department.id; 输出员工对应的部门的全部信息: id last_name gender did dept_name --> <resultMap id="MyDifEmp" type="com.bean.Employee"> <!--column : 指定结果集的具体的那一列; property:指定的JavaBean对应的属性--> <id column="id" property="id"/> <result column="last_name" property="lastName"/> <result column="gender" property="gender"/> <result column="email" property="email"/> <result column="did" property="dept.id"/> <result column="dept_name" property="dept.departmentName"/> </resultMap> <!--public Employee getEmpAndDept2(Integer id); //association 定义封装规则!--> <select id="getEmpAndDept" resultMap="MyDifEmp"> SELECT e.id id,e.last_name last_name,e.`gender` gender,e.`d_id` d_id, d.id did,d.`dept_name` dept_name,email FROM tbl_employee e,tbl_dept d WHERE e.`d_id`=d.`id` AND e.id=#{id}; </select> <!--方法2:-使用association可以指定联合的javaBean的对象 --> <resultMap id="MyDifEmp2" type="com.bean.Employee"> <!--column : 指定结果集的具体的那一列; property:指定的JavaBean对应的属性--> <id column="id" property="id"/> <result column="last_name" property="lastName"/> <result column="gender" property="gender"/> <result column="email" property="email"/> <!--使用association可以指定联合的javaBean的对象;(定义单个对象的封装规则!) property="dept";指定那个属性是联合的对象;javaType="dept";指定那个属性对象的类型;--> <association property="dept" javaType="com.bean.Department"> <id column="did" property="id"/> <result column="dept_name" property="departmentName"/> </association> </resultMap> <!--public Employee getEmpAndDept2(Integer id); //关联查询--> <select id="getEmpAndDept2" resultMap="MyDifEmp2"> SELECT e.id id,e.last_name last_name,e.`gender` gender,e.`d_id` d_id, d.id did,d.`dept_name` dept_name,email FROM tbl_employee e,tbl_dept d WHERE e.`d_id`=d.`id` AND e.id=#{id}; </select> </mapper>
3.编写测试代码
package com.test.resultMap; import com.bean.Employee; import com.dao.EmployeeMapperPlus; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Test; import java.io.IOException; import java.io.InputStream; public class test_tp30 { public SqlSessionFactory getSqlSessionFactory() throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream=Resources.getResourceAsStream(resource); return new SqlSessionFactoryBuilder().build(inputStream); } @Test public void test06() throws Exception{ SqlSession openSession = getSqlSessionFactory().openSession(); try { EmployeeMapperPlus mapper = openSession.getMapper(EmployeeMapperPlus.class); System.out.println("--------多表关联查询,级联属性的封装结果!--------"); Employee employee = mapper.getEmpAndDept(1); System.out.println(employee); System.out.println("--------多表关联查询,使用association进行连接!--------"); Employee employee2 = mapper.getEmpAndDept2(1); System.out.println(employee2); openSession.commit();//默认是不自动提交数据的,需要我们自己手动提交 }finally { openSession.close(); } } }
测试结果
--------多表关联查询,级联属性的封装结果!-------- DEBUG 12-01 10:37:03,938 ==> Preparing: SELECT e.id id,e.last_name last_name,e.`gender` gender,e.`d_id` d_id, d.id did,d.`dept_name` dept_name,email FROM tbl_employee e,tbl_dept d WHERE e.`d_id`=d.`id` AND e.id=?; (BaseJdbcLogger.java:145) DEBUG 12-01 10:37:03,958 ==> Parameters: 1(Integer) (BaseJdbcLogger.java:145) DEBUG 12-01 10:37:03,968 <== Total: 1 (BaseJdbcLogger.java:145) Employee{id=1, lastName='jerry', email='jerry@163.com', gender='1'} --------多表关联查询,使用association进行连接!-------- DEBUG 12-01 10:37:03,969 ==> Preparing: SELECT e.id id,e.last_name last_name,e.`gender` gender,e.`d_id` d_id, d.id did,d.`dept_name` dept_name,email FROM tbl_employee e,tbl_dept d WHERE e.`d_id`=d.`id` AND e.id=?; (BaseJdbcLogger.java:145) DEBUG 12-01 10:37:03,969 ==> Parameters: 1(Integer) (BaseJdbcLogger.java:145) DEBUG 12-01 10:37:03,970 <== Total: 1 (BaseJdbcLogger.java:145) Employee{id=1, lastName='jerry', email='jerry@163.com', gender='1'} Process finished with exit code 0