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

news/2024/9/24 23:28:12/

前言:

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/news/1428236.html

相关文章

SG-多项式平滑滤波器(Codesys完整源代码+算法详解)

1、PLC的一阶低通滤波器实现 PLC信号处理系列之一阶低通(RC)滤波器算法_一阶rc滤波器 数字-CSDN博客文章浏览阅读4.2k次。1、先看看RC滤波的优缺点 优点:采用数字滤波算法来实现动态的RC滤波,则能很好的克服模拟滤波器的缺点; 1、在模拟常数要求较大的场合这种算法显得更为…

【回溯】Leetcode 131. 分割回文串【中等】

分割回文串 给你一个字符串 s&#xff0c;请你将 s 分割成一些子串&#xff0c;使每个子串都是 回文串 。返回 s 所有可能的分割方案 示例 1&#xff1a; 输入&#xff1a; s “aab” 输出&#xff1a;[[“a”,“a”,“b”],[“aa”,“b”]] 解题思路 1、使用回溯算法来生…

黑客常用命令

环境变量 bat是批处理文件 复制命令行 ctrlc 鼠标右击 enter 粘贴 ctrlv 鼠标右击 域名是ip的另一种表现形式 ping www.baidu.com -n 2 ping2次 TTL值可以用于大概知道对方服务器操作系统类型 netstat -an net user显示系统的用户 net user abc adb /add 创建用户…

OpenAI、蚂蚁集团、谷歌、科大讯飞等联合编制大模型安全国际标准,已正式发布

4月15日-19日&#xff0c;第27届联合国科技大会在瑞士日内瓦召开。16日&#xff0c;在以“塑造AI的未来”为主题的AI边会上&#xff0c;世界数字技术院&#xff08;WDTA&#xff09;发布了一系列突破性成果&#xff0c;包括《生成式人工智能应用安全测试标准》和《大语言模型安…

Linux 认识与学习Bash——2

1 read 从键盘读取变量的值 read 后面不带变量&#xff0c;那么默认会给REPLY变量赋值 #!/bin/bash echo -n "请输入你的名字&#xff1a;" read name echo "欢迎您 $name" echo "----------------"echo -n "请输入你的名字2&#xff1a;&q…

双臂复合机器人平台叠方块例程使用与自启设置

睿尔曼双臂升降复合机器人平台&#xff0c;旨在为机器人教育提供强大的实训平台&#xff0c;该平台全自主研发&#xff0c;实现机器人建图导航、路径规划&#xff0c;机械臂运动学、动力学、轨迹规划、视觉识别等算法和应用&#xff0c;提供开放式的软件框架&#xff0c;为教学…

Rust 入门-更换镜像源(MAC)

1、创建或修改文件内容 首先是在 crates.io 之外添加新的注册服务&#xff0c;在 $HOME/.cargo/config.toml &#xff08;如果文件不存在则手动创建一个&#xff09;中添加以下内容 [source.crates-io] replace-with ustc[source.ustc] registry "git://mirrors.ustc.…

oracle快速定位数据库瓶颈

oracle快速定位数据库瓶颈 –数据库巡检或性能优化方法各异&#xff0c;但首要的是要发现数据库性能瓶颈&#xff0c;系统自带的statspack,或awr太耗时. 查询数据库等待事件top10,关注前前几个等待事件&#xff0c;关注前三个等待事件是否有因果或关联关系–oracle 9i select…