MyBatis的SQL映射文件
<mapper>:映射文件的根元素节点,只有一个属性namespace(命名空间),用于区分不同的mapper,全局唯一。
绑定DAO接口,当namespace绑定某一接口之后,可以不用写该接口的实现类,MyBatis会通过接口的完整限定名查找到对应的mapper配置来执行SQL语句。因此namespace的命名必须要和接口同名。
<cache>:配置给定命名空间的缓存。
<cache-ref>:从其他命名空间引用缓存配置。
<resultMap>:用来描述数据库结果集和对象的对应关系。
<sql>:可以重用的SQL块,也可以被其他语句引用。
<insert>:映射插入语句。
<update>:映射更新语句。
<delete>:映射删除语句。
<select>:映射查询语句。
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 6 <mapper namespace="cn.smbms.dao.user.UserMapper"> 7 8 <!-- 查询用户表记录数 --> 9 <select id="count" resultType="int"> 10 select count(1) as count from smbms_user 11 </select> 12 13 <!-- 当数据库中的字段信息与对象的属性不一致时需要通过resultMap来映射 --> 14 <resultMap type="User" id="userList"> 15 <result property="id" column="id"/> 16 <result property="userCode" column="userCode"/> 17 <result property="userName" column="userName"/> 18 <result property="phone" column="phone"/> 19 <result property="birthday" column="birthday"/> 20 <result property="gender" column="gender"/> 21 <result property="userRole" column="userRole"/> 22 <result property="userRoleName" column="roleName"/> 23 </resultMap> 24 25 26 <!-- 查询用户列表(参数:对象入参) --> 27 <select id="getUserList" resultMap="userList" parameterType="User"> 28 select u.*,r.roleName from smbms_user u,smbms_role r 29 where u.userName like CONCAT ('%',#{userName},'%') 30 and u.userRole = #{userRole} and u.userRole = r.id 31 </select> 32 33 <!-- 查询用户列表(参数:Map) --> 34 <select id="getUserListByMap" resultType="User" parameterType="Map"> 35 select * from smbms_user 36 where userName like CONCAT ('%',#{uName},'%') and userRole = #{uRole} 37 </select> 38 39 </mapper>
<select id="getUserListByMap" resultType="User" parameterType="Map">:这是一个id为getUserListByMap的映射语句,参数类型为String,返回结果的类型是User。
id:命名空间中唯一的标识符,可以被用来引用这条语句,与接口中的方法名一致。
resultType:(自动映射)查询语句返回结果类型的完全限定或别名(update、insert、delete元素中均没有此属性)。
parameterType:表示查询语句传入参数的类型的完全限定或别名,参数的传递使用#{参数名},它告诉MyBatis生成PreparedStatement参数。
(MyBatis传入参数类型可以是Java基础数据类型,但是只适用于一个参数的情况,通过#{参数名}即可获取传入的值。若是多参数入参,需要复杂数据类型支持,包括Java实体类、Map集合,通过)
resultMap:(自定义映射)联表查询或者多表查询时,表内字段重复的情况下,使用resultMap自定义结果映射。
resultType与resultMap的区别:
resultType属性与resultMap属性绝对不能同时存在。
resultType直接表示返回类型,resultMap则是对外部resultMap定义的引用,对应外部的resultMap的id,表示返回结果映射到哪一个resultMap上。
在MyBatis进行查询映射的时候,其实查询出来的每个字段值都是以Map的形式存储的,其中键对应字段名,值则是其对应的值。当元素提供的返回类型是resultType的时候,MyBatis会将Map里面的键值对取出赋给resultType所指定的对象对应的属性(即调用对应的对象里的属性的setter方法进行填充)。由此可以看出,其实MyBatis的每个查询映射的返回类型都是resultMap,只是当我们提供的的返回类型是resultType的时候,MyBatis会自动把对应的值赋给resultType所指定对象的属性。
resultMap自动映射级别,默认的映射级别为PARTIAL,自动进行赋值填充。若要满足需求,则需要设置MyBatis对于resultMap的自动映射级别为NONE,即禁止自动匹配。修改mybatis-config.xml
<settings> <!-- resultMap的自动映射级别 ,默认为PARTIAL,现在为NONE,不会自动匹配 --> <setting name="autoMappingBehavior" value="NONE"/> </settings>
================================================================================================================================================================================================================
使用@Param注解实现多参数入参
<!-- 修改当前用户密码 -->
<update id="updatePwd"> update smbms_user set userPassword=#{userPassword} where id=#{id}
</update>
/** * 修改当前用户密码 * @param id * @param pwd * @return */ public int updatePwd(@Param("id")Integer id, @Param("userPassword")String pwd);
使用注解@Param来传入多个参数,如@Param("userPassword")String pwd,相当于将改参数pwd重新命名为userPassword,在映射的SQL中需要使用#{注解名称},如#{userPassword}。
================================================================================================================================================================================================================
resultMap实现高级结果映射
resultMap元素的属性id(resultMap的唯一标识)、type(表示该resultMap的映射结果类型)。
resultMap子节点id(对应数据库的主键)、result(映射到JavaBean的某个属性)、association、collectioncollectioncollectioncollectiocollection。collectioncollectioncollectioncollection
association:仅处理一对一的关联关系(提供了resultMap属性,代码重用)。
1
//association(一对一的关系,在实体类User内添加一个属性) private Role role; //用户角色
<!-- 根据roleId获取用户列表 association start--> 2 <resultMap type="User" id="userRoleResult"> 3 <id property="id" column="id"/> 4 <result property="userCode" column="userCode" /> 5 <result property="userName" column="userName" /> 6 <result property="userRole" column="userRole" />
<!--property:映射数据库列的实体对象的属性 javaType:完整的Java类名或者别名--> 7 <association property="role" javaType="Role" > 8 <id property="id" column="r_id"/> 9 <result property="roleCode" column="roleCode"/> 10 <result property="roleName" column="roleName"/> 11 </association> 12 </resultMap> 13 14 <select id="getUserListByRoleId" parameterType="Integer" resultMap="userRoleResult"> 15 select u.*,r.id as r_id,r.roleCode,r.roleName from smbms_user u,smbms_role r 16 where u.userRole = #{userRole} and u.userRole = r.id 17 </select> 18 <!-- association end -->
collection:处理一对多的关联关系。
1
//collection(一对多关系,在实体类User添加一个属性,返回类型为集合) private List<Address> addressList;//用户地址列表
<!-- 获取指定用户的地址列表(user表-address表:1对多关系) collection start--> 2 <resultMap type="User" id="userAddressResult"> 3 <id property="id" column="id"/> 4 <result property="userCode" column="userCode"/> 5 <result property="userName" column="userName"/>
<!--property:映射数据库列的实体对象的属性 ofType:完整的Java类名或者别名--> 6 <collection property="addressList" ofType="Address"> 7 <id property="id" column="a_id"/> 8 <result property="postCode" column="postCode"/> 9 <result property="tel" column="tel"/> 10 <result property="contact" column="contact"/> 11 <result property="addressDesc" column="addressDesc"/> 12 </collection> 13 </resultMap> 14 15 <select id="getAddressListByUserId" parameterType="Integer" resultMap="userAddressResult"> 16 select u.*,a.id as a_id,a.contact,a.addressDesc,a.postCode,a.tel 17 from smbms_user u,smbms_address a where u.id = a.userId and u.id=#{id} 18 </select> 19 <!-- collection end -->
使用association/collection的时候,resultMap的自动映射级别
在没有设置autoMappingBehavior的时候,也就是默认情况下(PARTIAL),若是普通数据类型的属性,会自动匹配所有,但是若是有内部嵌套(association/collection),那么输入结果不会自动匹配所有。
除非手工设置autoMappingBehavior的value为FULL(自动匹配所有)。
<settings> <!-- resultMap的自动映射级别 ,默认为PARTIAL,现在为FULL,内部嵌套(association/collection)自动匹配所有 --> <setting name="autoMappingBehavior" value="FULL"/> </settings>
总结:
resultMap的自动映射级别
NONE:禁止自动匹配。
PARTIAL:自动匹配所有属性,有内部嵌套除外。
FULL:自动匹配所有。

浙公网安备 33010602011771号