项目mybatis操作数据库参考:

http://how2j.cn/k/springboot/springboot-mybatis/1649.html?p=78908

junit对controller层测试参考:

https://www.cnblogs.com/PollyLuo/p/9630822.html

mysql版本:5.5.62

点击下载

1、kotlin版springboot项目创建

访问https://start.spring.io/, 创建项目demo(maven + kotlin + springboot 2.1.7, 其他默认)。

2、创建数据库及表

create database test;
use test;
CREATE TABLE category_ (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(30),
PRIMARY KEY (id)
) DEFAULT CHARSET=UTF8;

insert into category_ values(null, 'Aa');
insert into category_ values(null, 'Bb');
insert into category_ values(null, 'Cc');
insert into category_ values(null, 'Dd');
insert into category_ values(null, 'Ee');
insert into category_ values(null, 'Ff');
insert into category_ values(null, 'Gg');


insert into category_ values(null, 'Hh');
insert into category_ values(null, 'Ii');
insert into category_ values(null, 'Jj');
insert into category_ values(null, 'Kk');
insert into category_ values(null, 'Ll');
insert into category_ values(null, 'Mm');
insert into category_ values(null, 'Nn');


insert into category_ values(null, 'Oo');
insert into category_ values(null, 'Pp');
insert into category_ values(null, 'Qq');
insert into category_ values(null, 'Rr');
insert into category_ values(null, 'Ss');
insert into category_ values(null, 'Tt');


insert into category_ values(null, 'Uu');
insert into category_ values(null, 'Vv');
insert into category_ values(null, 'Ww');
insert into category_ values(null, 'Xx');
insert into category_ values(null, 'Yy');
insert into category_ values(null, 'Zz');

 

3、将项目demo导入idea,等待maven导入依赖jar包。

修改pom.xml,增加mysql数据库连接jar包。

        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>

在src/main/resources包下的application.properties文件中增加数据库访问参数(包括mysql数据库用户名及密码)、端口号等。

server.port=8080

spring.datasource.password=admin
spring.datasource.username=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false

在com.example.demo目录下新建entity包,创建类Category.kt

package com.example.demo.entity

class Category {
var id : Int? = null;
var name : String? = null;
}

修改pom.xml,增加mybatis注解jar包。

        <!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>

在com.example.demo包下创建mapper包,创建接口类CategoryMapper.kt, 实现增、删、改、查。

package com.example.demo.mapper

import com.example.demo.entity.Category
import org.apache.ibatis.annotations.* @Mapper
interface CategoryMapper { @Select(" select * from category_")
fun list() : List<Category> @Insert(" insert into category_ values(null, #{name})")
fun insert(category: Category) : Int @Delete(" delete from category_ where id = #{id}")
fun delete(id : Int) @Update(" update category_ set name=#{name} where id = #{id}")
fun update(category: Category) : Int @Select( " select * from category_ where id = #{id}")
fun get(id : Int) : Category }

修改pom.xml增加相关依赖,在src/test/kotlin路径下com.example.demo路径下创建类CategoryMapperTest.kt,并执行相关测试。

        <!-- https://mvnrepository.com/artifact/org.jetbrains.kotlin/kotlin-test-junit5 -->
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test-junit5</artifactId>
<version>1.2.70</version>
<scope>test</scope>
</dependency>
package com.example.demo

import com.example.demo.mapper.CategoryMapper
import org.junit.Assert
import org.springframework.boot.test.context.SpringBootTest
import javax.annotation.Resource
import kotlin.test.Test @SpringBootTest
class CategoryMapperTest { @Resource
private lateinit var categoryMapper1: CategoryMapper @Resource
private val categoryMapper2: CategoryMapper? = null @Test
fun test() {
val size1 = categoryMapper1.list().size;
val size2 = categoryMapper2!!.list().size;
Assert.assertEquals(size1, size2)
} }

4、修改pom.xml,添加web相关依赖。

        <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

在src/main/kotlin目录下com.example.demo包下新建controller包,创建kotlin类HelloController.kt

package com.example.demo.controller

import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController @RestController
class HelloController { @GetMapping("/hello")
fun hello() : String {
return "hello";
}
}

修改pom.xml,  添加单元测试相关依赖。

        <!-- https://mvnrepository.com/artifact/org.jetbrains.kotlin/kotlin-test-junit5 -->
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test-junit5</artifactId>
<version>1.2.70</version>
<scope>test</scope>
</dependency>

在src/test/kotlin路径下 com.example.demo包下创建kotlin类HelloControllerTest.kt

package com.example.demo

import org.junit.jupiter.api.Test
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.http.HttpMethod
import org.springframework.test.context.web.WebAppConfiguration
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
import org.springframework.test.web.servlet.setup.MockMvcBuilders
import org.springframework.web.context.WebApplicationContext
import javax.annotation.Resource @SpringBootTest
@WebAppConfiguration
class HelloControllerTest { @Resource
private lateinit var wac : WebApplicationContext @Test
fun test() {
val mockMvc = MockMvcBuilders.webAppContextSetup(wac).build()
val result = mockMvc.perform(MockMvcRequestBuilders.request(HttpMethod.GET, "/hello"))
.andExpect(status().isOk)
.andDo(::println)
.andReturn().response.contentAsString;
println(result)
} }

