implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-webflux'
implementation 'com.fasterxml.jackson.module:jackson-module-kotlin'
implementation 'io.projectreactor.kotlin:reactor-kotlin-extensions'
implementation 'org.jetbrains.kotlin:kotlin-reflect'
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-reactor'
runtimeOnly 'com.mysql:mysql-connector-j'
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'io.projectreactor:reactor-test'
implementation ("cn.hutool:hutool-all:5.8.10")
UserModel
package cloud.mgl.model
import jakarta.persistence.Entity
import jakarta.persistence.GeneratedValue
import jakarta.persistence.GenerationType
import jakarta.persistence.Id
import org.hibernate.annotations.DynamicInsert
import org.hibernate.annotations.DynamicUpdate
@Entity(name = "user")
@DynamicInsert
@DynamicUpdate
class UserModel {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
var id: Long? = null
var name: String? = null
var email: String? = null
var age: Long? = null
}
UserDao
package cloud.mgl.dao
import cloud.mgl.model.UserModel
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository
@Repository
interface UserDao: JpaRepository<UserModel, Long>
UserTest
package cloud.mgl
import cloud.mgl.dao.UserDao
import cloud.mgl.model.UserModel
import cn.hutool.json.JSONUtil
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.transaction.annotation.Transactional
import kotlin.jvm.optionals.getOrNull
@SpringBootTest
class SpringBoot3NativeDemoApplicationTests {
@Autowired
private lateinit var dao: UserDao
/**
* 查询所有
*/
@Test
fun contextLoads() {
val findAll = dao.findAll()
println(findAll.size)
findAll.forEach {
println(it.name)
}
}
// 新增
@Test
fun add() {
val vo = UserModel()
vo.name = "张三之测试native"
vo.age = 23
vo.email = "sldkfjsldf@zs.com"
dao.save(vo)
}
//通过id获取记录
@Test
fun get() {
val optional = dao.findById(1600461460750872583)
if (!optional.isEmpty) {
println(JSONUtil.toJsonStr(optional.get()))
}
}
//删除
@Test
fun delete() {
dao.deleteById(1600461460750872580)
}
//更新
@Test
fun update() {
val vo = dao.findById(1600461460750872582).orElse(null)
vo.name = "张三"
vo.age = 22
dao.save(vo)
}
}
UserController
package cloud.mgl.controller
import cloud.mgl.model.UserModel
import cloud.mgl.service.UserService
import cn.hutool.json.JSONUtil
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.DeleteMapping
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
@PostMapping("/")
fun add(user: UserModel) {
service.add(user)
}
@DeleteMapping("/")
fun delete(user: UserModel) {
service.add(user)
}
@PutMapping("/")
fun update(user: UserModel) {
service.add(user)
}
@GetMapping("/")
fun query(user: UserModel): String {
val query = service.query(user)
return JSONUtil.toJsonStr(query)
}
@GetMapping("/{id}")
fun query(@PathVariable id: Long): String {
val query = service.get(id)
return JSONUtil.toJsonStr(query)
}
}
UserService
package cloud.mgl.service
import cloud.mgl.model.UserModel
interface UserService {
fun add(userModel: UserModel)
fun delete(id: Long)
fun update(userModel: UserModel)
fun query(userModel: UserModel): MutableList<UserModel>
fun get(id: Long): UserModel
}
UserServiceImpl
package cloud.mgl.service.impl
import cloud.mgl.dao.UserDao
import cloud.mgl.model.UserModel
import cloud.mgl.service.UserService
import cn.hutool.core.bean.BeanUtil
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
@Service
class UserServiceImpl : UserService {
@Autowired
private lateinit var dao: UserDao
override fun add(userModel: UserModel) {
dao.save(userModel)
}
override fun delete(id: Long) {
dao.deleteById(id)
}
override fun update(userModel: UserModel) {
val model = dao.findById(userModel.id!!).orElse(null)
BeanUtil.copyProperties(userModel, model)
/*model.age = userModel.age
model.name = userModel.name
model.email = userModel.email*/
dao.save(model)
}
override fun query(userModel: UserModel): MutableList<UserModel> {
return dao.findAll()
}
override fun get(id: Long): UserModel {
return dao.findById(id).orElse(null)
}
}
package cloud.mgl.config
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.web.cors.CorsConfiguration
import org.springframework.web.cors.UrlBasedCorsConfigurationSource
import org.springframework.web.filter.CorsFilter
@Configuration
class WebAppConfig {
@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)
}
}
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.zaxxer.hikari.HikariDataSource
username: demo
password: demo
url: jdbc:mysql://server.com:3306/spring-boot3-demo
jpa:
show-sql: true