当前位置:   article > 正文

kotlin使用spring data jpa(一)

kotlin spring data jpa

简单查询

spring data jpa是spring全家桶成员之一,用于操作各种数据库,下面写一个查询订单的简单示例

  1. 引入sqlserver驱动包及jpa所需包
  1. implementation('org.springframework.boot:spring-boot-starter-data-jpa')
  2. runtimeOnly('com.microsoft.sqlserver:mssql-jdbc')

2.配置连接字符串

  1. spring.datasource.url=jdbc:sqlserver://machinename or ip:1433;database=dbname
  2. spring.datasource.username=username
  3. spring.datasource.password=password
  4. spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
  5. spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
  6. #关闭初始化数据库
  7. spring.jpa.generate-ddl=false
  8. #显示生成的sql
  9. spring.jpa.show-sql=true

3.创建entity,可用idea生成

  1. @Entity
  2. @Table(name = "Order", schema = "dbo")
  3. class OrderEntity {
  4. @get:Id
  5. @get:Column(name = "OrderID")
  6. var orderId: String? = null
  7. @get:Basic
  8. @get:Column(name = "OrderStatus")
  9. var orderStatus: Byte = 0
  10. @get:Basic
  11. @get:Column(name = "OrderDate")
  12. var orderDate: LocalDateTime? = null
  13. @get:Basic
  14. @get:Column(name = "CurrencyCode")
  15. var currencyCode: String? = null
  16. @get:Basic
  17. @get:Column(name = "OrderAmount")
  18. var orderAmount: BigDecimal? = null
  19. }

4.创建repository

  1. @Repository
  2. interface OrderRepository:JpaRepository<OrderEntity,String>

可以简单调用一下了

  1. @Autowired
  2. private lateinit var orderRepository: OrderRepository
  3. orderRepository.findAll()

转载于:https://my.oschina.net/weidedong/blog/2396136

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

闽ICP备14008679号