當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringBoot2.0 基础案例(09):集成JPA持久层框架,简化数据库操作
生活随笔
收集整理的這篇文章主要介紹了
SpringBoot2.0 基础案例(09):集成JPA持久层框架,简化数据库操作
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、JAP框架簡介
JPA(Java Persistence API)意即Java持久化API,是Sun官方在JDK5.0后提出的Java持久化規范。主要是為了簡化持久層開發以及整合ORM技術,結束Hibernate、TopLink、JDO等ORM框架各自為營的局面。JPA是在吸收現有ORM框架的基礎上發展而來,易于使用,伸縮性強。
二、與SpringBoot2.0整合
1、核心依賴
<!-- JPA框架 --> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>2、配置文件
spring:application:name: node09-boot-jpadatasource:url: jdbc:mysql://localhost:3306/data_jpa?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=trueusername: rootpassword: rootdriver-class-name: com.mysql.jdbc.Driverjpa:hibernate:ddl-auto: updateshow-sql: trueddl-auto幾種配置說明
1)create
每次加載hibernate時都刪除上一次的生成的表,然后根據bean類重新來生成新表,容易導致數據丟失,(建議首次創建時使用)。
2)create-drop
每次加載hibernate時根據bean類生成表,但是sessionFactory一關閉,表就自動刪除。
3)update
第一次加載hibernate時根據bean類會自動建立起表的結構,以后加載hibernate時根據bean類自動更新表結構,即使表結構改變了但表中的行仍然存在不會刪除以前的行。
4)validate
每次加載hibernate時,驗證創建數據庫表結構,只會和數據庫中的表進行比較,不會創建新表,但是會插入新值。
3、實體類對象
就是根據這個對象生成的表結構。
@Table(name = "t_user") @Entity public class User {@Id@GeneratedValueprivate Integer id;@Columnprivate String name;@Columnprivate Integer age;// 省略 GET SET }4、JPA框架的用法
定義對象的操作的接口,繼承JpaRepository核心接口。
import com.boot.jpa.entity.User; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; @Repository public interface UserRepository extends JpaRepository<User,Integer> {// 但條件查詢User findByAge(Integer age);// 多條件查詢User findByNameAndAge(String name, Integer age);// 自定義查詢@Query("from User u where u.name=:name")User findSql(@Param("name") String name); }5、封裝一個服務層邏輯
import com.boot.jpa.entity.User; import com.boot.jpa.repository.UserRepository; import org.springframework.stereotype.Service; import javax.annotation.Resource; @Service public class UserService {@Resourceprivate UserRepository userRepository ;// 保存public void addUser (User user){userRepository.save(user) ;}// 根據年齡查詢public User findByAge (Integer age){return userRepository.findByAge(age) ;}// 多條件查詢public User findByNameAndAge (String name, Integer age){return userRepository.findByNameAndAge(name,age) ;}// 自定義SQL查詢public User findSql (String name){return userRepository.findSql(name) ;}// 根據ID修改public void update (User user){userRepository.save(user) ;}//根據id刪除一條數據public void deleteStudentById(Integer id){userRepository.deleteById(id);} }三、測試代碼塊
import com.boot.jpa.JpaApplication; import com.boot.jpa.entity.User; import com.boot.jpa.service.UserService; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import javax.annotation.Resource; @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = JpaApplication.class) public class UserJpaTest {@Resourceprivate UserService userService ;@Testpublic void addUser (){User user = new User() ;user.setName("知了一笑");user.setAge(22);userService.addUser(user);User user1 = new User() ;user1.setName("cicada");user1.setAge(23);userService.addUser(user1);}@Testpublic void findByAge (){Integer age = 22 ;// User{id=3, name='知了一笑', age=22}System.out.println(userService.findByAge(age));}@Testpublic void findByNameAndAge (){System.out.println(userService.findByNameAndAge("cicada",23));}@Testpublic void findSql (){// User{id=4, name='cicada', age=23}System.out.println(userService.findSql("cicada"));}@Testpublic void update (){User user = new User() ;// 如果這個主鍵不存在,會以主鍵自增的方式新增入庫user.setId(3);user.setName("哈哈一笑");user.setAge(25);userService.update(user) ;}@Testpublic void deleteStudentById (){userService.deleteStudentById(5) ;} }四、源代碼地址
GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 碼云地址:知了一笑 https://gitee.com/cicadasmile/spring-boot-base
總結
以上是生活随笔為你收集整理的SpringBoot2.0 基础案例(09):集成JPA持久层框架,简化数据库操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 让Visual Studio 2005
- 下一篇: Linux常用命令(第二版) --网络通