springboot+Mybatis批量增删改查

wylc123 1年前 ⋅ 1924 阅读

首先是mapper部分:

<select id="selectList" resultType="pd" parameterType="pd">
        select
        <include refid="Base_Column_List"></include>
        from description
        where 1=1
        <if test="id != null">
            AND id = #{id,jdbcType=INTEGER}
        </if>
    </select>
    <!--批量删除-->
    <delete id="deleteByPrimaryKey" parameterType="java.util.List">
        delete from description
        where id
        in
        <foreach collection="list" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <!--批量添加-->
    <insert id="insert" parameterType="java.util.List"  useGeneratedKeys="true">
        <selectKey resultType="java.lang.Integer" keyProperty="id" order="AFTER">
            SELECT
            LAST_INSERT_ID()
        </selectKey>
        insert into description
        (
        dataname, dataexplain, type, name, add_date
        )
        values
        <foreach collection="list" item="params" index="index"
                 separator=",">
            (
            #{params.dataname,jdbcType=VARCHAR},
            #{params.dataexplain,jdbcType=VARCHAR},
            #{params.type,jdbcType=VARCHAR},
            #{params.name,jdbcType=VARCHAR},
            curdate())
        </foreach>
    </insert>
    <insert id="insertSelective" parameterType="pd">//这里的pd是我们封装的工具类,类似于hashmap,可替换成map
        insert into description
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="dataname != null">
                dataname,
            </if>
            <if test="describe != null">
                describe,
            </if>
            <if test="type != null">
                type,
            </if>
            <if test="name != null">
                name,
            </if>
            <if test="addData != null">
                add_data,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id,jdbcType=INTEGER},
            </if>
            <if test="dataname != null">
                #{dataname,jdbcType=VARCHAR},
            </if>
            <if test="describe != null">
                #{describe,jdbcType=VARCHAR},
            </if>
            <if test="type != null">
                #{type,jdbcType=VARCHAR},
            </if>
            <if test="name != null">
                #{name,jdbcType=VARCHAR},
            </if>
            <if test="addData != null">
                #{addData,jdbcType=DATE},
            </if>
        </trim>
    </insert>
    <update id="updateByPrimaryKeySelective" parameterType="pd">
        update description
        <set>
            <if test="dataname != null">
                dataname = #{dataname,jdbcType=VARCHAR},
            </if>
            <if test="describe != null">
                describe = #{describe,jdbcType=VARCHAR},
            </if>
            <if test="type != null">
                type = #{type,jdbcType=VARCHAR},
            </if>
            <if test="name != null">
                name = #{name,jdbcType=VARCHAR},
            </if>
            <if test="addData != null">
                add_data = #{addData,jdbcType=DATE},
            </if>
        </set>
        where id = #{id,jdbcType=INTEGER}
    </update>
    <!--批量修改,修改不同字段,但是效率低-->
   <!-- <update id="updateByPrimaryKey" parameterType="java.util.List">
        <foreach collection="list" index="index" item="params" open="" separator=";">
        update description
            set dataname = #{params.dataname,jdbcType=VARCHAR},
                dataexplain =  #{params.dataexplain,jdbcType=VARCHAR},
            type =  #{params.type,jdbcType=VARCHAR},
            name =  #{params.name,jdbcType=VARCHAR},
            add_date = curdate()
            where id=#{params.id}
        </foreach>
    </update>-->
    <!--批量修改,只能能修改相同字段-->
    <update id="updateByPrimaryKey" parameterType="pd">
        update description
        set dataname = #{dataname,jdbcType=VARCHAR},
        dataexplain =  #{dataexplain,jdbcType=VARCHAR},
        type =  #{type,jdbcType=VARCHAR},
        name =  #{name,jdbcType=VARCHAR},
        add_date = curdate()
        where id in
        <foreach collection="id" item="params" open="(" separator="," close=")">
           #{params}
        </foreach>
    </update>

dao层:

public interface DescriptionMapper {
    void deleteByPrimaryKey(List<Integer> list);

    void insert(List<PageData> list);

    List<PageData> selectList(PageData pd);

    void updateByPrimaryKey(PageData pd);

}

测试controller:

/**
 *@Author qinwei
 * 删除元素
 *@Date:14:17 2017/11/21
 *@param: * @param null
 */
    @RequestMapping(value = "/delete", produces = "application/json;charset=UTF-8")
    public String delete() {
        PageData pd = this.getPageData();//获取参数
        List<Integer>list=new ArrayList<>();
        try {
            String params[] = pd.getString("id").split(",");//参数jie()
            for (int i = 0; i < params.length; i++) {
             list.add(Integer.valueOf(params[i]));
            }
            descriptionService.deleteByPrimaryKey(list);
            return JSONUtil.JsonString(new JsonResult(1, "删除成功!", null));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("错误:"+e.toString());
            return JSONUtil.JsonString(new JsonResult(1, "服务器内部错误!", null));
        }
    }
    /**
     *@Author qinwei
     * 批量添加数据元素
     *@Date:14:51 2017/11/21
     *@param: * @param null
     */
    @RequestMapping(value = "/insert", produces = "application/json;charset=UTF-8")
    public String add(){
        PageData pd=this.getPageData();
        List<PageData>list=new ArrayList<>();
        try {
            pd.put("dataname","1");
            pd.put("dataexplain","1");
            pd.put("type","1");
            pd.put("name","1");
            list.add(pd);
            pd.put("dataname","1");
            pd.put("dataexplain","1");
            pd.put("type","1");
            pd.put("name","1");
            list.add(pd);
            descriptionService.insert(list);
            return JSONUtil.JsonString(new JsonResult(1, "添加成功!", null));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("错误:"+e.toString());
            return JSONUtil.JsonString(new JsonResult(1, "服务器内部错误!", null));
        }
    }
    /**
     *@Author qinwei
     * 批量添加数据元素
     *@Date:14:51 2017/11/21
     *@param: * @param null
     */
    @RequestMapping(value = "/update", produces = "application/json;charset=UTF-8")
    public String update(){
        PageData pd=this.getPageData();
        List<Integer>list=new ArrayList<>();
        try {
            list.add(1);
            list.add(64);
            pd.put("dataname","46546464645");
            pd.put("dataexplain","12");
            pd.put("type","1");
            pd.put("name","1");
            pd.put("id",list);

            descriptionService.updateByPrimaryKey(pd);
            return JSONUtil.JsonString(new JsonResult(1, "修改成功!", null));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("错误:"+e.toString());
            return JSONUtil.JsonString(new JsonResult(1, "修改失败!", null));
        }
    }
更多内容请访问:IT源点

相关文章推荐

全部评论: 0

    我有话说: