项目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、创建数据库及表

  1. create database test;
  2. use test;
  3. CREATE TABLE category_ (
  4. id int(11) NOT NULL AUTO_INCREMENT,
  5. name varchar(30),
  6. PRIMARY KEY (id)
  7. ) 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');

  1.  

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');

  1.  

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');

  1.  

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');

  1.  

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

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

  1. <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
  2. <dependency>
  3. <groupId>mysql</groupId>
  4. <artifactId>mysql-connector-java</artifactId>
  5. <version>5.1.47</version>
  6. </dependency>

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

  1. server.port=8080
  2.  
  3. spring.datasource.password=admin
  4. spring.datasource.username=root
  5. spring.datasource.driver-class-name=com.mysql.jdbc.Driver
  6. spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false

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

  1. package com.example.demo.entity
  2.  
  3. class Category {
  4. var id : Int? = null;
  5. var name : String? = null;
  6. }

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

  1. <!-- mybatis -->
  2. <dependency>
  3. <groupId>org.mybatis.spring.boot</groupId>
  4. <artifactId>mybatis-spring-boot-starter</artifactId>
  5. <version>1.1.1</version>
  6. </dependency>

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

  1. package com.example.demo.mapper
  2.  
  3. import com.example.demo.entity.Category
  4. import org.apache.ibatis.annotations.*
  5.  
  6. @Mapper
  7. interface CategoryMapper {
  8.  
  9. @Select(" select * from category_")
  10. fun list() : List<Category>
  11.  
  12. @Insert(" insert into category_ values(null, #{name})")
  13. fun insert(category: Category) : Int
  14.  
  15. @Delete(" delete from category_ where id = #{id}")
  16. fun delete(id : Int)
  17.  
  18. @Update(" update category_ set name=#{name} where id = #{id}")
  19. fun update(category: Category) : Int
  20.  
  21. @Select( " select * from category_ where id = #{id}")
  22. fun get(id : Int) : Category
  23.  
  24. }

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

  1. <!-- https://mvnrepository.com/artifact/org.jetbrains.kotlin/kotlin-test-junit5 -->
  2. <dependency>
  3. <groupId>org.jetbrains.kotlin</groupId>
  4. <artifactId>kotlin-test-junit5</artifactId>
  5. <version>1.2.70</version>
  6. <scope>test</scope>
  7. </dependency>
  1. package com.example.demo
  2.  
  3. import com.example.demo.mapper.CategoryMapper
  4. import org.junit.Assert
  5. import org.springframework.boot.test.context.SpringBootTest
  6. import javax.annotation.Resource
  7. import kotlin.test.Test
  8.  
  9. @SpringBootTest
  10. class CategoryMapperTest {
  11.  
  12. @Resource
  13. private lateinit var categoryMapper1: CategoryMapper
  14.  
  15. @Resource
  16. private val categoryMapper2: CategoryMapper? = null
  17.  
  18. @Test
  19. fun test() {
  20. val size1 = categoryMapper1.list().size;
  21. val size2 = categoryMapper2!!.list().size;
  22. Assert.assertEquals(size1, size2)
  23. }
  24.  
  25. }

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

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. </dependency>

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

  1. package com.example.demo.controller
  2.  
  3. import org.springframework.web.bind.annotation.GetMapping
  4. import org.springframework.web.bind.annotation.RestController
  5.  
  6. @RestController
  7. class HelloController {
  8.  
  9. @GetMapping("/hello")
  10. fun hello() : String {
  11. return "hello";
  12. }
  13. }

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

  1. <!-- https://mvnrepository.com/artifact/org.jetbrains.kotlin/kotlin-test-junit5 -->
  2. <dependency>
  3. <groupId>org.jetbrains.kotlin</groupId>
  4. <artifactId>kotlin-test-junit5</artifactId>
  5. <version>1.2.70</version>
  6. <scope>test</scope>
  7. </dependency>

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

  1. package com.example.demo
  2.  
  3. import org.junit.jupiter.api.Test
  4. import org.springframework.boot.test.context.SpringBootTest
  5. import org.springframework.http.HttpMethod
  6. import org.springframework.test.context.web.WebAppConfiguration
  7. import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
  8. import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
  9. import org.springframework.test.web.servlet.setup.MockMvcBuilders
  10. import org.springframework.web.context.WebApplicationContext
  11. import javax.annotation.Resource
  12.  
  13. @SpringBootTest
  14. @WebAppConfiguration
  15. class HelloControllerTest {
  16.  
  17. @Resource
  18. private lateinit var wac : WebApplicationContext
  19.  
  20. @Test
  21. fun test() {
  22. val mockMvc = MockMvcBuilders.webAppContextSetup(wac).build()
  23. val result = mockMvc.perform(MockMvcRequestBuilders.request(HttpMethod.GET, "/hello"))
  24. .andExpect(status().isOk)
  25. .andDo(::println)
  26. .andReturn().response.contentAsString;
  27. println(result)
  28. }
  29.  
  30. }

点击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. XAML 特效

    <Window x:Class="WpfApp5.MainWindow" xmlns="http://schemas.microsoft.com/winfx/200 ...

  2. iOS如何才能在招聘中表现得靠谱?

    http://www.cocoachina.com/programmer/20150707/12414.html 近一年内陆续面试了不少人了,从面试者到面试官的转变让我对 iOS 招聘有了更多的感受. ...

  3. 2019-10-23-WPF-使用-SharpDx-渲染博客导航

    title author date CreateTime categories WPF 使用 SharpDx 渲染博客导航 lindexi 2019-10-23 21:10:13 +0800 2019 ...

  4. @atcoder - AGC036F@ Square Constraints

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一个整数 N,统计有多少个 0~2N-1 的排列 \(P_0 ...

  5. 我为什么飞行 10000 公里去西班牙参加 KubeCon?

    2019 年 5 月 20 日至 23 日, 由 Cloud Native Computing Foundation (CNCF) 主办的云原生技术大会 KubeCon + CloudNativeCo ...

  6. poj1741 树上距离小于等于k的对数 点分治 入门题

    #include <iostream> #include <stdio.h> #include <string.h> #include <algorithm& ...

  7. H3C ARP

  8. 详解ThinkPHP支持的URL模式有四种普通模式、PATHINFO、REWRITE和兼容模式

    URL模式     URL_MODEL设置 普通模式    0 PATHINFO模式     1 REWRITE模式     2 兼容模式     3 如果你整个应用下面的模块都是采用统一的URL模式 ...

  9. html选择题

    1.下面关于css样式和html样式的不同之处说法正确的是(A) A.html样式只影响应用它的文本和使用所选html样式创建的文本 B.css样式只可以设置文字字体样式        不仅仅能够设置 ...

  10. hadoop 端口总结

    localhost:50030/jobtracker.jsp localhost:50060/tasktracker.jsp localhost:50070/dfshealth.jsp 1. Name ...