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>