kotlin+springboot+mybatis-puls+mysql搭建web工程

​ 前段时间研究了spring security及OAuth2系列之后,本来打算研究spring的,但是部门发生了一些情况,我转岗到了另一个部门,具体做内部CICD的产品,需要用kotlin开发,于是我需要从零开始学习Kotlin语言。于是我连忙找了本Kotlin的书籍看了几天主要的语法章节,想着需要做点demo让自己熟悉一下,于是有了这篇文章。

​ 本片主要讲怎么基于kotlin搭建springboot-web项目。

在IDEA上创建gradle项目

​ 在IDEA上创建kotlin项目有多种方式,这里我采用了如图的方式

引入所需依赖

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
testCompile group: 'junit', name: 'junit', version: '4.12'
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.4.4'
implementation group: 'mysql', name: 'mysql-connector-java', version: '8.0.27'
implementation group: 'com.baomidou', name: 'mybatis-plus-boot-starter', version: '3.4.3.3' }

​ 这里引入springboot\mysql-connector-java\mybatis-plus等组件,用的是基于groovy语言构建的gradle脚本,不知道这么脚本怎么来的,怎么到https://mvnrepository.com/ maven仓库查找gradle对应的依赖脚本。

新建各个文件(dao\service\mapper\entity\controller)

新建启动类

@SpringBootApplication
@MapperScan("com.canway.app.dao")
open class MainApplicationClass fun main(args: Array<String>) {
runApplication<MainApplicationClass>(*args)
}

新建entity(user表)

import com.fasterxml.jackson.annotation.JsonFormat
import java.util.Date class TUser {
var id: Int? = null; var username: String? = null; var age: Int? = null; var sex: Int? = null; @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", shape = JsonFormat.Shape.STRING)
var createTime: Date? = null; @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", shape = JsonFormat.Shape.STRING)
var updateTime: Date? = null;
}

新建service/impl

import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.canway.app.dto.PageQuery
import com.canway.app.entity.TUser interface TUserService { fun queryById(id: Int): TUser? fun queryByPage(pageQuery: PageQuery): Page<TUser?> fun insert(tUser: TUser): TUser fun update(tUser: TUser): TUser fun deleteById(id: Int): Boolean
}
package com.canway.app.service.impl

import com.baomidou.mybatisplus.core.toolkit.Wrappers
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.canway.app.dao.TUserDao
import com.canway.app.dto.PageQuery
import com.canway.app.entity.TUser
import com.canway.app.service.TUserService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service @Service
class TUserServiceImpl @Autowired constructor(private var tUserDao: TUserDao): TUserService{ // 不同过在构造函数中注入属性的话,可以通过寻常注入方式
// @Resource
// private lateinit var tUserDao: TUserDao; override fun queryById(id: Int): TUser? {
return tUserDao.queryById(id)
} override fun queryByPage(pageQuery: PageQuery): Page<TUser?> {
val wrapper = Wrappers.lambdaQuery<TUser>()
// wrapper.like(TUser::getUsername, query.getUsername());
// wrapper.like(TUser::getUsername, query.getUsername());
val page: Page<TUser?> = Page<TUser?>(pageQuery.getPageNum(), pageQuery.getPageSize(), true)
val page1: Page<TUser?> = tUserDao.selectPage(page, wrapper)
return page1
} override fun insert(tUser: TUser): TUser {
this.tUserDao.insert(tUser);
return tUser;
} override fun update(tUser: TUser): TUser {
this.tUserDao.update(tUser);
return tUser;
} override fun deleteById(id: Int): Boolean {
return this.tUserDao.deleteById(id) > 0
}
}

新建dao

package com.canway.app.dao

