SpringBoot整合MyBaitis增删改查

Goods实体类

 yml配置文件

GoodsMapper接口定义

/**
 * 查询商品信息
 * @return
 */
List<Goods> findAll();

/**
 * 删除商品信息
 * @param id
 * @return
 */
int del(int id);

/**
 * 新增商品信息
 * @param goods
 * @return
 */
int save(Goods goods);

/**
 * 根据id查询商品信息
 * @param id
 * @return
 */
Goods findById(int id);

/**
 * 修改商品信息
 * @param goods
 * @return
 */
int update(Goods goods);

controller类

package com.cjb.springboot02.controller;

import com.cjb.springboot02.pojo.Goods;
import com.cjb.springboot02.service.GoodsService;
import com.cjb.springboot02.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @RequestMapping("findAll")
    @ResponseBody
    public R findAll(){
        List<Goods> all = goodsService.findAll();
        System.out.println(all.toString());
        return R.ok().code(200).data("all",all);
    }
    @RequestMapping("findById")
    @ResponseBody
    public R findById(@RequestParam("id") int id){
        Goods byId = goodsService.findById(id);
        return R.ok().code(200).data("byId",byId);
    }
    @RequestMapping("del")
    @ResponseBody
    public R del(@RequestParam("id") int id){
        int count = goodsService.del(id);
        return R.ok().code(200).data("count",count);
    }
    @RequestMapping("save")
    @ResponseBody
    public R save(Goods goods){
        int count = goodsService.save(goods);
        return R.ok().code(200).data("count",count);
    }
    @RequestMapping("update")
    @ResponseBody
    public R update(Goods goods){
        int count = goodsService.update(goods);
        return R.ok().code(200).data("count",count);
    }
}

xml文件

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.cjb.springboot02.mapper.GoodsMapper">

    <select id="findAll" resultType="Goods">
        select * from goods
    </select>

    <select id="findById" resultType="Goods">
        select * from goods where goodsId = #{goodsId}
    </select>

    <update id="update" parameterType="goods">
        update goods
        <set>
            <if test="goodsName!=null">
                goodsName = #{goodsName},
            </if>
            <if test="goodsPrice!=null">
                goodsPrice = #{goodsPrice},
            </if>
            <if test="goodsNum!=null">
                goodsNum = #{goodsNum},
            </if>
            <if test="goodsType!=null">
                goodsType = #{goodsType},
            </if>
        </set>
        where goodsId = #{goodsId}
    </update>

    <insert id="save" parameterType="goods">
        INSERT INTO goods(goodsName,goodsPrice,goodsNum,goodsType)
            VALUES (#{goodsName},#{goodsPrice},#{goodsNum},#{goodsType})
    </insert>

    <delete id="del" parameterType="int">
        delete from goods where goodsId = #{goodsId}
    </delete>

</mapper>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值