11.6 数据库实体类

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

1. package com.easy.kotlin.chapter11_kotlin_springboot.entity

3. import java.util.*
4. import javax.persistence.*

6. @Entity
7. class Article {
8. @Id
9. @GeneratedValue(strategy = GenerationType.IDENTITY)
10. var id: Long = -1
11. @Version
12. var version: Long = 0
13. var title: String = ""
14. var content: String = ""
15. var author: String = ""
16. var gmtCreated: Date = Date()
17. var gmtModified: Date = Date()
18. var isDeleted: Int = 0 //1 Yes 0 No
19. var deletedDate: Date = Date()

21. override fun toString(): String {
22. return "Article(id=$id, version=$version, title='$title', content='$content', author='$author', gmtCreated=$gmtCreated, gmtModified=$gmtModified, isDeleted=$isDeleted, deletedDate=$deletedDate)"
23. }

25. }

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

1. package com.springboot.in.action.entity

3. import java.util.Date
4. import javax.persistence.{ Entity, GeneratedValue, GenerationType, Id }
5. import scala.language.implicitConversions
6. import scala.beans.BeanProperty

8. @Entity
9. class HttpApi {
10. @Id
11. @GeneratedValue(strategy = GenerationType.AUTO)
12. @BeanProperty
13. var id: Integer = _

15. @BeanProperty
16. var httpSuiteId: Integer = _
17. //用例名称
18. @BeanProperty
19. var name: String = _

21. //用例状态: -1未执行 0失败 1成功
22. @BeanProperty
23. var state: Integer = _
24. ...

26. }

我们这个是一个博客文章的简单实体类。再次重启运行应用,我们去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 -