SpringBoot如何整合SpringDataJPA
这篇文章主要介绍了SpringBoot整合SpringDataJPA代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
一、pom.xml添加依赖
org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-data-jpa mysql mysql-connector-java runtime com.alibaba fastjson 1.2.62 org.springframework.boot spring-boot-starter-test
二、配置数据源以及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")
publicclassDeptDTO{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="deptno")
privateIntegerdeptNo;
@Column(name="dname")
privateStringdName;
@Column(name="db_source")
privateStringdbSource;
publicIntegergetDeptNo(){
returndeptNo;
}
publicvoidsetDeptNo(IntegerdeptNo){
this.deptNo=deptNo;
}
publicStringgetdName(){
returndName;
}
publicvoidsetdName(StringdName){
this.dName=dName;
}
publicStringgetDbSource(){
returndbSource;
}
publicvoidsetDbSource(StringdbSource){
this.dbSource=dbSource;
}
}
四、创建jpa
publicinterfaceDeptRepositoryextendsJpaRepository,JpaSpecificationExecutor ,Serializable{ }
我们DeptRepository继承了JpaRepository接口(SpringDataJPA提供的简单数据操作接口)、JpaSpecificationExecutor(SpringDataJPA提供的复杂查询接口)、Serializable(序列化接口)。我们并不需要做其他的任何操作了,因为SpringBoot以及SpringDataJPA会为我们全部搞定,SpringDataJPA内部使用了类代理的方式让继承了它接口的子接口都以spring管理的Bean的形式存在,也就是说我们可以直接使用@Autowired注解在spring管理bean使用
五、创建控制器controller
@RestController
@RequestMapping("/dept")
publicclassDeptController{
@Autowired
privateDeptRepositorydeptRepository;
@RequestMapping(value="/findAll",method={RequestMethod.POST})
publicListfindAllDept(){
returndeptRepository.findAll();//findAll是jpa提供的查询接口
}
@RequestMapping(value="/addDept",method={RequestMethod.POST})
publicDeptDTOsaveDept(@RequestBodyDeptDTOdeptDTO){
deptRepository.save(deptDTO);
returndeptDTO;
}
}
六、测试controller
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes={JpaApplication.class})//是该项目的启动类
@WebAppConfiguration
@ContextConfiguration
publicclassDeptControllerTest{
@Autowired
privateWebApplicationContextcontext;
privateMockMvcmvc;
@Before
publicvoidsetUp()throwsException{
mvc=MockMvcBuilders
.webAppContextSetup(context)
.build();
}
@Test
publicvoidtestQuery()throwsException{
MvcResultresult=mvc.perform(MockMvcRequestBuilders.post("/dept/findAll")).andReturn();
MockHttpServletResponseresponse=result.getResponse();
Stringcontent=response.getContentAsString();
ListdeptDTOS=JSON.parseArray(content,DeptDTO.class);
for(DeptDTOdeptDTO:deptDTOS){
System.out.println(deptDTO.getdName());
}
}
@Test
publicvoidtestAdd()throwsException{
DeptDTOdeptDto=newDeptDTO();
deptDto.setdName("海盗船");
deptDto.setDbSource("cloudDB1");
System.out.println(JSON.toJSONString(deptDto));
MvcResultresult=mvc.perform(MockMvcRequestBuilders.post("/dept/addDept")
.contentType(MediaType.APPLICATION_JSON).content(JSON.toJSONString(deptDto)))
.andReturn();
MockHttpServletResponseresponse=result.getResponse();
Stringcontent=response.getContentAsString();
DeptDTOdeptDTO=JSON.parseObject(content,DeptDTO.class);
System.out.println(deptDTO.getDeptNo());
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。