springboot开发读取信息得到商品列表
---------------------------------------------------------------------------------------------------------
ItemDOMapper.java
---------------------------------------------------------------------------------------------------------
package com.miaoshaproject.dao;
import java.util.List;
import com.miaoshaproject.dataobject.ItemDO;
public interface ItemDOMapper {
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table item
*
* @mbg.generated
*/
List<ItemDO> listItem();
int deleteByPrimaryKey(Integer id);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table item
*
* @mbg.generated
*/
int insert(ItemDO record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table item
*
* @mbg.generated
*/
int insertSelective(ItemDO record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table item
*
* @mbg.generated
*/
ItemDO selectByPrimaryKey(Integer id);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table item
*
* @mbg.generated
*/
int updateByPrimaryKeySelective(ItemDO record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table item
*
* @mbg.generated
*/
int updateByPrimaryKey(ItemDO record);
}
---------------------------------------------------------------------------------------------------------
ItemServiceImpl.java
---------------------------------------------------------------------------------------------------------
package com.miaoshaproject.service.impl;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.miaoshaproject.dao.ItemDOMapper;
import com.miaoshaproject.dao.ItemStockDOMapper;
import com.miaoshaproject.dataobject.ItemDO;
import com.miaoshaproject.dataobject.ItemStockDO;
import com.miaoshaproject.error.BusinessException;
import com.miaoshaproject.error.EmBusinessError;
import com.miaoshaproject.service.ItemService;
import com.miaoshaproject.service.model.ItemModel;
import com.miaoshaproject.validator.ValidationResult;
import com.miaoshaproject.validator.ValidatorImpl;
@Service
public class ItemServiceImpl implements ItemService {
@Autowired
private ValidatorImpl validator;
@Autowired
private ItemDOMapper itemDOMapper;
@Autowired
private ItemStockDOMapper itemStockDOMapper;
public List<ItemModel> listItem(){
List<ItemDO> itemDOList=itemDOMapper.listItem();
List<ItemModel> itemModelList=itemDOList.stream().map(itemDO->{
ItemStockDO itemStockDO =itemStockDOMapper.selectByItemId(itemDO.getId());
ItemModel itemModel=this.convertModelFromDataObject(itemDO, itemStockDO);
return itemModel;
}).collect(Collectors.toList());
return itemModelList;
}
private ItemDO convertItemDOFromItemModel(ItemModel itemModel) {
if(itemModel==null) {
return null;
}
ItemDO itemDO=new ItemDO();
BeanUtils.copyProperties(itemModel, itemDO);
itemDO.setPrice(itemModel.getPrice().doubleValue());
return itemDO;
}
private ItemStockDO convertItemStockFromItemModel(ItemModel itemModel) {
if(itemModel==null) {
return null;
}
ItemStockDO itemStockDO=new ItemStockDO();
itemStockDO.setItemId(itemModel.getId());
itemStockDO.setStock(itemModel.getStock());
return itemStockDO;
}
@Transactional
public ItemModel createItem(ItemModel itemModel) throws BusinessException {
//校验入参
ValidationResult result=validator.validate(itemModel);
if(result.isHasErrors()) {
throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR,result.getErrMsg());
}
//转化itemmodel-》dataobject
ItemDO itemDO=this.convertItemDOFromItemModel(itemModel);
//写入数据库
itemDOMapper.insertSelective(itemDO);
itemModel.setId(itemDO.getId());
ItemStockDO itemStockDO=this.convertItemStockFromItemModel(itemModel);
itemStockDOMapper.insertSelective(itemStockDO);
//返回对象
return this.getItemById(itemModel.getId());
}
public ItemModel getItemById(Integer id) {
ItemDO itemDO=itemDOMapper.selectByPrimaryKey(id);
if(itemDO==null) {
return null;
}
//操作获取库存数量
ItemStockDO itemStockDO=itemStockDOMapper.selectByItemId(id);
//dataobject->model
ItemModel itemModel =convertModelFromDataObject(itemDO, itemStockDO);
return itemModel;
}
private ItemModel convertModelFromDataObject(ItemDO itemDO,ItemStockDO itemStockDO) {
ItemModel itemModel =new ItemModel();
BeanUtils.copyProperties(itemDO, itemModel);
itemModel.setPrice(new BigDecimal(itemDO.getPrice()));
itemModel.setStock(itemStockDO.getStock());
return itemModel;
}
}
---------------------------------------------------------------------------------------------------------
ItemController.java
---------------------------------------------------------------------------------------------------------
package com.miaoshaproject.controller;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.miaoshaproject.controller.viewobject.ItemVO;
import com.miaoshaproject.error.BusinessException;
import com.miaoshaproject.response.CommonReturnType;
import com.miaoshaproject.service.ItemService;
import com.miaoshaproject.service.model.ItemModel;
@Controller("/item")
@RequestMapping("/item")
//解决跨域问题
@CrossOrigin(allowCredentials="true",origins= {"*"})
public class ItemController extends BaseController{
@Autowired
private ItemService itemService;
//商品列表
@RequestMapping(value="/list",method= {RequestMethod.GET})
@ResponseBody
public CommonReturnType listItem(){
List<ItemModel> itemModelList=itemService.listItem();
//使用stream api将list中的itemModel变成ItemVO
List<ItemVO> itemVOList=itemModelList.stream().map(itemModel->{
ItemVO itemVO=this.convertVOFromModel(itemModel);
return itemVO;
}).collect(Collectors.toList());
return CommonReturnType.create(itemVOList);
}
//创建商品控制器
@RequestMapping(value="/create",method= {RequestMethod.POST},consumes= {CONTENT_TYPE_FORMED})
@ResponseBody
public CommonReturnType createItem(@RequestParam(name="title")String title,
@RequestParam(name="description")String description,
@RequestParam(name="price")BigDecimal price,
@RequestParam(name="stock")Integer stock,
@RequestParam(name="imgUrl")String imgUrl) throws BusinessException {
//封装service用来创建商品
ItemModel itemModel=new ItemModel();
itemModel.setTitle(title);
itemModel.setDescription(description);
itemModel.setImgUrl(imgUrl);
itemModel.setStock(stock);
itemModel.setPrice(price);
ItemModel itemModelForReturn=itemService.createItem(itemModel);
ItemVO itemVO=convertVOFromModel(itemModelForReturn);
return CommonReturnType.create(itemVO);
}
//获取商品
@RequestMapping(value="/get",method= {RequestMethod.GET})
@ResponseBody
public CommonReturnType getItem(@RequestParam(name="id")Integer id) throws BusinessException {
ItemModel itemModel=itemService.getItemById(id);
ItemVO itemVO=convertVOFromModel(itemModel);
return CommonReturnType.create(itemVO);
}
private ItemVO convertVOFromModel(ItemModel itemModel) {
if(itemModel==null) {
return null;
}
ItemVO itemVO=new ItemVO();
BeanUtils.copyProperties(itemModel, itemVO);
return itemVO;
}
}
---------------------------------------------------------------------------------------------------------
ItemDOMapper.xml
---------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.miaoshaproject.dao.ItemDOMapper">
<resultMap id="BaseResultMap" type="com.miaoshaproject.dataobject.ItemDO">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
<id column="id" jdbcType="INTEGER" property="id" />
<result column="title" jdbcType="VARCHAR" property="title" />
<result column="price" jdbcType="DOUBLE" property="price" />
<result column="description" jdbcType="VARCHAR" property="description" />
<result column="sales" jdbcType="INTEGER" property="sales" />
<result column="img_url" jdbcType="VARCHAR" property="imgUrl" />
</resultMap>
<sql id="Base_Column_List">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
id, title, price, description, sales, img_url
</sql>
<select id="listItem" resultMap="BaseResultMap">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
select
<include refid="Base_Column_List" />
from item order by sales DESC;
</select>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
select
<include refid="Base_Column_List" />
from item
where id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
delete from item
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.miaoshaproject.dataobject.ItemDO" useGeneratedKeys="true" keyProperty="id">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
insert into item (id, title, price, description, sales, img_url)
values (#{id,jdbcType=INTEGER}, #{title,jdbcType=VARCHAR}, #{price,jdbcType=DOUBLE},
#{description,jdbcType=VARCHAR}, #{sales,jdbcType=INTEGER}, #{imgUrl,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" parameterType="com.miaoshaproject.dataobject.ItemDO" useGeneratedKeys="true" keyProperty="id">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
insert into item
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="title != null">
title,
</if>
<if test="price != null">
price,
</if>
<if test="description != null">
description,
</if>
<if test="sales != null">
sales,
</if>
<if test="imgUrl != null">
img_url,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=INTEGER},
</if>
<if test="title != null">
#{title,jdbcType=VARCHAR},
</if>
<if test="price != null">
#{price,jdbcType=DOUBLE},
</if>
<if test="description != null">
#{description,jdbcType=VARCHAR},
</if>
<if test="sales != null">
#{sales,jdbcType=INTEGER},
</if>
<if test="imgUrl != null">
#{imgUrl,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.miaoshaproject.dataobject.ItemDO">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
update item
<set>
<if test="title != null">
title = #{title,jdbcType=VARCHAR},
</if>
<if test="price != null">
price = #{price,jdbcType=DOUBLE},
</if>
<if test="description != null">
description = #{description,jdbcType=VARCHAR},
</if>
<if test="sales != null">
sales = #{sales,jdbcType=INTEGER},
</if>
<if test="imgUrl != null">
img_url = #{imgUrl,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.miaoshaproject.dataobject.ItemDO">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
update item
set title = #{title,jdbcType=VARCHAR},
price = #{price,jdbcType=DOUBLE},
description = #{description,jdbcType=VARCHAR},
sales = #{sales,jdbcType=INTEGER},
img_url = #{imgUrl,jdbcType=VARCHAR}
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>