项目结构图
pom.xml文件如下
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zhenqi</groupId>
<artifactId>springboot_jpa</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot_jpa</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.7</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 持久层 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- h2内存数据库 -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
实体类User.java
package com.zhenqi.study.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
/**
* Created by wuming on 2017/7/18.
*/
@Entity
public class User {
@Id
@GeneratedValue
private Long id;
private String name;
private Integer age;
public User() {
}
public User(String name, Integer age) {
this.name = name;
this.age = age;
}
}
定义DAO接口,UserRepository.java
package com.zhenqi.study.repository;
import com.zhenqi.study.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
/**
* Created by wuming on 2017/7/19.
*/
public interface UserRepository extends JpaRepository<User, Long> {
List<User> findByNameLike(String name);
}
测试类
package com.zhenqi.study;
import com.zhenqi.study.entity.User;
import com.zhenqi.study.repository.UserRepository;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootJpaApplicationTests {
@Autowired
UserRepository userRepository;
@Test
public void proxy() throws Exception {
System.out.println(userRepository.getClass());
}
@Test
public void save() throws Exception {
for (int i = 0; i < 10; i++) {
User user = new User("zhenqi" + i, 25 + i);
userRepository.save(user);
}
}
@Test
public void all() throws Exception {
save();
assertThat(userRepository.findAll()).hasSize(10);
}
@Test
public void findByName() throws Exception {
save();
assertThat(userRepository.findByNameLike("zhen%")).hasSize(10);
}
@After
public void destroy() throws Exception {
userRepository.deleteAll();
}
}
最后附上配置文件 application.properties
spring.jpa.generate-ddl=true
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create
spring.jpa.properties.hibernate.format_sql=false