前几天由于工作需要,便开始学习了kotlin,java基础扎实学起来也还算比较快,对于kotlin这个编程语言自然是比java有趣一些,因此就有了使用kotlin搭建基于spring boot的开发环境。这次先分享mybatis版本的
1 maven的pom文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.debug</groupId>
<artifactId>KSpringBoot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>KSpringBoot</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jre8</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-kotlin</artifactId>
<version>2.9.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/libs-snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>1.1.2</version>
<configuration>
<compilerPlugins>
<plugin>spring</plugin>
</compilerPlugins>
<jvmTarget>1.8</jvmTarget>
</configuration>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>1.1.2</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!--fork : 如果没有该项配置,devtools不会起作用,即应用不会restart -->
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
kotlin相关的东西就不特别挑出来了,也基本是固定的写法
2 spring boot的主配置文件和mybatis配置文件
#DataBase DataSources
spring.datasource.url = jdbc:mysql://localhost:3306/debugxwz
spring.datasource.username = root
spring.datasource.password = 123456
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10
mybatis.typeAliasesPackage=com.debug.pojo
mybatis.config-location=classpath:mybatis-config.xml
mybatis.mapperLocations=classpath:mapper/*.xml
logging.level.com.debug=debug
数据源的配置还和之前一样,特别注意mybatis的这段配置,大家根据实际情况调整就行
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--根据需要加入有关配置-->
</configuration>
3 编写实体类,因为是kotlin写法和之前java有了一些差异
package com.debug.pojo
data class UserInfo (
val id:Int,
val name:String?=null,
val address:String?=null
)
4 编写映射器,和java的写法区别不是太大
package com.debug.dao
import org.apache.ibatis.annotations.Mapper
import com.debug.pojo.UserInfo
@Mapper
interface UserMapper {
//根据id获取用户信息
fun findUserById(questionId: Int): UserInfo
//保存用户信息
fun saveUser(user:UserInfo)
//修改用户信息
fun updateUser(user:UserInfo)
//删除用户信息
fun removeUserById(user:UserInfo)
}
5 service层代码
package com.debug.service
import com.debug.pojo.UserInfo
import org.springframework.transaction.annotation.Transactional
import org.springframework.transaction.annotation.Propagation
interface IUserService {
fun findUserById(userId: Int): UserInfo
fun saveUser(user:UserInfo)
fun updateUser(user:UserInfo)
fun removeUserById(user:UserInfo)
}
package com.debug.service.impl
import com.debug.service.IUserService
import javax.annotation.Resource
import com.debug.dao.UserMapper
import com.debug.pojo.UserInfo
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import javafx.scene.control.Pagination
import org.springframework.transaction.annotation.Propagation
@Service(value="userSrvice")
open class UserService:IUserService{
@Resource
lateinit var userMapper:UserMapper
override fun findUserById(userId: Int):UserInfo{
return userMapper.findUserById(userId)
}
@Transactional(propagation=Propagation.REQUIRED)
override fun saveUser(user:UserInfo){
userMapper.saveUser(user)
}
@Transactional(propagation=Propagation.REQUIRED)
override fun updateUser(user:UserInfo){
userMapper.updateUser(user)
}
@Transactional(propagation=Propagation.REQUIRED)
override fun removeUserById(user:UserInfo){
userMapper.removeUserById(user)
}
}
需要注意和java不同的是,如果有bean的注入,需要在前面加上lateinit
6 controller的代码
package com.debug.controller
import org.springframework.web.bind.annotation.RestController
import javax.annotation.Resource
import com.debug.service.IUserService
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.ResponseBody
import com.debug.pojo.UserInfo
import javax.servlet.http.HttpServletRequest
import com.debug.service.impl.UserService
@RestController
class UserController {
@Resource
lateinit var userService: IUserService;
@RequestMapping("/getUserById")
@ResponseBody
fun getUserById(req:HttpServletRequest):UserInfo {
val id:Int=Integer.parseInt(req.getParameter("id"))
val info:UserInfo=userService.findUserById(id)
return info
}
@RequestMapping("/saveUser")
@ResponseBody
fun saveUser():String {
val u=UserInfo(0,"蔡依林","河南")
userService.saveUser(u)
return "保存成功";
}
@RequestMapping("/updateUser")
@ResponseBody
fun updateUser():String {
val u=UserInfo(3,"赵雅芝")
userService.updateUser(u)
return "修改成功";
}
@RequestMapping("/removeUserById")
@ResponseBody
fun removeUserById():String {
val u=UserInfo(4)
userService.removeUserById(u)
return "删除成功";
}
}
controller和之前java的也没太大的差别
7 启动程序的kotlin类
package com.debug
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
@SpringBootApplication
open class KotlinDemoApplication
fun main(args: Array<String>) {
SpringApplication.run(KotlinDemoApplication::class.java, *args)
}
这里注意不能少些open,否则spring boot起不来还会报一堆异常
下面顺带把mybatis的配置文件贴一贴:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.debug.dao.UserMapper" >
<resultMap id="userBaseResultMap" type="com.debug.pojo.UserInfo" >
<id column="id" property="id" />
<result column="name" property="name" />
<result column="address" property="address" />
</resultMap>
<sql id="User_Base_Column_List" >
id, name, address
</sql>
<select id="findUserById" resultMap="userBaseResultMap" >
SELECT
<include refid="User_Base_Column_List" />
FROM user
WHERE id = #{id}
</select>
<insert id="saveUser" parameterType="com.debug.pojo.UserInfo">
insert into user(name,address) values(#{name},#{address})
</insert>
<update id="updateUser" parameterType="com.debug.pojo.UserInfo">
update user
<trim prefix="SET" suffixOverrides=",">
<if test="name != null">
name = #{name}
</if>
<if test="address != null">
address = #{address}
</if>
</trim>
where id=#{id}
</update>
<delete id="removeUserById" parameterType="com.debug.pojo.UserInfo">
delete from user where id=#{id}
</delete>
</mapper>