前言
mybatis是基于“数据库结构不可控”的思想建立的,也就是我们希望数据库遵循第三范式或bcnf,但实际事与愿违,那么结果集映射就是mybatis为我们提供这种理想与现实间转换的手段了,而resultmap就是结果集映射的配置标签了。
在深入resultmap标签前,我们需要了解从sql查询结果集到javabean或pojo实体的过程。
从sql查询结果到领域模型实体
通过jdbc查询得到resultset对象
遍历resultset对象并将每行数据暂存到hashmap实例中,以结果集的字段名或字段别名为键,以字段值为值
根据resultmap标签的type属性通过反射实例化领域模型
根据resultmap标签的type属性和id、result等标签信息将hashmap中的键值对,填充到领域模型实例中并返回
一、resultmap1、属性说明
id属性 ,resultmap标签的标识。
type属性 ,返回值的全限定类名,或类型别名。
automapping属性 ,值范围true(默认值)|false, 设置是否启动自动映射功能,自动映射功能就是自动查找与字段名小写同名的属性名,并调用setter方法。而设置为false后,则需要在resultmap内明确注明映射关系才会调用对应的setter方法。
2、基本作用:建立sql查询结果字段与实体属性的映射关系
示例1:通过setter构造领域模型
public class estudent{ private long id; private string name; private int age; // getter,setter方法 /** * 必须提供一个无参数的构造函数 */ public estudent(){}}
<select id="getstudent" resultmap="getstudentrm"> select id, name, age from tstudent</select><resultmap id="getstudentrm" type="estudnet"> <id property="id" column="id"/> <result property="studentname" column="name"/> <result property="studentage" column="age"/></resultmap>
子元素说明:
id元素 ,用于设置主键字段与领域模型属性的映射关系
result元素 ,用于设置普通字段与领域模型属性的映射关系
id、result语句属性配置细节:
属性描述
property 需要映射到javabean 的属性名称。
column 数据表的列名或者标签别名。
javatype 一个完整的类名,或者是一个类型别名。如果你匹配的是一个javabean,那mybatis 通常会自行检测到。然后,如果你是要映射到一个hashmap,那你需要指定javatype 要达到的目的。
jdbctype 数据表支持的类型列表。这个属性只在insert,update 或delete 的时候针对允许空的列有用。jdbc 需要这项,但mybatis 不需要。如果你是直接针对jdbc 编码,且有允许空的列,而你要指定这项。
typehandler 使用这个属性可以覆写类型处理器。这项值可以是一个完整的类名,也可以是一个类型别名。
示例2:通过构造函数构造领域模型
<select id="getstudent" resultmap="getstudentrm"> select id, name, age from tstudent</select><resultmap id="getstudentrm" type="estudnet"> <constructor> <idarg column="id" javatype="_long"/> <arg column="name" javatype="string"/> <arg column="age" javatype="_int"/> </constructor></resultmap>
子元素说明:
constructor元素 ,指定使用指定参数列表的构造函数来实例化领域模型。注意:其子元素顺序必须与参数列表顺序对应
idarg子元素 ,标记该入参为主键
arg子元素 ,标记该入参为普通字段(主键使用该子元素设置也是可以的)
3、一对一关系、一对多关系查询
注意:在采用嵌套结果的方式查询一对一、一对多关系时,必须要通过resultmap下的id或result标签来显式设置属性/字段映射关系,否则在查询多条记录时会仅仅返回最后一条记录的情况。
association联合联合元素用来处理“一对一”的关系。需要指定映射的java实体类的属性,属性的javatype(通常mybatis 自己会识别)。对应的数据库表的列名称。如果想覆写的话返回结果的值,需要指定typehandler。
不同情况需要告诉mybatis 如何加载一个联合。mybatis 可以用两种方式加载:
select: 执行一个其它映射的sql 语句返回一个java实体类型。较灵活;
resultsmap: 使用一个嵌套的结果映射来处理通过join查询结果集,映射成java实体类型。
例如,一个班级对应一个班主任。
首先定义好班级中的班主任 private teacherentity teacherentity;
使用select实现联合
例:班级实体类中有班主任的属性,通过联合在得到一个班级实体时,同时映射出班主任实体。
这样可以直接复用在teachermapper.xml文件中定义好的查询teacher根据其id的select语句。而且不需要修改写好的sql语句,只需要直接修改resultmap即可。
classmapper.xml文件部分内容:
<resultmap type="classentity" id="classresultmap"> <id property="classid" column="class_id" /> <result property="classname" column="class_name" /> <result property="classyear" column="class_year" /> <association property="teacherentity" column="teacher_id" select="getteacher"/> </resultmap> <select id="getclassbyid" parametertype="string" resultmap="classresultmap"> select * from class_tbl ct where ct.class_id = #{classid}; </select>
teachermapper.xml文件部分内容:
<resultmap type="teacherentity" id="teacherresultmap"> <id property="teacherid" column="teacher_id" /> <result property="teachername" column="teacher_name" /> <result property="teachersex" column="teacher_sex" /> <result property="teacherbirthday" column="teacher_birthday"/> <result property="workdate" column="work_date"/> <result property="professional" column="professional"/> </resultmap> <select id="getteacher" parametertype="string" resultmap="teacherresultmap"> select * from teacher_tbl tt where tt.teacher_id = #{teacherid} </select>
使用resultmap实现联合
与上面同样的功能,查询班级,同时查询器班主任。需在association中添加resultmap(在teacher的xml文件中定义好的),新写sql(查询班级表left join教师表),不需要teacher的select。
修改classmapper.xml文件部分内容:
<resultmap type="classentity" id="classresultmap"> <id property="classid" column="class_id" /> <result property="classname" column="class_name" /> <result property="classyear" column="class_year" /> <association property="teacherentity" column="teacher_id" resultmap="teacherresultmap"/> </resultmap> <select id="getclassandteacher" parametertype="string" resultmap="classresultmap"> select * from class_tbl ct left join teacher_tbl tt on ct.teacher_id = tt.teacher_id where ct.class_id = #{classid}; </select>
其中的teacherresultmap请见上面teachermapper.xml文件部分内容中。
collection聚集聚集元素用来处理“一对多”的关系。需要指定映射的java实体类的属性,属性的javatype(一般为arraylist);列表中对象的类型oftype(java实体类);对应的数据库表的列名称;
不同情况需要告诉mybatis 如何加载一个聚集。mybatis 可以用两种方式加载:
1. select: 执行一个其它映射的sql 语句返回一个java实体类型。较灵活;
2. resultsmap: 使用一个嵌套的结果映射来处理通过join查询结果集,映射成java实体类型。
例如,一个班级有多个学生。
首先定义班级中的学生列表属性:private list<studententity> studentlist;
使用select实现聚集
用法和联合很类似,区别在于,这是一对多,所以一般映射过来的都是列表。所以这里需要定义javatype为arraylist,还需要定义列表中对象的类型oftype,以及必须设置的select的语句名称(需要注意的是,这里的查询student的select语句条件必须是外键classid)。
classmapper.xml文件部分内容:
<resultmap type="classentity" id="classresultmap"> <id property="classid" column="class_id" /> <result property="classname" column="class_name" /> <result property="classyear" column="class_year" /> <association property="teacherentity" column="teacher_id" select="getteacher"/> <collection property="studentlist" column="class_id" javatype="arraylist" oftype="studententity" select="getstudentbyclassid"/> </resultmap> <select id="getclassbyid" parametertype="string" resultmap="classresultmap"> select * from class_tbl ct where ct.class_id = #{classid}; </select>
studentmapper.xml文件部分内容:
<!-- java属性,数据库表字段之间的映射定义 --> <resultmap type="studententity" id="studentresultmap"> <id property="studentid" column="student_id" /> <result property="studentname" column="student_name" /> <result property="studentsex" column="student_sex" /> <result property="studentbirthday" column="student_birthday" /> </resultmap> <!-- 查询学生list,根据班级id --> <select id="getstudentbyclassid" parametertype="string" resultmap="studentresultmap"> <include refid="selectstudentall" /> where st.class_id = #{classid} </select>
使用resultmap实现聚集
使用resultmap,就需要重写一个sql,left join学生表。
<resultmap type="classentity" id="classresultmap"> <id property="classid" column="class_id" /> <result property="classname" column="class_name" /> <result property="classyear" column="class_year" /> <association property="teacherentity" column="teacher_id" resultmap="teacherresultmap"/> <collection property="studentlist" column="class_id" javatype="arraylist" oftype="studententity" resultmap="studentresultmap"/> </resultmap> <select id="getclassandteacherstudent" parametertype="string" resultmap="classresultmap"> select * from class_tbl ct left join student_tbl st on ct.class_id = st.class_id left join teacher_tbl tt on ct.teacher_id = tt.teacher_id where ct.class_id = #{classid}; </select>
其中的teacherresultmap请见上面teachermapper.xml文件部分内容中。studentresultmap请见上面studentmapper.xml文件部分内容中。
4. 动态映射关系
通过 discriminator子元素 (鉴别器)可以实现动态映射关系信息的设置。具体示例如下:
public class estudent{ private long id; private string name; private string juniorhighschool; private string seniorhighschool; private int during; // 在本校就读时间 // getter,setter方法 /** * 必须提供一个无参数的构造函数 */ public estudent(){}}
情景:查询学生信息的seniorhighschool信息,若就读时间during字段值为4、5、6时,则以juniorhighschool字段作所为seniorhighschool信息。
<select id="getstundent" resultmap="rm"> select id, name, juniorhighschool, seniorhighschool, during from tstudent</select><resultmap id="rm" type="estudent"> // 若不加这句,则当将juniorhighschool赋予给seniorhighschool属性时,juniorhighschool属性将为null <result column="juniorhighschool" property="juniorhighschool"/> <discriminator column="during" javatype="_int"> // 形式1:通过resulttype设置动态映射信息 <case value="4" resulttype="estudent"> <result column="juniorhighschool" property="seniorhighschool"/> </case> // 形式2: 通过resultmap设置动态映射信息 <case value="5" resultmap="dynamicrm"/> <case value="6" resultmap="dynamicrm"/> </discriminator></resultmap><resultmap id="dynamicrm" type="estudent"> <result column="juniorhighschool" property="seniorhighschool"/></resultmap>
注意:上面关于 discriminator子元素 的 case元素 的 resulttype属性 和 resultmap元素 的 type属性 ,均不是直指返回的领域模型类型,而是指定根据判断条件后得到映射关系,可通过 id子元素 和 result子元素 重写映射关系。
5. id元素,result元素,idarg元素,arg元素,discriminator元素的共同属性
javatype属性 :java类的全限定名,或别名
jdbctype属性 :jdbc类型, jdbc类型为cud操作时列可能为空时进行处理
typehandler属性 :指定类型处理器的全限定类名或类型别名
column属性 :指定sql查询结果的字段名或字段别名。将用于jdbc的 resultset.getstring(columnname)
本文讲解了mybatis属性详解,更多相关内容请关注。
相关推荐:
mysql数据库多表操作
mysql数据库单表查询
oracle数据库输出输入
以上就是mybatis属性详解的详细内容。