141. Spring Boot MyBatis Upgrade - Annotation - @Result

 

【Video & Communication Platform】

à SpringBoot Video 

http://study.163.com/course/introduction.htm?courseId=1004329008&utm_campaign=commission&utm_source=400000000155061&utm_medium=share

à  SpringCloud video

http://study.163.com/course/introduction.htm?courseId=1004638001&utm_campaign=commission&utm_source=400000000155061&utm_medium=share

à Spring Boot source code 

https://gitee.com/happyangellxq520/spring-boot

à Spring Boot communication platform 

http://412887952-qq-com.iteye.com/blog/2321532

 

 

[This blog has a supporting video, the video address: " Spring Boot MyBatis Upgrade - Annotation - @Result ( HD) " , click the following to read the original text in the official account, and the video will explain in more detail]

@Result Description:

@Result modifies the returned result set, and the associated entity class attributes and database fields correspond one-to-one. If the entity class attributes and database attribute names are consistent, this attribute does not need to be modified. This annotation is equivalent to <ResultMap> in the XML configuration file .

 

Scenario example:

( 1 ) Scenario 1 : One-to-one correspondence between associated entity class attributes and database fields

       The query in this case is the simplest statement:

@Select("select *from Demo where id=#{id}")
public Demo selectById(int id);

 

 

( 2 ) Scenario 2 : Some attributes of associated entity class attributes do not correspond to database fields

       For example , the Demo entity class:

    private int id;
    private String name;
    private Date updateTime;

 

       Database table information:

id    int
name     varchar
update_time         datetime

 

 

Then the query statement should look like this:

@Select("select *from Demo where id=#{id}")
    @Results({
       @Result(property="updateTime",column="update_time")
    })
public Demo selectById2(int id);

 

 

 

( 3 ) Scenario 3 : On the basis of the above, gender is an enumeration type

       There is a gender enumeration class:

public enum SexEnum {
    MAN, WOMAN
}

 

       相应的实体类:

private int id;
    private String name;
    private Date updateTime;
    private SexEnum sexEnum;

 

       数据库表信息:

CREATE TABLE `demo` (
   id int(11) NOT NULL AUTO_INCREMENT,
   name  varchar(100) DEFAULT NULL,
  update_time  datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  sex_enum  enum('MAN','WOMAN') DEFAULT NULL,
  PRIMARY KEY (`id`)
);

 

 

       查询语句:

    @Select("select *from Demo where id=#{id}")
    @Results({
       @Result(property="updateTime",column="update_time"),
       @Result(property="sexEnum",column="sex_enum",javaType=SexEnum.class)
    })
    public Demo selectById2(int id);

 

 

好了对于@Result就介绍到这里,最后对几个常用的注解总结下:

@Select是查询类的注解,所有的查询均使用这个

@Result修饰返回的结果集,关联实体类属性和数据库字段一一对应,如果实体类属性和数据库属性名保持一致,就不需要这个属性来修饰。

@Insert插入数据库使用,直接传入实体类会自动解析属性到对应的值

@Update负责修改,也可以直接传入对象

@delete负责删除

 

视频&交流平台

à SpringBoot网易云课堂视频

http://study.163.com/course/introduction.htm?courseId=1004329008

à Spring Boot交流平台

http://412887952-qq-com.iteye.com/blog/2321532

 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326925786&siteId=291194637