spring jpa study notes (a) of the integrated

A, pom configuration

<?xml version="1.0"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.module.demo</groupId>
        <artifactId>module-parent</artifactId>
        <version>${module.version}</version>
        <relativePath>../../module-parent/pom.xml</relativePath>
    </parent>
    <artifactId>module-jpa-dao</artifactId>
    <name>module-jpa-dao</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </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> // database drivers and incorporated JPA
        <dependency>
            <groupId>com.module.demo</groupId>
            <artifactId>module-dao-interface</artifactId>
        </dependency>
    </dependencies>
</project>

 

Two, Jpa Tool database table generating entity object

     Because after more database tables, and entity objects need to write in-depth study of the knowledge and understanding of the entity subject areas jpa, in order to correctly configured, thus giving the integration work to bring a great deal of difficulty, based on this, in order to reduce integration costs and use the eclipse of the JPA tool in the tool database tables directly mapped to the entity classes.

1, eclipse build Database Connection

(1) click on the eclipse of the window -> show view ---> other ---> Data Management -> Data Source Explorer Open Database Connectivity Management Views

        

 

 (2) establishing a database connection

      Used here is mysql.

     

          

   

 

 

 

         Data Source Explorer appears This view represents the database connection has been set up.

 

(3) add JPA Tool project

Select the project, right click and choose properties

                     

 

 

 

 (4) (3) is added to the project JPA Tool, this time generated by the entity classes JPA TOOL

      

 

 

After the above steps, the following com.module.demo.dap.jpa.entity generates corresponding entity class, such as:

package com.module.demo.dao.jpa.entity;

import java.io.Serializable;
import javax.persistence.*;


/**
 * The persistent class for the student database table.
 * 
 */
@Entity
@NamedQuery(name="Student.findAll", query="SELECT s FROM Student s")
public class Student implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;

    private String name;

    //bi-directional many-to-one association to Timetable
    @ManyToOne
    @JoinColumn(name="timaeable")
    private Timetable timetable;

    public Student() {
    }

    public int getId() {
        return this.id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Timetable getTimetable() {
        return this.timetable;
    }

    public void setTimetable(Timetable timetable) {
        this.timetable = timetable;
    }

}

 

 Three, JPA configuration

(1) Configuration dao layer (i.e., disposed warehouse database access layer)

package com.module.demo.dao.jpa.repository;

import org.springframework.data.repository.PagingAndSortingRepository;

import com.module.demo.dao.jpa.entity.Student;

public  interface StudentRepository the extends  PagingAndSortingRepository <Student, Integer> {    // must inherit
  
}

 

 (2) After the above configuration, the need to configure the scanning jpa entity classes and warehouses scan

      There are two configurations in a scanning manner, using JPA annotations; other generic annotation    A) JPA annotations

package com.module.demo.dao;

import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@Configuration
@PropertySource ( "the CLASSPATH: jpa.properties" ) // JPA personalized configuration
@ComponentScan (basePackageClasses = JpaConfiguration. Class )
 @EnableJpaRepositories      // specified scanning DAO layer, where no specific configuration of the package, the default will be scanned in the current packet belongs to the class and its sub-packets
 @EntityScan    // entity class specified scan , where no specific configuration of the package, the default will be scanned in the current packet belongs to the class and its sub-package
 public  class JpaConfiguration {
}

  b) Common Annotations

  • Add annotations spring.main.allow-bean-definition-overriding = true in application.yml file;
spring:
  application:
    name: web
  profiles:
    active: "@package.env@"
# datasource:
#    type: com.alibaba.druid.pool.DruidDataSource
  main: 
    the allow -bean-Definition-overriding:  to true      // define a coverage allows bean
    
server:
  port: 8090
  servlet:
    context-path: /web

 

  • Add class configuration @EnableAutoConfiguration
@Configuration
@PropertySource("classpath:jpa.properties")
@ComponentScan(basePackageClasses=JpaConfiguration.class)
/*@EnableJpaRepositories
@EntityScan*/
@EnableAutoConfiguration // loading automatically based on the corresponding configuration class classpath
public  class JpaConfiguration {
}

 

Guess you like

Origin www.cnblogs.com/sandyflower/p/11615007.html