一、父工程搭建
1.搭建父工程模块
创建父工程模块,因为父工程不需要写代码所以删除src目录。
父工程的pom文件,注意:父工程打pom包
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mychanggou</groupId>
<artifactId>mychanggou</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<description>
父工程 统一版本的控制 所有的工程都应该继承父工程
</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<!--打包的时候跳过测试-->
<skipTests>true</skipTests>
</properties>
<!--依赖包-->
<dependencies>
<!--鉴权-->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.0</version>
</dependency>
<!--测试包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!--fastjson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.51</version>
</dependency>
<!--swagger文档 可选的-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
<!--
http://localhost:9011/swagger-ui.html
-->
</dependencies>
<!--springcloud的版本控制-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
2.搭建changgou-gateway微服务网关的父工程
在父工程下创建Gateway微服务网关父工程模块,同样删除src目录。
<?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>mychanggou</artifactId>
<groupId>com.mychanggou</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<description>
网关 父工程
</description>
<artifactId>muchanggou-gateway</artifactId>
</project>
3.创建changgou-service微服务父工程
在父工程下创建service微服务父工程,同样删除src目录。
<?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>mychanggou</artifactId>
<groupId>com.mychanggou</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<description>
微服务的父工程
</description>
<artifactId>mychanggou-service</artifactId>
<dependencies>
<dependency>
<groupId>com.mychanggou</groupId>
<artifactId>mychanggou-common-db</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
4.创建changgou-service-api模块父工程
在父工程下创建service-api模块父工程,同样删除src
<?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>mychanggou</artifactId>
<groupId>com.mychanggou</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<description>
通用的API javean feign的父工程
</description>
<artifactId>mychanggou-service_api</artifactId>
</project>
5.web模块的父工程
在父工程下创建web模块的父工程,删除src
<?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>mychanggou</artifactId>
<groupId>com.mychanggou</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>mychanggou-web</artifactId>
<packaging>pom</packaging>
<description>
web工程所在的父工程
</description>
</project>
二、搭建Eureka模块
创建Eureka子模块。作为微服务注册中心。
1.pom文件:
<?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>mychanggou</artifactId>
<groupId>com.mychanggou</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>mychanggou-eureka</artifactId>
<!--eureka的服务端 注册中心-->
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
2.application.yml文件:
server:
port: 7001
eureka:
instance:
hostname: 127.0.0.1
client:
register-with-eureka: false #是否将自己注册到eureka中
fetch-registry: false #是否从eureka中获取信息
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
spring:
application:
name: eureka
3.主启动类
package com.mychanggou;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* @author :LiuShihao
* @date :Created in 2020/8/11 3:36 下午
* @desc :注册中心启动类
*/
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class);
}
}
三、搭建公共模块
创建公共子模块common。
有一句话叫:一个项目好不好,就看工具抽取有多少。
1.pom文件
<?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>mychanggou</artifactId>
<groupId>com.mychanggou</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>mychanggou-common</artifactId>
<dependencies>
<!--web起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- redis 使用-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--openfeign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--微信支付-->
<dependency>
<groupId>com.github.wxpay</groupId>
<artifactId>wxpay-sdk</artifactId>
<version>0.0.3</version>
</dependency>
<!--httpclient支持-->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
</dependencies>
</project>
四、搭建数据访问工程
创建common-db模块,主要就是访问数据库的依赖
1.pom文件
<?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>mychanggou</artifactId>
<groupId>com.mychanggou</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>mychanggou-common-db</artifactId>
<!--依赖-->
<dependencies>
<!--对mychanggou-common的依赖-->
<dependency>
<groupId>com.mychanggou</groupId>
<artifactId>mychanggou-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--通用mapper起步依赖-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.0.4</version>
</dependency>
<!--MySQL数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--mybatis分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
</dependencies>
</project>
五、搭建商品微服务模块
1.创建changgou-service-goods-api
在changgou-service-api父工程模块下创建changgou-service-goods-api模块,主要是商品的实体类。
注意:需要在cahnggou-service-api父工程的pom文件中导入依赖
<!--通用的common 要有feign的依赖-->
<dependency>
<groupId>com.mychanggou</groupId>
<artifactId>mychanggou-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--每个工程都有Pojo,都需要用到该包对应的注解-->
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
2.创建changgou-service-goods模块
在changgou-service父工程下创建 changgou-service-goods子模块。
在changgou-service父工程pom文件中引入common公共模块依赖:
<dependencies>
<dependency>
<groupId>com.mychanggou</groupId>
<artifactId>mychanggou-common-db</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
1.changgou-service-goods模块的pom文件
注意:changgou-service-goods模块需要引入changgou-service-goods-api模块的依赖。
<?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>mychanggou-service</artifactId>
<groupId>com.mychanggou</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>mycahnggou-service-goods</artifactId>
<description>
cahgngou_goods数据库的管理,商品微服务工程
</description>
<!--依赖-->
<dependencies>
<dependency>
<groupId>com.mychanggou</groupId>
<artifactId>mychanggou-service-goods-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
2.application.yml文件:
server:
port: 18081
spring:
application:
name: goods
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://192.168.211.132:3306/changgou_goods?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
username: root
password: 123456
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:7001/eureka
instance:
prefer-ip-address: true
feign:
hystrix:
enabled: true
#使用通用mapper 可以实现0sql语句,可以用也可以不用下以下配置 随便
mybatis:
configuration:
map-underscore-to-camel-case: true
mapper-locations: classpath:mapper/*Mapper.xml
type-aliases-package: com.changgou.goods.pojo
3.主启动类
package com.mychanggou;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* @author :LiuShihao
* @date :Created in 2020/9/15 11:32 下午
* @desc :
*/
@SpringBootApplication
@EnableEurekaClient//开启Eureka客户端
public class GoodsApplication {
public static void main(String[] args) {
SpringApplication.run(GoodsApplication.class);
}
}
六、实现商品微服务品牌的增删改查
1.品牌表:
2.表映射实体类
@Table和@Id都是JPA注解,@Table用于配置表与实体类的映射关系,@Id用 于标识主键属性。
3.DAO层
在changgou-service-goods微服务下创建
com.changgou.goods.dao.BrandMapper接口,代码如下:
继承了Mapper接口,就自动实现了增删改查的常用方法。
import com.mychanggou.goods.pojo.Brand;
import tk.mybatis.mapper.common.Mapper;
/**
* @author :LiuShihao
* @date :Created in 2020/9/16 1:50 下午
* @desc :品牌表的dao层
*/
public interface BrandMapper extends Mapper<Brand> {
}
在GoodsApplication主启动类上添加上MapperScan注解@MapperScan(basePackages = "com.mychanggou.goods.dao")
。
4.业务层
创建com.changgou.goods.service.BrandService接口,代码如下:
public interface BrandService {
/***
* 查询所有品牌
* @return
*/
List<Brand> findAll();
}
创建com.changgou.goods.service.impl.BrandServiceImpl实现类,代码如下:
@Service
public class BrandServiceImpl implements BrandService {
@Autowired
BrandMapper brandMapper;
@Override
public List<Brand> findAll() {
return brandMapper.selectAll();
}
}
5.控制层
控制层 com.changgou.goods包下创建controller包 ,包下创建类
@RestController
@RequestMapping("/brand")
public class BrandController {
@Autowired
BrandService brandService;
/***
* 查询全部数据 * @return */
@GetMapping
public Result<Brand> findAll(){
List<Brand> brandList = brandService.findAll();
return new Result<Brand>(true, StatusCode.OK,"查询成功",brandList) ;
}
}
6.启动测试
1)启动报错
2)解决方法:
去除对默认日志的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
3)启动成功
4)查询成功
5)其他方法
- 根据ID查询品牌
- 根据ID查询实体数据
- 增加
- 修改
- 删除
- 条件查询
- 分页查询
- 分页+条件查询
不再一一展示。
7.完整控制层、业务层、DAO层代码
controller:
package com.mychanggou.goods.controller;
import com.github.pagehelper.PageInfo;
import com.mychanggou.goods.goods.pojo.Brand;
import com.mychanggou.goods.service.BrandService;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* @author :LiuShihao
* @date :Created in 2020/9/16 1:58 下午
* @desc :
*/
@RestController
@RequestMapping("/brand")
@CrossOrigin//跨域 :A域名访问B域名的数据 就存在跨域;域名/端口/协议 不一致的时候就会产生跨域问题
public class BrandController {
@Autowired
BrandService brandService;
/***
* 查询全部数据 * @return */
@GetMapping
public Result<Brand> findAll(){
List<Brand> brandList = brandService.findAll();
return new Result<Brand>(true, StatusCode.OK,"查询成功",brandList) ;
}
/**
* 根据ID查询
* @param id
* @return
*/
@GetMapping("/{id}")
public Result<Brand> findById(@PathVariable Integer id){
//根据ID查询
Brand brand = brandService.findById(id);
return new Result<Brand>(true,StatusCode.OK,"查询成功",brand);
}
/**
* 新增品牌数据
* @param brand
* @return
*/
@PostMapping
public Result add(@RequestBody Brand brand){
brandService.add(brand);
return new Result(true,StatusCode.OK,"添加成功");
}
/**
* 修改品牌数据
* @param brand
* @param id
* @return
*/
@PutMapping(value="/{id}")
public Result update(@RequestBody Brand brand,@PathVariable Integer id) {
//设置ID
brand.setId(id);
//修改数据
brandService.update(brand);
return new Result(true, StatusCode.OK, "修改成功");
}
/**
* 删除品牌数据
* @param id
* @return
*/
@DeleteMapping(value = "/{id}" )
public Result delete(@PathVariable Integer id){
brandService.delete(id);
return new Result(true,StatusCode.OK,"删除成功");
}
/**
* 多条件查询
* @param brand
* @return
*/
@PostMapping(value = "/search" )
public Result<List<Brand>> findList(@RequestBody(required= false) Brand brand) {
List<Brand> list = brandService.findList(brand);
return new Result<List<Brand>>(true, StatusCode.OK, "查询成功", list);
}
/**
* 分页查询
* @param page
* @param size
* @return
*/
@GetMapping(value = "/search/{page}/{size}" )
public Result<PageInfo<Brand>> findPage(@PathVariable("page") int page,@PathVariable("size") int size){
PageInfo<Brand> pageInfo = brandService.findPage(page, size);
return new Result<PageInfo<Brand>>(true,StatusCode.OK,"查询成 功",pageInfo);
}
/**
* 多条件查询+分页
* @param brand
* @param page
* @param size
* @return
*/
@PostMapping(value = "/search/{page}/{size}" )
public Result<PageInfo<Brand>> findPage(@RequestBody(required =false) Brand brand, @PathVariable int page, @PathVariable int size){
PageInfo<Brand> pageInfo = brandService.findPage(brand, page, size);
return new Result<PageInfo<Brand>>(true,StatusCode.OK,"查询成功",pageInfo);
}
}
业务层接口:
package com.mychanggou.goods.service;
import com.github.pagehelper.PageInfo;
import com.mychanggou.goods.goods.pojo.Brand;
import java.util.List;
/**
* @author :LiuShihao
* @date :Created in 2020/9/16 1:54 下午
* @desc :
*/
public interface BrandService {
List<Brand> findAll();
Brand findById(Integer id);
void add(Brand brand);
void update(Brand brand);
void delete(Integer id);
List<Brand> findList(Brand brand);
PageInfo<Brand> findPage(int page, int size);
PageInfo<Brand> findPage(Brand brand, int page, int size);
}
业务层实现类:
package com.mychanggou.goods.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mychanggou.goods.dao.BrandMapper;
import com.mychanggou.goods.goods.pojo.Brand;
import com.mychanggou.goods.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;
import java.util.List;
/**
* @author :LiuShihao
* @date :Created in 2020/9/16 1:54 下午
* @desc :
*/
@Service
public class BrandServiceImpl implements BrandService {
@Autowired
BrandMapper brandMapper;
@Override
public List<Brand> findAll() {
return brandMapper.selectAll();
}
@Override
public Brand findById(Integer id) {
return brandMapper.selectByPrimaryKey(id);
}
@Override
public void add(Brand brand) {
brandMapper.insert(brand);
}
@Override
public void update(Brand brand) {
brandMapper.updateByPrimaryKey(brand);
}
@Override
public void delete(Integer id) {
brandMapper.deleteByPrimaryKey(id);
}
@Override
public List<Brand> findList(Brand brand) {
//构建查询条件
Example example = createExample(brand); //根据构建的条件查询数据
return brandMapper.selectByExample(example);
}
@Override
public PageInfo<Brand> findPage(int page, int size) {
//静态分页
PageHelper.startPage(page,size);
//分页查询
return new PageInfo<Brand>(brandMapper.selectAll());
}
@Override
public PageInfo<Brand> findPage(Brand brand, int page, int size) {
//分页
PageHelper.startPage(page,size);
//搜索数据
Example example = createExample(brand);
return new PageInfo<Brand>(brandMapper.selectByExample(example));
}
/**
* 构建查询对象
* @param brand
* @return
*/
public Example createExample(Brand brand){
Example example=new Example(Brand.class);
Example.Criteria criteria = example.createCriteria();
if(brand!=null){
// 品牌名称
if(!StringUtils.isEmpty(brand.getName())){
criteria.andLike("name","%"+brand.getName()+"%");
}
// 品牌图片地址
if(!StringUtils.isEmpty(brand.getImage())){
criteria.andLike("image","%"+brand.getImage()+"%");
}
// 品牌的首字母
if(!StringUtils.isEmpty(brand.getLetter())){
criteria.andLike("letter","%"+brand.getLetter()+"%");
}
// 品牌id
if(!StringUtils.isEmpty(brand.getLetter())){
criteria.andEqualTo("id",brand.getId());
}
// 排序
if(!StringUtils.isEmpty(brand.getSeq())){
criteria.andEqualTo("seq",brand.getSeq());
}
}
return example;
}
}
DAO层:
package com.mychanggou.goods.dao;
import com.mychanggou.goods.goods.pojo.Brand;
import tk.mybatis.mapper.common.Mapper;
/**
* @author :LiuShihao
* @date :Created in 2020/9/16 1:50 下午
* @desc :品牌表的dao层
*/
public interface BrandMapper extends Mapper<Brand> {
}