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

前言:

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

    @Version
    var 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 {

    @Autowired
    private 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.mfbz.cn/a/572496.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

解决“ImportError: DLL load failed while importing _rust: 找不到指定的程序的问题

运行 scrapy startproject wikiSpider 报错&#xff1a;ImportError: DLL load failed while importing _rust: 找不到指定的程序。 经过尝试 可以更换Python解释器版本来解决 1、点击crtlalts打开设置 点击项目>解释器 选择3.11解释器 &#xff08;我原来报错用的3.9的解…

C++11(第一篇)【C/C++复习版】

目录 1、统一的列表初始化 2、所有容器新增initializer_list构造 3、auto、decltype和typeid 4、nullptr 5、 范围for 6、STL中的变化 array&#xff08;新容器&#xff09; forward_list&#xff08;新容器&#xff09; cbegin、cend、crbegin、crend&#xff08;新方法…

WEB网站服务器安全漏洞扫描环境搭建及漏洞工具扫描

一、适用环境 1、企业自建有门户网站&#xff1b; 2、使用Struts框架的WEB网站&#xff1b; 3、网站服务器涉及有数据库之类的项目&#xff0c;如&#xff1a;微信登录、手机登录、充值、收费等。 4、使用安卓版、苹果版、电脑版结合的缴费类网站平台。 5、方便但需提高安全性…

linux-centos修改时区时间

修改时区为北京时间 先输入tzselect&#xff0c;输入5&#xff0c;再输入9&#xff0c;再输入1&#xff0c;最后再输入1就行了 修改系统时间和硬件时间 查看当前时间 命令date修改系统时间 命令date -s "2024-04-21 18:30:30"查看硬件时间 命令hwclock --show修改…

AIGC Chat GPT 用思维导图总结,数据分析所需要掌握的Excel知识

你还不会制作思维导图吗&#xff1f; 现在已经可以零门槛一键生成&#xff0c;只需跟AI说一句话&#xff0c;就能完成&#xff01;&#xff01;&#xff01; 生成一个思维导图&#xff0c;主题是数据分析师需要掌握的Excel知识&#xff0c;在新窗口生成思念导图。 AIGC ChatG…

ONES 功能上新|ONES Wiki 新功能一览

支持在 ONES Wiki 页面中使用分栏进行横向排版&#xff0c;丰富排版方式&#xff0c;帮助用户以更丰富的版式展示内容。 应用场景&#xff1a; 页面的布局对内容的阅读有很大的影响。当页面中有图文混排的需求时&#xff0c;可以通过分栏来组织页面结构&#xff0c;以更清晰、更…

倾囊相授,ChatGPT干货技巧全在这里!如果没有这个方法我不可能学好ChatGPT

ChatGPT虽然已经问世一年多&#xff0c;但不少朋友还处于刚接触的阶段。于是&#xff0c;我们特别梳理了一些常见问题&#xff0c;尝试着用通俗的语言解释清楚这些内容。 1. ChatGPT的官方网址 https://www.chatgpt.com 你只要Google搜索能打开&#xff0c;这个网址肯定能打开。…

2024年成都市“蓉贝”软件人才年度评估及资金支持申报对象内容、材料要求

一、申报对象 经2023年评估合格的第一批&#xff08;2019年评聘&#xff09;、第二批&#xff08;2020年评聘&#xff09;、第三批&#xff08;2021年评聘&#xff09;“蓉贝”软件人才&#xff0c;2022年评聘的第四批“蓉贝”软件人才。 二、评估内容 &#xff08;一&#…

java和python刷题的一些语法规则总结(未完成)

语法总结 Java篇1、代码补全2、编程题中常用头文件3、编程题常用的内置方法4、模版 Python篇1、2、编程题中常用的头文件3、编程题中常用的内置方法4、伪代码模版 去哪练习&#xff1f; 1、LeetCode上有个面试模拟 2、牛客公司真题&#xff08;ACM模式&#xff09; ⚠️ 笔试均…

AI-数学-高中-44导数的运算法则

原作者视频&#xff1a;【导数】【一数辞典】3导数的运算法则&#xff08;略难&#xff09;_哔哩哔哩_bilibili 三种求导表达方式一样的&#xff0c;中间的比较常用&#xff1a; 链式法则&#xff1a;从外向内&#xff1a;

Vue3 实现 Three.js粒子特效

效果 <template><div id"waves" /> </template><script setup> import { ref, onMounted, onUnmounted } from "vue"; import * as THREE from "three";const amountX ref(50); const amountY ref(50); const color …

MATLAB实现蚁群算法栅格路径优化

蚁群算法是一种模拟自然界中蚂蚁觅食行为的优化算法&#xff0c;常用于解决路径规划问题。在栅格路径优化中&#xff0c;蚁群算法可以帮助找到从起点到终点的最优路径。以下是蚁群算法栅格路径优化的基本流程步骤&#xff1a; 初始化参数&#xff1a; (1)设置蚂蚁数量&#xff…

JavaScript实现代码雨

一、功能描述 使用canvas实现一个代码雨的功能&#xff0c;炫一个~~~ 二、上码 html <canvas id"canvas"></canvas> js let canvas document.querySelector(canvas);let ctx canvas.getContext(2d);// screen.availWidth:可视区域的宽度canvas.width…

Blender游戏资产优化技巧

创建视频游戏资产既具有挑战性又富有回报。 经过一些研究并根据我的经验&#xff0c;这里有三个技巧可以帮助你使用 Blender 优化游戏资产。 在 Blender 中优化游戏资源的三种技术可以归结为拥有高效的 3D 模型拓扑、通过烘焙优化纹理&#xff0c;以及最后通过 Blender 节点的…

【Spring AI 来了】

spring官方已经有Spring AI 插件&#xff0c;每个程序员必定拥抱AI&#xff0c;也意味着不就以后AI的open API 会成为我们开发成的基础jdk。 下面的内容也是AI直接根据网址给我翻译的&#xff0c;连格式都是生成的。AI应用已经渗透到各行各业了&#xff0c;并且会改变我们每个…

【八股】Java基础、集合、JVM

面向对象三大特性 1 封装&#xff1a; 将 方法 和 属性 写到同一个类中&#xff0c;并将属性 私有化&#xff0c;生成 get set方法&#xff0c;外部访问属性需要通过get和set方法,内部可以直接访问属性&#xff0c;这样的一个类我们认为它完成了封装。 2 继承&#xff1a; 子…

神经网络手写数字识别

⚠申明&#xff1a; 未经许可&#xff0c;禁止以任何形式转载&#xff0c;若要引用&#xff0c;请标注链接地址。 全文共计4077字&#xff0c;阅读大概需要3分钟 &#x1f308;更多学习内容&#xff0c; 欢迎&#x1f44f;关注&#x1f440;【文末】我的个人微信公众号&#xf…

python安装pytorch@FreeBSD

先上结论&#xff0c;最后在conda下安装成功了&#xff01; PyTorch是一个开源的人工智能深度学习框架&#xff0c;由Facebook人工智能研究院&#xff08;FAIR&#xff09;基于Torch库开发并维护。PyTorch提供了一个高效、灵活且易于使用的工具集&#xff0c;用于构建和训练深…

Python-VBA函数之旅-iter函数

目录 一、iter函数的常见应用场景&#xff1a; 二、iter函数使用注意事项&#xff1a; 三、如何用好iter函数&#xff1f; 1、iter函数&#xff1a; 1-1、Python&#xff1a; 1-2、VBA&#xff1a; 2、推荐阅读&#xff1a; 个人主页&#xff1a;神奇夜光杯-CSDN博客 …

AndroidStudio 新建工程的基本修改及事件添加

注&#xff1a;2022.3.1&#xff0c;新建Empty Activity默认是Kotlin&#xff0c;可以选择新建Empty View Activity&#xff0c;修改语言为JAVA 应用名称 修改应用名称 路径&#xff1a;res-values-strings.xml 是否显示应用名称 路径&#xff1a;res-values-themes.xml …
最新文章