使用spring-data-jpa案例
This commit is contained in:
Executable
+61
@@ -0,0 +1,61 @@
|
|||||||
|
<?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.didispace</groupId>
|
||||||
|
<artifactId>demo</artifactId>
|
||||||
|
<version>1.0.0</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<name>demo</name>
|
||||||
|
<description>Spring Boot project</description>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>1.3.2.RELEASE</version>
|
||||||
|
<relativePath/> <!-- lookup parent from repository -->
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<java.version>1.8</java.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>mysql</groupId>
|
||||||
|
<artifactId>mysql-connector-java</artifactId>
|
||||||
|
<version>5.1.21</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package com.didispace;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class Application {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(Application.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
package com.didispace.domain;
|
||||||
|
|
||||||
|
import javax.persistence.Column;
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.GeneratedValue;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author 程序猿DD
|
||||||
|
* @version 1.0.0
|
||||||
|
* @date 16/3/21 下午3:35.
|
||||||
|
* @blog http://blog.didispace.com
|
||||||
|
*/
|
||||||
|
@Entity
|
||||||
|
public class User {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
@Column(nullable = false)
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
@Column(nullable = false)
|
||||||
|
private Integer age;
|
||||||
|
|
||||||
|
public User(){}
|
||||||
|
|
||||||
|
public User(String name, Integer age) {
|
||||||
|
this.name = name;
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getAge() {
|
||||||
|
return age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAge(Integer age) {
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package com.didispace.domain;
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
import org.springframework.data.repository.query.Param;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author 程序猿DD
|
||||||
|
* @version 1.0.0
|
||||||
|
* @date 16/3/23 下午2:34.
|
||||||
|
* @blog http://blog.didispace.com
|
||||||
|
*/
|
||||||
|
public interface UserRepository extends JpaRepository<User, Long> {
|
||||||
|
|
||||||
|
User findByName(String name);
|
||||||
|
|
||||||
|
User findByNameAndAge(String name, Integer age);
|
||||||
|
|
||||||
|
@Query("from User u where u.name=:name")
|
||||||
|
User findUser(@Param("name") String name);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
spring.datasource.url=jdbc:mysql://localhost:3306/test
|
||||||
|
spring.datasource.username=root
|
||||||
|
spring.datasource.password=root
|
||||||
|
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
|
||||||
|
|
||||||
|
spring.jpa.properties.hibernate.hbm2ddl.auto=create-drop
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
package com.didispace;
|
||||||
|
|
||||||
|
import com.didispace.domain.User;
|
||||||
|
import com.didispace.domain.UserRepository;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
|
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
@SpringApplicationConfiguration(Application.class)
|
||||||
|
public class ApplicationTests {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserRepository userRepository;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test() throws Exception {
|
||||||
|
|
||||||
|
// 创建10条记录
|
||||||
|
userRepository.save(new User("AAA", 10));
|
||||||
|
userRepository.save(new User("BBB", 20));
|
||||||
|
userRepository.save(new User("CCC", 30));
|
||||||
|
userRepository.save(new User("DDD", 40));
|
||||||
|
userRepository.save(new User("EEE", 50));
|
||||||
|
userRepository.save(new User("FFF", 60));
|
||||||
|
userRepository.save(new User("GGG", 70));
|
||||||
|
userRepository.save(new User("HHH", 80));
|
||||||
|
userRepository.save(new User("III", 90));
|
||||||
|
userRepository.save(new User("JJJ", 100));
|
||||||
|
|
||||||
|
// 测试findAll, 查询所有记录
|
||||||
|
Assert.assertEquals(10, userRepository.findAll().size());
|
||||||
|
|
||||||
|
// 测试findByName, 查询姓名为FFF的User
|
||||||
|
Assert.assertEquals(60, userRepository.findByName("FFF").getAge().longValue());
|
||||||
|
|
||||||
|
// 测试findUser, 查询姓名为FFF的User
|
||||||
|
Assert.assertEquals(60, userRepository.findUser("FFF").getAge().longValue());
|
||||||
|
|
||||||
|
// 测试findByNameAndAge, 查询姓名为FFF并且年龄为60的User
|
||||||
|
Assert.assertEquals("FFF", userRepository.findByNameAndAge("FFF", 60).getName());
|
||||||
|
|
||||||
|
// 测试删除姓名为AAA的User
|
||||||
|
userRepository.delete(userRepository.findByName("AAA"));
|
||||||
|
|
||||||
|
// 测试findAll, 查询所有记录, 验证上面的删除是否成功
|
||||||
|
Assert.assertEquals(9, userRepository.findAll().size());
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user