import com.baomidou.mybatisplus.core.mapper.BaseMapper
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.canway.app.entity.TUser
import org.apache.ibatis.annotations.Param interface TUserDao: BaseMapper<TUser> {
/**
* 通过ID查询单条数据
*
* @param id 主键
* @return 实例对象
*/
fun queryById(id: Int): TUser? /**
* 查询指定行数据
*
* @param tUser 查询条件
* @return 对象列表
*/
fun queryByPage(page: Page<TUser?>?, @Param("tUser") tUser: TUser?): List<TUser?>? /**
* 统计总行数
*
* @param tUser 查询条件
* @return 总行数
*/
fun count(tUser: TUser?): Long /**
* 新增数据
*
* @param tUser 实例对象
* @return 影响行数
*/
override fun insert(tUser: TUser?): Int /**
* 批量新增数据(MyBatis原生foreach方法)
*
* @param entities List<TUser> 实例对象列表
* @return 影响行数
</TUser> */
fun insertBatch(@Param("entities") entities: List<TUser?>?): Int /**
* 批量新增或按主键更新数据(MyBatis原生foreach方法)
*
* @param entities List<TUser> 实例对象列表
* @return 影响行数
* @throws org.springframework.jdbc.BadSqlGrammarException 入参是空List的时候会抛SQL语句错误的异常,请自行校验入参
</TUser> */
fun insertOrUpdateBatch(@Param("entities") entities: List<TUser?>?): Int /**
* 修改数据
*
* @param tUser 实例对象
* @return 影响行数
*/
fun update(tUser: TUser?): Int /**
* 通过主键删除数据
*
* @param id 主键
* @return 影响行数
*/
fun deleteById(id: Int?): Int
}

新建controller

package com.canway.app.controller

import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.canway.app.dto.PageQuery
import com.canway.app.entity.TUser
import com.canway.app.service.TUserService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.ResponseEntity
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.PutMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController @RestController
@RequestMapping("tUser")
class UserController { @Autowired
private val tUserService: TUserService? = null @GetMapping("/hello")
fun helloworld(): String {
return "Hello world"
} @GetMapping("{id}")
fun queryById(@PathVariable("id") id: Int): ResponseEntity<TUser?> {
return ResponseEntity.ok(this.tUserService?.queryById(id))
} @PostMapping
fun add(tUser: TUser): ResponseEntity<TUser> {
return ResponseEntity.ok(this.tUserService?.insert(tUser));
} @GetMapping
fun queryByPage(query: PageQuery): ResponseEntity<Page<TUser?>> {
return ResponseEntity.ok(this.tUserService?.queryByPage(query))
} @PutMapping
fun edit(tUser: TUser): ResponseEntity<TUser> {
return ResponseEntity.ok(this.tUserService?.update(tUser))
} @DeleteMapping
fun deleteById(id: Int): ResponseEntity<Boolean> {
return ResponseEntity.ok(this.tUserService?.deleteById(id))
}
}

新建mybatis配置类

package com.canway.app.config

import com.baomidou.mybatisplus.annotation.DbType
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration @Configuration
open class MybatisPlusConfig { @Bean
open fun mybatisPlusInterceptor(): MybatisPlusInterceptor{
var interceptor = MybatisPlusInterceptor();
//向Mybatis过滤器链中添加分页拦截器
interceptor.addInnerInterceptor(PaginationInnerInterceptor(DbType.MYSQL))
interceptor.addInnerInterceptor(OptimisticLockerInnerInterceptor())
//还可以添加i他的拦截器
return interceptor
}
}

添加配置

spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/java_web?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
username: root
password: 123456
hikari:
minimum-idle: 1
maximum-pool-size: 3
connection-timeout: 5000 mybatis-plus:
configuration:
map-underscore-to-camel-case: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

总结

​ 基于kotlin+springboot+mybatis-plus+mysql的gradle-web项目到这里就结束,整个过程中还是让自己熟悉了一些kotlin的具体语法,像函数声明、变量、类的定义等,后续写业务的时候才能相对快速熟悉其它语法。

