Mybatis入门和配置(2)
Mybatis入门和配置(1)原文链接
动态Sql
通过mybatis提供的各种标签方法实现动态拼接sql
举例: 需求是:根据姓名和性别查询用户,有可能是姓名性别都没选中,有可能只有一个姓名或只有一个性别,或两个条件都有
if标签的使用
通过使用if标签可以实现判断各种查询情景
<select id="queryUserByWhere" parameterType="user" resultType="user">
SELECT id, username, birthday, sex, address FROM `user`
WHERE 1=1
<if test="sex != null and sex != ''">
AND sex = #{sex}
</if>
<if test="username != null and username != ''">
AND username LIKE '%${username}%'
</if>
</select>
where标签的使用
通过where标签可以对上面代码进行改造,将不用再写where和1=1的条件
<select id="queryUserByWhere" parameterType="user" resultType="user">
SELECT id, username, birthday, sex, address FROM `user`
<!-- where标签可以自动添加where,同时处理sql语句中第一个and关键字 -->
<where>
<if test="sex != null">
AND sex = #{sex}
</if>
<if test="username != null and username != ''">
AND username LIKE '%${username}%'
</if>
</where>
</select>
sql片段
通过sql标签可以实现将重复性的条件提取出来直接在sql中引入即可(include)
<!-- 声明sql片段 -->
<sql id="userFields">
id, username, birthday, sex, address
</sql>
SELECT <include refid="userFields" /> FROM `user`
foreach标签
<select id="queryUserByIds" parameterType="queryVo" resultType="user">
SELECT * FROM `user`
<where>
<!-- foreach标签,进行遍历 -->
<!-- collection:遍历的集合,这里是QueryVo的ids属性 -->
<!-- item:遍历的项目,可以随便写,,但是和后面的#{}里面要一致 -->
<!-- open:在前面添加的sql片段 -->
<!-- close:在结尾处添加的sql片段 -->
<!-- separator:指定遍历的元素之间使用的分隔符 -->
<foreach collection="ids" item="item" open="id IN (" close=")"
separator=",">
#{item}
</foreach>
</where>
</select>
关联查询
一对一查询:
需求: 查询所有订单信息,关联查询下单用户信息
方式一: 使用resultType
改造订单pojo类,此pojo类中包括了订单信息和用户信息
这样返回对象的时候,mybatis自动把用户信息也注入进来了
方式二: 使用resultMap
定义专门的resultMap用于映射一对一查询结果。
Mapper.xml配置如下:
<resultMap type="order" id="orderUserResultMap">
<id property="id" column="id" />
<result property="userId" column="user_id" />
<result property="number" column="number" />
<result property="createtime" column="createtime" />
<result property="note" column="note" />
<!-- association :配置一对一属性 -->
<!-- property:order里面的User属性名 -->
<!-- javaType:属性类型 -->
<association property="user" javaType="user">
<!-- id:声明主键,表示user_id是关联查询对象的唯一标识-->
<id property="id" column="user_id" />
<result property="username" column="username" />
<result property="address" column="address" />
</association>
</resultMap>
一对多查询:
需求: 查询所有用户信息及用户关联的订单信息
查询方法:
第一步,实体类中配置一对多关系属性
第二步,编写Mapper.xml
<resultMap type="user" id="userOrderResultMap">
<id property="id" column="id" />
<result property="username" column="username" />
<result property="birthday" column="birthday" />
<result property="sex" column="sex" />
<result property="address" column="address" />
<!-- 配置一对多的关系 -->
<collection property="orders" javaType="list" ofType="order">
<!-- 配置主键,是关联Order的唯一标识 -->
<id property="id" column="id" />
<result property="number" column="number" />
<result property="createtime" column="createtime" />
<result property="note" column="note" />
</collection>
</resultMap>
<!--配置查询-->
<select id="queryUserOrder" resultMap="userOrderResultMap">
SELECT * FROM `user` u LEFT JOIN `order` o ON u.id = o.user_id
</select>
第三步,在UserMapper接口中定义查询方法
第四步,测试