快速整合SpringDataJPA 基礎(chǔ)

序言:
SpringDataJPA秉承大道至簡(jiǎn)的設(shè)計(jì)理念,給我們的數(shù)據(jù)層開發(fā)帶來(lái)的極大的便利。諸如基于注解就可完成實(shí)體-數(shù)據(jù)庫(kù)的映射關(guān)系,提供自帶的通用Repo接口、接口方法約定名稱即可實(shí)現(xiàn)數(shù)據(jù)訪問等特性都是值得稱贊的功能。

正文:Spring Data JPA 實(shí)戰(zhàn)步驟

1.引入依賴

在pom.xml 中添加ORM框架(JPA)和數(shù)據(jù)庫(kù)驅(qū)動(dòng)(MySQL-Conn)的依賴。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <!--繼承信息 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.M4</version>
        <relativePath/>
    </parent>
    
    <!--依賴管理 -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

2. 添加數(shù)據(jù)源

在application.yml 添加數(shù)據(jù)源信息,具體如下:

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/socks?useSSL=false
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver

3. 編寫數(shù)據(jù)層代碼

具體代碼:
在倉(cāng)庫(kù)中添加用戶接口,然后繼承通用倉(cāng)庫(kù)接口JpaRepository,即可獲得通用的增刪改查功能。例如findAll 、getOne 等常用方法。

public interface UserRepository extends JpaRepository<User, String> {

    List<User> findByUsername(String username);
}

4. 添加數(shù)據(jù)庫(kù)記錄

在Navicat 連接本地?cái)?shù)據(jù)庫(kù),隨便打開查詢窗口,然后執(zhí)行腳本內(nèi)容,如下:

DROP DATABASE IF EXISTS `socks`;
CREATE DATABASE `socks`;
USE `SOCKS`;
DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user` (
  `USER_ID` varchar(50) ,
  `USERNAME` varchar(50) ,
  `PASSWORD` varchar(50) 
) ;

INSERT INTO `t_user` VALUES ('1', 'admin', 'admin');
INSERT INTO `t_user` VALUES ('2', 'yizhiwazi', '123456');

5. 啟動(dòng)項(xiàng)目

在啟動(dòng)項(xiàng)目前,記得先添加好數(shù)據(jù)庫(kù)socks和用戶表t_user ,并插入幾條測(cè)試數(shù)據(jù)。準(zhǔn)備好數(shù)據(jù)后,啟動(dòng)項(xiàng)目,測(cè)試相關(guān)接口。

@SpringBootApplication
@EnableJpaRepositories("com.hehe.repository") //指定倉(cāng)庫(kù)的掃描路徑
public class JpaApplication {

    public static void main(String[] args) {
        SpringApplication.run(JpaApplication.class, args);
    }
}

6. 單元測(cè)試

import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class)
@SpringBootTest
public class JpaApplicationTest {
    @Autowired
    UserRepository userRepository;
    @Test
    public void testuser() {
        //添加測(cè)試數(shù)據(jù)
        userRepository.save(new User("1111","test-data-jpa","first-pass"));
        userRepository.save(new User("9527","test-data-jpa","second-pass"));
        //開始進(jìn)行測(cè)試
        assertThat(userRepository.existsById("9527")).isEqualTo(true);
        assertThat(userRepository.findByUsername("test-data-jpa").size()).isEqualTo(2);
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 序言:SpringDataJPA秉承大道至簡(jiǎn)的設(shè)計(jì)理念,給我們的數(shù)據(jù)層開發(fā)帶來(lái)的極大的便利。諸如基于注解就可完成實(shí)...
    一只襪子閱讀 7,146評(píng)論 5 5
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,284評(píng)論 6 342
  • 關(guān)于Mongodb的全面總結(jié) MongoDB的內(nèi)部構(gòu)造《MongoDB The Definitive Guide》...
    中v中閱讀 32,315評(píng)論 2 89
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,680評(píng)論 19 139
  • 昨天手機(jī)不能充電了,去維修店修,人家說主板壞了,回去想了想,還是換個(gè)新的吧。想入手華為p9,可是太貴,想買榮耀7,...
    RogueQ閱讀 174評(píng)論 0 0

友情鏈接更多精彩內(nèi)容