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