技術/楊33
本文將以圖文的形式搭建微服務的父工程和子工程,它們之間互相依賴,子工程繼承父工程。最後給IDEA工具添加熱部署功能。
一、搭建一個父工程的操作步驟
步驟5:父工程只需保留pom.xml文件即可,主要用來依賴管理子工程。
以他的文件,隱藏以.idea、.iml等後綴結尾的文件,刪除src文件夾。這樣做的目的是讓項目外觀層次更加清晰。
步驟6: 將UTF-8設置統一的編碼格式
步驟7:idea打開註解支持
步驟8:選擇編譯的jdk版本
二、在父工程下創建子工程《服務提供者支付模塊》的步驟
步驟1:新建module
步驟2:改pom.xml文件
<code><
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
>cloudprojectartifactId
><
groupId
>com.project.cloudgroupId
><
version
>1.0-SNAPSHOTversion
>parent
><
modelVersion
>4.0.0modelVersion
><
artifactId
>cloud-provider-payment8001artifactId
><
dependencies
><
dependency
><
groupId
>org.springframework.bootgroupId
><
artifactId
>spring-boot-starter-webartifactId
>dependency
><
dependency
><
groupId
>org.springframework.bootgroupId
><
artifactId
>spring-boot-starter-actuatorartifactId
>dependency
><
dependency
><
groupId
>org.projectlombokgroupId
><
artifactId
>lombokartifactId
>dependency
><
dependency
><
groupId
>org.mybatis.spring.bootgroupId
><
artifactId
>mybatis-spring-boot-starterartifactId
>dependency
><
dependency
><
groupId
>mysqlgroupId
><
artifactId
>mysql-connector-javaartifactId
><
scope
>runtimescope
>dependency
><
dependency
><
groupId
>com.alibabagroupId
><
artifactId
>druidartifactId
>dependency
>dependencies
>project
>/<code>
步驟3:寫application.yml配置文件
<code>server:
port:
8001
spring:
application:
name:
cloud-provider-payment8001
datasource:
username:
root
password:
emc123123
url:
jdbc:mysql://192.168.109.130:3307/jdbc
driver-class-name:
com.mysql.jdbc.Driver
type:
com.alibaba.druid.pool.DruidDataSource
mybatis:
mapperLocations:
classpath:mapper/*.xml
type-aliases-package:
com.cloud.entities
/<code>
步驟4:編寫主啟動類
<code>package
com.cloud;import
org.springframework.boot.SpringApplication;import
org.springframework.boot.autoconfigure.SpringBootApplication;public
class
PaymentMain8001
{public
static
void
main
(String[] args)
{ SpringApplication.run(PaymentMain8001.
class
,args
); } }/<code>
步驟5:編寫實現業務的類。包括實體bean、dao類、service類、service實現類、controller類、*mapper.xml文件、建數據庫表。
- 實體bean
<code>package
com.cloud.entities;import
lombok.AllArgsConstructor;import
lombok.Data;import
lombok.NoArgsConstructor;import
java.io.Serializable;public
class
Payment
implements
Serializable
{private
long
id;private
String serial; }/<code>
<code>package
com.cloud.entities;import
lombok.Data;import
lombok.NoArgsConstructor;public
class
CommonResult
<T
> {private
Integer code;private
String message;private
Tdata
;public
CommonResult(Integer code, String message, Tdata
) {this
.code = code;this
.message = message;this
.data
=data
; } }/<code>
- dao類
<code>package
com
.cloud
.dao
;import
com
.cloud
.entities
.Payment
;import
org
.apache
.ibatis
.annotations
.Mapper
;import
org
.apache
.ibatis
.annotations
.Param
;@Mapper
public interface PaymentDao {public
int
create
(Payment
payment
);public
Payment
getPayment
(@Param
("id") Long id); }/<code>
- service類
<code>package
com.cloud.service;import
com.cloud.entities.Payment;import
org.apache.ibatis.annotations.Param;public
interface
PaymentService
{public
int
create
(Payment payment)
;public
PaymentgetPayment
(@Param(
"id"
) Long id) ; }/<code>
<code>package
com
.cloud
.service
.impl
;import
com
.cloud
.dao
.PaymentDao
;import
com
.cloud
.entities
.Payment
;import
com
.cloud
.service
.PaymentService
;import
org
.springframework
.stereotype
.Service
;import
javax
.annotation
.Resource
;@Service
public class PaymentServiceImpl implements PaymentService {@Resource
PaymentDao paymentDao;@Override
public int create(Payment payment) {return
paymentDao
.create
(payment
); }@Override
public Payment getPayment(Long id) {return
paymentDao
.getPayment
(id
); } }/<code>
- controller類
<code>package
com.cloud.controller;import
com.cloud.entities.CommonResult;import
com.cloud.entities.Payment;import
com.cloud.service.PaymentService;import
org.springframework.web.bind.annotation
.GetMapping;import
org.springframework.web.bind.annotation
.PathVariable;import
org.springframework.web.bind.annotation
.PostMapping;import
org.springframework.web.bind.annotation
.RestController;import
javax.annotation
.Resource;public
class
PaymentController
{ PaymentService paymentService;public
CommonResult create(Payment payment) { int result = paymentService.create(payment); System.out
.println("創建結果"
+ result);if
(result >0
) {return
new CommonResult(200
,"插入數據庫成功"
, result); }else
{return
new CommonResult(444
,"插入數據庫失敗"
,null
); } }public
CommonResult getPayment(Long
id) { Payment payment = paymentService.getPayment(id); System.out
.println("查詢結果"
+ payment);if
(payment !=null
) {return
new CommonResult(200
,"查詢數據庫成功"
, payment); }else
{return
new CommonResult(444
,"數據庫沒有對應記錄"
+ id,null
); } } }/<code>
- *mapper.xml文件
<code>><
mapper
namespace
="com.cloud.dao.PaymentDao"
><
resultMap
id
="BaseResultMap"
type
="com.cloud.entities.Payment"
><
id
column
="id"
property
="id"
jdbcType
="BIGINT"
>id
><
id
column
="serial"
property
="serial"
jdbcType
="VARCHAR"
>id
>resultMap
><
select
id
="getPayment"
resultType
="Long"
resultMap
="BaseResultMap"
> select * from payment where id = #{id}select
><
insert
id
="create"
parameterType
="Payment"
useGeneratedKeys
="true"
keyProperty
="id"
> insert into payment (serial) values (#{serial})insert
>mapper
>/<code>
- 建數據庫表
<code>DROP
TABLE
IF
EXISTS
`payment`
;CREATE
TABLE
`payment`
(`id`
bigint
(20
)NOT
NULL
AUTO_INCREMENTCOMMENT
'ID'
,`serial`
varchar
(200
)DEFAULT
''
, PRIMARYKEY
(`id`
) )ENGINE
=InnoDB
AUTO_INCREMENT=1
DEFAULT
CHARSET
=utf8;/<code>
步驟6:啟動子工程,測試controller類的兩個接口是否成功
使用postman工具調試接口。
三、IDEA添加熱部署devtools
有了熱部署,在微服務架構中,不用再挨個重啟多個服務,省事很多。
1、子工程pom.xml中添加devtools依賴
<code><
dependency
><
groupId
>org.springframework.bootgroupId
><
artifactId
>spring-boot-devtoolsartifactId
><
scope
>runtimescope
><
optional
>trueoptional
>dependency
>/<code>
2、父工程添加maven插件
<code><
build
><
plugins
><
plugin
><
groupId
>org.springframework.bootgroupId
><
artifactId
>spring-boot-maven-pluginartifactId
><
configuration
><
fork
>truefork
><
addResources
>trueaddResources
>
configuration
>plugin
>plugins
>build
>/<code>
3、setting中設置compiler
4、快捷鍵 ctrl+shift+alt+/,打開下面窗口,打勾,關閉即可。
5、重啟IDEA,熱部署就生效了。
作者:楊33,北京互聯網公司在職Java開發,專注分享寫作乾貨。歡迎關注我,期待你的點贊評論。