标签:val 背景 query creat cat 表数据 false sel 继承
这里的动态分表查询并不是动态构造sql语句,而是利用SpEL操作同一结构的不同张表。
DROP TABLE IF EXISTS "public"."data_2017"; CREATE TABLE "public"."data_2017" ( "id" int4 NOT NULL, "name" varchar(255) COLLATE "default" ) WITH (OIDS=FALSE)
实际工作中们需要根据请求去选择查询17年的表还是18年的表。执行的sql语句除了表名不一致,其他均一致。
SELECT id,name FROM table
/** * Created by dingshuo on 2017/6/5. */ @MappedSuperclass public class AbstractMappedType { private int id; private String name; @Id @Column(name = "id") @JsonIgnore public int getId() { return id; } public void setId(int id) { this.id = id; } @Column(name = "name") public String getName() { return name; } public void setName(String name) { this.name = name; } }
/** * Created by dingshuo on 2017/6/5. */ @Entity @Table(name = "DATA_2017", schema = "public", catalog = "powermanager") public class Data2017 extends AbstractMappedType { } /** * Created by dingshuo on 2017/6/5. */ @Entity @Table(name = "DATA_2018", schema = "public", catalog = "powermanager") public class Data2018 extends AbstractMappedType { }
/** * Created by dingshuo on 2017/6/5. */ @NoRepositoryBean public interface MappedTypeRepository<T extends AbstractMappedType> extends Repository<T, Long> { @Query("select t from #{#entityName} t where t.id = ?1") List<T> findById(int id); @Query("select t from #{#entityName} t ") List<T> findALL(); }
/** * Created by dingshuo on 2017/6/5. */ public interface Data2017Repository extends MappedTypeRepository<Data2017>{ }
@RestController public class TestController { @Autowired Data2017Repository repository; @GetMapping(value = "/test") public String test(){ List<Data2017> object=repository.findById(1); List<Data2017> objec1t=repository.findALL(); return "OK"; } }
标签:val 背景 query creat cat 表数据 false sel 继承
原文地址:http://www.cnblogs.com/tilv37/p/6944182.html