Mybatis深入了解----动态SQL

什么是动态SQL?

===============================================================================


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

实例

========================================================================


用户信息综合查询列表和用户信息查询列表总数这两个statement的定义使用动态sql。对查询条件进行判断,如果输入参数不为空才进行查询条件拼接。

Mapper.xml

================================================================================


<!-- 用户信息综合查询

#{userCustom.sex}:取出pojo包装对象中性别值

${userCustom.username}:取出pojo包装对象中用户名称

–>

<select id=“findUserList” parameterType=“cn.itcast.mybatis.po.UserQueryVo”

resultType=“cn.itcast.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.itcast.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>

测试代码

==========================================================================


//用户信息的综合 查询

@Test

public void testFindUserList() throws Exception {

SqlSession sqlSession = sqlSessionFactory.openSession();

//创建UserMapper对象,mybatis自动生成mapper代理对象

UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

//创建包装对象,设置查询条件

UserQueryVo userQueryVo = new UserQueryVo();

UserCustom userCustom = new UserCustom();

//由于这里使用动态sql,如果不设置某个值,条件不会拼接在sql中

//userCustom.setSex(“1”);

userCustom.setUsername(“小明”);

userQueryVo.setUserCustom(userCustom);

//调用userMapper的方法

List list = userMapper.findUserList(userQueryVo);

System.out.println(list);

}

sql片段

===========================================================================


将上边实现的动态sql判断代码块抽取出来,组成一个sql片段。其它的statement中就可以引用sql片段。方便程序员进行开发。

定义sql片段


<!-- 定义sql片段

id:sql片段的唯 一标识

经验:是基于单表来定义sql片段,这样话这个sql片段可重用性才高

在sql片段中不要包括 where

–>

<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>

引用sql片段


<!-- 用户信息综合查询

#{userCustom.sex}:取出pojo包装对象中性别值

${userCustom.username}:取出pojo包装对象中用户名称

–>

<select id=“findUserList” parameterType=“cn.itcast.mybatis.po.UserQueryVo”

resultType=“cn.itcast.mybatis.po.UserCustom”>

SELECT * FROM USER

<!–

where可以自动去掉条件中的第一个and

–>

<where>

<include refid=“query_user_where”></include>

</where>

</select>

其中的关键是这一句引用:

<include refid=“query_user_where”></include>

foreach

=============================================================================


  • 16
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值