環境 | 版本 |
---|---|
JDK | 1.8 |
Maven | 3.6+ |
Maven模板 | maven-archetype-size |
刪除父工程src文件 |
回顧:html
① Maven中dependencyManagement和dependencies的區別java
Maven使用dependencyManagement元素來提供一種管理依賴版本號的方式。一般會在一個組織或者項 目的最頂層的父POM中看到。使用pom.xml中 的dependencyManagement元素能讓全部中引用一個依賴而不用顯示的列出版本號。Maven會沿着父子層次向上找,直到找到一個擁有dependencyManagement元素的項目,而後它就會使用這個元素中指定的版本號。另外若是某個子項目須要使用另外的版本只須要聲明version便可。注意:dependencyManagement裏只是聲明依賴,並不實現引入,所以子項目要顯示的聲明須要的依賴。mysql
② Maven中如何跳過單元測試web
在Idea的Maven側欄點擊閃電圖標,使Maven跳過test生命週期便可。spring
<?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.polaris</groupId> <artifactId>com.polaris.springcloud</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <modules> </modules> <!-- 統一管理jar包版本 --> <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> <log4j.version>1.2.17</log4j.version> <lombok.version>1.16.18</lombok.version> <mysql.version>5.1.47</mysql.version> <druid.version>1.1.16</druid.version> <mybatis.spring.boot.version>1.3.0</mybatis.spring.boot.version> </properties> <!-- 子模塊繼承以後,提供做用:鎖定版本+子modlue不用寫groupId和version --> <dependencyManagement> <dependencies> <!--spring boot 2.2.2--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.2.2.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <!--spring cloud Hoxton.SR1--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR1</version> <type>pom</type> <scope>import</scope> </dependency> <!--spring cloud alibaba 2.1.0.RELEASE--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2.1.0.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid.version}</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis.spring.boot.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> <optional>true</optional> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> <addResources>true</addResources> </configuration> </plugin> </plugins> </build> </project>
cloud-provider-payment8001sql
<?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>com.polaris.springcloud</artifactId> <groupId>com.polaris</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>cloud-provider-payment8001</artifactId> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency> <!--mysql-connector-java--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!--jdbc--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!-- 通用配置 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> </project>
server: port: 8001 spring: application: name: cloud-payment-service datasource: type: com.alibaba.druid.pool.DruidDataSource # 當前數據源操做類型 driver-class-name: org.gjt.mm.mysql.Driver # mysql驅動包 url: jdbc:mysql://mpolaris.top:3306/cloud-test?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: 123456 mybatis: mapperLocations: classpath:mapper/*.xml type-aliases-package: com.polaris.springcloud.entities # 全部Entity別名類所在包
/** * @Author polaris * @Date 2021/1/21 1:00 */ @SpringBootApplication public class PaymentMain { public static void main(String[] args) { SpringApplication.run(PaymentMain.class,args); } }
建表數據庫
entitiesapache
主實體 與 Json封裝體api
@Data @AllArgsConstructor @NoArgsConstructor public class Payment implements Serializable { private Long id; private String serial; }
@Data @AllArgsConstructor @NoArgsConstructor public class CommonResult<T> { private Integer code; private String message; private T data; public CommonResult(Integer code, String message){ this(code,message,null); } }
daorestful
接口PaymentDao與mybatis映射文件PaymentMapper
@Mapper public interface PaymentDao { int save(Payment payment); Payment getPaymentById(@Param("id") Long id); }
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.polaris.springcloud.dao.PaymentDao"> <insert id="save" parameterType="Payment" useGeneratedKeys="true" keyProperty="id"> insert into payment(serial) values(#{serial}); </insert> <resultMap id="BaseResultMap" type="com.polaris.springcloud.entities.Payment"> <id column="id" property="id" jdbcType="BIGINT"/> <id column="serial" property="serial" jdbcType="VARCHAR"/> </resultMap> <select id="getPaymentById" parameterType="Long" resultMap="BaseResultMap"> select * from payment where id=#{id}; </select> </mapper>
service
接口與實現類
@Service public class PaymentServiceImpl implements PaymentService { @Resource private PaymentDao paymentDao; @Override public int save(Payment payment) { return paymentDao.save(payment); } @Override public Payment getPaymentById(Long id) { return paymentDao.getPaymentById(id); } }
controller
@RestController @Slf4j @RequestMapping("/payment") public class PaymentController { @Resource private PaymentService paymentService; @PostMapping("/save") public CommonResult save(Payment payment) { int result = paymentService.save(payment); log.info("===> result: " + result); if(result > 0) { return new CommonResult(200,"保存到數據庫成功",result); } return new CommonResult(400,"保存到數據庫失敗",null); } @GetMapping("/get/{id}") public CommonResult<Payment> save(@PathVariable("id") Long id) { Payment paymentById = paymentService.getPaymentById(id); log.info("===> payment: " + paymentById); if(paymentById != null) { return new CommonResult(200,"查詢成功",paymentById); } return new CommonResult(400,"查詢失敗",null); } }
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> <addResources>true</addResources> </configuration> </plugin> </plugins> </build>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency>
快捷鍵 ctrl + shift + alt + /,打開Registry
勾選
重啓Idea,便可測試代碼時不用手動重啓
注意:該功能只能在開發階段使用,上線前必定要關閉
cloud-consumer-order80
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
server: port: 80
是什麼
RestTemplate提供了多種便捷 訪問遠程Http服務 的方法,是一種簡單便捷的訪問restful服務模板類,是Spring提供的用於訪問Rest服務的 客戶端模板工具類
如何使用
使用restTemplate訪問restful接口很是的簡單粗暴。
url
REST請求地址requestMap
請求參數ResponseBean.classs
HTTP響應轉換被轉換成的對象類型config配置類
@Configuration public class ApplicationContextConfig { @Bean public RestTemplate getRestTemplate() { return new RestTemplate(); } }
entities
與提供者支付模塊一致
controller
@RestController @Slf4j @RequestMapping("/consumer") public class OrderController { @Resource private RestTemplate restTemplate; private static final String PAYMENT_URL = "http://localhost:8001"; @GetMapping("/payment/save") public CommonResult<Payment> save(Payment payment) { return restTemplate.postForObject(PAYMENT_URL + "/payment/save", payment,CommonResult.class); } @GetMapping("/payment/get/{id}") public CommonResult<Payment> get(@PathVariable("id") Long id) { return restTemplate.getForObject(PAYMENT_URL + "/payment/get/" + id, CommonResult.class); } }
啓動兩個模塊沒有出現Run Dashbord的問題
在工程目錄下找.idea文件夾下的workspace.xml添加以下代碼便可
<component name="RunDashboard"> <option name="configurationTypes"> <set> <option value="SpringBootApplicationConfigurationType" /> </set> </option> <option name="ruleStates"> <list> <RuleState> <option name="name" value="ConfigurationTypeDashboardGroupingRule" /> </RuleState> <RuleState> <option name="name" value="StatusDashboardGroupingRule" /> </RuleState> </list> </option> </component>
插入成功可是沒有內容的問題
解決:支付模塊這裏必定要加@RequestBody註解
@PostMapping("/save") public CommonResult save(@RequestBody Payment payment) { int result = paymentService.save(payment); log.info("===> result: " + result); if(result > 0) { return new CommonResult(200,"保存到數據庫成功",result); } return new CommonResult(400,"保存到數據庫失敗",null); }
重複代碼,公共接口,第三方接口,工具類等均可以放在這裏
pom.xml
Hutool是一個小而全的Java工具類庫,是項目中「util」包友好的替代
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <!-- hutool工具類 --> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.1.0</version> </dependency> </dependencies>
將cloud-api-common安裝到maven倉庫
修改訂單80和支付8001代碼
刪除各自原先的enntities
各自添加pom內容
<!-- 公共模塊 --> <dependency> <groupId>com.polaris</groupId> <artifactId>cloud-api-common</artifactId> <version>${project.version}</version> </dependency>