Skip to content
This repository has been archived by the owner on Mar 6, 2020. It is now read-only.

Latest commit

 

History

History
90 lines (63 loc) · 2.62 KB

mapping.md

File metadata and controls

90 lines (63 loc) · 2.62 KB

database

Eloquent ORM for Java

目录

总览

数据映射是用将数据库字段与java对象进行相互转换的必要手段, 理解为数据
反向生成代码
数据类型应该使用包装类型替代基本类型 例如使用Integer替代int
任意一个普通pojo对象即可, 下面是一个例子

package temp.pojo;

import gaarason.database.eloquent.Column;
import gaarason.database.eloquent.Primary;
import gaarason.database.eloquent.Table;
import lombok.Data;

import java.io.Serializable;
import java.math.BigInteger;
import java.util.Date;

@Data
@Table(name = "student")
public class Student implements Serializable {

    @Primary()
    @Column(name = "id", unsigned = true)
    private Long id;

    @Column(name = "name", length = 20, comment = "姓名")
    private String name;

    @Column(name = "age", unsigned = true, comment = "年龄")
    private Integer age;

    @Column(name = "sex", unsigned = true, comment = "性别1男2女")
    private Integer sex;

    @Column(name = "teacher_id", unsigned = true, comment = "教师id")
    private Long teacherId;

    @Column(name = "is_deleted")
    private Byte isDeleted;
 
    @Column(name = "created_at", insertable = false, updatable = false, comment = "新增时间")
    private Date createdAt;
    
    @Column(name = "updated_at", insertable = false, updatable = false, comment = "更新时间")
    private Date updatedAt;
}

数据库建议

数据库字段不建议允许为null, 如果允许为null则在使用ORM新增等操作时,需要声明每一个字段,因为程序不能分辨null值的意义

注解

Table

  • gaarason.database.eloquent.Table 用于确定当前pojo映射的数据表名
  • pojo的类名是对应表名的大驼峰时,可以省略(eg: temp.pojo.SupTeacher对应数据表sup_teacher时,可以省略)

Primary

  • gaarason.database.eloquent.Primary 用于确定当前数据表的主键

Column

  • gaarason.database.eloquent.Column 用于确定每个数据字段的具体属性
  • insertable以及updatablefalse时, 对应字段的ORM操作将被忽略
  • 如果某个数据对象没有Primary注解, 则大多数ORM操作将被禁用