sql映射文件
SQL映射是MyBatis框架最具有特殊的部分,功能强大而且使用简单。MyBatis框架的主要思想是将SQL语句从程序代码中分离出来,对JDBC访问数据库的代码进行封装,从程序消除了所以SQL参数设置以及处理结果集的JDBC代码,从而大幅减少数据访问层的编码量。并且,SQL语句与代码解耦,可以最大限度的实现对SQL语句的灵活管理,方便SQL调优以保证性能。
SQL映射文件中的几个顶级元素介绍如下
mapper:SQL映射文件的根元素。只有一个namespace,用于区分不同的mapper,必须全局唯一。
cache:为给定命名空间配置缓存。
cache-ref:引用其他命名空间中的缓存配置。
resultMap:用来描述查询结果集中的字段和Java实体类属性的对应关系。
sql:定义可重用的SQL语句块。可以在其他语句映射中引用,提高编写和维护SQL语句的效率。
insert:映射insert语句。
update:映射update语句。
delete:映射delete语句。
select:映射select语句。
MyBatis框架的条件查询
单一条件查询
<select id="getUsersByRealName" resultType="sysuser" parameterType="String">
select * from t_sys_user where realName like concat('%',#{param},'%')
</select>
parameterType属性,它表示为SQL语句传入参数的类型,使用完全限定名或别名,支持基础数据类型和复杂数据类型。
多条件查询
一、将条件封装成Java对象作为入参
在mapper接口中定义方法并使用实体类对象入参
与接口方法绑定的SQL语句中分别使用#{realName}和#{roleId}(即#{属性名})来访问实体类的对应属性
public List<SysUser> getUserListByPojo(SysUser sysUser);
在mapper映射文件中使用
<select id="getUserListByPojo" resultType="sysuser" parameterType="sysuser">
select * from
t_sys_user
where
realName like concat('%',#{realName},'%')
and
roleId=#{roleId}
</select>
二、将查询条件封装成Map对象入参
使用Map类型传递多个参数,绑定的SQL语句中使用#{Map的key}来获取参数值
在mapper接口中定义方法并使用Map对象入参
public List<SysUser> getUserListByMap(Map<String,Object> userMap);
在mapper映射文件中使用
<select id="getUserListByMap" resultType="sysuser" parameterType="map">
select * from
t_sys_user
where
realName like concat('%',#{rName},'%')
and
roleId=#{rId}
</select>
三、使用@Param注解实现多参数入参
在mapper接口中定义方法并使用@Param注解定义参数名
public List<SysUser> getUserListByParams(@Param("realName")String realName,@Param("roleId")int roleId);
在mapper映射文件中使用
<select id="getUserListByParams" resultType="sysuser">
select * from
t_sys_user
where
realName like concat('%',#{realName},'%')
and
roleId=#{roleId}
</select>
MyBatis框架的结果映射
使用resultMap元素自定义结果映射
<resultMap id="userWithRoleName" type="sysuser">
<id property="id" column="id"/>
<result property="roleName" column="userRoleName"/>
</resultMap>
使用方式
<select id="getUserListWithRoleName" parameterType="sysuser" resultMap="userWithRoleName">
select u.*,r.roleName
from t_sys_user u,t_sys_role r
where u.realName like concat('%',#{realName},'%')
and u.roleId=#{roleId}
and u.roleId=r.id
</select>
嵌套结果映射
association元素
用于处理“has-one”类型的关系
<resultMap id="userWithSysRole" type="sysuser">
<id property="id" column="id"/>
<result property="realName" column="realName"/>
<association property="sysRole" javaType="sysrole">
<id property="id" column="rid"/>
<result property="code" column="code"/>
<result property="roleName" column="roleName"/>
</association>
</resultMap>
association元素的主要属性:
property:实体类中用来映射查询结果子集的属性
javaType:property指定的属性的数据类型
association元素包含如下子元素:
id
result
collection元素
collectio元素和association元素作用非常类似,只不过表达的是“一对多”关系,即实体类内部嵌套的是一个集合类型的属性
<resultMap id="userWithAddresses" type="sysuser">
<id property="id" column="id"/>
<result property="account" column="account"/>
<result property="realName" column="realName"/>
<collection property="addressList" ofType="address">
<id property="id" column="aid"/>
<result property="contact" column="contact"/>
<result property="addressDesc" column="addressDesc"/>
<result property="postCode" column="postCode"/>
<result property="tel" column="tel"/>
</collection>
</resultMap>
collection元素有如下常用属性
property:实体类中用来映射查询结果子集的集合属性
ofType:property指定的集合属性中的元素的数据类型
实用小知识
resultMap的自动映射
<!-- 全自动映射 -->
<setting name="autoMappingBehavior" value="FULL"/>