springcloud 父类 pom.xml
本文内容纲要:
父类:pom.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<projectxmlns="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.0http://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>
<!--springCloud最新的稳定版FinchleySR2-->
<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
<?xmlversion="1.0"encoding="UTF-8"?>
<projectxmlns="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包下建实体类:
packagecn.dingyi.springcloud.entities;
importjava.io.Serializable;
/**
*author:dingyi
*time:2019/8/26002617:09
*/
publicclassProductimplementsSerializable{
privateLongpid;
privateStringproductName;
privateStringdbSource;
publicProduct(StringproductName){
this.productName=productName;
}
publicProduct(){
}
publicProduct(Longpid,StringproductName,StringdbSource){
this.pid=pid;
this.productName=productName;
this.dbSource=dbSource;
}
publicLonggetPid(){
returnpid;
}
publicvoidsetPid(Longpid){
this.pid=pid;
}
publicStringgetProductName(){
returnproductName;
}
publicvoidsetProductName(StringproductName){
this.productName=productName;
}
publicStringgetDbSource(){
returndbSource;
}
publicvoidsetDbSource(StringdbSource){
this.dbSource=dbSource;
}
}
子类2:
pom.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<projectxmlns="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.0http://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>
<!--springbootweb启动器-->
<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:
packagecn.dingyi.springcloud.mapper;
importcn.dingyi.springcloud.entities.Product;
importjava.util.List;
/**
*author:dingyi
*time:2019/8/27002710:58
*/
//@Mapper//如果不写可以必须在启动类上@MapperScan
publicinterfaceProductMapper{
ProductfindById(Longpid);
List<Product>findAll();
booleanaddProduct(Productproduct);
}
service:
packagecn.dingyi.springcloud.Service;
importcn.dingyi.springcloud.entities.Product;
importjava.util.List;
/**
*author:dingyi
*time:2019/8/27002715:35
*/
publicinterfaceProductService{
ProductfindById(Longpid);
List<Product>findAll();
booleanaddProduct(Productproduct);
}
serviceimpl:
packagecn.dingyi.springcloud.Service.impl;
importcn.dingyi.springcloud.Service.ProductService;
importcn.dingyi.springcloud.entities.Product;
importcn.dingyi.springcloud.mapper.ProductMapper;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.stereotype.Service;
importjava.util.List;
/**
*author:dingyi
*time:2019/8/27002715:36
*/
@Service
publicclassProductServiceImplimplementsProductService{
@Autowired
privateProductMapperproductMapper;
@Override
publicProductfindById(Longpid){
returnproductMapper.findById(pid);
}
@Override
publicList<Product>findAll(){
returnproductMapper.findAll();
}
@Override
publicbooleanaddProduct(Productproduct){
returnproductMapper.addProduct(product);
}
}
controller:
packagecn.dingyi.springcloud.controller;
importcn.dingyi.springcloud.Service.ProductService;
importcn.dingyi.springcloud.entities.Product;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.web.bind.annotation.*;
importjava.util.List;
/**
*author:dingyi
*time:2019/8/27002715:40
*/
@RestController
publicclassProductController{
@Autowired
privateProductServiceproductService;
@RequestMapping(value="/product/get/{id}",method=RequestMethod.GET)
publicProductget(@PathVariable("id")Longid){
returnproductService.findById(id);
}
@RequestMapping(value="/product/get/list",method=RequestMethod.GET)
publicList<Product>getAll(){
returnproductService.findAll();
}
@RequestMapping(value="/product/add",method=RequestMethod.GET)
publicbooleanadd(@RequestBodyProductproduct){
returnproductService.addProduct(product);
}
}
启动类:
packagecn.dingyi.springcloud;
importorg.mybatis.spring.annotation.MapperScan;
importorg.springframework.boot.SpringApplication;
importorg.springframework.boot.autoconfigure.SpringBootApplication;
/**
*author:dingyi
*time:2019/8/27002715:48
*/
@MapperScan("cn.dingyi.springcloud.mapper")
@SpringBootApplication
publicclassProductProvider_8001{
publicstaticvoidmain(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
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEconfiguration
PUBLIC"-//mybatis.org//DTDConfig3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!--开启驼峰命名,一定是这个名,改一个字母或大小写不一样都启动不了-->
<settingname="mapUnderscoreToCamelCase"value="true"/>
</settings>
</configuration>
mybatis:
mapper:
productMapper.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEmapper
PUBLIC"-//mybatis.org//DTDConfig3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mappernamespace="cn.dingyi.springcloud.mapper.ProductMapper">
<selectid="findById"resultType="Product"parameterType="Long">
select*fromproductwherepid=#{pid};
--SELECTpid,product_name,db_
--sourceFROMproductwherepid=#{pid};
</select>
<selectid="findAll"resultType="Product">
select*fromproduct;
--SELECTpid,product_name,db_sourceFROMproduct;
</select>
<insertid="addProduct"parameterType="Product">
insertintoproduct(productName,dbsource)values(#{productName},#{dbSource})
--INSERTINTOproduct(product_name,db_source)VALUES(#{productName},#{dbSource})
</insert>
</mapper>
子类3:消费者对象:
java包:
config:
ConfigBean:
packagecn.dingyi.springcloud.config;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
importorg.springframework.web.client.RestTemplate;
/**
*author:dingyi
*time:2019/8/27002716:56
*/
@Configuration
publicclassConfigBean{
//向容器中添加RestTemplate组件直接该组件调用EREST接口
@Bean
publicRestTemplategetRestTemplate(){
returnnewRestTemplate();
}
}
java包:
controller:
ProductController_Consumer:
packagecn.dingyi.springcloud.controller;
importcn.dingyi.springcloud.entities.Product;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.web.bind.annotation.PathVariable;
importorg.springframework.web.bind.annotation.RequestBody;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.bind.annotation.RestController;
importorg.springframework.web.client.RestTemplate;
importjava.util.List;
/**
*author:dingyi
*time:2019/8/27002716:59
*/
@RestController
publicclassProductController_Consumer{
privatestaticfinalStringREST_URL_PREFIX="http://localhost:8001";
@Autowired
privateRestTemplaterestTemplate;
@RequestMapping(value="/consumer/get/{id}")
publicProductget(@PathVariable("id")Longid){
returnrestTemplate.getForObject(REST_URL_PREFIX+"/consumer/get/"+id,Product.class);
}
@RequestMapping(value="consumer/get/list")
publicList<Product>list(){
returnrestTemplate.getForObject(REST_URL_PREFIX+"/consumer/get/list",List.class);
}
@RequestMapping(value="/consumer/add")
publicbooleanadd(@RequestBodyProductproduct){
returnrestTemplate.postForObject(REST_URL_PREFIX+"/product/add",product,boolean.class);
}
}
rexources包下:
application.yml:
server:
port:80
本文内容总结:
原文链接:https://www.cnblogs.com/dingyi-boss/p/11419227.html