SpringDataJPA笔记(10)-动态设置表名
在实际使用中可能会遇到需要动态设置表名的情况,特别是通常在后台管理系统里面,总有一些相似的功能需要抽象出来写一些公共的方法,以减少代码开发量,降低重复劳动
首先看BaseRepository的代码
@NoRepositoryBean
public interface BaseRepository<T, ID extends Serializable> extends JpaRepository<T, ID>, JpaSpecificationExecutor<T>, Serializable {
@Transactional
@Modifying(clearAutomatically = true)
@Query("update #{#entityName} t set t.age=?2 where t.id = ?1")
int updateAge(ID id, int age);
@Query("select t.id from #{#entityName} t ")
List<ID> findIds();
}
然后创建一个BaseController
@Slf4j
public class BaseController<R extends BaseRepository<T, ID>, T extends AnimalEntity, ID extends Serializable> {
@Autowired
private R repository;
@ApiOperation(value = "baseAll", httpMethod = "GET")
@GetMapping(value