spring-data-jpa
来源:互联网 发布:美蓝漫画无法连接网络? 编辑:程序博客网 时间:2024/06/06 23:08
1.config pom.xml
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.companyname.bank</groupId> <artifactId>consumerBanking</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>consumerBanking</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.2.3.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <properties> <!-- use UTF-8 for everything --> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> </properties> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-releases</id> <name>Spring Releases</name> <url>https://repo.spring.io/libs-release</url> </repository> <repository> <id>org.jboss.repository.releases</id> <name>JBoss Maven Release Repository</name> <url>https://repository.jboss.org/nexus/content/repositories/releases</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-releases</id> <name>Spring Releases</name> <url>https://repo.spring.io/libs-release</url> </pluginRepository> </pluginRepositories></project>
2.About Spring Boot Maven plugin
It collects all the jars on the classpath and builds a single, runnable "über-jar", which makes it more convenient to execute and transport your service.
It searches for the
public static void main()
method to flag as a runnable class.It provides a built-in dependency resolver that sets the version number to matchSpring Boot dependencies. You can override any version you wish, but it will default to Boot’s chosen set of versions.
3.Define entity
package hello;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;@Entitypublic class Customer { @Id @GeneratedValue(strategy=GenerationType.AUTO) private long id; private String firstName; private String lastName; protected Customer() {} public Customer(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } @Override public String toString() { return String.format( "Customer[id=%d, firstName='%s', lastName='%s']", id, firstName, lastName); }}4.extends the
CrudRepository
interface. Spring Data JPA creates an implementation on the fly when you run the application.package hello;import java.util.List;import org.springframework.data.repository.CrudRepository;public interface CustomerRepository extends CrudRepository<Customer, Long> { List<Customer> findByLastName(String lastName);}5.to test
package hello;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.CommandLineRunner;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class Application implements CommandLineRunner { @Autowired CustomerRepository repository; public static void main(String[] args) { SpringApplication.run(Application.class); } @Override public void run(String... strings) throws Exception { // save a couple of customers repository.save(new Customer("Jack", "Bauer")); repository.save(new Customer("Chloe", "O'Brian")); repository.save(new Customer("Kim", "Bauer")); repository.save(new Customer("David", "Palmer")); repository.save(new Customer("Michelle", "Dessler")); // fetch all customers System.out.println("Customers found with findAll():"); System.out.println("-------------------------------"); for (Customer customer : repository.findAll()) { System.out.println(customer); } System.out.println(); // fetch an individual customer by ID Customer customer = repository.findOne(1L); System.out.println("Customer found with findOne(1L):"); System.out.println("--------------------------------"); System.out.println(customer); System.out.println(); // fetch customers by last name System.out.println("Customer found with findByLastName('Bauer'):"); System.out.println("--------------------------------------------"); for (Customer bauer : repository.findByLastName("Bauer")) { System.out.println(bauer); } }}
6.
you can run the application using mvn spring-boot:run. Or you can build the JAR file with mvn clean package and run the JAR by typing
0 0
- Jpa到Spring data jpa
- Spring Data JPA
- Spring-Data-Jpa
- Spring Data JPA
- Spring data jpa
- Spring Data JPA 使用
- Spring Data JPA
- Spring Data JPA
- spring data JPA 配置
- Spring Data JPA
- Spring Jpa Data
- spring-data-jpa 使用
- spring data jpa初试
- Spring Data jpa Repository
- spring data JPA 配置
- spring Data Jpa入门
- Spring Data JPA入门
- Spring Data JPA
- Python单例模式的4种实现方法(转)
- jdbc编程新闻系统总结
- JVM垃圾回收与性能调优
- Android学习之 第 6 章 数据存储全方案,详解 持久化技术
- servlet开发9-JSP sample1
- spring-data-jpa
- 【STL源码剖析读书笔记】【第5章】关联式容器之hash_set、hash_map、hash_multiset和hash_multimap
- 第十一章:MongoDB一些理论知识整理
- Android短彩信数据库解析
- hihocoder #1052 : 基因工程
- 第11周项目 程序阅读(2)
- 你不知道的JSON的高效率用法
- 网页常用分享代码大全
- 原生javascript实现异步的7种方式