resultType
得到的数据的一次性输出。java类型的全限定类型或者使用别名。sql执行完以后直接按照该指定对象输出。
resultMap
作用:对于一个表格创建关系。可以解决实体类属性和数据库字段不一致的问题;提供一对一、一对多、多对多的映射。
属性列表:
属性 | 描述 |
---|---|
id | 命名空间中的唯一标识 |
type | 类的完全限定名,或类别名 |
autoMapping | 为本结果映射开启或者关闭自动映射 |
extends | 继承其他resultMap属性 |
标签列表: < id property=“” column=“”>
标签 | 描述 |
---|---|
constructor | 用于在实例化类时,注入结果到构造方法中。idArg:id参数,标记出作为ID的结果可以帮助提高整体性能;arg:将被注入到构造方法的一个普通结果 |
id | 标记ID结果 |
result | 注入到实体类属性的普通结果 |
association | 复杂类型关联,用于一对一的关联 |
collection | 复杂类型集合,用于一对多的关联 |
discriminator | 使用结果值决定使用哪个resultMap |
id和result标签参数:
属性 | 描述 |
---|---|
property | 映射到实体类的属性。如果实体类有该属性,则使用;否则mybatis会寻找给定名称的字段 |
column | 映射到数据库中的字段名。 |
javaType | java类的全限定名,或者类型别名。如果映射到Map时,要指定javaType值为Map |
jdbcType | JDBC类型,数据库中的字段类型。 |
typeHandler | 默认的类型处理器。 |
association
常用标签:
属性 | 描述 |
---|---|
property | 映射到实体类的属性。如果实体类有该属性,则使用;否则mybatis会寻找给定名称的字段 |
javatype | java类的全限定名,或者类型别名。如果映射到Map时,要指定javaType值为Map |
resultMap | 引入一个resultMap的映射对象 |
例子:该标签常用于一对一。一个学生的一门课程->对应的分数
<resultMap id="chooseCourseMap" type="com.ztest.Student">
<id property="stu_id" column="stu_id"></id>
<result property="stu_name" column="stu_name"></result>
<association property="chooseCourseList" javaType="com.ztest.ChooseCourse">
<id property="stu_id" column="stu_id"></id>
<id property="c_id" column="c_id"></id>
<result property="genre" column="genre"></result>
<result property="credit" column="credit"></result>
<result property="score" column="score"></result>
</association>
</resultMap>
collection
常用标签:
属性 | 描述 |
---|---|
property | 映射到实体类的属性。如果实体类有该属性,则使用;否则mybatis会寻找给定名称的字段 |
javatype | java类的全限定名,或者类型别名。如果映射到Map时,要指定javaType值为Map |
ofType | 指定一对多集合存放的实体类类型 |
resultMap | 引入一个resultMap的映射对象 |
例子:常用与一对多。
<resultMap id="chooseCourseMap" type="com.ztest.Student">
<id property="stu_id" column="stu_id"></id>
<result property="stu_name" column="stu_name"></result>
<!-- 一对多 -->
<collection property="chooseCourseList" ofType="com.ztest.ChooseCourse" javaType="List">
<id property="stu_id" column="stu_id"></id>
<id property="c_id" column="c_id"></id>
<result property="genre" column="genre"></result>
<result property="credit" column="credit"></result>
<result property="score" column="score"></result>
</collection>
</resultMap>
多对多
例子:
z_course:课程表
z_student:学生表
z_choosecourse:表示学生表和课程表的中间表,即学生所选课程的分数以及对应学分等。
方法一:
在student实体类中加入:
<resultMap id="chooseCourseMap" type="com.ztest.Student">
<id property="stu_id" column="stu_id"></id>
<result property="stu_name" column="stu_name"></result>
<!-- 一对多 -->
<collection property="courseList" ofType="com.ztest.Course" javaType="List">
<id property="c_id" column="c_id" />
<result property="course_name" column="course_name" />
</collection>
<!-- 一对多 -->
<collection property="chooseCourseList" ofType="com.ztest.ChooseCourse" javaType="List">
<id property="stu_id" column="stu_id"></id>
<id property="c_id" column="c_id"></id>
<result property="genre" column="genre"></result>
<result property="credit" column="credit"></result>
<result property="score" column="score"></result>
</collection>
</resultMap>
返回结果:
"result": [
{
"chooseCourseList": [
{
"c_id": 5,
"credit": "4",
"genre": "主修",
"score": 98,
"stu_id": 14
},
{
"c_id": 6,
"credit": "4",
"genre": "主修",
"score": 80,
"stu_id": 14
}
],
"course": [{
"c_id": 5,
"course_name": "math"
},
{
"c_id": 6,
"course_name": "yuwen"
}
],
"stu_id": 14,
"stu_name": "xz"
}
]
方法二:
在中间表加入了两个对应实体类属性;
在course表中加入了映射:
student表和方法一中的一样。
映射实现如下:
<resultMap id="chooseCourseMap" type="com.stec.masterdata.entity.promis.ztest.Student">
<id property="stu_id" column="stu_id"></id>
<result property="stu_name" column="stu_name"></result>
<!-- 一对多 -->
<collection property="chooseCourseList" resultMap="chooseMap" />
</resultMap>
<resultMap type="com.stec.masterdata.entity.promis.ztest.ChooseCourse" id="chooseMap">
<id property="stu_id" column="stu_id"></id>
<id property="c_id" column="c_id"></id>
<result property="genre" column="genre"></result>
<result property="credit" column="credit"></result>
<result property="score" column="score"></result>
<!-- 一对一 -->
<association property="course" javaType="com.stec.masterdata.entity.promis.ztest.Course">
<id property="c_id" column="c_id" />
<result property="course_name" column="course_name" />
</association>
</resultMap>
最后得到三层嵌套结果:
"result": [
{
"chooseCourseList": [
{
"c_id": 5,
"course": {
"c_id": 5,
"course_name": "math"
},
"credit": "4",
"genre": "主修",
"score": 98,
"stu_id": 14
},
{
"c_id": 6,
"course": {
"c_id": 6,
"course_name": "yuwen"
},
"credit": "4",
"genre": "主修",
"score": 80,
"stu_id": 14
}
],
"stu_id": 14,
"stu_name": "xz"
}
]
关键点在于使用了collection中的resultMap属性。
jdbcType和javaType对照表
补充说明:
mysql中的jdbcType blob和Longtext字段类型区别:添加链接描述
-
blob(binary large object)是二进制大对象,可以容纳可变量数量的数据,其中blob分为四种类型:TinyBlob、blob、mediumBlob和LongBlob。
text也分为四种类型:tinyText,Text,MediumText,LongText。 -
blob被视为二进制字符串,text被视为非二进制字符串;
blob没有字符集,并且排序和比较基于列值字节的数值值。
text有字符集,按照字符集的校对规则对值进行排序和比较。
blob加入不了值。
-
blob和text不能有默认值。
当保存或检索BLOB和TEXT列的值时不删除尾部空格。 -
text只能存储纯文本,长度不足时扩展;blob除了存储文本外,由于其二进制存储格式,所以还可以保存图片。
varchar和varbinary的区别:添加链接描述
- varchar是可变长度二进制字符类型,如果对应的数据库排序规则是utf8_general_ci,查询不分大小写;排序规则为utf8_bin,区分大小写。varbinary是可变长度二进制字符类型,utf8_general_ci可以区分大小写。
- char(n)存储非二进制字符串,插入时,对于少于n字符在尾部加空格,查询时,丢弃尾部空格;
varchar(n)存储非二进制字符串,插入时,对于少于n字符在尾部不加空格,查询时,不丢弃尾部空格;
添加链接描述
mysql不支持full join,oracle支持,想实现full join解决方案
select * from t1 left join t2 on t1.id = t2.id
union
select * from t1 right join t2 on t1.id = t2.id