点击HelloControllerTest类前的三角号,即可执行单元测试,在下方窗口看到输出结果“hello”。

kotlin + springboot整合mybatis操作mysql数据库及单元测试的更多相关文章

  1. springboot整合mybatis连接mysql数据库出现SQLException异常

    在springboot整合mybatis连接数据库的时候,项目中遇到一个SQLException,我检查了properties配置文件,看数据源有没有配错,检查有没有打错字,在数据库中把sql语句查询 ...

  2. 如何用IDEA创建springboot(maven)并且整合mybatis连接mysql数据库和遇到的问题

    一.New->Project 二.点击next 三.在Group栏输入组织名,Artifact就是项目名.选择需要的java版本,点击next 四.添加需要的依赖 在这里我们也可以添加sql方面 ...

  3. SpringBoot 整合 hibernate 连接 Mysql 数据库

    前一篇搭建了一个简易的 SpringBoot Web 项目,最重要的一步连接数据库执行增删改查命令! 经过了一天的摸爬滚打,终于成功返回数据! 因为原来项目使用的 SpringMVC + Hibern ...

  4. springboot学习-springboot使用spring-data-jpa操作MySQL数据库

    我们在上一篇搭建了一个简单的springboot应用,这一篇将介绍使用spring-data-jpa操作数据库. 新建一个MySQL数据库,这里数据库名为springboot,建立user_info数 ...

  5. SpringBoot 集成Mybatis 连接Mysql数据库

    记录SpringBoot 集成Mybatis 连接数据库 防止后面忘记 1.添加Mybatis和Mysql依赖 <dependency> <groupId>org.mybati ...

  6. SpringBoot 整合Mybatis操作数据库

    1.引入依赖: <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId> ...

  7. SpringBoot 整合 Mybatis 和 Mysql (详细版)

    结构如下 1.引入相关依赖 <!--mysql--><dependency> <groupId>mysql</groupId> <artifact ...

  8. SpringBoot 使用Mybatis操作mysql示例

    1.准备数据库 创建数据库 create databases baodanjia; 创建帐号 create user 'baodanjia'@'%' identified by '123456' gr ...

  9. SpringBoot整合SpringData和Mysql数据库

    1.新建maven项目(具体的新建过程就不细说了) 2.添加maven依赖,也就是在pom.xml文件添加项目的依赖jar包: <project xmlns="http://maven ...

随机推荐

  1. python内置函数and匿名函数

    一.内置函数 什什么是内置函数? 就是python给你提供的. 拿来直接⽤用的函数, 比如print., input等等. 截⽌止 到python版本3.6.2 python⼀一共提供了了68个内置函 ...

  2. 突破!阿里云CDN实现毫秒级全网刷新

    通常在某网站使用了CDN节点来实现内容分发加速后,当源站内容更新的时候,CDN刷新系统会通过提交刷新请求将CDN节点上的指定缓存内容强制过期.当用户访问的时候,CDN节点将回源获取最新内容返回给用户, ...

  3. SDUT-2131_数据结构实验之栈与队列一:进制转换

    数据结构实验之栈与队列一:进制转换 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 输入一个十进制非负整数,将其转换成对 ...

  4. vue element 中自定义传值

    一直以来都不知道如何传自定义的值,一直只会默认的,今天终于找到方法了. 比如这个上传图片的控件,想带当前的index过去,就这样写.其它的类似 :http-request="(file,fi ...

  5. .net core 文件下载

    public IActionResult Dowanload(string id,int numTypes) { try { var memory = new MemoryStream(): //mo ...

  6. C# 递归、try

    一.递归 递归:在函数体内调用本函数自身,直到符合某一条件不再继续调用 两个需要满足的条件1.有反复调用自身函数的过程2.有函数的出口:有不再继续执行的条件 例子: 案例: (一).输入正整数n,求n ...

  7. 软件自动化测试 selenium IDE + Firebug + python脚本

    按顺序步骤来 一.安装软件   1.1.1 webDriver(就是selenium IDE) 解析:本来这两个东西就合成一个了,但是更新到后来,安装的时候又独立安装的.    安装  Python  ...

  8. Python--day64--内容回顾

    1,内容回顾 1.ORM外键操作 图书表和出版社表 多对一的关系 #书 class Book(models.Model): id = models.AutoField(primary_key=True ...

  9. JDBC 时间处理

    Java中用类java.util.Date对日期/时间做了封装,此类提供了对年.月.日.时.分.秒.毫秒以及时区的控制方法,同时也提供一些工具方法,比如日期/时间的比较,前后判断等. java.uti ...

  10. js(二) 实现省市联动(json)

    通过HTML页面的city的select选取的value值,从json里面获取相对应的键值对,最后将值拼接到下拉框中 function x1(th) { //通过传入的th的value获取相对应的ci ...