mybatis使用if 和foreach, mybatis插入数据返回主键

mybatis使用if进行选择性操作:

我们在修改数据库字段的时候,有可能需要对多个字段同时操作,也又可能需要操作某一个字段,这就需要我们编写多个不同的sql片段进行数据操作,这样就会造成代码的重复编写:
所以这时我们可以使用if进行判断组装sql片段:代码如下:

"updateGoodsByIdAll" parameterType="java.util.HashMap">
        UPDATE `test`.`goods`
            SET 
             <if test="id != null and id != ''">
                `id` = #{id}
             if>
             <if test="num != null and num != 0">
                ,`num` = #{num}
             if>
             <if test="price != null">
                ,`price` = #{price}
             if>

             <if test="statu != null">
                ,`statu` = #{statu}
             if>

             <if test="name != null">
                ,`name` = #{name}
             if>

            WHERE
                (`id` = #{id})
    

foreach语法:我们使用使用mybatis的foreach语句主要因为我们传递的参数有可能是一个数组,
语法如下:

id="updateGoodsByIdAll2">
        update goods set name='zhangsan' where id in 
        "list" open="(" close=")" separator="," item="ids">
            #{ids}
        
    

collection = list : 传输类型为集合
open: 循环以 ( 开始
close: 循环以 ) 结束
separator = “,”: 表示数组元素遍历出来以逗号隔开

ArrayList zhangsan = new ArrayList();
        zhangsan.add(5);
        zhangsan.add(6);
        zhangsan.add(7);
        zhangsan.add(8);
        session.update("com.xingxue.dao.GoodsMapper.updateGoodsByIdAll2", zhangsan);
        session.commit();

我们传入的数组名和sql片段没有任何关系。

对于mybatis来说,还提供了很多比较实用的标签,但是实际实用不多,

where标签就是用于替换我们sql的where的关键字的
<select id="selectGoodsById2" parameterType="java.util.HashMap" resultType="java.util.HashMap">
        select * from goods 
        <where>
            id = #{id}
        where>
    select>

set标签就是用于替换我们sql的set关键字的

"updateGoodById3" parameterType="java.util.HashMap">
        UPDATE `test`.`goods`
            
             <if test="id != null and id != ''">
                `id` = #{id}
             if>
             <if test="num != null and num != 0">
                ,`num` = #{num}
             if>
             <if test="price != null">
                ,`price` = #{price}
             if>
             <if test="statu != null">
                ,`statu` = #{statu}
             if>
             <if test="name != null">
                ,`name` = #{name}
             if>
             
            WHERE
                (`id` = #{id})
    

trim用户替换特殊标记:

"updateGoodById4" parameterType="java.util.HashMap">
        UPDATE `test`.`goods`
            "SET" prefixOverrides=",">

             <if test="num != null and num != 0">
                ,`num` = #{num}
             if>
             <if test="price != null">
                ,`price` = #{price}
             if>
             <if test="statu != null">
                ,`statu` = #{statu}
             if>
             <if test="name != null">
                ,`name` = #{name}
             if>
            

            WHERE
                (`id` = #{id})
    

choose就是一个升级版本的if..else

        
            <when test="studentName!=null and studentName!='' ">     
                    ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')      
            when>     
            <when test="studentSex!= null and studentSex!= '' ">     
                    AND ST.STUDENT_SEX = #{studentSex}      
            when>     
            <when test="studentBirthday!=null">     
                AND ST.STUDENT_BIRTHDAY = #{studentBirthday}      
            when>     
                

                 
             

注意if是每个都要判断,choose是满足了一个,其他就不是进行判断。

mybatis插入数据返回主键

mysql返回主键

"insertGoodsGetId" parameterType="java.util.HashMap">

        "goodsId" order="AFTER" resultType="int">
            SELECT LAST_INSERT_ID() 
        
        INSERT INTO `test`.`goods` (
            `id`,
            `name`,
            `price`,
            `num`,
            `statu`,
            `typeid`,
            `createdate`
        )
        VALUES
            (
                null,
                #{name},
                '300.00',
                '300',
                '1',
                '2',
                '2018-07-10 18:10:13'
            );
    

java代码:

param.put("goodsId", null);
        param.put("name", "shuyunhao");
        int result = session.insert("com.xingxue.dao.GoodsMapper.insertGoodsGetId", param);
        System.out.println(result);
        System.out.println(param);
        session.commit();

oracle的方式:

"goodsId" order="AFTER" resultType="int">
            select goods_seq.nextval from dua;
        
        INSERT INTO `test`.`goods` (
            `id`,
            `name`,
            `price`,
            `num`,
            `statu`,
            `typeid`,
            `createdate`
        )
        VALUES
            (
                null,
                #{name},
                '300.00',
                '300',
                '1',
                '2',
                '2018-07-10 18:10:13'
            );
    

注意,oracle和mysql获取时,执行的sql不一样。

你可能感兴趣的:(mybatis)