sprintboot中的数据库注解

@Results —-表字段与数据库字段映射

@Entity —- 生成表

@Transient —-后台与数据库不映射

@Id —-主键

@Transactional —-事务

@GeneratedValuestrategy=GenerationType.IDENTITY —–自增值

@Table - 映射表名 —– 映射表名

@Column(name = “dict_name”,columnDefinition=”varchar(100) COMMENT ‘字典名’”) —– 字段名、类型、注释

@Select

@Update

@Delete

@Insert

@UpdateTimestamp —— 更新时自动更新时间

@CreationTimestamp ——创建时自动更新时间

@Version —– 版本号,更新时自动加1

1.@Entity

@Entity说明这个class是实体类,并且使用默认的orm规则,即class名即数据库表中表名,class字段名即表中的字段名

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@Entity

@DynamicUpdate
@Data
public class OrderDetail {
@Id
private String detailId;
private String orderId;
private String productId;
private String productName;
private BigDecimal productPrice;
private Integer productQuantity;
private String productIcon;
private Date createTime;
private Date updateTime;
}

运行将对应生成数据库表名为OrderDetail的表,里面字段对应上面的字段。

2.@DynamicUpdate

动态更新表。

如果我们在更新表时,只想更新某个字段,就不要加 @DynamicUpdate,通常为了更新表时的效率,都是不加的.反之,如果我们更新某个字段时,更新所有的字段,就可以加上 @DynamicUpdate.

@DynamicUpdate属性:设置为true,设置为true,表示update对象的时候,生成动态的update语句,如果这个字段的值是null就不会被加入到update语句中,默认false。
比如只想更新某个属性,但是却把整个对象的属性都更新了,这并不是我们希望的结果,我们希望的结果是:我更改了哪些字段,只要更新我修改的字段就够了。

3.@Table

@Table注解用来标识实体类与数据表的对应关系,默认和类名一致。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
@Entity
@Table(name="c_user")
public class User {
@Id
@GeneratedValue
private Long id;

@NotNull
@Column(length = 50)
private String userName;

@NotNull
@Column(length = 20 , unique = true, nullable = false)
private Long mobile;

@Column(length = 20 , unique = true)
private String email;

@NotNull
@Column(columnDefinition="tinyint")
private Integer status;

private String password;
private String nickname;
private Integer companyId;
private Integer departmentId;
private Date regTime;
private String regIp;
private Integer loginNum;
}
4. @Column

@Column注解来标识实体类中属性与数据表中字段的对应关系。共有10个属性,这10个属性均为可选属性:

  • name属性定义了被标注字段在数据库表中所对应字段的名称;
  • unique属性表示该字段是否为唯一标识,默认为false。如果表中有一个字段需要唯一标识,则既可以使用该标记,也可以使用@Table标记中的@UniqueConstraint。
  • nullable属性表示该字段是否可以为null值,默认为true。如果属性里使用了验证类里的@NotNull注释,这个属性可以不写。
  • insertable属性表示在使用“INSERT”脚本插入数据时,是否需要插入该字段的值。
  • updatable属性表示在使用“UPDATE”脚本插入数据时,是否需要更新该字段的值。insertable和updatable属性一般多用于只读的属性,例如主键和外键等。这些字段的值通常是自动生成的。
  • columnDefinition属性表示创建表时,该字段创建的SQL语句,一般用于通过Entity生成表定义时使用。若不指定该属性,通常使用默认的类型建表,若此时需要自定义建表的类型时,可在该属性中设置。(也就是说,如果DB中表已经建好,该属性没有必要使用。)
  • table属性定义了包含当前字段的表名。
  • length属性表示字段的长度,当字段的类型为varchar时,该属性才有效,默认为255个字符。
  • precision属性和scale属性表示精度,当字段类型为double时,precision表示数值的总长度,scale表示小数点所占的位数。
5.@Transactional 事务
@Transactional 注解的属性信息
属性名 说明
name 当在配置文件中有多个 TransactionManager , 可以用该属性指定选择哪个事务管理器。
propagation 事务的传播行为,默认值为 REQUIRED。
isolation 事务的隔离度,默认值采用 DEFAULT。
timeout 事务的超时时间,默认值为-1。如果超过该时间限制但事务还没有完成,则自动回滚事务。
read-only 指定事务是否为只读事务,默认值为 false;为了忽略那些不需要事务的方法,比如读取数据,可以设置 read-only 为 true。
rollback-for 用于指定能够触发事务回滚的异常类型,如果有多个异常类型需要指定,各类型之间可以通过逗号分隔。
no-rollback- for 抛出 no-rollback-for 指定的异常类型,不回滚事务。
6.@Transient

