当前位置:   article > 正文

Kotlin整合Spring Boot实现数据库增删改查(mybatis版)_kotlin 使用springboot 查询数据库

kotlin 使用springboot 查询数据库

前几天由于工作需要,便开始学习了kotlin,java基础扎实学起来也还算比较快,对于kotlin这个编程语言自然是比java有趣一些,因此就有了使用kotlin搭建基于spring boot的开发环境。这次先分享mybatis版本的

1 maven的pom文件

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.debug</groupId>
  5. <artifactId>KSpringBoot</artifactId>
  6. <version>0.0.1-SNAPSHOT</version>
  7. <packaging>jar</packaging>
  8. <name>KSpringBoot</name>
  9. <url>http://maven.apache.org</url>
  10. <properties>
  11. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  12. </properties>
  13. <parent>
  14. <groupId>org.springframework.boot</groupId>
  15. <artifactId>spring-boot-starter-parent</artifactId>
  16. <version>2.0.0.BUILD-SNAPSHOT</version>
  17. </parent>
  18. <dependencies>
  19. <dependency>
  20. <groupId>org.springframework.boot</groupId>
  21. <artifactId>spring-boot-starter-web</artifactId>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.jetbrains.kotlin</groupId>
  25. <artifactId>kotlin-stdlib-jre8</artifactId>
  26. <version>1.1.2</version>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.jetbrains.kotlin</groupId>
  30. <artifactId>kotlin-reflect</artifactId>
  31. <version>1.1.2</version>
  32. </dependency>
  33. <dependency>
  34. <groupId>com.fasterxml.jackson.module</groupId>
  35. <artifactId>jackson-module-kotlin</artifactId>
  36. <version>2.9.3</version>
  37. </dependency>
  38. <dependency>
  39. <groupId>mysql</groupId>
  40. <artifactId>mysql-connector-java</artifactId>
  41. </dependency>
  42. <dependency>
  43. <groupId>org.mybatis.spring.boot</groupId>
  44. <artifactId>mybatis-spring-boot-starter</artifactId>
  45. <version>1.3.1</version>
  46. </dependency>
  47. <dependency>
  48. <groupId>junit</groupId>
  49. <artifactId>junit</artifactId>
  50. <version>3.8.1</version>
  51. <scope>test</scope>
  52. </dependency>
  53. </dependencies>
  54. <repositories>
  55. <repository>
  56. <id>spring-snapshots</id>
  57. <name>Spring Snapshots</name>
  58. <url>https://repo.spring.io/libs-snapshot</url>
  59. <snapshots>
  60. <enabled>true</enabled>
  61. </snapshots>
  62. </repository>
  63. </repositories>
  64. <build>
  65. <pluginManagement>
  66. <plugins>
  67. <plugin>
  68. <artifactId>kotlin-maven-plugin</artifactId>
  69. <groupId>org.jetbrains.kotlin</groupId>
  70. <version>1.1.2</version>
  71. <configuration>
  72. <compilerPlugins>
  73. <plugin>spring</plugin>
  74. </compilerPlugins>
  75. <jvmTarget>1.8</jvmTarget>
  76. </configuration>
  77. <executions>
  78. <execution>
  79. <id>compile</id>
  80. <phase>compile</phase>
  81. <goals>
  82. <goal>compile</goal>
  83. </goals>
  84. </execution>
  85. <execution>
  86. <id>test-compile</id>
  87. <phase>test-compile</phase>
  88. <goals>
  89. <goal>test-compile</goal>
  90. </goals>
  91. </execution>
  92. </executions>
  93. <dependencies>
  94. <dependency>
  95. <groupId>org.jetbrains.kotlin</groupId>
  96. <artifactId>kotlin-maven-allopen</artifactId>
  97. <version>1.1.2</version>
  98. </dependency>
  99. </dependencies>
  100. </plugin>
  101. <plugin>
  102. <groupId>org.springframework.boot</groupId>
  103. <artifactId>spring-boot-maven-plugin</artifactId>
  104. <configuration>
  105. <!--fork : 如果没有该项配置,devtools不会起作用,即应用不会restart -->
  106. <fork>true</fork>
  107. </configuration>
  108. </plugin>
  109. </plugins>
  110. </pluginManagement>
  111. </build>
  112. </project>
kotlin相关的东西就不特别挑出来了,也基本是固定的写法

2 spring boot的主配置文件和mybatis配置文件

  1. #DataBase DataSources
  2. spring.datasource.url = jdbc:mysql://localhost:3306/debugxwz
  3. spring.datasource.username = root
  4. spring.datasource.password = 123456
  5. spring.datasource.driverClassName = com.mysql.jdbc.Driver
  6. spring.datasource.max-active=20
  7. spring.datasource.max-idle=8
  8. spring.datasource.min-idle=8
  9. spring.datasource.initial-size=10
  10. mybatis.typeAliasesPackage=com.debug.pojo
  11. mybatis.config-location=classpath:mybatis-config.xml
  12. mybatis.mapperLocations=classpath:mapper/*.xml
  13. logging.level.com.debug=debug
数据源的配置还和之前一样,特别注意mybatis的这段配置,大家根据实际情况调整就行
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6. <!--根据需要加入有关配置-->
  7. </configuration>
3 编写实体类,因为是kotlin写法和之前java有了一些差异

  1. package com.debug.pojo
  2. data class UserInfo (
  3. val id:Int,
  4. val name:String?=null,
  5. val address:String?=null
  6. )
4 编写映射器,和java的写法区别不是太大

  1. package com.debug.dao
  2. import org.apache.ibatis.annotations.Mapper
  3. import com.debug.pojo.UserInfo
  4. @Mapper
  5. interface UserMapper {
  6. //根据id获取用户信息
  7. fun findUserById(questionId: Int): UserInfo
  8. //保存用户信息
  9. fun saveUser(user:UserInfo)
  10. //修改用户信息
  11. fun updateUser(user:UserInfo)
  12. //删除用户信息
  13. fun removeUserById(user:UserInfo)
  14. }
5  service层代码

  1. package com.debug.service
  2. import com.debug.pojo.UserInfo
  3. import org.springframework.transaction.annotation.Transactional
  4. import org.springframework.transaction.annotation.Propagation
  5. interface IUserService {
  6. fun findUserById(userId: Int): UserInfo
  7. fun saveUser(user:UserInfo)
  8. fun updateUser(user:UserInfo)
  9. fun removeUserById(user:UserInfo)
  10. }

  1. package com.debug.service.impl
  2. import com.debug.service.IUserService
  3. import javax.annotation.Resource
  4. import com.debug.dao.UserMapper
  5. import com.debug.pojo.UserInfo
  6. import org.springframework.stereotype.Service
  7. import org.springframework.transaction.annotation.Transactional
  8. import javafx.scene.control.Pagination
  9. import org.springframework.transaction.annotation.Propagation
  10. @Service(value="userSrvice")
  11. open class UserService:IUserService{
  12. @Resource
  13. lateinit var userMapper:UserMapper
  14. override fun findUserById(userId: Int):UserInfo{
  15. return userMapper.findUserById(userId)
  16. }
  17. @Transactional(propagation=Propagation.REQUIRED)
  18. override fun saveUser(user:UserInfo){
  19. userMapper.saveUser(user)
  20. }
  21. @Transactional(propagation=Propagation.REQUIRED)
  22. override fun updateUser(user:UserInfo){
  23. userMapper.updateUser(user)
  24. }
  25. @Transactional(propagation=Propagation.REQUIRED)
  26. override fun removeUserById(user:UserInfo){
  27. userMapper.removeUserById(user)
  28. }
  29. }
需要注意和java不同的是,如果有bean的注入,需要在前面加上lateinit

6  controller的代码

  1. package com.debug.controller
  2. import org.springframework.web.bind.annotation.RestController
  3. import javax.annotation.Resource
  4. import com.debug.service.IUserService
  5. import org.springframework.web.bind.annotation.RequestMapping
  6. import org.springframework.web.bind.annotation.ResponseBody
  7. import com.debug.pojo.UserInfo
  8. import javax.servlet.http.HttpServletRequest
  9. import com.debug.service.impl.UserService
  10. @RestController
  11. class UserController {
  12. @Resource
  13. lateinit var userService: IUserService;
  14. @RequestMapping("/getUserById")
  15. @ResponseBody
  16. fun getUserById(req:HttpServletRequest):UserInfo {
  17. val id:Int=Integer.parseInt(req.getParameter("id"))
  18. val info:UserInfo=userService.findUserById(id)
  19. return info
  20. }
  21. @RequestMapping("/saveUser")
  22. @ResponseBody
  23. fun saveUser():String {
  24. val u=UserInfo(0,"蔡依林","河南")
  25. userService.saveUser(u)
  26. return "保存成功";
  27. }
  28. @RequestMapping("/updateUser")
  29. @ResponseBody
  30. fun updateUser():String {
  31. val u=UserInfo(3,"赵雅芝")
  32. userService.updateUser(u)
  33. return "修改成功";
  34. }
  35. @RequestMapping("/removeUserById")
  36. @ResponseBody
  37. fun removeUserById():String {
  38. val u=UserInfo(4)
  39. userService.removeUserById(u)
  40. return "删除成功";
  41. }
  42. }
controller和之前java的也没太大的差别

7  启动程序的kotlin类

  1. package com.debug
  2. import org.springframework.boot.SpringApplication
  3. import org.springframework.boot.autoconfigure.SpringBootApplication
  4. @SpringBootApplication
  5. open class KotlinDemoApplication
  6. fun main(args: Array<String>) {
  7. SpringApplication.run(KotlinDemoApplication::class.java, *args)
  8. }

这里注意不能少些open,否则spring boot起不来还会报一堆异常

下面顺带把mybatis的配置文件贴一贴:

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  3. <mapper namespace="com.debug.dao.UserMapper" >
  4. <resultMap id="userBaseResultMap" type="com.debug.pojo.UserInfo" >
  5. <id column="id" property="id" />
  6. <result column="name" property="name" />
  7. <result column="address" property="address" />
  8. </resultMap>
  9. <sql id="User_Base_Column_List" >
  10. id, name, address
  11. </sql>
  12. <select id="findUserById" resultMap="userBaseResultMap" >
  13. SELECT
  14. <include refid="User_Base_Column_List" />
  15. FROM user
  16. WHERE id = #{id}
  17. </select>
  18. <insert id="saveUser" parameterType="com.debug.pojo.UserInfo">
  19. insert into user(name,address) values(#{name},#{address})
  20. </insert>
  21. <update id="updateUser" parameterType="com.debug.pojo.UserInfo">
  22. update user
  23. <trim prefix="SET" suffixOverrides=",">
  24. <if test="name != null">
  25. name = #{name}
  26. </if>
  27. <if test="address != null">
  28. address = #{address}
  29. </if>
  30. </trim>
  31. where id=#{id}
  32. </update>
  33. <delete id="removeUserById" parameterType="com.debug.pojo.UserInfo">
  34. delete from user where id=#{id}
  35. </delete>
  36. </mapper>


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/运维做开发/article/detail/1009444
推荐阅读
相关标签
  

闽ICP备14008679号