kotlin+springboot+mybatis-puls+mysql搭建gradle-web工程
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工程的更多相关文章
- Spring-boot+Mybatis+Maven+MySql搭建实例
转自:https://www.jianshu.com/p/95fb7be049ae 最近读了spring-boot开发手册,spring-boot相比于spring-mvc封装了很多常用的依赖,并且内 ...
- Ubuntu+Django+Nginx+uWSGI+Mysql搭建Python Web服务器
Ubuntu+Django+Nginx+uWSGI+Mysql搭建Python Web服务器 闲着无聊的时候部署了一个Django项目玩,用vm虚拟机部署的. 准备工作 我使用的系统是Ubuntu16 ...
- Java逆向工程SpringBoot + Mybatis Generator + MySQL
Java逆向工程SpringBoot+ Mybatis Generator + MySQL Meven pop.xml文件添加引用: <dependency> <groupId> ...
- SpringBoot+Mybatis+Maven+MySQL逆向工程实现增删改查
SpringBoot+Mybatis+MySQL+MAVEN逆向工程实现增删改查 这两天简单学习了下SpringBoot,发现这玩意配置起来是真的方便,相比于SpringMVC+Spring的配置简直 ...
- 【时区问题】SpringBoot+mybatis查询mysql的datetime类型数据时间差14小时
[时区问题]MyBatis查询MySQL的datetime类型数据时间差14小时 故障解决方式 与数据库连接时,定义时区,避免mybatis框架从mysql获取时区.在连接上加上 serverTime ...
- Spring+Mybatis+Maven+MySql搭建实例
林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文主要讲了如何使用Maven来搭建Spring+Mybatis+MySql的的搭建实例 ...
- springboot+mybatis+thymeleaf项目搭建及前后端交互
前言 spring boot简化了spring的开发, 开发人员在开发过程中省去了大量的配置, 方便开发人员后期维护. 使用spring boot可以快速的开发出restful风格微服务架构. 本文将 ...
- SpringBoot+Mybatis+Maven+MySql小案例
数据准备: 建表t_user ,插入数据..... 创建工程 构建pom.xml <?xml version="1.0" encoding="UTF-8" ...
- Springboot+Mybatis+Clickhouse+jsp 搭建单体应用项目(一)
一.服务器安装clickhouse服务 参阅 :https://www.cnblogs.com/liuyangfirst/p/13379064.html 二.连接数据库 成功 三.新建库 CREATE ...
- 通用mapper版+SpringBoot+MyBatis框架+mysql数据库的整合
转:https://blog.csdn.net/qq_35153200/article/details/79538440 开发环境: 开发工具:Intellij IDEA 2017.2.3 JDK : ...
随机推荐
- keybd_event模拟键盘按键,mouse_event怎么用
从 模仿UP主,用Python实现一个弹幕控制的直播间! - 蛮三刀酱 - 博客园 (cnblogs.com) 知道了 PyAutoGUI: * Moving the mouse and clicki ...
- 零基础学习java------38---------spring中关于通知类型的补充,springmvc,springmvc入门程序,访问保护资源,参数的绑定(简单数据类型,POJO,包装类),返回数据类型,三大组件,注解
一. 通知类型 spring aop通知(advice)分成五类: (1)前置通知[Before advice]:在连接点前面执行,前置通知不会影响连接点的执行,除非此处抛出异常. (2)正常返回通知 ...
- 使用 Addressables 来管理资源
使用 Addressables 来管理资源 一.安装 打开Package Manager,在Unity Technologies的目录下找到Addressables,更新或下载. 二.配置 依次打开W ...
- Oracle异常处理——ORA-01502:索引或这类索引的分区处于不可用状态
Oracle异常处理--ORA-01502:索引或这类索引的分区处于不可用状态参考自:https://www.cnblogs.com/lijiaman/p/9277149.html 1.原因分析经过查 ...
- linux之wc命令详解
Linux系统中wc(Word Count)命令的功能为统计指定文件中的字节数.字数.行数,并将统计结果显示输出. 1.命令格式 wc [options] 文件... 2.命令功能 统计指定文件中的字 ...
- Linux基础命令---mysqlshow显示数据库
mysqlshow mysqlshow是一个客户端的程序,它可以显示数据库的信息.表信息.字段信息. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.Fedora. 1.语法 ...
- zabbix之源码安装
#:官网地址 https://www.zabbix.com/documentation/4.0/zh/manual/installation/install #:解压并创建用户 root@ubuntu ...
- 【Java基础】Java中new对象的过程
序言 联系我上次写的关于Java内存的文章,对象访问在 Java 语言中无处不在,是最普通的程序行为,但即使是最简单的访问,也会却涉及 Java 栈.Java 堆.方法区这三个最重要内存区域之间的关联 ...
- [Java Web 王者归来]读书笔记1
第一章 Java web 开发概述 1 WEB服务器运行时一直在TCP 80(默认端口)监听, 若使用其他端口在url中需要显示标注端口号(例如:8080) 2 WEB服务器:微软的IIS.Apach ...
- 这样学习ZooKeeper离大厂所需技能要求还远吗
概述 定义 Apache ZooKeeper是一种用于构建分布式应用的高性能.高度可靠.开源的分布式协调服务,提供如配置信息维护.命名.分布式同步.组服务等功能,可以实现如分布式共识.组管理.领导选举 ...