1
0
Code Issues Pull Requests Packages Projects Releases Wiki Activity GitHub Gitee

创建微服务:microservice-provider-shop-8003;小程序商品列表筛选、商品详情完成

This commit is contained in:
2023-03-18 23:00:58 +08:00
parent ee7e2e9acb
commit 7eab148104
41 changed files with 1161 additions and 84 deletions

View File

@@ -0,0 +1,159 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>epp</artifactId>
<groupId>com.cxyxiaomo</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>microservice-provider-shop</artifactId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<!-- 实体类 -->
<dependency>
<groupId>com.cxyxiaomo</groupId>
<artifactId>microservice-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<!-- logback -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</dependency>
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Nacos -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- OpenFeign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<!-- Fastjson -->
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
</dependency>
<!-- OkHttp -->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
</dependency>
<!-- WebSocket -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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();
}

View File

@@ -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;
}
}

View File

@@ -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

View File

@@ -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

View File

@@ -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>

View File

@@ -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>

View File

@@ -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>

View File

@@ -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 );
}
}