动态sql
if 判断语句
<select id="queryUserListLikeUserName" resultType="User">
select * from tb_user where sex=1
<!-- if:判断 test:OGNL表达式
-->
<if test="userName!=null and userName.trim()!=''">
and user_name like '%' #{userName} '%'
</if>
</select>
choose when otherwise
<select id="queryUserListLikeUserNameOrAge" resultType="User">
select * from tb_user where sex=1
<!--
choose:条件选择
when:test-判断条件,一旦有一个when成立,后续的when都不再执行
otherwise:所有的when都不成立时,才会执行
-->
<choose>
<when test="userName!=null and userName.trim()!=''">
and user_name like '%' #{userName} '%'</when>
<when test="age != null">and age = #{age}</when>
<otherwise>
and user_name = 'zhangsan'
</otherwise>
</choose>
</select>
where
<select id="queryUserListLikeUserNameAndAge" resultType="User">
select * from tb_user
<!--
自动添加where关键字
有一定的纠错功能:去掉sql语句块之前多余的一个 and | or
通常结合if或者choose使用
-->
<where>
<if test="userName!=null and userName.trim()!=''">user_name like '%' #{userName} '%'</if>
<if test="age!=null">and age = #{age}</if>
</where>
</select>
set
<update id="updateUserSelective" >
UPDATE tb_user
<!--
set自动添加set关键字
也有一定的纠错功能:自动去掉sql语句块 之后 多余的一个 逗号
-->
<set>
<if test="userName!=null and userName.trim()!=''">
user_name = #{userName},
</if>
<if test="password!=null and password.trim()!=''">
password = #{password},
</if>
<if test="name!=null and name.trim()!=''">
name = #{name},
</if>
<if test="age!=null">
age = #{age},
</if>
<if test="sex!=null">
sex = #{sex},
</if>
updated = now(),
</set>
WHERE
(id = #{id});
</update>
foreach
<select id="queryUserListByIds" resultType="User">
select * from tb_user where id in
<!--
foreach:遍历集合
collection:接收的集合参数
item:遍历的集合中的一个元素
separator:分隔符
open:以什么开始
close:以什么结束
-->
<foreach collection="ids" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</select>
resultMap
解决列名和属性名不一致
查询数据的时候,查不到userName的信息,原因:数据库的字段名是user_name,而POJO中的属性名字是userName
两端不一致,造成mybatis无法填充对应的字段信息。
解决方案1:在sql语句中使用别名
解决方案2:参考驼峰匹配 --- mybatis-config.xml 的时候
解决方案3:resultMap自定义映射
ResultMap 的设计思想是,对于简单的语句根本不需要配置显式的结果映射,而对于复杂一点的语句只需要描述它们的关系就行了。
<resultMap id="userResultMap" type="User">
<id property="id" column="user_id" />
<result property="username" column="user_name"/>
<result property="password" column="hashed_password"/>
</resultMap>
<select id="selectUsers" resultMap="userResultMap">
select user_id, user_name, hashed_password
from some_table
where id = #{id}
</select>
实体类中属性password与数据库中字段名hashed_password不同,查询时查询不到,所以需要对字段名进行起别名映射。
property - 实体类
column - 数据库
对于数据库中与实体类相同的属性名,可以自动映射,就不需要在resultMap中进行起别名映射。
在UserMapper.xml中配置resultMap
在UserMapper.xml中使用resultMap:
resultMap的自动映射(AutoMapping)
在resultMap中,主键需要通过id子标签配置,表字段和属性名不一致的普通字段需要通过result子标签配置。
那么,字段名称匹配的字段要不要配置那?这个取决于resultMap中的autoMapping属性的值:
为true时:resultMap中的没有配置的字段如果和属性名称相同就会自动对应。
为false时:只针对resultMap中已经配置
<result>
标签的字段才会作映射。
并且resultMap会自动映射单表查询的结果集,单表查询时该属性默认为true
多表关联查询时,该属性默认为false为了避免不必要的麻烦,建议手动将其配置为true
使用resultType不能完成user对象的自动映射,需要手动完成结果集映射,即使用resultMap标签自定义映射。
在OrderMapper.xml中配置,结果集的映射,这时必须使用resultMap:
<resultMap type="Order" id="orderUserMap" autoMapping="true">
<id column="id" property="id"/>
<!--
association:一对一的映射
property:java的属性名
javaType:属性名对应的java类型
autoMapping:开启自动映射
子标签:参照resultMap
-->
<association property="user" javaType="User" autoMapping="true">
<id column="user_id" property="id"/>
</association>
</resultMap>
<!-- resultType不能完成user信息的映射,必须使用resultMap,resultMap的值对应resultMap标签的id,resultMap和resultType必须二选一 -->
<select id="queryOrderWithUser" resultMap="orderUserMap">
select * from tb_order a
LEFT JOIN tb_user b on a.user_id=b.id
where a.order_number = #{number}
</select>
resultMap的继承
本文来自博客园,作者:Lz_蚂蚱,转载请注明原文链接:https://www.cnblogs.com/leizia/p/17156318.html