使用kotlin集成springboot開發(fā)的超詳細(xì)教程_第1頁
使用kotlin集成springboot開發(fā)的超詳細(xì)教程_第2頁
使用kotlin集成springboot開發(fā)的超詳細(xì)教程_第3頁
使用kotlin集成springboot開發(fā)的超詳細(xì)教程_第4頁
使用kotlin集成springboot開發(fā)的超詳細(xì)教程_第5頁
已閱讀5頁,還剩12頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)

文檔簡介

第使用kotlin集成springboot開發(fā)的超詳細(xì)教程目錄一、安裝支持插件二、maven配置注意三、創(chuàng)建入口函數(shù)類四、編寫入口函數(shù)五、創(chuàng)建數(shù)據(jù)庫對象六、創(chuàng)建倉庫操作接口七、創(chuàng)建一個業(yè)務(wù)接口來聲明業(yè)務(wù)八、創(chuàng)建一個業(yè)務(wù)接口實現(xiàn)來實現(xiàn)聲明的業(yè)務(wù)九、創(chuàng)建一個http服務(wù)接口目前大多數(shù)都在使用java集成springboot進行開發(fā),本文演示僅僅將java換成kotlin,其他不變的情況下進行開發(fā)。

一、安裝支持插件

在idea中安裝kotlin插件(大多數(shù)情況下會默認(rèn)安裝了)

二、maven配置注意

kotlin目前不支持lombok所以不能使用或引用lombok相關(guān)的插件或依賴包,下面是一個排除示例,同時也不支持maven-enforcer-plugin

dependency

groupIdmon/groupId

artifactIda-common/artifactId

version1.0.0-SNAPSHOT/version

exclusions

exclusion

groupIdjectlombok/groupId

artifactIdlombok/artifactId

/exclusion

/exclusions

/dependency

maven的屬性配置

properties

project.build.sourceEncodingUTF-8/project.build.sourceEncoding

project.reporting.outputEncodingUTF-8/project.reporting.outputEncoding

java.version1.8/java.version

piler.source1.8/piler.source

piler.target1.8/piler.target

kotlin.version1.3.21/kotlin.version

!--增量編譯--

piler.incrementaltrue/piler.incremental

!--關(guān)閉協(xié)程編譯警告--

experimentalCoroutinesenable/experimentalCoroutines

spring-boot.version2.2.4.RELEASE/spring-boot.version

spring-cloud.versionHoxton.SR1/spring-cloud.version

swagger.version2.7.0/swagger.version

main.classcom.lc.github.KotlinDemoApplication/main.class

querydsl.version4.2.1/querydsl.version

mapstruct.version1.3.1.Final/mapstruct.version

/properties

必要的依賴

dependencies

!--反射支持包,不需要可以去掉--

dependency

groupIdorg.jetbrains.kotlin/groupId

artifactIdkotlin-reflect/artifactId

version${kotlin.version}/version

/dependency

!--jdk8下的kotlin支持包--

dependency

groupIdorg.jetbrains.kotlin/groupId

artifactIdkotlin-stdlib-jdk8/artifactId

version${kotlin.version}/version

/dependency

/dependencies

編譯部分

build

plugins

plugin

groupIdorg.jetbrains.kotlin/groupId

artifactIdkotlin-maven-plugin/artifactId

configuration

correctErrorTypestrue/correctErrorTypes

languageVersion${kotlin.language.version}/languageVersion

/configuration

dependencies

dependency

groupIdorg.jetbrains.kotlin/groupId

artifactIdkotlin-maven-allopen/artifactId

version${kotlin.version}/version

/dependency

/dependencies

executions

execution

idkapt/id

goals

goalkapt/goal

/goals

configuration

correctErrorTypestrue/correctErrorTypes

sourceDirs

sourceDirsrc/main/kotlin/sourceDir

sourceDirsrc/main/java/sourceDir

/sourceDirs

annotationProcessorPaths

annotationProcessorPath

groupIdcom.google.dagger/groupId

artifactIddagger-compiler/artifactId

version2.9/version

/annotationProcessorPath

annotationProcessorPath

groupIdcom.querydsl/groupId

artifactIdquerydsl-apt/artifactId

version${querydsl.version}/version

classifierjpa/classifier

/annotationProcessorPath

annotationProcessorPath

groupIdorg.mapstruct/groupId

artifactIdmapstruct-processor/artifactId

version${mapstruct.version}/version

/annotationProcessorPath

/annotationProcessorPaths

/configuration

/execution

execution

idcompile/id

goals

goalcompile/goal

/goals

configuration

sourceDirs

