序言:
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);
}
}