zoukankan      html  css  js  c++  java
  • @Formula

    @Formula  计算临时属性。

    相当于可以关联查询字段,然后放在实体中当做属性使用。

    任务:在User实体层,增加一个额外的属性,来获取Test表中的name字段。

    1 表结构

    User表

    Test表

    2 User实体层(省略了部分字段。)

    @Entity
    @Table(name = "user", catalog = "testdb")
    @DynamicUpdate
    public class User {
        private Integer id;
        private String uuid;
     
        private String test;
     
        @Id
        @GeneratedValue
        @Column(name = "id")
        public Integer getId() {
            return id;
        }
     
        public void setId(Integer id) {
            this.id = id;
        }
     
        @Column(name = "uuid")
        public String getUuid() {
            return uuid;
        }
     
        public void setUuid(String uuid) {
            this.uuid = uuid;
        }
     
        
        @Formula("(select t.name from test as t where t.id=id)")
        public String getTest() {
            return test;
        }
     
        public void setTest(String test) {
            this.test = test;
        }
    }

    我们可以看到,在getTest上,增加了@Formula注解。查询表时,就会关联Test表的name到属性上。

    注意:

    首先 表增加别名。

    其次 注意sql两段的小括号()

    最后 t.id=id 的最后那个id,对应属性上的@Colun(value = "id")

    3 通过jpa创建一个dao

    @Repository
    public interface UserDao extends JpaRepository<User,Integer> {
    }

    4 测试

    @Autowired
        private UserDao userDao;
     
        @Test
        public void contextLoads() {
            System.out.println(userDao.findById(2).get().getTest());
        }
  • 相关阅读:
    how to pass a Javabean to server In Model2 architecture.
    What is the Web Appliation Archive, abbreviation is "WAR"
    Understaning Javascript OO
    Genetic Fraud
    poj 3211 Washing Clothes
    poj 2385 Apple Catching
    Magic Star
    关于memset的用法几点
    c++ 函数
    zoj 2972 Hurdles of 110m
  • 原文地址:https://www.cnblogs.com/hanjun0612/p/11128095.html
Copyright © 2011-2022 走看看