注解@Column实现实体类到数据库字段的映射以及解决映射过程中的type#[]类型错误

注解@Column实现实体类到数据库字段的映射解决映射过程中的type#[]类型错误

1、@GeneratedValue实现id的自增长

实体类中:

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;

xml文件中:(constraints中放的是字段的属性,primaryKey="true"表示该字段为主键,nullable="false"表示该字段不可为空)

<createTable tableName="tablename">
            <column name="id" type="int" autoIncrement="true">
                <constraints primaryKey="true" nullable="false"/>
            </column>
</createTable>

2、@Column映射数据库的字段

实体类:(name里面的字段名要和xml里面的对应,如果有下划线比如ztb_id,就一定要写,xml里面不能驼峰命名;如果,没有下划线,那么name可以不写)

@Column(name = "ztb_id", nullable = false)
 private int ztbId;

@Column(length = 40, nullable = false)
private String fkkk;

 xml文件中:

<column name="ztb_id" type="int">
                <constraints nullable="false"/>
</column>

<column name="fkkk" type="nvarchar2(40)">
                <constraints nullable="false"/>
</column>

3、@Nationalized支持中文

实体类:

@Nationalized
@Column(length = 40, nullable = false)
private String fkkk;

4、@Enumerated支持枚举类型

枚举类:

import java.io.Serializable;

public enum ListType implements Serializable {
    DAXIU, GXGZ
}

实体类:

@Enumerated(EnumType.STRING)
@Column(name = "list_type", length = 10, nullable = false)
private ListType listType;

xml:

<column name="list_type" type="varchar2(10)">
                <constraints nullable="false"/>
</column>

5、@Column(columnDefinition)解决映射过程中的type#[]类型错误。String类型默认映射的数值类型是varchar,columnDefinition可以进行额外指定(有时候编译运行会出现类型错误,所以就需要)

实体类:

@Column(nullable = false, columnDefinition = "char")
private String qksj;

xml:

<column name="qksj" type="char(10)">
                <constraints nullable="false"/>
</column>
原文地址:https://www.cnblogs.com/pzw23/p/11958640.html