亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何使用kotlin集成springboot開發

發布時間:2021-09-05 19:23:42 來源:億速云 閱讀:175 作者:小新 欄目:開發技術

這篇文章給大家分享的是有關如何使用kotlin集成springboot開發的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

一、安裝支持插件

idea中安裝 kotlin插件(大多數情況下會默認安裝了)

如何使用kotlin集成springboot開發

二、maven配置 注意

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

<dependency>
            <groupId>com.ayouran.common</groupId>
            <artifactId>a-common</artifactId>
            <version>1.0.0-SNAPSHOT</version>
            <exclusions>
                 <exclusion>
                       <groupId>org.projectlombok</groupId>
                       <artifactId>lombok</artifactId>
                 </exclusion>
            </exclusions>
</dependency>

maven的屬性配置

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <kotlin.version>1.3.21</kotlin.version>
        <!--增量編譯-->
        <kotlin.compiler.incremental>true</kotlin.compiler.incremental>
        <!--關閉協程編譯警告-->
        <experimentalCoroutines>enable</experimentalCoroutines>
        <spring-boot.version>2.2.4.RELEASE</spring-boot.version>
        <spring-cloud.version>Hoxton.SR1</spring-cloud.version>
        <swagger.version>2.7.0</swagger.version>
        <main.class>com.lc.github.KotlinDemoApplication</main.class>
        <querydsl.version>4.2.1</querydsl.version>
        <mapstruct.version>1.3.1.Final</mapstruct.version>
    </properties>

必要的依賴

<dependencies>
       <!--反射支持包,不需要可以去掉-->
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-reflect</artifactId>
            <version>${kotlin.version}</version>
        </dependency>
     <!--jdk8下的 kotlin支持包-->
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-stdlib-jdk8</artifactId>
            <version>${kotlin.version}</version>
        </dependency>
    </dependencies>

編譯部分

 <build>
        <plugins>
            <plugin>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-maven-plugin</artifactId>
                <configuration>
                    <correctErrorTypes>true</correctErrorTypes>
                    <languageVersion>${kotlin.language.version}</languageVersion>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.jetbrains.kotlin</groupId>
                        <artifactId>kotlin-maven-allopen</artifactId>
                        <version>${kotlin.version}</version>
                    </dependency>
                </dependencies>
                <executions>
                    <execution>
                        <id>kapt</id>
                        <goals>
                            <goal>kapt</goal>
                        </goals>
                        <configuration>
                            <correctErrorTypes>true</correctErrorTypes>
                            <sourceDirs>
                                <sourceDir>src/main/kotlin</sourceDir>
                                <sourceDir>src/main/java</sourceDir>
                            </sourceDirs>
                            <annotationProcessorPaths>
                                <annotationProcessorPath>
                                    <groupId>com.google.dagger</groupId>
                                    <artifactId>dagger-compiler</artifactId>
                                    <version>2.9</version>
                                </annotationProcessorPath>
                                <annotationProcessorPath>
                                    <groupId>com.querydsl</groupId>
                                    <artifactId>querydsl-apt</artifactId>
                                    <version>${querydsl.version}</version>
                                    <classifier>jpa</classifier>
                                </annotationProcessorPath>
                                <annotationProcessorPath>
                                    <groupId>org.mapstruct</groupId>
                                    <artifactId>mapstruct-processor</artifactId>
                                    <version>${mapstruct.version}</version>
                                </annotationProcessorPath>
                            </annotationProcessorPaths>
                        </configuration>
                    </execution>
                    <execution>
                        <id>compile</id>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                        <configuration>
                            <sourceDirs>
                                <sourceDir>src/main/kotlin</sourceDir>
                                <sourceDir>src/main/java</sourceDir>
                            </sourceDirs>
                        </configuration>
                    </execution>
                    <execution>
                        <id>test-kapt</id>
                        <goals>
                            <goal>test-kapt</goal>
                        </goals>
                        <configuration>
                            <sourceDirs>
                                <sourceDir>src/test/kotlin</sourceDir>
                                <sourceDir>src/test/java</sourceDir>
                            </sourceDirs>
                            <annotationProcessorPaths>
                                <annotationProcessorPath>
                                    <groupId>com.google.dagger</groupId>
                                    <artifactId>dagger-compiler</artifactId>
                                    <version>2.9</version>
                                </annotationProcessorPath>
                                <annotationProcessorPath>
                                    <groupId>com.querydsl</groupId>
                                    <artifactId>querydsl-apt</artifactId>
                                    <version>${querydsl.version}</version>
                                    <classifier>jpa</classifier>
                                </annotationProcessorPath>
                                <annotationProcessorPath>
                                    <groupId>org.mapstruct</groupId>
                                    <artifactId>mapstruct-processor</artifactId>
                                    <version>${mapstruct.version}</version>
                                </annotationProcessorPath>
                            </annotationProcessorPaths>
                        </configuration>
                    </execution>
                    <execution>
                        <id>test-compile</id>
                        <goals>
                            <goal>test-compile</goal>
                        </goals>
                        <configuration>
                            <sourceDirs>
                                <sourceDir>src/test/kotlin</sourceDir>
                                <sourceDir>src/test/java</sourceDir>
                                <sourceDir>target/generated-sources/kapt/test</sourceDir>
                            </sourceDirs>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <proc>none</proc>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>${lombok.version}</version>
                        </path>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>${mapstruct.version}</version>
                        </path>
                    </annotationProcessorPaths>
                    <compilerArgs>
                        <arg>-Amapstruct.suppressGeneratorTimestamp=true</arg>
                        <arg>-Amapstruct.defaultComponentModel=spring</arg>
                    </compilerArgs>
                </configuration>
                <executions>
                    <!-- Replacing default-compile as it is treated specially by maven -->
                    <execution>
                        <id>default-compile</id>
                        <phase>none</phase>
                    </execution>
                    <!-- Replacing default-testCompile as it is treated specially by maven -->
                    <execution>
                        <id>default-testCompile</id>
                        <phase>none</phase>
                    </execution>
                    <execution>
                        <id>java-compile</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>java-test-compile</id>
                        <phase>test-compile</phase>
                        <goals>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <archive>
                                <manifest>
                                    <mainClass>${main.class}</mainClass>
                                </manifest>
                            </archive>
                            <descriptorRefs>
                                <descriptorRef>jar-with-dependencies</descriptorRef>
                            </descriptorRefs>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

