Skip to content
Snippets Groups Projects
Commit e27f611a authored by Jerry Xu's avatar Jerry Xu
Browse files

GoodsCategoryAPI

parent 1fae892d
No related branches found
No related tags found
No related merge requests found
Showing
with 258 additions and 21 deletions
......@@ -4,16 +4,22 @@ package com.example.freshonline.controller;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSON;
import com.example.freshonline.constants.Constants;
import com.example.freshonline.enums.respVerifyRule.VerifyRule;
import com.example.freshonline.model.StockedGoods;
import com.example.freshonline.service.GoodsService;
import com.example.freshonline.utils.RespBuilder;
import com.example.freshonline.utils.ValidationChecker;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.*;
import org.springframework.web.bind.annotation.*;
import com.example.freshonline.model.joined_tables.GoodsCategory;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
/**
* @author Josh Sun
......@@ -21,7 +27,35 @@ import org.springframework.web.bind.annotation.RestController;
@RestController
public class GoodsController {
/**
@Autowired
private GoodsService goodsService;
@GetMapping("/goodsdetails/{goods_id}")
public JSONObject getGoodsDetails(@PathVariable("goods_id") String id) {
JSONObject res = new JSONObject();
try{
Integer goods_id = Integer.parseInt(id);
GoodsCategory gc = goodsService.goodsDetails(goods_id);
JSONObject data = (JSONObject) JSONObject.toJSON(gc);
res.put("code", 0);
res.put("data",data);
return res;
}
catch(Exception e){
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
res.put("code", 1);
res.put("msg", sw.toString());
return res;
}
}
/**
* @author Josh Sun
* @param price_low_req
* @param price_high_req
......
......@@ -3,18 +3,14 @@ package com.example.freshonline.dao;
import com.example.freshonline.model.Category;
import com.example.freshonline.model.CategoryExample;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
public interface CategoryMapper {
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table category
*
* @mbg.generated Fri Jan 28 11:53:04 EST 2022
* @mbg.generated Thu Jan 27 22:18:17 EST 2022
*/
long countByExample(CategoryExample example);
......@@ -22,7 +18,7 @@ public interface CategoryMapper {
* This method was generated by MyBatis Generator.
* This method corresponds to the database table category
*
* @mbg.generated Fri Jan 28 11:53:04 EST 2022
* @mbg.generated Thu Jan 27 22:18:17 EST 2022
*/
int deleteByExample(CategoryExample example);
......@@ -30,7 +26,7 @@ public interface CategoryMapper {
* This method was generated by MyBatis Generator.
* This method corresponds to the database table category
*
* @mbg.generated Fri Jan 28 11:53:04 EST 2022
* @mbg.generated Thu Jan 27 22:18:17 EST 2022
*/
int deleteByPrimaryKey(Integer id);
......@@ -38,7 +34,7 @@ public interface CategoryMapper {
* This method was generated by MyBatis Generator.
* This method corresponds to the database table category
*
* @mbg.generated Fri Jan 28 11:53:04 EST 2022
* @mbg.generated Thu Jan 27 22:18:17 EST 2022
*/
int insert(Category record);
......@@ -46,7 +42,7 @@ public interface CategoryMapper {
* This method was generated by MyBatis Generator.
* This method corresponds to the database table category
*
* @mbg.generated Fri Jan 28 11:53:04 EST 2022
* @mbg.generated Thu Jan 27 22:18:17 EST 2022
*/
int insertSelective(Category record);
......@@ -54,7 +50,7 @@ public interface CategoryMapper {
* This method was generated by MyBatis Generator.
* This method corresponds to the database table category
*
* @mbg.generated Fri Jan 28 11:53:04 EST 2022
* @mbg.generated Thu Jan 27 22:18:17 EST 2022
*/
List<Category> selectByExample(CategoryExample example);
......@@ -62,7 +58,7 @@ public interface CategoryMapper {
* This method was generated by MyBatis Generator.
* This method corresponds to the database table category
*
* @mbg.generated Fri Jan 28 11:53:04 EST 2022
* @mbg.generated Thu Jan 27 22:18:17 EST 2022
*/
Category selectByPrimaryKey(Integer id);
......@@ -70,7 +66,7 @@ public interface CategoryMapper {
* This method was generated by MyBatis Generator.
* This method corresponds to the database table category
*
* @mbg.generated Fri Jan 28 11:53:04 EST 2022
* @mbg.generated Thu Jan 27 22:18:17 EST 2022
*/
int updateByExampleSelective(@Param("record") Category record, @Param("example") CategoryExample example);
......@@ -78,7 +74,7 @@ public interface CategoryMapper {
* This method was generated by MyBatis Generator.
* This method corresponds to the database table category
*
* @mbg.generated Fri Jan 28 11:53:04 EST 2022
* @mbg.generated Thu Jan 27 22:18:17 EST 2022
*/
int updateByExample(@Param("record") Category record, @Param("example") CategoryExample example);
......@@ -86,7 +82,7 @@ public interface CategoryMapper {
* This method was generated by MyBatis Generator.
* This method corresponds to the database table category
*
* @mbg.generated Fri Jan 28 11:53:04 EST 2022
* @mbg.generated Thu Jan 27 22:18:17 EST 2022
*/
int updateByPrimaryKeySelective(Category record);
......@@ -94,7 +90,7 @@ public interface CategoryMapper {
* This method was generated by MyBatis Generator.
* This method corresponds to the database table category
*
* @mbg.generated Fri Jan 28 11:53:04 EST 2022
* @mbg.generated Thu Jan 27 22:18:17 EST 2022
*/
int updateByPrimaryKey(Category record);
}
\ No newline at end of file
package com.example.freshonline.dao;
import com.example.freshonline.model.joined_tables.GoodsCategory;
public interface GoodsCategoryMapper {
GoodsCategory selectByGoodsID(Integer id);
}
\ No newline at end of file
......@@ -2,6 +2,7 @@ package com.example.freshonline.model;
import java.math.BigDecimal;
public class StockedGoods {
/**
*
......@@ -497,4 +498,11 @@ public class StockedGoods {
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return super.toString() + " "+ getName() + " " + getId() + " " + getDescription();
}
}
\ No newline at end of file
package com.example.freshonline.model.joined_tables;
import com.example.freshonline.model.Category;
import com.example.freshonline.model.StockedGoods;
public class GoodsCategory extends StockedGoods{
private Category cate1;
private Category cate2;
private Category cate3;
private int cateid;
public GoodsCategory(Category cate1, Category cate2, Category cate3) {
this.cate1 = cate1;
this.cate2 = cate2;
this.cate3 = cate3;
}
public int getCateid() {
return cateid;
}
public void setCateid(int cateid) {
this.cateid = cateid;
}
public GoodsCategory() {
super();
}
public Category getcate1() {
return cate1;
}
public void setcate1(Category cate1) {
this.cate1 = cate1;
}
public Category getcate2() {
return cate2;
}
public void setcate2(Category cate2) {
this.cate2 = cate2;
}
public Category getcate3() {
return cate3;
}
public void setcate3(Category cate3) {
this.cate3 = cate3;
}
@Override
public String toString() {
return super.toString() + "\n" + cateid + cate1;
}
}
package com.example.freshonline.service;
import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.freshonline.model.StockedGoods;
import com.example.freshonline.utils.ResultSetJSONConverter;
import org.springframework.beans.factory.annotation.*;
import org.springframework.stereotype.Service;
import com.example.freshonline.dao.GoodsCategoryMapper;
import com.example.freshonline.model.joined_tables.GoodsCategory;
import java.io.IOException;
import java.math.BigDecimal;
......@@ -13,10 +21,45 @@ import java.sql.SQLException;
import java.util.*;
import java.sql.ResultSet;
@Service
public class GoodsService {
final BigDecimal MIN_PRICE = BigDecimal.valueOf(0), MAX_PRICE = BigDecimal.valueOf(10000);
@Autowired
private GoodsCategoryMapper goodsCategoryMapper;
public GoodsCategory goodsDetails(Integer id) throws Exception{
GoodsCategory gc = goodsCategoryMapper.selectByGoodsID(id);
return gc;
// String sql = "SELECT good.*, cate1.id as id1, cate1.name as name1, cate2.id as id2, cate2.name as name2, cate3.id as id3, cate3.name as name3 "
// +"FROM "
// +"stocked_goods as good "
// +"inner join category as cate3 "
// +"on good.category_id = cate3.id "
// +"inner join category as cate2 "
// +"on cate3.parent_id = cate2.id "
// +"inner join category as cate1 "
// +"on cate2.parent_id = cate1.id "
// +"where good.id = "+id.toString()+"; ";
// Class.forName("com.mysql.cj.jdbc.Driver");
// Properties pros;
// Connection conn = null;
// pros = new Properties();
// pros.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("application.properties"));
// Class.forName(pros.getProperty("spring.datasource.driver-class-name"));
// conn = DriverManager.getConnection(pros.getProperty("spring.datasource.url"), pros.getProperty("spring.datasource.username"), pros.getProperty("spring.datasource.password"));
// PreparedStatement ps = conn.prepareStatement(sql);
// ResultSet result = ps.executeQuery();
// return ResultSetJSONConverter.convert(result);
}
/**
* @author Josh Sun
......
package com.example.freshonline.utils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.sql.ResultSet;
public class ResultSetJSONConverter {
public static JSONArray convert(ResultSet resultSet) throws Exception {
JSONArray jsonArray = new JSONArray();
while (resultSet.next()) {
int columns = resultSet.getMetaData().getColumnCount();
JSONObject obj = new JSONObject();
for (int i = 0; i < columns; i++)
obj.put(resultSet.getMetaData().getColumnLabel(i + 1).toLowerCase(), resultSet.getObject(i + 1));
jsonArray.add(obj);
}
return jsonArray;
}
}
......@@ -4,7 +4,7 @@
spring.datasource.url=jdbc:mysql://localhost:3306/FreshOnline?useUnicode=true&characterEncoding=utf8&allowPublicKeyRetrieval=true&useSSL=false
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=
spring.datasource.password=Bin86087
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.example.freshonline.dao
\ No newline at end of file
<?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.example.freshonline.dao.GoodsCategoryMapper">
<resultMap id="selectByGoodsIDMap" type="com.example.freshonline.model.joined_tables.GoodsCategory">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="type" jdbcType="TINYINT" property="type" />
<result column="price" jdbcType="DECIMAL" property="price" />
<result column="storage" jdbcType="DECIMAL" property="storage" />
<result column="sales" jdbcType="DECIMAL" property="sales" />
<result column="onsale" jdbcType="TINYINT" property="onsale" />
<result column="sale_price" jdbcType="DECIMAL" property="salePrice" />
<result column="rate" jdbcType="DECIMAL" property="rate" />
<result column="rate_count" jdbcType="INTEGER" property="rateCount" />
<result column="brand" jdbcType="VARCHAR" property="brand" />
<result column="category_id" jdbcType="INTEGER" property="categoryId" />
<result column="is_new" jdbcType="TINYINT" property="isNew" />
<result column="pic" jdbcType="VARCHAR" property="pic" />
<result column="description" jdbcType="LONGVARCHAR" property="description" />
<result property="cateid" jdbcType="INTEGER" column="id1" />
<association property="cate1" javaType="com.example.freshonline.model.Category">
<id property="id" jdbcType="INTEGER" column="id1"/>
<result property="name" column="name1" />
</association>
<association property="cate2" javaType="com.example.freshonline.model.Category">
<id property="id" column="id2" />
<result property="name" column="name2" />
</association>
<association property="cate3" javaType="com.example.freshonline.model.Category">
<id property="id" column="id3"/>
<result property="name" column="name3"/>
</association>
</resultMap>
<select id="selectByGoodsID" parameterType="int" resultMap="selectByGoodsIDMap">
SELECT good.*, cate1.id as id1, cate1.name as name1, cate2.id as id2, cate2.name as name2, cate3.id as id3, cate3.name as name3
FROM
stocked_goods as good
inner join category as cate3
on good.category_id = cate3.id
inner join category as cate2
on cate3.parent_id = cate2.id
inner join category as cate1
on cate2.parent_id = cate1.id
where good.id = #{id}
</select>
</mapper>
\ No newline at end of file
package com.example.freshonline.dao;
import org.junit.jupiter.api.Test;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.stereotype.Component;
import com.example.freshonline.model.joined_tables.GoodsCategory;
import java.util.List;
@SpringBootTest
@MapperScan("com.example.freshonline.dao")
public class GoodsCategoryTest {
@Autowired
private GoodsCategoryMapper goodsCategoryMapper;
@Test
public void test1(){
GoodsCategory a = goodsCategoryMapper.selectByGoodsID(1);
System.err.println(a);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment