Spring Boot 3.0 GA 正式版尝鲜之增删改查(CURD)Kotlin版 (gradle)

Spring Boot 3.0 GA 正式版尝鲜之增删改查(CURD)Kotlin版 (gradle)

  1. 创建工程

  2. gradle开启snapshots库 repositories增加如下库

    repositories {
        maven("https://oss.sonatype.org/content/repositories/snapshots/")
    }
    
  3. 增加依赖

    implementation("com.zaxxer:HikariCP")
    implementation("com.baomidou:mybatis-plus-boot-starter:3.5.2.7-SNAPSHOT")
    implementation ("cn.hutool:hutool-all:5.8.10")
    
  4. 跨域访问配置,创建WebAppConfig类,填入如下内容,

    @Configuration
    class WebConfig {
        @Bean
        fun corsFilter(): CorsFilter? {
            val source = UrlBasedCorsConfigurationSource()
            val config = CorsConfiguration()
    
            // 允许跨域的头部信息
            config.addAllowedHeader("*")
            // 允许跨域的方法
            config.addAllowedMethod("*")
            // 可访问的外部域
            config.addAllowedOrigin("*")
            // 需要跨域用户凭证(cookie、HTTP认证及客户端SSL证明等)
            //config.setAllowCredentials(true);
            //config.addAllowedOriginPattern("*");
    
            // 跨域路径配置
            source.registerCorsConfiguration("/**", config)
            return CorsFilter(source)
        }
    }
    
  5. 打开dbeaver(本人使用的数据库客户端工具),连接数据并创建数据库(spring-boot3-demo), 并执行如下语句

    DROP TABLE IF EXISTS user;
    
    CREATE TABLE user
    (
        id BIGINT(20) NOT NULL COMMENT '主键ID',
        name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
        age INT(11) NULL DEFAULT NULL COMMENT '年龄',
        email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
        PRIMARY KEY (id)
    );
    
  6. 填入数据库配置 application.yml

    spring:
      datasource:
        type: com.zaxxer.hikari.HikariDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        username: demo
        password: demo
        url: jdbc:mysql://server.com:3306/spring-boot3-demo
    
    mybatis-plus:
      configuration:
        log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    
  7. 创建controller,service,model,mapper

    • UserController

      package cloud.mgl.controller
      
      import cloud.mgl.model.User
      import cloud.mgl.service.UserService
      import cn.hutool.json.JSONUtil
      import org.springframework.beans.factory.annotation.Autowired
      import org.springframework.web.bind.annotation.GetMapping
      import org.springframework.web.bind.annotation.PathVariable
      import org.springframework.web.bind.annotation.PostMapping
      import org.springframework.web.bind.annotation.PutMapping
      import org.springframework.web.bind.annotation.RestController
      
      @RestController
      class UserController {
      
      
          @Autowired
          private lateinit var service: UserService
      
          @GetMapping("/")
          fun home(user: User): String {
              val list = service.query(user)
              return JSONUtil.toJsonStr(list)
          }
      
          @PutMapping("/")
          fun put(user: User): String {
              val list = service.update(user)
              return JSONUtil.toJsonStr(list)
          }
      
          @PostMapping("/")
          fun post(user: User): String {
              val list = service.add(user)
              return JSONUtil.toJsonStr(list)
          }
      
          @GetMapping("/{id}")
          fun get(@PathVariable("id") id: Long): String {
              val user = service.get(id)
              return JSONUtil.toJsonStr(user)
          }
      
      }
      
    • UserService

      package cloud.mgl.service
      
      import cloud.mgl.model.User
      
      interface UserService {
          fun add(user: User)
          fun delete(id: Long)
          fun update(user: User)
          fun query(user: User): MutableList<User>
          fun get(id: Long): User?
      }
      
    • UserServiceImpl

      package cloud.mgl.service.impl
      
      import cloud.mgl.mapper.UserMapper
      import cloud.mgl.model.User
      import cloud.mgl.service.UserService
      import cn.hutool.core.bean.BeanUtil
      import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
      import org.springframework.beans.factory.annotation.Autowired
      import org.springframework.stereotype.Service
      
      @Service
      class UserServiceImpl: UserService {
      
          @Autowired
          private lateinit var mapper: UserMapper
      
      
          override fun add(user: User) {
              mapper.insert(user)
          }
      
          override fun delete(id: Long) {
              mapper.deleteById(id)
          }
      
          override fun update(user: User) {
              mapper.updateById(user)
          }
      
          override fun query(user: User): MutableList<User> {
      
              val params = QueryWrapper<User?>()
      
              val userMap = BeanUtil.beanToMap(user,false,true)
      
              params.allEq(userMap)
      
              val list = mapper.selectList(params)
      
      
              list.forEach {
                  println(it.age)
              }
              return list
          }
      
          override fun get(id: Long): User? {
              return mapper.selectById(id)
          }
      }
      
    • UserMapper

      package cloud.mgl.mapper
      
      import cloud.mgl.model.User
      import com.baomidou.mybatisplus.core.mapper.BaseMapper
      
      interface UserMapper :BaseMapper<User> {
      }
      
    • UserModel

      package cloud.mgl.model
      
      class User {
          var id: Long? = null
          var name: String? = null
          var age: Int? = null
          var email: String? = null
      }
      
  8. 启动类上面添加 @MapperScan("cloud.mgl.mapper") , 并启动程序即可

打包二进制

  • 未来可期

源码地址

本文版权归 123456.blog 所有
转载请附上原文出处链接和本声明
本文链接:
数字工·「」· https://123456.blog ip: 34.225.194.102
Copyright © 2022 Hotpot. All rights reserved. Made with ❤ , 3Q ; Deployed: Kubernetes(k8s),Docker
Design by HotpoT; VvE3 ,NuxT3 123456.blog@0.1.0