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