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 : ...
随机推荐
- Linux学习 - 数值运算
1 declare 声明变量类型 declare [+/-] [选项] 变量名 - 给变量设定类型属性 + 取消变量的类型属性 -i 将变量声明为整数型 -x 将变量声明为环境变量(同export) ...
- echo -e "\033[字背景颜色;字体颜色m字符串\033[0m
格式: echo -e "\033[字背景颜色;字体颜色m字符串\033[0m" 例如: echo -e "\033[41;36m something here \033 ...
- Springboot Oauth2 集成Swagger2权限验证实战
Swagger是什么?能干什么?在这就不展开讲解了.本文主要讲解如何集成OAuth2的Password模式权限验证,验证接口是否具有权限. 引入依赖 <dependency> <gr ...
- 【Python】【Module】json and pickle
Python中用于序列化的两个模块 json 用于[字符串]和 [python基本数据类型] 间进行转换 pickle 用于[python特有的类型] 和 [python基本数据类型]间进 ...
- 基于spring sringmvc mybatis 做的导入导出
导入 pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://ww ...
- Hibernate的基本功能:对数据库的增删改查(创建对象实例)
一.通过实例化的对象向数据库添加新记录 package com.yh.test; import org.hibernate.Session; import org.hibernate.SessionF ...
- 【C/C++】【输入】关于scanf:输入空格,多次使用
一.C/C++中带空格字符串的输入 C++中的cin和C中的scanf都是遇到空格或回车结束. 如果要让scanf接收空格,可以用读入字符集合的方式.%[] char a[100]; scanf(&q ...
- 如何简单的理解LSTM——其实没有那么复杂(转载)
转载地址:https://www.jianshu.com/p/4b4701beba92 1.循环神经网络 人类针对每个问题的思考,一般不会是完全的从头开始思考.正如当你阅读这篇译文的时候,你会根据已经 ...
- 【简】题解 AWSL090429 【原子】
预处理出每个原子最近的不能合并的位置 枚举当前位置和前面断开的位置合并 发现还是不能过 考虑用选段树优化 但是因为每次转移的最优点是在前面可以合并的范围内 dp值加上当前的到该点的最大值 因为每个位置 ...
- Boss直聘App上“天使投资、VC、PE” 与“A轮、B轮、C轮融资”的关系
我们经常看到朋友圈里某某公司获得了某轮融资,所谓的A轮B轮究竟是个什么概念呢?今天就跟小伙伴们分享一下A.B.C.D轮融资与天使投资.VC.PE的关系. 天使投资(AI):天使投资所投的是一些非常早期 ...