11.6 数据库实体类

我们在上面已经完成了MySQL数据源的配置,下面我们来写一个实体类。新建package com.easy.kotlin.chapter11_kotlin_springboot.entity ,然后新建Article实体类:

  1. package com.easy.kotlin.chapter11_kotlin_springboot.entity
  2. import java.util.*
  3. import javax.persistence.*
  4. @Entity
  5. class Article {
  6. @Id
  7. @GeneratedValue(strategy = GenerationType.IDENTITY)
  8. var id: Long = -1
  9. @Version
  10. var version: Long = 0
  11. var title: String = ""
  12. var content: String = ""
  13. var author: String = ""
  14. var gmtCreated: Date = Date()
  15. var gmtModified: Date = Date()
  16. var isDeleted: Int = 0 //1 Yes 0 No
  17. var deletedDate: Date = Date()
  18. override fun toString(): String {
  19. return "Article(id=$id, version=$version, title='$title', content='$content', author='$author', gmtCreated=$gmtCreated, gmtModified=$gmtModified, isDeleted=$isDeleted, deletedDate=$deletedDate)"
  20. }
  21. }

类似的实体类,我们在Java中需要生成一堆getter/setter方法;如果我们用Scala写还需要加个 注解@BeanProperty, 例如

  1. package com.springboot.in.action.entity
  2. import java.util.Date
  3. import javax.persistence.{ Entity, GeneratedValue, GenerationType, Id }
  4. import scala.language.implicitConversions
  5. import scala.beans.BeanProperty
  6. @Entity
  7. class HttpApi {
  8. @Id
  9. @GeneratedValue(strategy = GenerationType.AUTO)
  10. @BeanProperty
  11. var id: Integer = _
  12. @BeanProperty
  13. var httpSuiteId: Integer = _
  14. //用例名称
  15. @BeanProperty
  16. var name: String = _
  17. //用例状态: -1未执行 0失败 1成功
  18. @BeanProperty
  19. var state: Integer = _
  20. ...
  21. }

我们这个是一个博客文章的简单实体类。再次重启运行应用,我们去MySQL的Schema: blog 里面去看,发现数据库自动生成了 Table: article , 它的表字段信息如下:

Field Type Null Key Default Extra
id bigint(20) NO PRI NULL auto_increment
author varchar(255) YES NULL
content varchar(255) YES NULL
deleted_date datetime YES NULL
gmt_created datetime YES NULL
gmt_modified datetime YES NULL
is_deleted int(11) NO NULL
title varchar(255) YES NULL
version bigint(20) NO NULL -