SpringBoot3 + Kotlin + mybatis-plus + Swagger3后端开发样例

devtools/2024/9/22 14:58:21/

前言:

Kotlin 是一种在 JVM(Java 虚拟机)、Android 和浏览器端运行的静态类型编程语言。以下是关于 Kotlin 的总结介绍:
1、语言特性:
简洁性:Kotlin 旨在提供简洁且安全的代码,同时保持与 Java 的互操作性。
静态类型:Kotlin 支持静态类型,这有助于编译器在编译时捕获错误。
空安全:Kotlin 具有强大的空安全特性,可以避免许多常见的空指针异常。
扩展函数:允许为现有类添加新功能,而无需修改原始代码。
2、主要优势:
安全性:Kotlin 提供了一系列特性来增强代码的安全性,如空安全性和智能转换。
互操作性:Kotlin 与 Java 高度兼容,使得在现有 Java 项目中集成 Kotlin 变得容易。
简洁高效:Kotlin 代码通常更简洁,且执行效率高。
现代特性:支持函数式编程特性,如 lambda 表达式和高阶函数。
3、应用领域:
Android 开发:Kotlin 是 Android 开发的首选语言,许多新项目和库都采用 Kotlin 作为主要编程语言。
后端开发:Kotlin 可以与 Spring 等框架无缝集成,用于构建后端服务。
服务器端开发:适用于各种服务器端应用,包括使用 JVM 的应用。
前端开发:虽然主要用于后端和 Android,但 Kotlin/JS 版本也支持前端开发。
4、社区支持:
Kotlin 拥有活跃的社区和大量的学习资源,包括官方文档、教程和开源项目。
许多大型项目已经开始使用 Kotlin,如 Square 的 Retrofit 和 OkHttp。
5、发展趋势:
随着 Kotlin 的不断发展和完善,它在各种应用场景中的使用越来越广泛。
Kotlin 的空安全性和简洁性吸引了越来越多的开发者

一、引包

  <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.0.0</version></parent><properties><maven.compiler.source>17</maven.compiler.source><maven.compiler.target>17</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>${java.version}</maven.compiler.source><maven.compiler.target>${java.version}</maven.compiler.target><springfox.swagger3.version>3.0.0</springfox.swagger3.version><kotlin.version>1.8.21</kotlin.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId></dependency><dependency><groupId>com.zaxxer</groupId><artifactId>HikariCP</artifactId><version>3.2.0</version></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.3.2</version></dependency><dependency><groupId>org.springdoc</groupId><artifactId>springdoc-openapi-starter-webmvc-ui</artifactId><version>2.2.0</version></dependency><dependency><groupId>org.springdoc</groupId><artifactId>springdoc-openapi-starter-webmvc-api</artifactId><version>2.2.0</version></dependency><dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId><version>4.4.0</version></dependency><dependency><groupId>org.jetbrains.kotlin</groupId><artifactId>kotlin-stdlib-jdk8</artifactId><version>${kotlin.version}</version></dependency><dependency><groupId>org.jetbrains.kotlin</groupId><artifactId>kotlin-test</artifactId><version>${kotlin.version}</version><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><configuration><skip>true</skip></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-assembly-plugin</artifactId><configuration><descriptors><descriptor>assembly.xml</descriptor></descriptors></configuration><executions><execution><id>make-assembly</id><phase>package</phase><goals><goal>single</goal></goals></execution></executions></plugin><plugin><groupId>org.jetbrains.kotlin</groupId><artifactId>kotlin-maven-plugin</artifactId><version>${kotlin.version}</version><executions><execution><id>compile</id><phase>compile</phase><goals><goal>compile</goal></goals></execution><execution><id>test-compile</id><phase>test-compile</phase><goals><goal>test-compile</goal></goals></execution></executions><configuration><jvmTarget>${maven.compiler.target}</jvmTarget></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><executions><execution><id>compile</id><phase>compile</phase><goals><goal>compile</goal></goals></execution><execution><id>testCompile</id><phase>test-compile</phase><goals><goal>testCompile</goal></goals></execution></executions><configuration><source>${java.version}</source><target>${java.version}</target></configuration></plugin></plugins></build>

二、bean

import com.baomidou.mybatisplus.annotation.*
import java.io.Serializable
import java.math.BigDecimal/*** @author zc* @date 2024/2/23 10:26* @desc*/
@TableName("test_price")
class PriceBean: Serializable {@TableId(type = IdType.ASSIGN_UUID)var id: String? = null@TableField(value = "price")var price: BigDecimal? = null@Versionvar version: Int? = null
}

三、mapper

import com.baomidou.mybatisplus.core.mapper.BaseMapper
import com.zc.bean.PriceBean
import org.apache.ibatis.annotations.Mapper@Mapper
interface TestKotlinMapper: BaseMapper<PriceBean> {
}

四、service

import com.baomidou.mybatisplus.extension.service.IService
import com.zc.bean.PriceBean
import org.springframework.stereotype.Repository@Repository
interface TestKotlinService: IService<PriceBean> {
}