kotlin+springboot+mybatis-puls+mysql搭建gradle-web工程的更多相关文章

  1. Spring-boot+Mybatis+Maven+MySql搭建实例

    转自:https://www.jianshu.com/p/95fb7be049ae 最近读了spring-boot开发手册,spring-boot相比于spring-mvc封装了很多常用的依赖,并且内 ...

  2. Ubuntu+Django+Nginx+uWSGI+Mysql搭建Python Web服务器

    Ubuntu+Django+Nginx+uWSGI+Mysql搭建Python Web服务器 闲着无聊的时候部署了一个Django项目玩,用vm虚拟机部署的. 准备工作 我使用的系统是Ubuntu16 ...

  3. Java逆向工程SpringBoot + Mybatis Generator + MySQL

    Java逆向工程SpringBoot+ Mybatis Generator + MySQL Meven pop.xml文件添加引用: <dependency> <groupId> ...

  4. SpringBoot+Mybatis+Maven+MySQL逆向工程实现增删改查

    SpringBoot+Mybatis+MySQL+MAVEN逆向工程实现增删改查 这两天简单学习了下SpringBoot,发现这玩意配置起来是真的方便,相比于SpringMVC+Spring的配置简直 ...

  5. 【时区问题】SpringBoot+mybatis查询mysql的datetime类型数据时间差14小时

    [时区问题]MyBatis查询MySQL的datetime类型数据时间差14小时 故障解决方式 与数据库连接时,定义时区,避免mybatis框架从mysql获取时区.在连接上加上 serverTime ...

  6. Spring+Mybatis+Maven+MySql搭建实例

    林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文主要讲了如何使用Maven来搭建Spring+Mybatis+MySql的的搭建实例 ...

  7. springboot+mybatis+thymeleaf项目搭建及前后端交互

    前言 spring boot简化了spring的开发, 开发人员在开发过程中省去了大量的配置, 方便开发人员后期维护. 使用spring boot可以快速的开发出restful风格微服务架构. 本文将 ...

  8. SpringBoot+Mybatis+Maven+MySql小案例

    数据准备: 建表t_user ,插入数据..... 创建工程 构建pom.xml <?xml version="1.0" encoding="UTF-8" ...

  9. Springboot+Mybatis+Clickhouse+jsp 搭建单体应用项目(一)

    一.服务器安装clickhouse服务 参阅 :https://www.cnblogs.com/liuyangfirst/p/13379064.html 二.连接数据库 成功 三.新建库 CREATE ...

  10. 通用mapper版+SpringBoot+MyBatis框架+mysql数据库的整合

    转:https://blog.csdn.net/qq_35153200/article/details/79538440 开发环境: 开发工具:Intellij IDEA 2017.2.3 JDK : ...

随机推荐

  1. c++ cmake及包管理工具conan简单入门

    cmake是一个跨平台的c/c++工程管理工具,可以通过cmake轻松管理我们的项目 conan是一个包管理工具,能够自动帮助我们下载及管理依赖,可以配合cmake使用 这是一个入门教程,想深入了解的 ...

  2. [web安全] 利用pearcmd.php从LFI到getshell

    有一段时间没写blog了,主要是事多,加上学的有些迷茫,所以内耗比较大.害,沉下心好好学吧. 漏洞利用背景: 允许文件包含,但session等各种文件包含都已经被过滤了.ctf题中可以关注regist ...

  3. 【leetcode】653. Two Sum IV - Input is a BST

    Given the root of a Binary Search Tree and a target number k, return true if there exist two element ...

  4. APK 反编译以及遇到的问题

    APK反编译: https://www.cnblogs.com/geeksongs/p/10864200.html 遇到的问题 https://www.jianshu.com/p/55bf5f688e ...

  5. Virtual Destructor

    Deleting a derived class object using a pointer to a base class that has a non-virtual destructor re ...

  6. 查看IP访问量的shell脚本汇总

    第一部分,1,查看TCP连接状态 netstat -nat |awk '{print $6}'|sort|uniq -c|sort -rn netstat -n | awk '/^tcp/ {++S[ ...

  7. JavaScript实现数组去重方法

    一.利用ES6 Set去重(ES6中最常用) function unique (arr) { return Array.from(new Set(arr)) } var arr = [1,1,'tru ...

  8. 使用$.post方式来实现页面的局部刷新功能

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  9. scrapy爬取招聘网站,items转换成dict遇到的问题

    pipelines代码 1 import json 2 3 class TencentJsonPipeline(object): 4 def __init__(self): 5 self.file = ...

  10. CF699A Launch of Collider 题解

    Content 有 \(n\) 辆车在一条数轴上,第 \(i\) 辆车在 \(a_i\) 上,每辆车要么向左,要么向右.开始,它们以 \(1\) 个单位每秒的速度在行驶.问你第一次撞车发生在第几秒的时 ...