zoukankan      html  css  js  c++  java
  • spring cloud 搭建(多JPA,JPA分页)

    随着项目的迭代。

    我们在老项目中,把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;
    }
    }

    七,测试



  • 相关阅读:
    Mysql多表关系
    Linux权限
    Linux安装python环境脚本
    ZJNU 2136
    ZJNU 2135
    ZJNU 2133
    ZJNU 1699
    ZJNU 1542
    ZJNU 1538
    ZJNU 1535
  • 原文地址:https://www.cnblogs.com/hanjun0612/p/12712762.html
Copyright © 2011-2022 走看看