您的位置:首页 > 移动开发

【MyBatis框架】mapper配置文件-关于动态sql

2015-06-21 12:23 836 查看
动态sql

1.什么是动态sql

mybatis核心 对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接、组装。

2.需求

用户信息综合查询列表和用户信息查询列表总数这两个statement的定义使用动态sql。

对查询条件进行判断,如果输入参数不为空才进行查询条件拼接。

3.mapper.xml

原查询语句配置:
<mapper namespace="cn.edu.hpu.mybatis.mapper.UserMapper">

<!-- 用户信息综合查询
#{UserCustom.sex}取出包装对象中性别值
${UserCustom.username}取得pojo包装对象中用户名称
-->
<select id="findUserList" parameterType="cn.edu.hpu.mybatis.PO.UserQueryVo"
resultType="cn.edu.hpu.mybatis.PO.UserCustom">
select * from user where user.sex=#{userCustom.sex} and user.username like '%${userCustom.username}%'
</select>

<!-- 用户信息综合查询总数 -->
<select id="findUserCount" parameterType="cn.edu.hpu.mybatis.PO.UserQueryVo" resultType="int">
select count(*) from user where user.sex=#{userCustom.sex} and user.username like '%${userCustom.username}%'
</select>
......
</mapper>


修改后的查询语句配置:
<!-- 用户信息综合查询
#{UserCustom.sex}取出包装对象中性别值
${UserCustom.username}取得pojo包装对象中用户名称
-->
<select id="findUserList" parameterType="cn.edu.hpu.mybatis.PO.UserQueryVo"
resultType="cn.edu.hpu.mybatis.PO.UserCustom">
select * from user

<!-- where标签可以自动去掉第一个and -->
<where>
<if test="userCustom!=null">
<if test="userCustom.sex!=null and userCustom.sex!=''">
and user.sex=#{userCustom.sex}
</if>
<if test="userCustom.username!=null and userCustom.username!=''">
and user.username like '%${userCustom.username}%'
</if>
</if>
</where>
</select>

<!-- 用户信息综合查询总数 -->
<select id="findUserCount" parameterType="cn.edu.hpu.mybatis.PO.UserQueryVo" resultType="int">
select count(*) from user

<!-- where标签可以自动去掉第一个and -->
<where>
<if test="userCustom!=null">
<if test="userCustom.sex!=null and userCustom.sex!=''">
and user.sex=#{userCustom.sex}
</if>
<if test="userCustom.username!=null and userCustom.username!=''">
and user.username like '%${userCustom.username}%'
</if>
</if>
</where>
</select>
 

4.测试代码
//用户信息综合查询
@Test
public void testFindUserList() throws Exception{

SqlSession sqlSession=sqlSessionFactory.openSession();

//创建UserMapper代理对象
UserMapper userMapper=sqlSession.getMapper(UserMapper.class);

//创建包装对象,设置查询条件
UserQueryVo userQueryVo=new UserQueryVo();
UserCustom userCustom=new UserCustom();
//由于这里使用动态sql,如果这里不设置某个值,条件不会拼接在sql中
//userCustom.setSex("男");
userCustom.setUsername("张三");
userQueryVo.setUserCustom(userCustom);

//调用userMapper的方法
List<UserCustom> users=userMapper.findUserList(userQueryVo);

for (int i = 0; i < users.size(); i++) {
UserCustom user=(UserCustom)users.get(i);
System.out.println(user.getId()+":"+user.getUsername());
}
}


测试结果:

1:张三

4:张三丰

输出日志:
DEBUG [main] - Opening JDBC Connection
DEBUG [main] - Created connection 31761534.
DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.Connection@1e4a47e]
DEBUG [main] - ==>  Preparing: select * from user WHERE user.username like '%张三%'
DEBUG [main] - ==> Parameters:
DEBUG [main] - <==      Total: 2


发现sql语句为select * from user WHERE user.username like '%张三%' ,并没有将sex拼接进去,说明我们的动态sql设置成功

相应的,把userCustom.setUsername("张三");也注释掉,发现输出日志:
DEBUG [main] - Opening JDBC Connection
DEBUG [main] - Created connection 24027753.
DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.Connection@16ea269]
DEBUG [main] - ==>  Preparing: select * from user
DEBUG [main] - ==> Parameters:
DEBUG [main] - <==      Total: 5


发现sql语句为select * from user,并没有将sex和username拼接进去,说明我们的动态sql设置成功

5.sql片段

5.1需求

将上边实现的动态sql判断代码块抽取出来,组成一个sql片段。其它的statement中就可以引用sql片段。

方便程序员进行开发。

5.2定义sql片段
<mapper namespace="cn.edu.hpu.mybatis.mapper.UserMapper">

<!-- 定义sql片段
id:sql片段的唯一标识
在sql片段中不要加入where
经验:一般我们定义sql片段是为了可重用性,是基于单表来定义sql片段,
这样的话这个sql片段可重用性才高-->
<sql id="query_user_where">
<if test="userCustom!=null">
<if test="userCustom.sex!=null and userCustom.sex!=''">
and user.sex=#{userCustom.sex}
</if>
<if test="userCustom.username!=null and userCustom.username!=''">
and user.username like '%${userCustom.username}%'
</if>
</if>
</sql>
......
</mapper>


5.3引用sql片段

在mapper.xml中定义的statement中引用sql片段:
<!-- 用户信息综合查询
#{UserCustom.sex}取出包装对象中性别值
${UserCustom.username}取得pojo包装对象中用户名称
-->
<select id="findUserList" parameterType="cn.edu.hpu.mybatis.PO.UserQueryVo"
resultType="cn.edu.hpu.mybatis.PO.UserCustom">
select * from user

<!-- where标签可以自动去掉第一个and -->
<where>
<!-- 应用sql片段的id,如果refid指定的id不再本mapper文件中,需要前边加namespace -->
<include refid="query_user_where"></include>
<!-- 在这里还可能要引用其他的sql片段 -->
</where>
</select>

<!-- 用户信息综合查询总数 -->
<select id="findUserCount" parameterType="cn.edu.hpu.mybatis.PO.UserQueryVo" resultType="int">
select count(*) from user

<!-- where标签可以自动去掉第一个and -->
<where>
<!-- 应用sql片段的id,如果refid指定的id不再本mapper文件中,需要前边加namespace -->
<include refid="query_user_where"></include>
<!-- 在这里还可能要引用其他的sql片段 -->
</where>
</select>


测试:
//用户信息综合查询
@Test
public void testFindUserList() throws Exception{

SqlSession sqlSession=sqlSessionFactory.openSession();

//创建UserMapper代理对象
UserMapper userMapper=sqlSession.getMapper(UserMapper.class);

//创建包装对象,设置查询条件
UserQueryVo userQueryVo=new UserQueryVo();
UserCustom userCustom=new UserCustom();
//由于这里使用动态sql,如果这里不设置某个值,条件不会拼接在sql中
userCustom.setSex("男");
userCustom.setUsername("张三");
userQueryVo.setUserCustom(userCustom);

//调用userMapper的方法
List<UserCustom> users=userMapper.findUserList(userQueryVo);

for (int i = 0; i < users.size(); i++) {
UserCustom user=(UserCustom)users.get(i);
System.out.println(user.getId()+":"+user.getUsername());
}
}


测试结果:

1:张三

4:张三丰

输出日志:
DEBUG [main] - Opening JDBC Connection
DEBUG [main] - Created connection 17689439.
DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.Connection@10deb5f]
DEBUG [main] - ==>  Preparing: select * from user
DEBUG [main] - ==> Parameters:
DEBUG [main] - <==      Total: 5


说明sql片段引用成功

小结:
sql片段方便程序员进行开发

转载请注明出处:http://blog.csdn.net/acmman/article/details/46581349
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: