上一篇我們講了spring boot 整合JdbcTemplate來進行數據的持久化,
這篇我們來說下怎么通過spring boot 整合JPA來實現數據的持久化。
一、代碼實現
- 修改pom,引入依賴
<!-- 引入jpa 依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>
- 修改application.properties,配置相關信息
#修改tomcat默認端口號 server.port=8090 #修改context path server.context-path=/test #配置數據源信息 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=root #配置jpa spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true spring.jackson.serialization.indent_output=true
- 創建實體類
package com.study.entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="t_user") public class User { @Id @GeneratedValue(strategy=GenerationType.AUTO) private Integer id; private String userName; private String password; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
- 創建repository接口並繼承CrudRepository
package com.study.repository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.query.Param; import com.study.entity.User; /** * 注意: * 1.這里這里是interface,不是class * * 2.CrudRepository里面的泛型,第一個是實體類,第二個是主鍵的類型 * * 3.由於crudRepository 里面已經有一些接口了,如deleteAll,findOne等, 我們直接調用即可 * * 4.當然,我們也可以根據自己的情況來實現自己的接口,如下面的getUser()方法,jpql語句和hql語句差不多 * * */ public interface UserRepository extends CrudRepository<User, Integer> { /** * 我們這里只需要寫接口,不需要寫實現,spring boot會幫忙自動實現 * * */ @Query("from User where id =:id ") public User getUser(@Param("id") Integer id); }
- 創建service
- 接口
package com.study.service; import com.study.entity.User; public interface UserService { public User getUser(Integer id); }
- 實現
package com.study.service.impl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.study.entity.User; import com.study.repository.UserRepository; import com.study.service.UserService; @Service public class UserServiceImpl implements UserService { @Autowired UserRepository repository; @Override public User getUser(Integer id) { //有兩種方式: //1.調用crudRepository的接口 // return repository.findOne(id); //2.調用我們自己寫的接口 return repository.getUser(id); } }
- 接口
- 創建controller
package com.study.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.study.entity.User; import com.study.service.UserService; @RestController public class UserController { @Autowired UserService service; @RequestMapping("/getUser/{id}") public User getUser(@PathVariable("id") Integer id){ return service.getUser(id); } }
- 測試,頁面以json格式顯示數據庫值
二、知識點引申
關於Repository知識點,可以去看下下面這篇文章
https://segmentfault.com/a/1190000012346333