一、动态SQL:通过mybatis提供的各种标签方法实现动态拼接sql
- <if> 标签: 判断
- <where>标签 :去除第一个and或者or
- <foreach>标签 :循环 可以循环数据也可以循环集合
- <sql>标签 :片段
- <include>标签:引用某sql片段
- <set>标签: 修改时 去除最后一个逗号,常用于update方法
<if> <where> <sql> <include> 标签示例:
注意:如果引用其它mapper.xml的sql片段,则在引用时需要加上namespace,如下:
<include refid="namespace.sql片段”/>
<sql id="findListUserWhere"> <where> <if test="username != null and username != ''"> --注意使用and连接 and username LIKE '%${username}%' --where去除第一个and </if> <if test="sex != null and sex != ''"> and sex = #{sex} </if> <if test="address != null and address != ''"> and address = #{address} </if> </where> </sql> <select id="findListUser" parameterType="user" resultType="user"> SELECT * FROM t_user <include refid="findListUserWhere"></include> --include引入sql标签 </select>
<foreach> 标签示例:
<select id="findListUserByArray" parameterType="queryVO" resultType="user"> SELECT * FROM t_user where id in <foreach collection="ids" open="(" close=")" separator="," item="id"> #{id} </foreach> </select>
<set>标签示例:
<update id="updateUser" parameterType="user"> update t_user <set> <if test="username != null and username != ''"> username=#{username}, </if> <if test="sex != null and sex != ''"> sex=#{sex}, </if> <if test="birthday != null"> birthday=#{birthday}, </if> <if test="address != null and address != ''"> address=#{address}, --set去除最后一个, </if> </set> where id = #{id} </update>
二、关联查询
关联查询,说白了就是表连接的查询。如:left join ...on ...
查询关系可分为一对一查询和一对多查询。
如用户和订单的关系:对应订单来说,每一笔订单只对应一位用户,属于一对一查询;对于用户来说,每一位用户可对于多个订单,属于一对多查询。
一对一 以订单类为主
<!-- association:构建一对一关联关系
property:表示关联查询的结果存储在com.ly.mybatis.pojo.Orders.java的user属性中
javaType:表示关联查询的结果类型,可写别名user -->
<association property="user" javaType="com.ly.mybatis.pojo.User">
<association property="user" javaType="user">
一对多 以用户类为主
<!-- collection:构建一对多关联关系
property:表示关联查询的结果存储在com.ly.mybatis.pojo.User.java的ordersList属性中
ofType:集合中包含的类型,可写别名orders -->
<collection property="ordersList" ofType="com.ly.mybatis.pojo.Orders">
<collection property="ordersList" ofType="orders">
查询方法有两种:
方法一:定义专门的POJO类作为输出类型,其中定义了sql查询结果集所有的字段。如OrdersUser这种联合类(不推荐)
方法二:使用resultMap,定义专门的resultMap用于映射一对一查询结果(官方推荐)
场景:用户与订单。每个订单只能对应一个用户,一对一查询
具体操作:1、定义Orders类,在Orders类中加入User类的属性,user属性中用于存储关联查询的用户信息,因为订单关联查询用户是一对一关系,所以这里使用单个User对象存储关联查询的用户信息。
2、OrdersMapper.xml中指定使用resultMap,且使用 构建一对一关联关系 的<association property="user" javaType="com.ly.mybatis.pojo.User">标签。
association:表示进行关联查询单条记录
property:表示关联查询的结果存储在com.ly.mybatis.pojo.Orders的user属性中,user对应Orders类中的user
javaType:表示关联查询的结果类型
<id property="id" column="user_Id"/>:查询结果的user_id列对应关联对象的id属性,这里是<id />表示user_id是关联查询对象的唯一标识。
<result property="username" column="username"/>:查询结果的username列对应关联对象的username属性。
3、结果:每笔订单对应一位用户
场景:用户与订单。每个用户可以有多笔订单,一对多查询
具体操作:1、定义User类,在User类中加入Orders类属性,Orders属性中用于存储关联查询的订单信息,因为用户关联查询订单是一对多关系,所以这里使用数组ordersList对象存储关联查询的订单信息。
2、OrdersMapper.xml中指定使用resultMap,且使用 构建一对多关联关系 的<collection property="ordersList" ofType="com.ly.mybatis.pojo.Orders">标签。
collection部分定义了用户关联的订单信息。表示关联查询结果集
property="ordersList":关联查询的结果集存储在User对象的上哪个属性。
ofType="orders":指定关联查询的结果集中的对象类型即List中的对象类型。此处可以使用别名,也可以使用全限定名。
<id />及<result/>的意义同一对一查询。
3、结果:每位用户可对应多订单