五、Impl

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.zc.bean.PriceBean
import com.zc.mapper.TestKotlinMapper
import org.springframework.stereotype.Service/*** @author zc* @date 2024/4/18 9:56* @desc*/
@Service
open class TestKotlinServcieImpl: ServiceImpl<TestKotlinMapper, PriceBean>(), TestKotlinService{}

六、controller

说明: swagger3 的实现请参考:SpringBoot3 集成Springdoc 实现Swagger3功能-CSDN博客

import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.zc.bean.PriceBean
import com.zc.service.TestKotlinService
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.Parameter
import io.swagger.v3.oas.annotations.tags.Tag
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.DeleteMapping
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.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController/*** @author zc* @date 2024/4/18 10:38* @desc*/
@RestController
@RequestMapping("/price")
@Tag(name = "kotlin")
class TestKotlinController {@Autowiredprivate lateinit var testKotlinService: TestKotlinService;@GetMapping("list")@Operation(summary = "list", description = "获取集合")fun getPriceBean(@RequestParam(name = "pageNum") @Parameter(name = "pageNum", description = "页数") pageNum: Long = 1,@RequestParam(name = "pageSize") @Parameter(name = "pageSize", description = "每页数") pageSize: Long = 10 ): List<PriceBean>{val page = Page<PriceBean>(pageNum, pageSize)return testKotlinService.list(page);}@PostMapping("add")@Operation(summary = "add", description = "创建")fun addPriceBean(@RequestBody priceBean: PriceBean): String{testKotlinService.save(priceBean);return "success"}@DeleteMapping("del/{id}")fun deletePriceBean(@PathVariable id: String): String{testKotlinService.removeById(id);return "success"}
}

七、swagger

八、学习文档

API 参考 · Kotlin 官方文档 中文版


http://www.ppmy.cn/devtools/7384.html

相关文章

Json三方库介绍

目录 Json是干什么的Json序列化代码Json反序列化代码 Json是干什么的 Json是一种轻量级的数据交换格式&#xff0c;也叫做数据序列化方式。Json完全独立于编程语言的文本格式来存储和表述数据。易于人阅读和编写&#xff0c;同时也易于机器解析和生成&#xff0c;并有效地提升…

逆向IDA中Dword,数据提取

我们可以看见数据是这样的&#xff0c;第一个是1cc 但是我们shifte就是 这个因为他的数据太大了&#xff0c;导致高位跑后面去了 这个时候&#xff0c;我们右键——convert——dword 这样就可以提取到争取的数据了 比如第一个数据 0x1cc a0xcc b0x1 print(hex((b<<8…

java设计模式之策略模式实操

一、背景 临床服务项目流向规则匹配&#xff0c;比如说医生开一个“CT”检查&#xff0c;该检查应该由哪个科室来执行&#xff0c;是通过流向规则配置来决定的&#xff0c;具体配置如下图&#xff1a; 通过相关的条件匹配&#xff0c;最终找到流向科室。 二、设计思路 有几个注…

【PyTorch】1-基础知识(张量、导数、CUDA)

PyTorch&#xff1a;1-基础知识 注&#xff1a;所有资料来源且归属于thorough-pytorch(https://datawhalechina.github.io/thorough-pytorch/)&#xff0c;下文仅为学习记录 1.1&#xff1a;张量 神经网络核心包&#xff1a;autograd&#xff08;自动微分&#xff09; 张量…

在线拍卖系统,基于SpringBoot+Vue+MySql开发的在线拍卖系统设计和实现

目录 一. 系统介绍 二. 功能模块 2.1. 管理员功能模块 2.2. 用户功能模块 2.3. 前台首页功能模块 2.4. 部分代码实现 一. 系统介绍 随着社会的发展&#xff0c;社会的各行各业都在利用信息化时代的优势。计算机的优势和普及使得各种信息系统的开发成为必需。 在线拍卖系…

electron项目打包慢、打包报错

项目使用了electron框架&#xff0c;在第一次打包或者网络条件不好的环境下进行打包时熟速度慢的出奇&#xff0c;甚至经常出现打包失败的情况&#xff08;如上面图片的报错&#xff09;。 这是因为&#xff0c;在electron打包的过程中&#xff0c;需要去官方源https://github.…

05集合-CollectionListSet

Collection体系的特点、使用场景总结 如果希望元素可以重复&#xff0c;又有索引&#xff0c;索引查询要快? 用ArrayList集合, 基于数组的。(用的最多) 如果希望元素可以重复&#xff0c;又有索引&#xff0c;增删首尾操作快? 用LinkedList集合, 基于链表的。 如果希望增…

【Ansible】02

【Ansible】01 Ansible playbook 剧本 ansible-playbook 常用于复杂任务的管理管理经常要完成的任务playbook也是通过模块和它的参数 , 在特定主机上执行任务playbook是一个文件 , 该文件中需要通过yaml格式进行书写将经常需要执行的任务写入一个文件剧本/文件中可以包换多…