sourceDirsrc/main/kotlin/sourceDir

sourceDirsrc/main/java/sourceDir

/sourceDirs

/configuration

/execution

execution

idtest-kapt/id

goals

goaltest-kapt/goal

/goals

configuration

sourceDirs

sourceDirsrc/test/kotlin/sourceDir

sourceDirsrc/test/java/sourceDir

/sourceDirs

annotationProcessorPaths

annotationProcessorPath

groupIdcom.google.dagger/groupId

artifactIddagger-compiler/artifactId

version2.9/version

/annotationProcessorPath

annotationProcessorPath

groupIdcom.querydsl/groupId

artifactIdquerydsl-apt/artifactId

version${querydsl.version}/version

classifierjpa/classifier

/annotationProcessorPath

annotationProcessorPath

groupIdorg.mapstruct/groupId

artifactIdmapstruct-processor/artifactId

version${mapstruct.version}/version

/annotationProcessorPath

/annotationProcessorPaths

/configuration

/execution

execution

idtest-compile/id

goals

goaltest-compile/goal

/goals

configuration

sourceDirs

sourceDirsrc/test/kotlin/sourceDir

sourceDirsrc/test/java/sourceDir

sourceDirtarget/generated-sources/kapt/test/sourceDir

/sourceDirs

/configuration

/execution

/executions

/plugin

plugin

groupIdorg.apache.maven.plugins/groupId

artifactIdmaven-compiler-plugin/artifactId

version3.8.1/version

configuration

procnone/proc

source${java.version}/source

target${java.version}/target

annotationProcessorPaths

path

groupIdjectlombok/groupId

artifactIdlombok/artifactId

version${lombok.version}/version

/path

path

groupIdorg.mapstruct/groupId

artifactIdmapstruct-processor/artifactId

version${mapstruct.version}/version

/path

/annotationProcessorPaths

compilerArgs

arg-Amapstruct.suppressGeneratorTimestamp=true/arg

arg-Amapstruct.defaultComponentModel=spring/arg

/compilerArgs

/configuration

executions

!--Replacingdefault-compileasitistreatedspeciallybymaven--

execution

iddefault-compile/id

phasenone/phase

/execution

!--Replacingdefault-testCompileasitistreatedspeciallybymaven--

execution

iddefault-testCompile/id

phasenone/phase

/execution

execution

idjava-compile/id

phasecompile/phase

goals

goalcompile/goal

/goals

/execution

execution

idjava-test-compile/id

phasetest-compile/phase

goals

goaltestCompile/goal

/goals

/execution

/executions

/plugin

plugin

groupIdorg.apache.maven.plugins/groupId

artifactIdmaven-assembly-plugin/artifactId

version2.6/version

executions

execution

idmake-assembly/id

phasepackage/phase

goals

goalsingle/goal

/goals

configuration

archive

manifest

mainClass${main.class}/mainClass

/manifest

/archive

descriptorRefs

descriptorRefjar-with-dependencies/descriptorRef

/descriptorRefs

/configuration

/execution

/executions

/plugin

/plugins

/build

有java與kotlin混合的情況,在上面的plugins下加入下面的編譯插件

plugin

groupIdorg.apache.maven.plugins/groupId

artifactIdmaven-compiler-plugin/artifactId

version3.8.1/version

configuration

procnone/proc

source${java.version}/source

target${java.version}/target

annotationProcessorPaths

path

groupIdorg.mapstruct/groupId

artifactIdmapstruct-processor/artifactId

version${mapstruct.version}/version

/path

/annotationProcessorPaths

compilerArgs

arg-Amapstruct.suppressGeneratorTimestamp=true/arg

arg-Amapstruct.defaultComponentModel=spring/arg

/compilerArgs

/configuration

executions

!--Replacingdefault-compileasitistreatedspeciallybymaven--

execution

iddefault-compile/id

phasenone/phase

/execution

!--Replacingdefault-testCompileasitistreatedspeciallybymaven--

execution

iddefault-testCompile/id

phasenone/phase

/execution

execution

idjava-compile/id

phasecompile/phase

goals

goalcompile/goal

/goals

/execution

execution

idjava-test-compile/id

phasetest-compile/phase

goals

goaltestCompile/goal

/goals

/execution

/executions

/plugin

此maven配置集成了querydsl、mapstruct、dagger2的編譯,基本上能滿足常規(guī)的使用

三、創(chuàng)建入口函數(shù)類

四、編寫入口函數(shù)

springboot的啟動方法及swagger的配置

@EnableAsync

@EnableSwagger2

@EnableScheduling

