mybatis动态sql

OGNL

if

想要在mybatis中根据属性来查找(比如根据学生的姓名 , 学生的编号)
原来需要在mapper接口中新写两个方法selectByname 和 selectByid
十分麻烦~
但是我们出现了十分方便的动态sql
在mapper接口上用一个方法
slectBynameOrid(Student student)
在xml文件上就可以这样写

 <select id="selectnewByidOrnewrong" parameterType="mybatis01.pojo.news" resultType="mybatis01.pojo.news">
 		//这里是语句
 		//如果填入id就连接第一个为:select * from news WHERE 1=1 and  id = #{id}
 		//如果填入newrong为select * from news WHERE 1=1 and newrong = #{newrong}
 		//都填入为select * from news WHERE 1=1 and  id = #{id} and newrong = #{newrong}
 		//都不填为select * from news WHERE 1=1 
        SELECT * from news WHERE 1=1
        <if test="id != null">
            AND id = #{id}
        </if>
        <if test="newrong != null">
           and newrong = #{newrong}
        </if>
    </select>

where

where一般就配合上边的if使用啦
刚才的语句

 SELECT * from news WHERE 1=1
        <if test="id != null">
            AND id = #{id}
        </if>
        <if test="newrong != null">
           and newrong = #{newrong}
        </if>

其中的where 1=1 感觉十分的low 还不易阅读
这时候如果使用where就会10分的拉风帅气啦

 <select id="selectnewByidOrnewrong" parameterType="mybatis01.pojo.news" resultType="mybatis01.pojo.news">
 		//取消了where 1=1 使用了<where> 标签 原理差不多 其中where 会将连接到的语句第一个and/or删除尾插
        SELECT * from news
        <where>
            <if test="id != null">
                AND id = #{id}
            </if>
            <if test="newrong != null">
                AND newrong = #{newrong}
            </if>
        </where>
    </select>

trim

可以取出指定的and/or语句 where和set标签也可以写在里面
在这里插入图片描述
是不是听不懂 我也是!! , 但是当我看到下面的例子是,一切都迎刃而解了

<select id="selectnewByidOrnewrong" parameterType="mybatis01.pojo.news" resultType="mybatis01.pojo.news">
		//当下面的if语句结果为true时 填入prifix的内容 消除遇到的第一个and
		//效果和上述的一模一样
        SELECT * from news
        <trim prefix="where" prefixOverrides="and">
            <if test="id != null">
                AND id = #{id}
            </if>
            <if test="newrong != null">
                AND newrong = #{newrong}
            </if>
        </trim>
    </select>

foreach

用于遍历
让我们举一个栗子,让我们通过一群id来查看是否有他
sql语句为:

select * from news where id in (1,2,3,4,5)

转化为标签为

 <select id="batcQuerynewByid" resultType="news">
 		//collection为list , map , set    , item 为每个的名字 , separator为分隔符 open为开始符 , close为结束符
        SELECT * from news WHERE id IN
        <foreach collection="list" item="id" separator="," open="(" close=")">
            #{id}
        </foreach>
    </select>

插入多个数据
sql语句为:

INSERT INTO news VALUES (1, “a”) , (2 , “b”) , (3 , “c”);

<insert id="charupilaing">
		//用()来包裹
        INSERT INTO news VALUES
        <foreach collection="list" item="new" separator=",">
            (#{new.id} , #{new.newrong})
        </foreach>
    </insert>

模糊查询

第一种使用sql中的语句:

 <select id="selectbyname" parameterType="String" resultType="news">
        SELECT * from news WHERE newrong like concat('%' , #{newrong} , '%')
    </select>

第二种使用bind标签:

<select id="selectbyname" parameterType="news" resultType="news">
        <bind name="ln" value="'%'+newrong+'%'"/>
        SELECT * from news WHERE newrong like #{ln}
    </select>

bind和其他OGNL语句一样需要插入的需要getter方法所以parameterType入参必须是有getter方法的String就不行


版权声明:本文为xasasaa原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。