spring cloud 搭建(多JPA,JPA分页)
2023-09-11 14:21:22 时间
随着项目的迭代。
我们在老项目中,把service和dao都放在相同module下,已经不合适了。
service和dao越来越臃肿。如下所示
我们就开始认为每个微服务,都拥有自己的model,dao,service。
而上图的module只是充当基类存在的使用。
这个时候JPA就会变成多个。
那么我们需要如何配置呢?
正文:
还是以 https://blog.csdn.net/hanjun0612/article/details/105239557 中service1服务举例。
我们在里面创建 model,dao.service
springboot版本
<groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.14.RELEASE</version>
一,启动类
通过这个指定JPA扫描特定的包!
@EnableJpaRepositories(basePackages = "com.test.service1.dao")
@SpringBootApplication @EnableEurekaClient @EnableJpaRepositories(basePackages = "com.test.service1.dao") public class Service1Application { public static void main(String[] args) { SpringApplication.run(Service1Application.class, args); } }
二,数据库(只需要id,name)
三,Model
@Entity的包:[Maven: org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.0.Final]
@Data @Entity @Table(name="test",catalog = "testdb") public class Test { private Integer id; private String name; @Id public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
四,Dao
JpaRepository的包:[Maven: org.springframework.data:spring-data-jpa:1.11.13.RELEASE]
方法一:(有警告,但不影响使用)
@Repository public interface TestDao extends JpaRepository<Test,Integer> { @Query(value="select * from test where name=:name ORDER BY ?#{#pageable}",nativeQuery=true) Page<Test> findList(@Param("name") String name, Pageable pageable); }
这里有个警告,但是不影响使用:Parameter with that position [1] did not exist
然后我修改成方法二,就可以了
方法二
/*#pageable*/ 作为注释(好奇怪的写法,警告居然就没了。)
@Repository public interface TestDao extends JpaRepository<Test,Integer> { @Query(value="select * from test where name=:name /*#pageable*/",nativeQuery=true) Page<Test> findList(@Param("name") String name, Pageable pageable); }
五,Service
service
public interface testService { Page<Test> findList(String name); }
Impl
@Service("testservice") public class testImpl implements testService { @Autowired TestDao testDao; @Override public Page<Test> findList(String name) { //参数1:当前页,【下标0开始】 //参数2:pageSize //参数3:排序方式 //参数4:排序根据 Pageable pageable = new PageRequest(1,3, Sort.Direction.DESC,"id"); Page<Test> page=testDao.findList(name,pageable); return page; } }
六,Controller
@RestController @RequestMapping("test") public class TestController { @Value("${isDebug}") private String isDebug; @Autowired testService testService; @GetMapping(value = "/hello") public String hello(){ return isDebug; } //JPA测试 @PostMapping(value="/test") public Page<Test> test() { Page<Test> page=testService.findList("t3"); return page; } }
七,测试
相关文章
- 关于spring中使用mysql数据库明明配置了事务管理却失效的原因及解决方法
- Java和Spring注解基本介绍
- spring-cloud-sleuth+zipkin追踪服务实现(一)
- Spring Cloud Zuul API服务网关之请求路由
- Spring Cloud Alibaba 简介(上)
- Spring Cloud Alibaba 2021.0.1.0 发布:版本号再也不迷糊了
- Spring Cloud Gateway现高风险漏洞,建议采取措施加强防护
- Spring Cloud Stream消费失败后的处理策略(四):重新入队(RabbitMQ)
- 基于 Java SSM(Spring+SpingMVC+Mybatis)+MySQL 实现(Web)软件测试用例在线评判系统【100010402】
- Spring Cloud 使用Feign调用微服务出现:QueryMap key must be a String:Long异常
- Spring MVC 原理小结
- 这个Spring Security登录插件牛啊,验证码、小程序、OAuth2都能快速接入
- 服务注册发现、配置中心集一体的 Spring Cloud Consul
- 有关Spring中Resource的继承关系(代码解读)
- spring框架漏洞整理(Spring Cloud Config路径穿越导致的信息泄露)
- spring框架漏洞整理(Springboot漏洞)
- Spring Cloud服务注册-Eureka介绍和部署
- 浅析Spring的@Bean注解:注解分类、简介,作用、好处及代码使用示例、Bean的xml方式和注解方式使用对比、@Bean的源码分析
- Spring+Ibatis集成开发实例
- Spring Cloud Config实现集群配置中心
- spring cloud 搭建(事务)
- spring cloud 搭建(注册中心)
- spring cloud 搭建
- 从0开始构建你的api网关--Spring Cloud Gateway网关实战及原理解析
- spring mvc DispatcherServlet详解之拾忆工具类utils
- Spring Security(1)--- 权限控制基本功能实现