父类:pom.xml
<?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>cn.dingyi</groupId>
<artifactId>microservice-cloud-01</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>../microservice-cloud-02-api</module>
<module>../microvice-cloud-03-provider-product8001</module>
</modules>
<!--手动指定pom-->
<packaging>pom</packaging>
<!--springboot版本 2.0.7-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.7.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>
<junit.version>4.12</junit.version>
<!--spring Cloud 最新的稳定版 Finchley SR2-->
<spring-cloud.version>Finchley.SR2</spring-cloud.version>
</properties>
<!--依赖声明-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<!--maven不支持多继承,使用import来依赖管理配置-->
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!--数据源-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
子类1:
实体类:pom.xml
<?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>microservice-cloud-01</artifactId>
<groupId>cn.dingyi</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../microservice-cloud-01/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>cn.dingyi</groupId>
<artifactId>microservice-cloud-02-api</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>
java包下建实体类:
package cn.dingyi.springcloud.entities;
import java.io.Serializable;
/**
* author:dingyi
* time:2019/8/26 0026 17:09
*/
public class Product implements Serializable {
private Long pid;
private String productName;
private String dbSource;
public Product(String productName) {
this.productName = productName;
}
public Product() {
}
public Product(Long pid, String productName, String dbSource) {
this.pid = pid;
this.productName = productName;
this.dbSource = dbSource;
}
public Long getPid() {
return pid;
}
public void setPid(Long pid) {
this.pid = pid;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getDbSource() {
return dbSource;
}
public void setDbSource(String dbSource) {
this.dbSource = dbSource;
}
}
子类2:
pom.xml
<?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>microservice-cloud-01</artifactId>
<groupId>cn.dingyi</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../microservice-cloud-01/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>cn.dingyi</groupId>
<artifactId>microvice-cloud-03-provider-product8001</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>cn.dingyi</groupId>
<artifactId>microservice-cloud-02-api</artifactId>
<version>${project.version}</version>
</dependency>
<!--springboot web启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--导入 mybatis 启动器-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>
java包:
mapper:
package cn.dingyi.springcloud.mapper;
import cn.dingyi.springcloud.entities.Product;
import java.util.List;
/**
* author:dingyi
* time:2019/8/27 0027 10:58
*/
//@Mapper//如果不写可以 必须在启动类上@MapperScan
public interface ProductMapper {
Product findById(Long pid);
List<Product> findAll();
boolean addProduct(Product product);
}
service:
package cn.dingyi.springcloud.Service;
import cn.dingyi.springcloud.entities.Product;
import java.util.List;
/**
* author:dingyi
* time:2019/8/27 0027 15:35
*/
public interface ProductService {
Product findById(Long pid);
List<Product> findAll();
boolean addProduct(Product product);
}
serviceimpl:
package cn.dingyi.springcloud.Service.impl;
import cn.dingyi.springcloud.Service.ProductService;
import cn.dingyi.springcloud.entities.Product;
import cn.dingyi.springcloud.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* author:dingyi
* time:2019/8/27 0027 15:36
*/
@Service
public class ProductServiceImpl implements ProductService {
@Autowired
private ProductMapper productMapper;
@Override
public Product findById(Long pid) {
return productMapper.findById(pid);
}
@Override
public List<Product> findAll() {
return productMapper.findAll();
}
@Override
public boolean addProduct(Product product) {
return productMapper.addProduct(product);
}
}
controller:
package cn.dingyi.springcloud.controller;
import cn.dingyi.springcloud.Service.ProductService;
import cn.dingyi.springcloud.entities.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* author:dingyi
* time:2019/8/27 0027 15:40
*/
@RestController
public class ProductController {
@Autowired
private ProductService productService;
@RequestMapping(value = "/product/get/{id}",method = RequestMethod.GET)
public Product get(@PathVariable("id")Long id){
return productService.findById(id);
}
@RequestMapping(value = "/product/get/list",method = RequestMethod.GET)
public List<Product> getAll(){
return productService.findAll();
}
@RequestMapping(value = "/product/add",method = RequestMethod.GET)
public boolean add(@RequestBody Product product){
return productService.addProduct(product);
}
}
启动类:
package cn.dingyi.springcloud;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* author:dingyi
* time:2019/8/27 0027 15:48
*/
@MapperScan("cn.dingyi.springcloud.mapper")
@SpringBootApplication
public class ProductProvider_8001 {
public static void main(String[] args) {
SpringApplication.run(ProductProvider_8001.class,args);
}
}
resources:
application.yml
server:
port: 8001
mybatis:
config-location: classpath:mybatis/mybatis.cfg.xml #mybatis 配置文件路径
type-aliases-package: cn.dingyi.springcloud.entities # entity别名类所在包
mapper-locations: classpath:mybatis/mapper/**/*.xml
spring:
application:
name: microservice-product #这个很重要,这在以后的服务与服务之间相互调用一般都是根据这个name
name
datasource:
type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型
driver-class-name: com.mysql.cj.jdbc.Driver # mysql驱动包
url: jdbc:mysql://localhost:3306/springcloud_dd?serverTimezone=GMT%2B8 # 数据库名称
username: root
password: root
dbcp2:
min-idle: 5 # 数据库连接池的最小维持连接数
initial-size: 5 # 初始化连接数
max-total: 5 # 最大连接数
max-wait-millis: 150 # 等待连接获取的最大超时时间
#https://www.cnblogs.com/lijun6/p/11409694.html
mybatis:
mybatis.cfg.xml
<?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="mapUnderscoreToCamelCase" value="true"/>
</settings>
</configuration>
mybatis:
mapper:
productMapper.xml
<?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="cn.dingyi.springcloud.mapper.ProductMapper">
<select id="findById" resultType="Product" parameterType="Long">
select * from product where pid=#{pid};
-- SELECT pid,product_name,db_
-- source FROM product where pid=#{pid};
</select>
<select id="findAll" resultType="Product">
select * from product;
-- SELECT pid,product_name,db_source FROM product;
</select>
<insert id="addProduct" parameterType="Product">
insert into product(productName,dbsource) values (#{productName},#{dbSource})
-- INSERT INTO product(product_name,db_source) VALUES(#{productName},#{dbSource})
</insert>
</mapper>
子类3:消费者对象:
java包:
config:
ConfigBean:
package cn.dingyi.springcloud.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* author:dingyi
* time:2019/8/27 0027 16:56
*/
@Configuration
public class ConfigBean {
// 向容器中添加RestTemplate 组件 直接该组件调用EREST接口
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
java包:
controller:
ProductController_Consumer:
package cn.dingyi.springcloud.controller;
import cn.dingyi.springcloud.entities.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.List;
/**
* author:dingyi
* time:2019/8/27 0027 16:59
*/
@RestController
public class ProductController_Consumer {
private static final String REST_URL_PREFIX="http://localhost:8001";
@Autowired
private RestTemplate restTemplate;
@RequestMapping(value = "/consumer/get/{id}")
public Product get(@PathVariable("id")Long id){
return restTemplate.getForObject(REST_URL_PREFIX + "/consumer/get/" + id,Product.class);
}
@RequestMapping(value = "consumer/get/list")
public List<Product> list(){
return restTemplate.getForObject(REST_URL_PREFIX + "/consumer/get/list",List.class);
}
@RequestMapping(value = "/consumer/add")
public boolean add(@RequestBody Product product){
return restTemplate.postForObject(REST_URL_PREFIX+"/product/add",product,boolean.class);
}
}
rexources包下:
application.yml:
server:
port: 80