11.6 数据库实体类
我们在上面已经完成了MySQL数据源的配置,下面我们来写一个实体类。新建package com.easy.kotlin.chapter11_kotlin_springboot.entity
,然后新建Article
实体类:
package com.easy.kotlin.chapter11_kotlin_springboot.entity
import java.util.*
import javax.persistence.*
@Entity
class Article {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
var id: Long = -1
@Version
var version: Long = 0
var title: String = ""
var content: String = ""
var author: String = ""
var gmtCreated: Date = Date()
var gmtModified: Date = Date()
var isDeleted: Int = 0 //1 Yes 0 No
var deletedDate: Date = Date()
override fun toString(): String {
return "Article(id=$id, version=$version, title='$title', content='$content', author='$author', gmtCreated=$gmtCreated, gmtModified=$gmtModified, isDeleted=$isDeleted, deletedDate=$deletedDate)"
}
}
类似的实体类,我们在Java中需要生成一堆getter/setter方法;如果我们用Scala写还需要加个 注解@BeanProperty, 例如
package com.springboot.in.action.entity
import java.util.Date
import javax.persistence.{ Entity, GeneratedValue, GenerationType, Id }
import scala.language.implicitConversions
import scala.beans.BeanProperty
@Entity
class HttpApi {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@BeanProperty
var id: Integer = _
@BeanProperty
var httpSuiteId: Integer = _
//用例名称
@BeanProperty
var name: String = _
//用例状态: -1未执行 0失败 1成功
@BeanProperty
var state: Integer = _
...
}
我们这个是一个博客文章的简单实体类。再次重启运行应用,我们去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 | - |
当前内容版权归 JackChan1999 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 JackChan1999 .