版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第使用kotlin集成springboot开发的超详细教程目录一、安装支持插件二、maven配置注意三、创建入口函数类四、编写入口函数五、创建数据库对象六、创建仓库操作接口七、创建一个业务接口来声明业务八、创建一个业务接口实现来实现声明的业务九、创建一个http服务接口目前大多数都在使用java集成springboot进行开发,本文演示仅仅将java换成kotlin,其他不变的情况下进行开发。
一、安装支持插件
在idea中安装kotlin插件(大多数情况下会默认安装了)
二、maven配置注意
kotlin目前不支持lombok所以不能使用或引用lombok相关的插件或依赖包,下面是一个排除示例,同时也不支持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
!--关闭协程编译警告--
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的编译,基本上能满足常规的使用
三、创建入口函数类
四、编写入口函数
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相关API")
.title("ayouram-flow")
.version("1.0")
.build())
.pathMapping("/")
overridefunrun(varargargs:String){
println("***************************ok***********************************")
}
五、创建数据库对象
importcom.fasterxml.jackson.annotation.JsonFormat
importorg.hibernate.annotations.DynamicInsert
importorg.hibernate.annotations.DynamicUpdate
importjava.util.*
importjavax.persistence.*
/****
*设备流量规则
@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'设备编号'")
vardeviceNo:String=null
@Column(name="rules_no",columnDefinition="varchar(18)COMMENT'规则编号'")
varrulesNo:String=null
@JsonFormat(pattern="yyyy-MM-ddHH:mm:ss",timezone="GMT+8")
@Column(name="create_at",columnDefinition="datetimeCOMMENT'创建时间'")
varcreateAt:Date=null
@Column(name="update_at",columnDefinition="datetimeCOMMENT'修改时间'")
varupdateAt:Date=null
*触发jpaupdate代码需要执行的逻辑
@PreUpdate
funpreUpdate(){
updateAt=Date()
*自动设置必要字段的值
@PrePersist
funprePersist(){
updateAt=Date()
createAt=updateAt
deleted=BaseEnum.NOT_REMOVE.index
}
注解使用了hibernate的功能主要用于自动创建/更新表结构以及索引的生成,如果需要mybatis版本的,只需要去掉这里面的注释就好。
六、创建仓库操作接口
基于springboot-data-jpa的repository
@Repository
interfaceDeviceRulesRepository:JpaRepositoryDeviceRules,Long,
JpaSpecificationExecutorDeviceRules,QuerydslPredicateExecutorDeviceRules{
fungetAllByDeviceNoAndDeleted(deviceNo:String,deleted:Int):OptionalMutableListDeviceRules
}
七、创建一个业务接口来声明业务
interfaceDeviceService{
*查询设备的路由规则
funqueryDeviceFlowRules(aPageRequest:APageRequest):PageResultQueryDeviceFlowRulesVO
}
八、创建一个业务接口实现来实现声明的业务
@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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026届迪庆市重点中学中考猜题物理试卷含解析
- 2026届四川省蓬安县重点达标名校中考物理全真模拟试题含解析
- 做账实操-学校的成本核算
- 2026年贵州省安顺市重点达标名校中考物理押题卷含解析
- 2026年山东省济南市历下区中考物理最后冲刺模拟试卷含解析
- 中职护理护理职业素养培养课件
- 中风急性期护理概述
- 招标管理制度
- 2026春苏科版三年级下册信息技术期末练习卷及答案
- 【泉州提线木偶戏本土市场深化路径研究16000字(论文)】
- 2026年全国保密教育线上培训考试试题及完整附答案
- 初三道德与法治中考复习:开放性设问之倡议书、标语与活动方案专项突破教案
- 2025年合肥高新区社区工作者招聘考试试卷真题
- 中国胃肠间质瘤诊疗指南(2025版)
- 北京市石景山区2026年5月初三综合练习(二模)语文试卷(含答案)
- 2026福建漳州水仙药业股份有限公司招聘14人考试备考题库及答案解析
- 2026河南开封工程职业学院招聘57人笔试备考试题及答案解析
- 2026年安徽省检察机关招聘书记员考试真题
- 2025年荣耀AI隐私安全白皮书
- 2026届山东省聊城市临清市重点达标名校中考押题生物预测卷含解析
- 2026年高考(湖南卷)化学试题及答案
评论
0/150
提交评论