kotlin + springboot整合mybatis操作mysql数据库及单元测试

项目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,‘category 1‘);
insert into category_ values(null,‘category 2‘);
insert into category_ values(null,‘category 3‘);
insert into category_ values(null,‘category 4‘);

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”。

原文地址:https://www.cnblogs.com/wushengwuxi/p/11324702.html

时间: 2024-07-29 11:39:11

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

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

1.New->Project 2.点击next 3.在Group栏输入组织名,Artifact就是项目名.其它选择默认就好,直接Next 4.添加依赖 5.修改您希望放项目的位置,点击finish,等待idea下载springboot项目模板和相关的依赖 项目构建完成后的目录结构 6.如果需要用mybatis连接mysql数据库 a.添加连接数据库的依赖(在pom.xml中添加以下内容,maven将自动把相关依赖放到maven仓库中,然后放到项目中) <!--连接数据库--><de

Spring mvc整合mybatis基于mysql数据库实现用户增删改查及其分页显示的完整入门实例【转】

Spring mvc整合mybatis例子, 基于mysql数据库实现对用户的增.删.改.查,及分页显示的完整例子. 查询显示用户 添加用户 更新用户 官方验证: 项目截图 必须修改applicationContext.xml中mysql的配置为本地的,否则启动失败. 另外jar包多一个ehcache.jar无关紧要,删除即可. 1. 使用阿里巴巴Druid连接池(高效.功能强大.可扩展性好的数据库连接池.监控数据库访问性能.支持Common-Logging.Log4j和JdkLog,监控数据库

SpringBoot整合mybatis、shiro、redis实现基于数据库的细粒度动态权限管理系统

1.前言本文主要介绍使用SpringBoot与shiro实现基于数据库的细粒度动态权限管理系统实例. 使用技术:SpringBoot.mybatis.shiro.thymeleaf.pagehelper.Mapper插件.druid.dataTables.ztree.jQuery 开发工具:intellij idea 数据库:mysql.redis 2.表结构还是是用标准的5张表来展现权限.如下图:image 分别为用户表,角色表,资源表,用户角色表,角色资源表.在这个demo中使用了mybat

SpringBoot整合mybatis使用pageHelper插件进行分页操作

SpringBoot整合mybatis分页操作 SpringBoot整合Mybatis进行分页操作,这里需要使用Mybatis的分页插件:pageHelper, 关于pageHelper的介绍,请查看官方文档: https://pagehelper.github.io/ 1.使用前配置 关于pageHelper的使用配置,主要有以下2个步骤: 1.1.在pom文件中导入pageHelper依赖 <dependency> <groupId>com.github.pagehelper&

springboot整合mybatis(SSM开发环境搭建)

0.项目结构: 1.application.properties中配置整合mybatis的配置文件.mybatis扫描别名的基本包与数据源 server.port=80 logging.level.org.springframework=DEBUG #springboot mybatis #jiazai mybatis peizhiwenjian mybatis.mapper-locations = classpath:mapper/*Mapper.xml mybatis.config-loca

SpringBoot整合Mybatis【非注解版】

接上文:SpringBoot整合Mybatis[注解版] 一.项目创建 新建一个工程 ? 选择Spring Initializr,配置JDK版本 ? 输入项目名 ? 选择构建web项目所需的staters(启动器) ? 选择与数据库相关的组件 ? 分析:Spring Boot基本上将我们实际项目开发中所遇到的所有场景都做了封装.它将所有的功能场景都抽取出来,做成了一个个的staters(启动器),只需要在项目的pom.xml配置文件里面引入这些starter相关场景的所有依赖都会导入进来.需要什

springboot整合mybatis通用Mapper

参考: https://blog.csdn.net/x18707731829/article/details/82814095 https://www.jianshu.com/p/6d2103451dcb SpringBoot整合MyBatis Generator可以帮助我们快速生成实体类.接口.mapper.xml文件,可以提高开发的效率,但是每次新增表都要执行一次Generator生成相应的代码,这样重复的操作生成增删查改重复的的代码,有没有一种通用的接口,不用生成mapper.xml文件,

练习小记2:SpringBoot整合MyBatis

本文主要介绍SpringBoot整合MyBatis的初步过程以及需要注意的细节. SpringBoot简介 官网https://spring.io/projects/spring-boot/ ? MyBatis简介 官网:https://mybatis.org/mybatis-3/zh/index.html MyBatis的优势: 历史发展 MyBatis是由Ibatis发展而来的,Ibatis1.x和Ibatis2.x,都称为Ibatis,在Ibatis3.x版本及以后都称为:MyBatis

记一次springboot访问linux的mysql数据库

今天使用springboot连接linux的mysql,期间一直报错,这里简单记录一下过程. 工具:idea-2017,linux-7.x,mysql-5.6.40. 首先说一下这几个注解的作用: (1)Spring Boot的@SpringBootApplication.@EnableAutoConfiguration.@ComponentScan注解,我经常在启动类上傻傻分不清, (2)@EnableAutoConfiguration这个注解是是Springboot根据我们所引入的jar包自