【JAVA - SSM】之MyBatis动态SQL

        动态SQL就是在SQL语句中添加一些标签,以完成某些逻辑。通常用到的动态SQL标签有<if>、<choose>、<where>、<trim>、<set>、<foreach>、<bind>、<sql>等。


1、if

        if是简单的条件判断,通过if语句我们可以实现某些简单的条件选择,一个例子的代码如下:

<select id="dynamicIfTest" parameterType="Blog" resultType="Blog">  
    select * from t_blog where 11 = 1  
    <if test="title != null">  
        and title = #{title}  
    </if>  
    <if test="content != null">  
        and content = #{content}  
    </if>  
    <if test="owner != null">  
        and owner = #{owner}  
    </if>  
</select>


2、choose

        choose标签的作用就相当于JAVA中的switch语句,只不过不是使用case与default搭配,而是使用when和otherwise搭配。一个例子的代码如下:

<select id="dynamicChooseTest" parameterType="Blog" resultType="Blog">  
    select * from t_blog where 11 = 1   
    <choose>  
        <when test="title != null">  
            and title = #{title}  
        </when>  
        <when test="content != null">  
            and content = #{content}  
        </when>  
        <otherwise>  
            and owner = "owner1"  
        </otherwise>  
    </choose>  
</select>
        choose语句和JAVA中的switch语句类似,都是按顺序从上向下判断,一旦有某个when的条件满足的时候,就会跳出choose,当所有when中的条件都不满足,就会执行otherwise中的语句。


3、trim

        trim标签的作用是可以在自己包含的内容前后加上前缀或后缀,与之对应的属性是prefix和suffix;trim标签也可以把包含内容中首、尾部的某些内容覆盖(即忽略),对应的属性是prefixOverrides和suffixOverrides。一个例子的代码如下:

<select id="dynamicTrimTest" parameterType="Blog" resultType="Blog">  
    select * from t_blog   
    <trim prefix="where" prefixOverrides="and|or">  
        <if test="title != null">  
            title = #{title}  
        </if>  
        <if test="content != null">  
            and content = #{content}  
        </if>  
        <if test="owner != null">  
            or owner = #{owner}  
        </if>  
    </trim>  
</select>
        上面这段代码的意思是:在这段代码的最前面加一个前缀where,然后把可能位于最前面的and或or给覆盖(忽略)掉。正因为trim标签有这样的功能,所以我们也可以非常简单的利用trim来代替where元素的功能。


4、where

        where标签的作用是为了简化SQL语句中where的条件判断的,一个例子的代码如下:

<select id="dynamicWhereTest" parameterType="Blog" resultType="Blog">  
    select * from t_blog   
    <where>  
        <if test="title != null">  
            and title = #{title}  
        </if>  
        <if test="content != null">  
            and content = #{content}  
        </if>  
        <if test="owner != null">  
            and owner = #{owner}  
        </if>  
    </where>  
</select>
        where标签的作用是会在写<where>的地方自动输出一个where,即省略了SQL语句中的where关键字;像上面这段动态SQL,假如第一个判断成立,那么MyBatis为我们生成的SQL语句是:select * from t_blog where title = #{title}; ,而不是:select * from t_blog where and title = #{title},即MyBatis的动态SQL会自动把第一个多余的and去掉,如果将这里的and换成or,也会有同样的效果。

5、set

        set标签主要用在更新操作的时候,它的功能和where元素差不多,主要是在包含的语句最前面添加一个set前缀,然后如果所包含的语句是以逗号结尾的话就将该逗号忽略,如果set包含的内容为空的话就会报错。一个例子的代码如下:

<update id="dynamicSetTest" parameterType="Blog">  
    update t_blog  
    <set>  
        <if test="title != null">  
            title = #{title},  
        </if>  
        <if test="content != null">  
            content = #{content},  
        </if>  
        <if test="owner != null">  
            owner = #{owner}  
        </if>  
    </set>  
    where id = #{id}  
</update>
        在上面这段代码中,如果set中的三个判断都不成立,即set中的内容为空,那么就会报错。有了set标签,我们就可以动态的更新那些修改了的字段了。


6、foreach

        foreach标签主要用于构建in条件,它可以在SQL语句中迭代一个集合。foreach标签的属性有item、index、collection、open、separator、close,其中,item表示集合中的元素进行迭代时的别名;index指定当前迭代的位置;open指定这段SQL的前缀;separator指定每个迭代元素之间的分隔符;close指定这段SQL的后缀;collection指定集合类型:

    (1)如果传入List列表,则collection的属性值为list,示例代码如下:

