一般结果映射
1.有一个数据表user_table,字段分别是user_id,user_name,user_password。
2.有一个JavaBean 实体类User
public class User { private int id; private String userame; private String userPassword; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getUserPassword() { return userPassword; } public void setUserPassword(String userPassword) { this.userPassword= userPassword; } }
3.编写.xml文件查询数据,内容如下
<select id="selectUsers" resultType="User"> select user_id, user_name, user_password from user_table where id = #{id} </select>
显然,发现数据表字段名与实体类属性名不一致,可以使用SQL的基础特性来统一字段名与实体类属性名
<select id="selectUsers" resultType="User"> select user_id as "id", user_name as "username", user_password as "userPassword" from user_table where id = #{id} </select>
但是属性名与字段名不一致并没有影像数据查询,还能查出数据表中的数据,这是因为MyBatis 会在幕后自动创建一个ResultMap,再根据字段名来映射列到 JavaBean 的属性上。我们也可以显示的写出来,使用外部ResultMap对象
<select id="selectUsers" resultMap="userResultMap"> select user_id, user_name, user_password from user_table where id = #{id} </select> <resultMap id="userResultMap" type="User"> <id property="id" column="user_id" /> <result property="username" column="user_name"/> <result property="userPassword" column="user_password"/> </resultMap>
- id标签:标记出作为 ID 的结果可以帮助提高整体性能
- result标签: 注入到字段或 JavaBean 属性的普通结果。property=""里面写实体类属性名。 column=""里面写数据表字段名或别名。
高级结果映射
MyBatis 创建时的一个思想是:数据库不可能永远是你所想或所需的那个样子,但是 ResultMap 可以帮助我们更好的解决这个问题。
结果映射(resultMap):
constructor
标签: 用于在实例化类时,注入结果到构造方法中
idArg标签
:ID 参数;标记出作为 ID 的结果可以帮助提高整体性能arg
标签:将被注入到构造方法的一个普通结果//构造器 public class User { //... public User(Integer id, String username, int age) { //... } }
为了将结果注入构造方法,MyBatis 需要通过某种方式定位相应的构造方法。
<constructor> <idArg column="id" javaType="java.lang.Integer"/> <arg column="username" javaType="String"/> <arg column="age" javaType="int"/> </constructor>
属性 描述 column
数据库中的列名,或者是列的别名。 javaType
一个 Java 类的完全限定名,或一个类型别名。 select
用于加载复杂类型属性的映射语句的 ID,它会从 column 属性中指定的列检索数据,作为参数传递给此 select 语句。 resultMap
结果映射的 ID,可以将嵌套的结果集映射到一个合适的对象树中。 它可以作为使用额外 select 语句的替代方案。
id
标签:一个 ID 结果;标记出作为 ID 的结果可以帮助提高整体性能result
标签:注入到字段或 JavaBean 属性的普通结果<id property="id" column="user_id"/> <result property="name" column="user_name"/>
这些元素是结果映射的基础。id 和 result 元素都将一个列的值映射到一个简单数据类型(String, int, double, Date 等)的属性或字段。这两者之间的唯一不同是,id 元素对应的属性会被标记为对象的标识符,在比较对象实例时使用; 这样可以提高整体的性能。
属性 描述 property
映射到列结果的字段或属性。如果 JavaBean 有这个名字的属性(property),会先使用该属性。 column
数据库中的列名,或者是列的别名。
association
标签: 一个复杂类型的关联;许多结果将包装成这种类型。嵌套结果映射:关联可以是resultMap
元素,或是对其它结果映射的引用。【一对一,多对一】关联的嵌套 Select 查询
属性 描述 property 映射到列结果的字段或属性。如果用来匹配的 JavaBean 存在给定名字的属性,那么它将会被使用。 column 数据库中的列名,或者是列的别名。 select 用于加载复杂类型属性的映射语句的 ID,它会从 column 属性指定的列中检索数据,作为参数传递给目标 select 语句。 javaType 一个 Java 类的完全限定名,或一个类型别名。 <!-- 每个学生对应的老师 --> <select id="findAll" resultMap="StudentTeacher"> select * from student1 </select> <resultMap id="StudentTeacher" type="com.qcby.entity.Student1"> <result property="id" column="id"/> <result property="Sname" column="Sname"/> <result property="sex" column="sex"/> <result property="age" column="age"/> <result property="t_id" column="t_id"/> <association property="teacher" column="t_id" javaType="com.qcby.entity.Teacher" select="getTeacher"/> </resultMap> <select id="getTeacher" resultType="com.qcby.entity.Teacher"> select * from teacher where id=#{t_id} </select>
关联的嵌套结果映射
<!-- 每个学生对应的老师 --> <select id="find" resultMap="StudentTchr"> select * from student1 left join teacher on student1.t_id=teacher.id </select> <resultMap id="StudentTchr" type="com.qcby.entity.Student1"> <result property="id" column="id"/> <result property="Sname" column="Sname"/> <result property="sex" column="sex"/> <result property="age" column="age"/> <result property="t_id" column="t_id"/><!--老师的id--> <association property="teacher" javaType="com.qcby.entity.Teacher"> <result property="id" column="id"/> <result property="Tname" column="Tname"/> </association> </resultMap>
collection
标签: 一个复杂类型的集合。嵌套结果映射 – 集合可以是resultMap
元素,或是对其它结果映射的引用。【一对多】集合的嵌套 Select 查询
<select id="findAll" resultMap="TeacherStudent"> select * from teacher </select> <resultMap id="TeacherStudent" type="com.qcby.entity.Teacher2"> <result property="id" column="id"/> <result property="Tname" column="Tname"/> <collection property="student2s" column="id" javaType="ArrayList" ofType="com.qcby.entity.Student2" select="getStudent"/> </resultMap> <select id="getStudent" resultType="com.qcby.entity.Student2"> select * from student1 where t_id=#{id} </select>
集合的嵌套结果映射
<!-- 每个老师对应的学生 --> <select id="find" resultMap="TeacherStudnt"> select * from teacher left join student1 on teacher.id=student1.t_id </select> <resultMap id="TeacherStudnt" type="com.qcby.entity.Teacher2"> <id property="id" column="id"/> <result property="Tname" column="Tname"/> <collection property="student2s" javaType="ArrayList" ofType="com.qcby.entity.Student2"> <result property="id" column="id"/> <result property="Sname" column="Sname"/> <result property="sex" column="sex"/> <result property="age" column="age"/> <result property="t_id" column="t_id"/><!--老师的id--> </collection> </resultMap>