1. 映射文件简介
1)MyBatis 的真正强大在于它的映射语句,也是它的魔力所在。由于它的异常强大,映射器的 XML 文件就显得相对简单。如果拿它跟具有相同功能的 JDBC 代码进行对比,你会立即发现省掉了将近 95% 的代码。MyBatis 就是针对 SQL 构建的,并且比普通的方法做的更好。
2)SQL 映射文件只有很少的几个顶级元素(按照应被定义的顺序列出):
- cache – 该命名空间的缓存配置。
- cache-ref – 引用其它命名空间的缓存配置。
- resultMap – 描述如何从数据库结果集中加载对象,是最复杂也是最强大的元素。
- parameterMap – 老式风格的参数映射。此元素已被废弃,并可能在将来被移除!请使用行内参数映射。文档中不会介绍此元素。
- sql – 可被其它语句引用的可重用语句块。
- nsert – 映射插入语句。
- update – 映射更新语句。
- delete –映射删除语句。
- select – 映射查询语句
2.select标签
1. 示例
<select id="getEmployeeById" resultType="employee" databaseId="mysql">
select id,last_name,email,salary,dept_id
from employees
where id = #{id}
</select>
2.属性说明
- id : 指定接口的方法名
- parameterType:设置传入的参数类型。可以不写,因为 MyBatis 可以通过类型处理器(TypeHandler)推断出具体传入语句的参数
- resultType:设置方法的返回值类型。注意:如果返回的是集合,设置的是集合包含的类型,而不是集合本身的类型。如:返回值是List<User>,resultType=“User”,而不是resultType=“List”
- resultMap:设置对外部resultMap高级结果集标签的引用。resultType 和 resultMap 之间只能同时使用一个。
- flushCache:将其设置为 true 后,只要语句被调用,都会导致本地缓存和二级缓存被清空,默认值:false。
- useCache:将其设置为 true 后,将会导致本条语句的结果被二级缓存缓存起来,默认值:对 select 元素为 true。
- databaseId:如果配置了数据库厂商标识(databaseIdProvider),MyBatis 会加载所有不带 databaseId 或匹配当前 databaseId 的语句;如果带和不带的语句都有,则不带的会被忽略。
3. insert、update、delete标签
1. 示例
<insert id="addEmployee">
insert into employees(last_name,email,salary,dept_id)
values(#{lastName},#{email},#{salary},#{deptId})
</insert>
2.属性说明
- id : 命名空间的唯一表示符,与接口名一致。
- parameterType:将会传入这条语句的参数的类全限定名或别名。这个属性是可选的,因为 MyBatis 可以通过类型处理器(TypeHandler)推断出具体传入语句的参数,默认值为未设置(unset)。
- flushCache:将其设置为 true 后,只要语句被调用,都会导致本地缓存和二级缓存被清空,默认值:(对 insert、update 和 delete 语句)true。
- useGeneratedKeys:仅适用于 insert 和 update)这会令 MyBatis 使用 JDBC 的 getGeneratedKeys 方法来取出由数据库内部生成的主键(比如:像 MySQL 和 SQL Server 这样的关系型数据库管理系统的自动递增字段),默认值:false。
- keyProperty:(仅适用于 insert 和 update)指定能够唯一识别对象的属性,MyBatis 会使用 getGeneratedKeys 的返回值或 insert 语句的 selectKey 子元素设置它的值,默认值:未设置(unset)。如果生成列不止一个,可以用逗号分隔多个属性名称。
- keyColumn:(仅适用于 insert 和 update)设置生成键值在表中的列名,在某些数据库(像 PostgreSQL)中,当主键列不是表中的第一列的时候,是必须设置的。如果生成列不止一个,可以用逗号分隔多个属性名称。
- databaseId:如果配置了数据库厂商标识(databaseIdProvider),MyBatis 会加载所有不带 databaseId 或匹配当前 databaseId 的语句;如果带和不带的语句都有,则不带的会被忽略。
4.resultMap自定义映射标签
1.示例
<!--
resultMap属性:引用高级结果集映射,设置为resultMap标签的id属性值
-->
<select id="getEmployeeById" resultMap="myResult">
select id,last_name,email,salary,dept_id
from employees
where id = #{id}
</select>
<resultMap id="myResult" type="com.zyd.model.Employee">
<id property="id" column="id"></id>
<result property="lastName" column="last_name"></result>
<result property="email" column="email"></result>
<result property="salary" column="salary"></result>
<result property="deptId" column="dept_id"></result>
</resultMap>
<resultMap id="collection1" type="department">
<id column="dept_id" property="id"></id>
<result column="name" property="name"></result>
<collection property="employee" ofType="employee">
<id column="empId" property="id"></id>
<result column="dept_id" property="dept_id"></result>
<result column="last_name" property="last_name"></result>
<result column="email" property="email"></result>
<result column="gender" property="gender"></result>
<result column="salary" property="salary"></result>
</collection>
</resultMap>
<resultMap id="resultMap2" type="Employee">
<id property="id" column="id"></id>
<result property="email" column="email"></result>
<result property="gender" column="gender"></result>
<result property="last_name" column="last_name"></result>
<result property="salary" column="salary"></result>
<result property="dept_id" column="dept_id"></result>
<association property="dept" javaType="department">
<id column="dept_id" property="id"></id>
<result column="name" property="name"></result>
</association>
</resultMap>
2.属性说明
- id:用于完成主键的映射
- result:用于完成普通列的映射
- association :一个复杂的类型关联;许多结果将包成这种类型
- collection : 复杂类型的集合
- type: 返回的类的全限定名或别名
- autoMapping:如果设置这个属性,MyBatis 将会为本结果映射开启或者关闭自动映射。 这个属性会覆盖全局的属性 autoMappingBehavior。默认值:未设置(unset)。