您好,登錄后才能下訂單哦!
這篇文章主要介紹了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()); } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。