SpringBoot如何整合SpringDataJPA

所属分类: 软件编程 / java 阅读数: 59
收藏 0 赞 0 分享

这篇文章主要介绍了SpringBoot整合SpringDataJPA代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

一、pom.xml添加依赖

<dependencies>
  <!--web-->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <!--spring data jpa-->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
  </dependency>
  <!--mysql-->
  <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
  </dependency>
  <!-- fastjson -->
  <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.62</version>
  </dependency>
  <!--test-->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
  </dependency>
</dependencies>

二、配置数据源以及jpa

server:
 port: 8080

#数据源
spring:
 datasource:
  url: jdbc:mysql://192.168.178.5:12345/cloudDB01?useUnicode=true&characterEncoding=UTF-8
  username: root
  password: 123456
  driver-class-name: com.mysql.jdbc.Driver
 jpa:
  database: MySQL
  show-sql: true
  hibernate:
   naming:
    physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

三、创建实体

@Entity
@Table(name = "dept")
public class DeptDTO {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "deptno")
  private Integer deptNo;
  @Column(name = "dname")
  private String dName;
  @Column(name = "db_source")
  private String dbSource;

  public Integer getDeptNo() {
    return deptNo;
  }

  public void setDeptNo(Integer deptNo) {
    this.deptNo = deptNo;
  }

  public String getdName() {
    return dName;
  }

  public void setdName(String dName) {
    this.dName = dName;
  }

  public String getDbSource() {
    return dbSource;
  }

  public void setDbSource(String dbSource) {
    this.dbSource = dbSource;
  }
}

四、创建jpa

public interface DeptRepository extends JpaRepository<DeptDTO, Integer>, JpaSpecificationExecutor<DeptDTO>, Serializable {
}

我们DeptRepository 继承了JpaRepository接口(SpringDataJPA提供的简单数据操作接口)、JpaSpecificationExecutor(SpringDataJPA提供的复杂查询接口)、Serializable(序列化接口)。我们并不需要做其他的任何操作了,因为SpringBoot以及SpringDataJPA会为我们全部搞定,SpringDataJPA内部使用了类代理的方式让继承了它接口的子接口都以spring管理的Bean的形式存在,也就是说我们可以直接使用@Autowired注解在spring管理bean使用

五、创建控制器controller

@RestController
@RequestMapping("/dept")
public class DeptController {

  @Autowired
  private DeptRepository deptRepository;
  
  @RequestMapping(value = "/findAll", method = {RequestMethod.POST})
  public List<DeptDTO> findAllDept(){
    return deptRepository.findAll(); //findAll是jpa提供的查询接口
  }

  @RequestMapping(value="/addDept", method={RequestMethod.POST})
  public DeptDTO saveDept(@RequestBody DeptDTO deptDTO){
    deptRepository.save(deptDTO);
    return deptDTO;
  }

}

六、测试controller

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = {JpaApplication.class}) //是该项目的启动类
@WebAppConfiguration
@ContextConfiguration
public class DeptControllerTest {

  @Autowired
  private WebApplicationContext context;

  private MockMvc mvc;

  @Before
  public void setUp() throws Exception {
    mvc = MockMvcBuilders
        .webAppContextSetup(context)
        .build();
  }

  @Test
  public void testQuery() throws Exception {
    MvcResult result=mvc.perform(MockMvcRequestBuilders.post("/dept/findAll")).andReturn();
    MockHttpServletResponse response = result.getResponse();
    String content = response.getContentAsString();
    List<DeptDTO> deptDTOS = JSON.parseArray(content, DeptDTO.class);
    for(DeptDTO deptDTO : deptDTOS){
      System.out.println(deptDTO.getdName());
    }
  }

  @Test
  public void testAdd() throws Exception {
    DeptDTO deptDto = new DeptDTO();
    deptDto.setdName("海盗船");
    deptDto.setDbSource("cloudDB1");
    System.out.println(JSON.toJSONString(deptDto));
    MvcResult result=mvc.perform(MockMvcRequestBuilders.post("/dept/addDept")
        .contentType(MediaType.APPLICATION_JSON).content(JSON.toJSONString(deptDto)))
        .andReturn();
    MockHttpServletResponse response = result.getResponse();
    String content = response.getContentAsString();
    DeptDTO deptDTO = JSON.parseObject(content, DeptDTO.class);
    System.out.println(deptDTO.getDeptNo());
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

JavaWeb项目部署到服务器详细步骤详解

这篇文章主要介绍了JavaWeb项目如何部署到服务器,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA基于支付宝小程序搭建springboot项目的详细步骤

这篇文章主要介绍了IDEA基于支付宝小程序搭建springboot项目的详细步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解SpringBoot应用服务启动与安全终止

这篇文章主要介绍了SpringBoot应用服务启动与安全终止,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Boot启动及退出加载项的方法

这篇文章主要介绍了Spring Boot启动及退出加载项的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Data Jpa 自动生成表结构的方法示例

这篇文章主要介绍了Spring Data Jpa 自动生成表结构的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA中osgi的开发应用指南详解

这篇文章主要介绍了IDEA中osgi的开发应用指南详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解用maven将dubbo工程打成jar包运行

这篇文章主要介绍了详解用maven将dubbo工程打成jar包运行,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解Java合并数组的两种实现方式

这篇文章主要介绍了Java合并数组的两种实现方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

使用Jenkins Pipeline自动化构建发布Java项目的方法

这篇文章主要介绍了使用Jenkins Pipeline自动化构建发布Java项目的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

使用Maven配置Spring的方法步骤

这篇文章主要介绍了使用Maven配置Spring的方法步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多