@Transient是希望该属性不会在数据表中产生字段,但又可以在程序中使用它。

7.@Results

(1)当数据库字段名与实体类对应的属性名不一致时,可以使用@Results映射来将其对应起来。column为数据库字段名,porperty为实体类属性名,jdbcType为数据库字段数据类型,id为是否为主键。

1
2
3
4
5
6
7
8

@Select({"select id, name, class_id from my_student"})
@Results({
@Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
@Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
@Result(column="class_id", property="classId", jdbcType=JdbcType.INTEGER)
})
List<Student> selectAll();

(2)@ResultMap的用法。当这段@Results代码需要在多个方法用到时,为了提高代码复用性,我们可以为这个@Results注解设置id,然后使用@ResultMap注解来复用这段代码。

1
2
3
4
5
6
7
8
9
10
11
12

@Select({"select id, name, class_id from my_student"})
@Results(id="studentMap", value={
@Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
@Result(column="class_id", property="classId", jdbcType=JdbcType.INTEGER)
})
List<Student> selectAll();

@Select({"select id, name, class_id from my_student where id = #{id}"})
@ResultMap(value="studentMap")

Student selectById(integer id);

(3)@One的用法。当我们需要通过查询到的一个字段值作为参数,去执行另外一个方法来查询关联的内容,而且两者是一对一关系时,可以使用@One注解来便捷的实现。比如当我们需要查询学生信息以及其所属班级信息时,需要以查询到的class_id为参数,来执行ClassesMapper中的selectById方法,从而获得学生所属的班级信息。可以使用如下代码。

1
2
3
4
5
6
7
Select({"select id, name, class_id from my_student"})
@Results(id="studentMap", value={
@Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
@Result(column="class_id", property="myClass", javaType=MyClass.class,
one=@One(select="com.mapper.MyClassMapper.selectById"))
})
List<Student> selectAllAndClassMsg();

(4)@Many的用法。与@One类似,只不过如果使用@One查询到的结果是多行,会抛出TooManyResultException异常,这种时候应该使用的是@Many注解,实现一对多的查询。比如在需要查询学生信息和每次考试的成绩信息时。

1
2
3
4
5
6
7
8
@Select({"select id, name, class_id from my_student"})
@Results(id="studentMap", value={
@Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
@Result(column="class_id", property="classId", jdbcType=JdbcType.INTEGER),
@Result(column="id", property="gradeList", javaType=List.class,
many=@Many(select="com.example.demo.mapper.GradeMapper.selectByStudentId"))
})
List<Student> selectAllAndGrade();

(5)传递多个参数。首先我们给这张表增加age(年龄)和gender(性别)两个参数。当我们需要根据age和gender查询学生的午餐,这时需要改写column属性的格式。等号左侧的age和gender对应java接口的参数,右侧的对应数据库字段名。即将查到的my_student表中age和gender字段的值,分别赋给getLunchByAgeAndGender方法中的age和gender参数,去查询对应的name(午餐名)。

1
2
3
4
5
6
7
8
9
10
11
@Select("select id, name, age, gender from my_student")
@Results({
@Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
@Result(column="class_id", property="classId", jdbcType=JdbcType.INTEGER),
@Result(column="{age=age,gender=gender}", property="lunch",
one=@One(select="com.example.demo.mapper.StudentMapper.getLunchByAgeAndGender")),
})
List<Student> selectAllAndLunch();

@Select("select name from lunch where student_age = #{age} and student_gender = #{gender}")
String getLunchByAgeAndGender(@Param("age") int age, @Param("gender") int gender);

文章目录
  1. 1. 1.@Entity
  2. 2. 2.@DynamicUpdate
  3. 3. 3.@Table
  4. 4. 4. @Column
  5. 5. 5.@Transactional 事务
  6. 6. @Transactional 注解的属性信息
  7. 7. 6.@Transient
  8. 8. 7.@Results
| 139.6k