创建微服务:microservice-provider-shop-8003;小程序商品列表筛选、商品详情完成
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
package com.cxyxiaomo.epp.shop;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
|
||||
// 启动类
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableDiscoveryClient
|
||||
@EnableFeignClients
|
||||
public class ShopProvider {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ShopProvider.class, args);
|
||||
}
|
||||
}
|
@@ -0,0 +1,68 @@
|
||||
package com.cxyxiaomo.epp.shop.controller;
|
||||
|
||||
import com.cxyxiaomo.epp.common.response.Res;
|
||||
import com.cxyxiaomo.epp.common.vo.GoodCategoryVO;
|
||||
import com.cxyxiaomo.epp.common.vo.GoodVO;
|
||||
import com.cxyxiaomo.epp.shop.service.GoodsServiceImpl;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/shop/good")
|
||||
public class GoodsController {
|
||||
|
||||
@Resource
|
||||
GoodsServiceImpl goodsService;
|
||||
|
||||
/**
|
||||
* 小程序端商品列表
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/miniprogram/list")
|
||||
public Res list(@RequestParam(value = "cateId", required = false) Integer cateId,
|
||||
@RequestParam(value = "searchText", required = false) String searchText) {
|
||||
if (cateId != null && cateId <= 0) {
|
||||
cateId = null;
|
||||
}
|
||||
if (searchText != null) {
|
||||
searchText = searchText.trim();
|
||||
if (searchText.contains("%") || searchText.contains("_")) {
|
||||
searchText = searchText
|
||||
.replaceAll("[_%]", "");
|
||||
}
|
||||
if ("".equals(searchText)) {
|
||||
searchText = null;
|
||||
}
|
||||
}
|
||||
List<GoodVO> list = goodsService.list(cateId, searchText);
|
||||
return Res.success(list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 小程序端商品详情
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/miniprogram/detail")
|
||||
public Res detail(@RequestParam("id") Long id) {
|
||||
GoodVO goodVO = goodsService.getById(id);
|
||||
return Res.success(goodVO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 小程序端商品分类列表
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/miniprogram/cateList")
|
||||
public Res cateList() {
|
||||
List<GoodCategoryVO> list = goodsService.cateList();
|
||||
return Res.success(list);
|
||||
}
|
||||
}
|
@@ -0,0 +1,16 @@
|
||||
package com.cxyxiaomo.epp.shop.dao;
|
||||
|
||||
import com.cxyxiaomo.epp.common.pojo.GoodCategory;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
@Repository
|
||||
public interface GoodsCategoryDao {
|
||||
|
||||
List<GoodCategory> list();
|
||||
|
||||
GoodCategory getById(Integer id);
|
||||
}
|
@@ -0,0 +1,17 @@
|
||||
package com.cxyxiaomo.epp.shop.dao;
|
||||
|
||||
import com.cxyxiaomo.epp.common.pojo.Good;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
@Repository
|
||||
public interface GoodsDao {
|
||||
|
||||
List<Good> list(@Param("cateId") Integer cateId, @Param("searchText") String searchText);
|
||||
|
||||
Good getById(Long id);
|
||||
}
|
@@ -0,0 +1,15 @@
|
||||
package com.cxyxiaomo.epp.shop.service;
|
||||
|
||||
import com.cxyxiaomo.epp.common.vo.GoodCategoryVO;
|
||||
import com.cxyxiaomo.epp.common.vo.GoodVO;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface GoodsService {
|
||||
|
||||
List<GoodVO> list(Integer cateId, String searchText);
|
||||
|
||||
GoodVO getById(Long id);
|
||||
|
||||
List<GoodCategoryVO> cateList();
|
||||
}
|
@@ -0,0 +1,43 @@
|
||||
package com.cxyxiaomo.epp.shop.service;
|
||||
|
||||
import com.cxyxiaomo.epp.common.pojo.Good;
|
||||
import com.cxyxiaomo.epp.common.pojo.GoodCategory;
|
||||
import com.cxyxiaomo.epp.common.vo.GoodCategoryVO;
|
||||
import com.cxyxiaomo.epp.common.vo.GoodVO;
|
||||
import com.cxyxiaomo.epp.shop.dao.GoodsCategoryDao;
|
||||
import com.cxyxiaomo.epp.shop.dao.GoodsDao;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class GoodsServiceImpl implements GoodsService {
|
||||
|
||||
@Resource
|
||||
GoodsDao goodsDao;
|
||||
|
||||
@Resource
|
||||
GoodsCategoryDao goodsCategoryDao;
|
||||
|
||||
@Override
|
||||
public List<GoodVO> list(Integer cateId, String searchText) {
|
||||
List<Good> list = goodsDao.list(cateId, searchText);
|
||||
List<GoodVO> goodVOS = GoodVO.convertFrom(list);
|
||||
return goodVOS;
|
||||
}
|
||||
|
||||
@Override
|
||||
public GoodVO getById(Long id) {
|
||||
Good good = goodsDao.getById(id);
|
||||
GoodVO goodVO = GoodVO.convertFrom(good);
|
||||
return goodVO;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<GoodCategoryVO> cateList() {
|
||||
List<GoodCategory> list = goodsCategoryDao.list();
|
||||
List<GoodCategoryVO> goodCategoryVOList = GoodCategoryVO.convertFrom(list);
|
||||
return goodCategoryVOList;
|
||||
}
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
spring:
|
||||
datasource:
|
||||
url: jdbc:mysql://127.0.0.1:3306/epp?useUnicode=true&characterEncoding=utf8&useSSL=false
|
||||
username: root
|
||||
password: 123456
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
server-addr: 106.75.217.14:8488
|
@@ -0,0 +1,23 @@
|
||||
server:
|
||||
port: 8003
|
||||
|
||||
# Mybatis 配置
|
||||
mybatis:
|
||||
type-aliases-package: com.cxyxiaomo.epp.pojo
|
||||
config-location: classpath:mybatis/mybatis-config.xml
|
||||
mapper-locations: classpath:mybatis/mapper/*.xml
|
||||
|
||||
# Spring 配置
|
||||
spring:
|
||||
application:
|
||||
name: microservice-provider-shop
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
server-addr: 127.0.0.1:8848
|
||||
datasource:
|
||||
type: com.alibaba.druid.pool.DruidDataSource
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
url: jdbc:mysql://127.0.0.1:3306/epp?useUnicode=true&characterEncoding=utf8&useSSL=false
|
||||
username: root
|
||||
password: root
|
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.cxyxiaomo.epp.shop.dao.GoodsCategoryDao">
|
||||
<select id="list" resultType="com.cxyxiaomo.epp.common.pojo.GoodCategory">
|
||||
SELECT * FROM goods_category
|
||||
WHERE 1 = 1
|
||||
order by `order` asc
|
||||
</select>
|
||||
<select id="getById" parameterType="java.lang.Integer" resultType="com.cxyxiaomo.epp.common.pojo.GoodCategory">
|
||||
SELECT * FROM goods_category
|
||||
WHERE id = #{id}
|
||||
order by `order` asc
|
||||
</select>
|
||||
</mapper>
|
@@ -0,0 +1,28 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.cxyxiaomo.epp.shop.dao.GoodsDao">
|
||||
<!--<insert id="insert" parameterType="com.cxyxiaomo.epp.common.pojo.Good">-->
|
||||
<!-- INSERT INTO report (`user_id`, `name`, `address`, `time`, `temperature`)-->
|
||||
<!-- VALUES (#{userId}, #{name}, #{address}, #{time}, #{temperature})-->
|
||||
<!--</insert>-->
|
||||
<select id="list" resultType="com.cxyxiaomo.epp.common.pojo.Good">
|
||||
SELECT *
|
||||
FROM goods
|
||||
WHERE 1 = 1
|
||||
<if test="cateId != null">
|
||||
AND category_id = #{cateId}
|
||||
</if>
|
||||
<if test="searchText != null">
|
||||
AND goods_name LIKE concat('%',#{searchText,jdbcType=VARCHAR},'%')
|
||||
</if>
|
||||
order by `sort_order` asc
|
||||
</select>
|
||||
<select id="getById" parameterType="java.lang.Long" resultType="com.cxyxiaomo.epp.common.pojo.Good">
|
||||
SELECT *
|
||||
FROM goods
|
||||
WHERE id = #{id}
|
||||
order by `sort_order` asc
|
||||
</select>
|
||||
</mapper>
|
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE configuration
|
||||
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-config.dtd">
|
||||
<configuration>
|
||||
<settings>
|
||||
<!--开启二级缓存-->
|
||||
<setting name="cacheEnabled" value="true"/>
|
||||
|
||||
<!--下划线转小驼峰-->
|
||||
<setting name="mapUnderscoreToCamelCase" value="true"/>
|
||||
</settings>
|
||||
</configuration>
|
@@ -0,0 +1,20 @@
|
||||
package com.cxyxiaomo;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Unit test for simple App.
|
||||
*/
|
||||
public class AppTest
|
||||
{
|
||||
/**
|
||||
* Rigorous Test :-)
|
||||
*/
|
||||
@Test
|
||||
public void shouldAnswerWithTrue()
|
||||
{
|
||||
assertTrue( true );
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user