【MyBatis框架】mapper配置文件-关于动态sql
2016-08-08 15:46
525 查看
转载:http://blog.csdn.net/acmman/article/details/46581349
动态sql
1.什么是动态sql
mybatis核心 对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接、组装。
2.需求
用户信息综合查询列表和用户信息查询列表总数这两个statement的定义使用动态sql。
对查询条件进行判断,如果输入参数不为空才进行查询条件拼接。
3.mapper.xml
原查询语句配置:
[html] view
plain copy
<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>
修改后的查询语句配置:
[html] view
plain copy
<!-- 用户信息综合查询
#{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.测试代码
[java] view
plain copy
//用户信息综合查询
@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:张三丰
输出日志:
[plain] view
plain copy
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("张三");也注释掉,发现输出日志:
[plain] view
plain copy
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片段
[html] view
plain copy
<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片段:
[html] view
plain copy
<!-- 用户信息综合查询
#{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>
测试:
[java] view
plain copy
//用户信息综合查询
@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:张三丰
输出日志:
[plain] view
plain copy
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片段方便程序员进行开发
动态sql
1.什么是动态sql
mybatis核心 对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接、组装。
2.需求
用户信息综合查询列表和用户信息查询列表总数这两个statement的定义使用动态sql。
对查询条件进行判断,如果输入参数不为空才进行查询条件拼接。
3.mapper.xml
原查询语句配置:
[html] view
plain copy
<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>
修改后的查询语句配置:
[html] view
plain copy
<!-- 用户信息综合查询
#{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.测试代码
[java] view
plain copy
//用户信息综合查询
@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:张三丰
输出日志:
[plain] view
plain copy
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("张三");也注释掉,发现输出日志:
[plain] view
plain copy
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片段
[html] view
plain copy
<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片段:
[html] view
plain copy
<!-- 用户信息综合查询
#{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>
测试:
[java] view
plain copy
//用户信息综合查询
@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:张三丰
输出日志:
[plain] view
plain copy
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片段方便程序员进行开发
相关文章推荐
- 【MyBatis框架】mapper配置文件-关于动态sql
- 【MyBatis框架】mapper配置文件-关于动态sql
- 【MyBatis框架】mapper配置文件-关于动态sql
- mapper配置文件-关于动态sql
- MyBatis框架的学习(四)——Mapper.xml文件中的输入和输出映射以及动态sql
- (转)MyBatis框架的学习(四)——Mapper.xml文件中的输入和输出映射以及动态sql
- MyBatis框架的学习(四)——Mapper.xml文件中的输入和输出映射以及动态sql
- ssm(Spring+Spring mvc+mybatis)Dao层配置sql的文件——DeptDaoMapper.xml
- 【SSM-MyBatis框架】Mapper.xml配置文件(mybatis的核心)
- mybatis的mapper配置文件配置oracle的判断是否成年的sql简洁写法。
- Mybatis学习之----手写配置文件时如何进行动态sql配置
- MyBatis(三)配置文件,输入输出映射,动态SQL
- ssm项目使用Mybatis动态拼接sql语句,生成的sql中文全部显示为???的问题(配置文件sql语句中文解析问题)
- 关于PHP-Zend framework2 框架 学习过程。 阅前须知: ZF2中的配置文件是可以静态文件配置来注册和通过相关函数动态注册。 1.EventManager(事件驱动),关于事件驱动,在ZF2相关资料没有详细说明,可以参考ANDROID的事件驱动,MFC的消息响应/事件驱动。
- SSM框架之Mybatis动态sql配置
- 03_MyBatis基本查询,mapper文件的定义,测试代码的编写,resultMap配置返回值,sql片段配置,select标签标签中的内容介绍,配置使用二级缓存,使用别名的数据类型,条件查询ma
- 【MyBatis框架】mapper配置文件-foreach标签
- 03_MyBatis基本查询,mapper文件的定义,测试代码的编写,resultMap配置返回值,sql片段配置,select标签标签中的内容介绍,配置使用二级缓存,使用别名的数据类型,条件查询ma
- MyBatis框架核心之(二)Mapper配置文件使用接口映射
- 【MyBatis框架】mapper配置文件-foreach标签