javakotlin混合的情況,在上面的 <plugins>下加入下面的編譯插件

<plugin>
                            <groupId>org.apache.maven.plugins</groupId>
                            <artifactId>maven-compiler-plugin</artifactId>
                            <version>3.8.1</version>
                            <configuration>
                                <proc>none</proc>
                                <source>${java.version}</source>
                                <target>${java.version}</target>
                                <annotationProcessorPaths>
                                    <path>
                                        <groupId>org.mapstruct</groupId>
                                        <artifactId>mapstruct-processor</artifactId>
                                        <version>${mapstruct.version}</version>
                                    </path>
                                </annotationProcessorPaths>
                                <compilerArgs>
                                    <arg>-Amapstruct.suppressGeneratorTimestamp=true</arg>
                                    <arg>-Amapstruct.defaultComponentModel=spring</arg>
                                </compilerArgs>
                            </configuration>
                            <executions>
                                <!-- Replacing default-compile as it is treated specially by maven -->
                                <execution>
                                    <id>default-compile</id>
                                    <phase>none</phase>
                                </execution>
                                <!-- Replacing default-testCompile as it is treated specially by maven -->
                                <execution>
                                    <id>default-testCompile</id>
                                    <phase>none</phase>
                                </execution>
                                <execution>
                                    <id>java-compile</id>
                                    <phase>compile</phase>
                                    <goals>
                                        <goal>compile</goal>
                                    </goals>
                                </execution>
                                <execution>
                                    <id>java-test-compile</id>
                                    <phase>test-compile</phase>
                                    <goals>
                                        <goal>testCompile</goal>
                                    </goals>
                                </execution>
                            </executions>
                        </plugin>

maven配置集成了 querydslmapstructdagger2的編譯,基本上能滿足常規的使用

三、創建入口函數類

如何使用kotlin集成springboot開發

四、編寫入口函數

springboot的啟動方法及 swagger的配置

@EnableAsync
@EnableSwagger2
@EnableScheduling
@SpringBootApplication
class KotlinDemoApplication : CommandLineRunner {
    companion object {
        @JvmStatic
        fun main(args: Array<String>) {
            SpringApplication.run(KotlinDemoApplication::class.java, *args)
        }
    }

    @Bean
    fun api(): Docket {
        return Docket(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("/")
    }

    override fun run(vararg args: String?) {
        println("*************************** ok ***********************************")
    }
}

五、創建數據庫對象

import com.fasterxml.jackson.annotation.JsonFormat
import org.hibernate.annotations.DynamicInsert
import org.hibernate.annotations.DynamicUpdate
import java.util.*
import javax.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
class DeviceRules {

    @Id
    @Column(name = "id", columnDefinition = "bigint(20) COMMENT 'ID,自增'")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    var id: Long? = null

    @Column(name = "device_no", columnDefinition = "varchar(18) COMMENT '設備編號'")
    var deviceNo: String? = null

    @Column(name = "rules_no", columnDefinition = "varchar(18) COMMENT '規則編號'")
    var rulesNo: String? = null

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @Column(name = "create_at", columnDefinition = "datetime COMMENT '創建時間'")
    var createAt: Date? = null

    @Column(name = "update_at", columnDefinition = "datetime COMMENT '修改時間'")
    var updateAt: Date? = null

    /**
     * 觸發jpa update代碼需要執行的邏輯
     */
    @PreUpdate
    fun preUpdate() {
        updateAt = Date()
    }

    /**
     * 自動設置必要字段的值
     */
    @PrePersist
    fun prePersist() {
        updateAt = Date()
        createAt = updateAt
        deleted = BaseEnum.NOT_REMOVE.index
    }
}

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

六、創建倉庫操作接口

基于 springboot-data-jparepository

@Repository
interface DeviceRulesRepository : JpaRepository<DeviceRules, Long>,
        JpaSpecificationExecutor<DeviceRules>, QuerydslPredicateExecutor<DeviceRules> {
    fun getAllByDeviceNoAndDeleted(deviceNo: String, deleted: Int): Optional<MutableList<DeviceRules>>
}

七、創建一個業務接口來聲明業務

interface DeviceService {
  
    /**
     * 查詢設備的路由規則
     */
    fun queryDeviceFlowRules(aPageRequest: APageRequest): PageResult<QueryDeviceFlowRulesVO>
}

八、創建一個業務接口實現來實現聲明的業務

@Service
class DeviceServiceImpl @Autowired
constructor(private val deviceRepository: DeviceRepository,
            private val deviceRulesRepository: DeviceRulesRepository,
            private val querydslUtlis: QuerydslUtlis,
            private val deviceMapstruct: DeviceMapstruct) : DeviceService {
    private val logger = LoggerFactory.getLogger(javaClass)
    
    override fun queryDeviceFlowRules(aPageRequest: APageRequest): PageResult<QueryDeviceFlowRulesVO> {
        val qDeviceRules = QDeviceRules.deviceRules
        val qFlowRules = QFlowRules.flowRules
        var rredicate: Predicate? = null
        if (StringUtils.isNotBlank(aPageRequest.query)) rredicate = qDeviceRules.deviceNo.eq(aPageRequest.query)
        val exprs = arrayOf<Expression<*>>(qDeviceRules.deviceNo, qDeviceRules.deleted, qFlowRules.rulesNo, qFlowRules.flowMax,
                qFlowRules.startTime, qFlowRules.endTime)
        val results = querydslUtlis.getQueryFactory()
                .select(*exprs)
                .from(qDeviceRules)
                .where(ExpressionUtils.allOf(rredicate))
                .leftJoin(qFlowRules)
                .on(qDeviceRules.rulesNo.eq(qFlowRules.rulesNo))
                .orderBy(qDeviceRules.createAt.desc())
                .offset((aPageRequest.pageIndex!! - 1) * aPageRequest.pageSize!!)
                .limit(aPageRequest.pageSize!!)
                .fetchResults()
        return PageUtlis.retPage(results, querydslUtlis.getCollection(results.results, exprs, QueryDeviceFlowRulesVO::class.java) as Collection<QueryDeviceFlowRulesVO>)
    }
}

這里使用了 querydsl來完成一個多表查詢

九、創建一個 http服務接口

@RestWrapper
@RestController
@RequestMapping("/device")
@Api(value = "device", description = "設備相關接口", tags = ["device"])
class DeviceController @Autowired
constructor(private val deviceService: DeviceService) {

    @GetMapping("/query_device")
    fun queryDevice(aPageRequest: APageRequest) = deviceService.queryDevice(aPageRequest)

    @GetMapping("/query_device_flow_rules")
    fun queryDeviceFlowRules(aPageRequest: APageRequest) = deviceService.queryDeviceFlowRules(aPageRequest)
}

至此完成一個基本的開發過程,大多數情況下可以直接將 java代碼粘貼到 kotlin文件中,會自動轉換成合適的 kotlin代碼(偶爾需要自己調整,畢竟編輯器不是萬能的)

感謝各位的閱讀!關于“如何使用kotlin集成springboot開發”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

营口市| 麻阳| 安国市| 孙吴县| 富顺县| 嵩明县| 且末县| 荥阳市| 灵石县| 壤塘县| 伊金霍洛旗| 牙克石市| 凌海市| 皮山县| 瑞金市| 洛川县| 亳州市| 临桂县| 隆德县| 宁都县| 新邵县| 新密市| 监利县| 义马市| 仙居县| 黑山县| 台南市| 吉林市| 山西省| 曲麻莱县| 吉隆县| 砚山县| 禹州市| 北票市| 延长县| 武冈市| 镇沅| 开江县| 金山区| 平武县| 凌源市|