<select id="dynamicForeachTest" resultType="Blog">  
    select * from t_blog where id in  
    <foreach collection="list" index="index" item="item" open="(" separator="," close=")">  
        #{item}  
    </foreach>  
</select>

    (2)如果传入数组,则collection的属性值为array,示例代码如下:

<select id="dynamicForeach2Test" resultType="Blog">  
    select * from t_blog where id in  
    <foreach collection="array" index="index" item="item" open="(" separator="," close=")">  
        #{item}  
    </foreach>  
</select>

    (3)如果传入Map集合,则collection的属性值是Map集合的key,示例代码如下(在这个例子中,Map中存储着一个key是ids的List):

<select id="dynamicForeach3Test" resultType="Blog">  
    select * from t_blog where title like "%"#{title}"%" and id in  
    <foreach collection="ids" index="index" item="item" open="(" separator="," close=")">  
        #{item}  
    </foreach>  
</select>

    (4)如果是Set集合,且Set中每个元素的类型是Map.Entry时,则collection的属性值为collection,此时index属性代表Map.Entry的key,item属性代表Map.Entry的value。示例代码如下:

<select id="dynamicForeachTest" resultType="Blog">  
    select * from t_blog where id in  
    <!-- 遍历的对象是Map.Entry时,index代表对应的key,item代表对应的value -->  
    <foreach collection="collection" index="key" item="value" open="(" separator="," close=")">  
        #{key}, #{value}  
    </foreach>  
</select>


7、bind

        bind标签主要用于模糊查询的字符串拼接,一个例子的代码如下:

<select id="fuzzyQuery" resultType="Blog" parameterType="java.lang.String">
    <bind name="titleLike" value="'%'+_parameter+'%'"/>  
    select * from t_blog where title like #{titleLike}    
</select>

8、SQL

        有时,如果动态SQL中包含的代码过长,且有可能在不同的SQL语句中重复用到,那么就可以将这段SQL提取出来作为SQL片段(也相当于封装)。抽取出来的SQL语句使用<sql>标签包裹,在使用到某个SQL片段时,使用<include>标签引入SQL片段。一个例子的代码如下:

<!-- 被抽取出来的SQL片段(id是这个SQL片段的唯一标识) -->
<!-- 注意:SQL片段一般都是基于单表创建的;SQL片段中最好不要包括where语句 -->
<sql id="blog_query">
	<if test="title!=null">
		and title = #{title}
	</if>
</sql>

<select id="findEmplyeeListDynamicSQL" parameterType="Blog" resultType="Blog">
	SELECT * FROM t_blog
	<where>
		<!-- 引用抽取出来的SQL片段。如果要引用的SQL片段在其他mapper文件中,则需要在前面添加namespace -->
		<include refid="blog_query"></include>
	</where>
</select>



  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的Java-SSM框架图书管理系统的制作流程: 1. 确定需求:根据图书管理系统的需求确定模块和功能,比如图书信息管理、借阅管理、用户管理、权限管理等。 2. 设计数据库:根据需求设计数据库,包括图书信息表、借阅信息表、用户信息表等。 3. 搭建开发环境:安装JDK、Maven、Tomcat等开发工具和环境。 4. 创建项目:使用Maven创建Java-SSM框架项目。 5. 配置框架:配置Spring、SpringMVC和Mybatis框架,包括配置数据源、事务管理等。 6. 编写代码:根据需求和数据库设计,编写Java代码实现系统功能,包括后端的业务逻辑和前端的页面展示。 7. 测试调试:对系统进行测试和调试,确保功能正常。 8. 部署上线:将系统部署到服务器上线,供用户使用。 具体实现的细节可以参考以下的步骤: 1. 创建Maven项目:使用Maven创建一个Java-SSM框架的项目。 2. 配置pom.xml文件:在pom.xml文件中添加所需的依赖,包括Spring、SpringMVC和Mybatis等。 3. 配置web.xml文件:配置web.xml文件,包括DispatcherServlet和ContextLoaderListener等。 4. 配置Spring配置文件:在Spring配置文件中配置数据源、事务管理等。 5. 配置Mybatis配置文件:在Mybatis配置文件中配置数据源和SQL映射文件等。 6. 编写Java代码:根据需求和数据库设计,编写Java代码实现系统功能,包括后端的业务逻辑和前端的页面展示。 7. 测试调试:对系统进行测试和调试,确保功能正常。 8. 部署上线:将系统部署到服务器上线,供用户使用。 以上是一个简单的Java-SSM框架图书管理系统的制作流程,具体实现还需要根据您的实际情况进行调整。希望对您有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值