sql查询结果,自动映射方法
•1、全局setting设置
–autoMappingBehavior默认是PARTIAL,开启自动映射的功能。唯一的要求是列名和javaBean属性名一致
–如果autoMappingBehavior设置为null则会取消自动映射
–数据库字段命名规范,POJO属性符合驼峰命名法,如A_COLUMN->aColumn,我们可以开启自动驼峰命名规则映射功能,mapUnderscoreToCamelCase=true。
•2、sql语句查询时,使用别名,完成bean的映射。
•3、自定义resultMap,实现高级结果集映射,完成自定义封装查询结果。
sql映射文件中 sql语句 的 resultMap 与 resultType 只能二选一。
测试数据库:
创建数据库:【第一次写的时候,居然写成tbl_employee{},sql语句没有{},应该是() 】
create table tbl_employee( id int(11) primary key auto_increment, last_name varchar(255), gender char(1), email varchar(255) )
添加数据:
id last_name gender email 1 mike 0 [email protected]
resultMap:简单映射
Junit单元测试: EmployeeMapperPlus mapper = openSession.getMapper(EmployeeMapperPlus.class); Employee employee = mapper.getEmpById(1); System.out.println(mapper.getClass()); System.out.println(employee); 接口文件: public Employee getEmpById(Integer id); sql映射文件: <!--自定义某个javaBean的封装规则 type:自定义规则的Java类型 id:唯一id方便引用 --> <resultMap type="com.mybatis.bean.Employee" id="MySimpleEmp"> <!--指定主键列的封装规则 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> <!-- resultMap:自定义结果集映射规则; --> <!-- public Employee getEmpById(Integer id); --> <select id="getEmpById" resultMap="MySimpleEmp"> select * from tbl_employee where id=#{id} </select>
eclipse:控制台输出数据库接口
Employee [id=1, lastName=mike, [email protected], gender=0]
resultMap:级联映射
1、新建雇员表:
create table tbl_employee( id int(11) primary key auto_increment, last_name varchar(255), gender char(1), email varchar(255) )
2、添加雇员信息:
id last_name gender email 1 mike 0 [email protected]
3、添加部门表:
create table tbl_dept( id int(11) primary key auto_increment, dept_name varchar(255) )
4、添加部门信息:
id dept_name 1 开发部 2 测试部
5、添加表关联
下面是两条sql语句,第一条语句后面要写分号,这样才能顺利执行。
alter table tbl_employee add column d_id int(11); alter table tbl_employee add CONSTRAINT fk_emp_dept FOREIGN key(d_id) REFERENCES tbl_dept(id)
6、
sql 客户端 查询:
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 from tbl_employee e,tbl_dept d where e.d_id = d.id and e.id =1
项目代码文件:
Employee.java文件:
package com.mybatis.bean; public class Employee { private int id; private String lastName; private String email; private String gender; private Department department; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public Department getDepartment() { return department; } public void setDepartment(Department department) { this.department = department; } @Override public String toString() { return "Employee [id=" + id + ", lastName=" + lastName + ", email=" + email + ", gender=" + gender + "]"; } }
Department.java
package com.mybatis.bean; import java.util.List; public class Department { private Integer id; private String departmentName; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getDepartmentName() { return departmentName; } public void setDepartmentName(String departmentName) { this.departmentName = departmentName; } @Override public String toString() { return "Department [id=" + id + ", departmentName=" + departmentName + "]"; } }
JUnit测试文件: EmployeeMapperPlus mapper = openSession.getMapper(EmployeeMapperPlus.class); Employee employee = mapper.getEmpAndDept(1); System.out.println(employee); System.out.println(employee.getDepartment()); 接口文件: public Employee getEmpAndDept(Integer id); sql映射文件: <!-- 场景一: 查询Employee的同时查询员工对应的部门 Employee===Department 一个员工有与之对应的部门信息; id last_name gender d_id did dept_name (private Department dept;) --> <!-- 联合查询:级联属性封装结果集 --> <resultMap type="com.mybatis.bean.Employee" id="MyDifEmp"> <id column="id" property="id"/> <result column="last_name" property="lastName"/> <result column="gender" property="gender"/> <result column="did" property="department.id"/> <result column="dept_name" property="department.departmentName"/> </resultMap> <!-- public Employee getEmpAndDept(Integer id);--> <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 FROM tbl_employee e,tbl_dept d WHERE e.d_id=d.id AND e.id=#{id} </select>
sql映射文件也可以替换 采用 association
标签方法:
<!-- 使用association定义关联的单个对象的封装规则; --> <resultMap type="com.mybatis.bean.Employee" id="MyDifEmp2"> <id column="id" property="id"/> <result column="last_name" property="lastName"/> <result column="gender" property="gender"/> <!-- association可以指定联合的javaBean对象 property="department":指定哪个属性是联合的对象 javaType:指定这个属性对象的类型[不能省略] --> <association property="department" javaType="com.mybatis.bean.Department"> <id column="did" property="id"/> <result column="dept_name" property="departmentName"/> </association> </resultMap> <!-- public Employee getEmpAndDept(Integer id);--> <select id="getEmpAndDept" 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 FROM tbl_employee e,tbl_dept d WHERE e.d_id=d.id AND e.id=#{id} </select>