- <?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.jfit.modules.form.dao.TableModelDao">
- <pre name="code" class="html"><resultMap type="liming.student.manager.data.model.StudentEntity" id="studentResultMap">
- ....
- </resultMap>
- <sql id="formTableModelColumns">
- ....
- </sql>
- <select id="get" resultType="TableModel">
- ....
- </select>
- <insert id="insert">
- ....
- </insert>
- <update id="update">
- ....
- </update>
- <delete id="delete">
- ....
- </delete>
- </mapper>
从映射文件可以看出,最外层的标签是
- <mapper>
在mybatis中,映射文件中的namespace是用于绑定Dao接口的,即面向接口编程。当你的namespace绑定接口后,你可以不用写接口实现类,mybatis会通过该绑定自动帮你找到对应要执行的SQL语句。请注意接口中的方法与映射文件中的SQL语句的ID一一对应 。
里面是一些基本的标签:
SQL 映射XML 文件一些初级的元素:
1. cache – 配置给定模式的缓存2. cache-ref – 从别的模式中引用一个缓存3. resultMap – 这是最复杂而却强大的一个元素了,它描述如何从结果集中加载对象4. sql – 一个可以被其他语句复用的SQL 块5. insert – 映射INSERT 语句6. update – 映射UPDATE 语句7. delete – 映射DELEETE 语句8. select - 映射SELECT语句
resultMap是结果集的映射,resultMap属性:type为java实体类;id为此resultMap的标识。
resultMap可以设置的映射:
1. constructor – 用来将结果反射给一个实例化好的类的构造器
a) idArg – ID 参数;将结果集标记为ID,以方便全局调用b) arg –反射到构造器的通常结果
2. id – ID 结果,将结果集标记为ID,以方便全局调用
3. result – 反射到JavaBean 属性的普通结果
4. association – 复杂类型的结合;多个结果合成的类型
a) nested result mappings – 几resultMap 自身嵌套关联,也可以引用到一个其它上
5. collection –复杂类型集合a collection of complex types
6. nested result mappings – resultMap 的集合,也可以引用到一个其它上
7. discriminator – 使用一个结果值以决定使用哪个resultMap
a) case – 基本一些值的结果映射的case 情形
i. nested result mappings –一个case 情形本身就是一个结果映射,因此也可以包括一些相同的元素,也可以引用一个外部resultMap。
id、result
id、result是最简单的映射,id为主键映射;result其他基本数据库表字段到实体类属性的映射。- 1.<resultMap type="liming.student.manager.data.model.StudentEntity" id="studentResultMap">
- 2. <id property="studentId" column="STUDENT_ID" javaType="String" jdbcType="VARCHAR"/>
- 3. <result property="studentName" column="STUDENT_NAME" javaType="String" jdbcType="VARCHAR"/>
- 4. <result property="studentSex" column="STUDENT_SEX" javaType="int" jdbcType="INTEGER"/>
- 5. <result property="studentBirthday" column="STUDENT_BIRTHDAY" javaType="Date" jdbcType="DATE"/>
- 6. <result property="studentPhoto" column="STUDENT_PHOTO" javaType="byte[]" jdbcType="BLOB" typeHandler="org.apache.ibatis.type.BlobTypeHandler" />
- 7.</resultMap>
constructor
- 1.<resultMap type="StudentEntity" id="studentResultMap" >
- 2. <constructor>
- 3. <idArg javaType="String" column="STUDENT_ID"/>
- 4. <arg javaType="String" column="STUDENT_NAME"/>
- 5. <arg javaType="String" column="STUDENT_SEX"/>
- 6. <arg javaType="Date" column="STUDENT_BIRTHDAY"/>
- 7. </constructor>
- 8.</resultMap>
association联合
联合元素用来处理“一对一”的关系。需要指定映射的Java实体类的属性,属性的javaType(通常MyBatis 自己会识别)。对应的数据库表的列名称。如果想覆写的话返回结果的值,需要指定typeHandler。不同情况需要告诉MyBatis 如何加载一个联合。MyBatis 可以用两种方式加载:
1. select: 执行一个其它映射的SQL 语句返回一个Java实体类型。较灵活;2. resultsMap: 使用一个嵌套的结果映射来处理通过join查询结果集,映射成Java实体类型。
- 1.<resultMap type="ClassEntity" id="classResultMap">
- 2. <id property="classID" column="CLASS_ID" />
- 3. <result property="className" column="CLASS_NAME" />
- 4. <result property="classYear" column="CLASS_YEAR" />
- 5. <association property="teacherEntity" column="TEACHER_ID" select="getTeacher"/>
- 6.</resultMap>
- 7.
- 8.<select id="getClassByID" parameterType="String" resultMap="classResultMap">
- 9. SELECT * FROM CLASS_TBL CT
- 10. WHERE CT.CLASS_ID = #{classID};
- 11.</select>
- 1.<resultMap type="TeacherEntity" id="teacherResultMap">
- 2. <id property="teacherID" column="TEACHER_ID" />
- 3. <result property="teacherName" column="TEACHER_NAME" />
- 4. <result property="teacherSex" column="TEACHER_SEX" />
- 5. <result property="teacherBirthday" column="TEACHER_BIRTHDAY"/>
- 6. <result property="workDate" column="WORK_DATE"/>
- 7. <result property="professional" column="PROFESSIONAL"/>
- 8.</resultMap>
- 9.
- 10.<select id="getTeacher" parameterType="String" resultMap="teacherResultMap">
- 11. SELECT *
- 12. FROM TEACHER_TBL TT
- 13. WHERE TT.TEACHER_ID = #{teacherID}
- 14.</select>
使用resultMap实现联合
- 1.<resultMap type="ClassEntity" id="classResultMap">
- 2. <id property="classID" column="CLASS_ID" />
- 3. <result property="className" column="CLASS_NAME" />
- 4. <result property="classYear" column="CLASS_YEAR" />
- 5. <association property="teacherEntity" column="TEACHER_ID" resultMap="teacherResultMap"/>
- 6.</resultMap>
- 7.
- 8.<select id="getClassAndTeacher" parameterType="String" resultMap="classResultMap">
- 9. SELECT *
- 10. FROM CLASS_TBL CT LEFT JOIN TEACHER_TBL TT ON CT.TEACHER_ID = TT.TEACHER_ID
- 11. WHERE CT.CLASS_ID = #{classID};
- 12.</select>
collection聚集
使用select实现聚集
- 1.<resultMap type="ClassEntity" id="classResultMap">
- 2. <id property="classID" column="CLASS_ID" />
- 3. <result property="className" column="CLASS_NAME" />
- 4. <result property="classYear" column="CLASS_YEAR" />
- 5. <association property="teacherEntity" column="TEACHER_ID" select="getTeacher"/>
- 6. <collection property="studentList" column="CLASS_ID" javaType="ArrayList" ofType="StudentEntity" select="getStudentByClassID"/>
- 7.</resultMap>
- 8.
- 9.<select id="getClassByID" parameterType="String" resultMap="classResultMap">
- 10. SELECT * FROM CLASS_TBL CT
- 11. WHERE CT.CLASS_ID = #{classID};
- 12.</select>
- 1.<!-- java属性,数据库表字段之间的映射定义 -->
- 2.<resultMap type="StudentEntity" id="studentResultMap">
- 3. <id property="studentID" column="STUDENT_ID" />
- 4. <result property="studentName" column="STUDENT_NAME" />
- 5. <result property="studentSex" column="STUDENT_SEX" />
- 6. <result property="studentBirthday" column="STUDENT_BIRTHDAY" />
- 7.</resultMap>
- 8.
- 9.<!-- 查询学生list,根据班级id -->
- 10.<select id="getStudentByClassID" parameterType="String" resultMap="studentResultMap">
- 11. <include refid="selectStudentAll" />
- 12. WHERE ST.CLASS_ID = #{classID}
- 13.</select>
使用resultMap实现聚集
- 1.<resultMap type="ClassEntity" id="classResultMap">
- 2. <id property="classID" column="CLASS_ID" />
- 3. <result property="className" column="CLASS_NAME" />
- 4. <result property="classYear" column="CLASS_YEAR" />
- 5. <association property="teacherEntity" column="TEACHER_ID" resultMap="teacherResultMap"/>
- 6. <collection property="studentList" column="CLASS_ID" javaType="ArrayList" ofType="StudentEntity" resultMap="studentResultMap"/>
- 7.</resultMap>
- 8.
- 9.<select id="getClassAndTeacherStudent" parameterType="String" resultMap="classResultMap">
- 10. SELECT *
- 11. FROM CLASS_TBL CT
- 12. LEFT JOIN STUDENT_TBL ST
- 13. ON CT.CLASS_ID = ST.CLASS_ID
- 14. LEFT JOIN TEACHER_TBL TT
- 15. ON CT.TEACHER_ID = TT.TEACHER_ID
- 16. WHERE CT.CLASS_ID = #{classID};
- 17.</select>
discriminator鉴别器
有时一个单独的数据库查询也许返回很多不同(但是希望有些关联)数据类型的结果集。鉴别器元素就是被设计来处理这个情况的,还有包括类的继承层次结构。鉴别器非常容易理解,因为它的表现很像Java语言中的switch语句。
定义鉴别器指定了column和javaType属性。列是MyBatis查找比较值的地方。JavaType是需要被用来保证等价测试的合适类型(尽管字符串在很多情形下都会有用)。
- 1.<resultMap type="liming.student.manager.data.model.StudentEntity" id="resultMap_studentEntity_discriminator">
- 2. <id property="studentId" column="STUDENT_ID" javaType="String" jdbcType="VARCHAR"/>
- 3. <result property="studentName" column="STUDENT_NAME" javaType="String" jdbcType="VARCHAR"/>
- 4. <result property="studentSex" column="STUDENT_SEX" javaType="int" jdbcType="INTEGER"/>
- 5. <result property="studentBirthday" column="STUDENT_BIRTHDAY" javaType="Date" jdbcType="DATE"/>
- 6. <result property="studentPhoto" column="STUDENT_PHOTO" javaType="byte[]" jdbcType="BLOB" typeHandler="org.apache.ibatis.type.BlobTypeHandler" />
- 7. <result property="placeId" column="PLACE_ID" javaType="String" jdbcType="VARCHAR"/>
- 8. <discriminator column="CLASS_ID" javaType="String" jdbcType="VARCHAR">
- 9. <case value="20000001" resultType="liming.student.manager.data.model.StudentEntity" >
- 10. <result property="classId" column="CLASS_ID" javaType="String" jdbcType="VARCHAR"/>
- 11. </case>
- 12. </discriminator>
- 13.</resultMap>
sql select insert update delete
- 2.<sql id="selectStudentAll">
- 3. SELECT ST.STUDENT_ID,
- 4. ST.STUDENT_NAME,
- 5. ST.STUDENT_SEX,
- 6. ST.STUDENT_BIRTHDAY,
- 7. ST.CLASS_ID
- 8. FROM STUDENT_TBL ST
- 9.</sql>
-
- <select id="getStudent" parameterType="String" resultMap="studentResultMap">
- <include refid="selectStudentAll"/>
- WHERE ST.STUDENT_ID = #{studentID}
- </select>
- 2.<select id="getStudent" parameterType="String" resultMap="studentResultMap">
- 3. SELECT ST.STUDENT_ID,
- 4. ST.STUDENT_NAME,
- 5. ST.STUDENT_SEX,
- 6. ST.STUDENT_BIRTHDAY,
- 7. ST.CLASS_ID
- 8. FROM STUDENT_TBL ST
- 9. WHERE ST.STUDENT_ID = #{studentID}
- 10.</select>
2.<insert id="insertStudentAutoKey" parameterType="StudentEntity">
3. <selectKey keyProperty="studentID" resultType="String" order="BEFORE">
4. select nextval('student')
5. </selectKey>
6. INSERT INTO STUDENT_TBL (STUDENT_ID,
7. STUDENT_NAME,
8. STUDENT_SEX,
9. STUDENT_BIRTHDAY,
10. CLASS_ID)
11. VALUES (#{studentID},
12. #{studentName},
13. #{studentSex},
14. #{studentBirthday},
15. #{classEntity.classID})
16.</insert>
- 2.<update id="updateStudent" parameterType="StudentEntity">
- 3. UPDATE STUDENT_TBL
- 4. SET STUDENT_TBL.STUDENT_NAME = #{studentName},
- 5. STUDENT_TBL.STUDENT_SEX = #{studentSex},
- 6. STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday},
- 7. STUDENT_TBL.CLASS_ID = #{classEntity.classID}
- 8. WHERE STUDENT_TBL.STUDENT_ID = #{studentID};
- 9.</update>
- 2.<delete id="deleteStudent" parameterType="StudentEntity">
- 3. DELETE FROM STUDENT_TBL WHERE STUDENT_ID = #{studentID}
- 4.</delete>
下篇讲述其他标签及参数传递