记录一次kotlin + spring boot3.0.5 + oracle 提供接口简单应用
·
流程
idea spring3.0.5- 依赖加入
sprng boot web + jpa + jdbc api + oracle driver + mysql driver(其实我也不知到哪个依赖用上了)
文件
// 启动类
package com.example.demo
import oracle.jdbc.pool.OracleDataSource
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.context.annotation.Bean
import javax.sql.DataSource
@SpringBootApplication
// !!!!!!!!!!!!!!!!!配置数据源
class Demo2Application{
@Bean
fun dataSource(): DataSource {
val ds = OracleDataSource()
// 这里配置你的oracle数据源,这里你要清楚你的数据源,包括 host,port,sid,username,password
ds.setURL("jdbc:oracle:thin:@222.16.65.250:1521:orcl")
ds.setUser("zbs10033")
ds.setPassword("123")
return ds
}
}
fun main(args: Array<String>) {
runApplication<Demo2Application>(*args)
}
// controller/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("/")
fun hello():String{
return "hello world"
}
}
// controller/ProductController.kt
// 对数据库实体类的控制类
package com.example.demo.controller
import com.example.demo.entity.Product
import com.example.demo.entity.ProductRepository
import org.springframework.web.bind.annotation.*
@RestController
@RequestMapping("/api/products")
class ProductController(private val productRepository: ProductRepository) {
@GetMapping("/")
fun getAllProducts(): List<Product> =
productRepository.findAll()
@PostMapping("/")
fun createProduct(@RequestBody product: Product): Product =
productRepository.save(product)
@GetMapping("/{id}")
fun getProductById(@PathVariable id: Long): Product =
productRepository.findById(id).orElseThrow()
@PutMapping("/{id}")
fun updateProductById(@PathVariable id: Long, @RequestBody product: Product): Product {
val existingProduct = productRepository.findById(id).orElseThrow()
existingProduct.name = product.name
existingProduct.price = product.price
return productRepository.save(existingProduct)
}
@DeleteMapping("/{id}")
fun deleteProductById(@PathVariable id: Long) = productRepository.deleteById(id)
}
// entity/Product.kt
package com.example.demo.entity
import jakarta.persistence.*
import org.jetbrains.annotations.NotNull
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository
@Entity
@Table(name = "PRODUCTS")
data class Product(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long? = null,
@NotNull
var name: String,
@NotNull
var price: Double
)
@Repository
interface ProductRepository : JpaRepository<Product, Long>
数据库的Products表设计
-- product表
create table Products(
id int primary key,
name char(50) not null,
price number not null
);
-- 加入一个数据
insert into products values(1,'zbs',12.12);
build.gradle(没学过,所以我也看不懂)
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id 'org.springframework.boot' version '3.0.5'
id 'io.spring.dependency-management' version '1.1.0'
id 'org.jetbrains.kotlin.jvm' version '1.7.22'
id 'org.jetbrains.kotlin.plugin.spring' version '1.7.22'
id 'org.jetbrains.kotlin.plugin.jpa' version '1.7.22'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'com.fasterxml.jackson.module:jackson-module-kotlin'
implementation 'org.jetbrains.kotlin:kotlin-reflect'
runtimeOnly 'com.mysql:mysql-connector-j'
runtimeOnly 'com.oracle.database.jdbc:ojdbc8'
implementation("com.oracle.database.jdbc:ojdbc8:21.1.0.0")
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
tasks.withType(KotlinCompile) {
kotlinOptions {
freeCompilerArgs = ['-Xjsr305=strict']
jvmTarget = '17'
}
}
tasks.named('test') {
useJUnitPlatform()
}
尝试运行
curl localhost:8080/api/products/1
因为是学校的数据库服务器,所以链接受限吧,不能长时间链接
尝试自己搭建docker oracle
更多推荐
所有评论(0)