@SpringBootApplication

classKotlinDemoApplication:CommandLineRunner{

companionobject{

@JvmStatic

funmain(args:ArrayString){

SpringApplication.run(KotlinDemoApplication::class.java,*args)

@Bean

funapi():Docket{

returnDocket(DocumentationType.SWAGGER_2)

.ignoredParameterTypes(Session::class.java)

.select()

//.apis(RequestHandlerSelectors.any())

.apis(RequestHandlerSelectors.basePackage("com.ayouran.flow.controllers"))

.paths(PathSelectors.any())

.build()

.apiInfo(ApiInfoBuilder()

.description("ayouram-flow相關(guān)API")

.title("ayouram-flow")

.version("1.0")

.build())

.pathMapping("/")

overridefunrun(varargargs:String){

println("***************************ok***********************************")

}

五、創(chuàng)建數(shù)據(jù)庫對象

importcom.fasterxml.jackson.annotation.JsonFormat

importorg.hibernate.annotations.DynamicInsert

importorg.hibernate.annotations.DynamicUpdate

importjava.util.*

importjavax.persistence.*

/****

*設(shè)備流量規(guī)則

@Entity

@Table(name="device_rules",

indexes=[Index(name="device_no",columnList="device_no"),

Index(name="rules_no",columnList="rules_no"),

Index(name="deleted",columnList="deleted")],

uniqueConstraints=[UniqueConstraint(name="device_no_rules_no",columnNames=["device_no","rules_no"])])

@DynamicUpdate

@DynamicInsert

classDeviceRules{

@Column(name="id",columnDefinition="bigint(20)COMMENT'ID,自增'")

@GeneratedValue(strategy=GenerationType.IDENTITY)

varid:Long=null

@Column(name="device_no",columnDefinition="varchar(18)COMMENT'設(shè)備編號'")

vardeviceNo:String=null

@Column(name="rules_no",columnDefinition="varchar(18)COMMENT'規(guī)則編號'")

varrulesNo:String=null

@JsonFormat(pattern="yyyy-MM-ddHH:mm:ss",timezone="GMT+8")

@Column(name="create_at",columnDefinition="datetimeCOMMENT'創(chuàng)建時間'")

varcreateAt:Date=null

@Column(name="update_at",columnDefinition="datetimeCOMMENT'修改時間'")

varupdateAt:Date=null

*觸發(fā)jpaupdate代碼需要執(zhí)行的邏輯

@PreUpdate

funpreUpdate(){

updateAt=Date()

*自動設(shè)置必要字段的值

@PrePersist

funprePersist(){

updateAt=Date()

createAt=updateAt

deleted=BaseEnum.NOT_REMOVE.index

}

注解使用了hibernate的功能主要用于自動創(chuàng)建/更新表結(jié)構(gòu)以及索引的生成,如果需要mybatis版本的,只需要去掉這里面的注釋就好。

六、創(chuàng)建倉庫操作接口

基于springboot-data-jpa的repository

@Repository

interfaceDeviceRulesRepository:JpaRepositoryDeviceRules,Long,

JpaSpecificationExecutorDeviceRules,QuerydslPredicateExecutorDeviceRules{

fungetAllByDeviceNoAndDeleted(deviceNo:String,deleted:Int):OptionalMutableListDeviceRules

}

七、創(chuàng)建一個業(yè)務(wù)接口來聲明業(yè)務(wù)

interfaceDeviceService{

*查詢設(shè)備的路由規(guī)則

funqueryDeviceFlowRules(aPageRequest:APageRequest):PageResultQueryDeviceFlowRulesVO

}

八、創(chuàng)建一個業(yè)務(wù)接口實現(xiàn)來實現(xiàn)聲明的業(yè)務(wù)

@Service

classDeviceServiceImpl@Autowired

constructor(privatevaldeviceRepository:DeviceRepository,

privatevaldeviceRulesRepository:DeviceRulesRepository,

privatevalquerydslUtlis:QuerydslUtlis,

privatevaldeviceMapstruct:DeviceMapstruct):DeviceService{

privatevallogger=LoggerFactory.getLogger(javaClass)

overridefunqueryDeviceFlowRules(aPageRequest:APageRequest):PageResultQueryDeviceFlowRulesVO{

valqDeviceRules=QDeviceRules.deviceRules

valqFlowRules=QFlowRules.flowRules

varrredicate:Predicate=null

if(StringUtils.isNotBlank(aPageRequest.query))rredicate=qDeviceRules.deviceNo.eq(aPageRequest.query)

valexprs=arrayOfExpression*(qDeviceRules.devic

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論