Spring Data Jpa Maven 创建

1:概述

实际现在都使用spring-boot 了 ,这里使用老构建方式只是为了复习spring 基础框架构建的例子

 

2:pom

 


    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4.0.0
    com.dgw
    dgw
    0.0.1-SNAPSHOT

    
        
            org.slf4j
            slf4j-api
            1.7.26
        
        
            commons-logging
            commons-logging
            1.2
        
        
            org.springframework
            spring-core
            5.2.2.RELEASE
        
        
            org.springframework
            spring-tx
            5.2.2.RELEASE
        
        
            org.springframework
            spring-core
            5.2.2.RELEASE
        

        
            org.springframework
            spring-beans
            5.2.2.RELEASE
        
        
            org.springframework
            spring
            5.2.2.RELEASE
            pom
        
        
        
            org.springframework.data
            spring-data-commons
            2.2.2.RELEASE
        
        
            org.springframework.data
            spring-data-jpa
            2.2.2.RELEASE
        
        
        
            com.mchange
            c3p0
            0.9.5.2
        
        
            org.hibernate
            hibernate-core
            5.2.10.Final
        
        
            mysql
            mysql-connector-java
            8.0.18
        
        
            org.hibernate
            hibernate-entitymanager
            5.2.10.Final
        
        
            org.springframework
            spring-test
            5.2.2.RELEASE
            test
        
        
            junit
            junit
            4.12
            test
        
    

 

 3:实体

@Table(name="JPA_PERSONS")
@Entity
public class Person {

    private Integer id;
    private String lastName;

    private String email;
    private Date birth;
    
    @GeneratedValue
    @Id
    public Integer getId() {
        return id;
    }

 

主要 Repository实现

public interface PersonRepository extends Repository {
    List findById(Integer id);
}

 

 

4: applicationContext.xml配置

xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    
    <context:component-scan base-package="domain">context:component-scan> 

    
    <context:property-placeholder location="classpath:db.properties"/>

    <bean id="dataSource"
        class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${jdbc.user}">property>
        <property name="password" value="${jdbc.password}">property>    
        <property name="driverClass" value="${jdbc.driverClass}">property>
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}">property>
        
        
    bean>

    
    <bean id="entityManagerFactory" 
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource">property>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">bean>
        property>
        <property name="packagesToScan" value="domain">property>
        <property name="jpaProperties">
            <props>
                
                
                
                <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategyprop>
                
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialectprop>
                <prop key="hibernate.show_sql">trueprop>
                <prop key="hibernate.format_sql">trueprop>
                <prop key="hibernate.hbm2ddl.auto">updateprop>
            props>
        property>
    bean>

    
    <bean id="transactionManager"
        class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory">property>    
    bean>

    
    <tx:annotation-driven transaction-manager="transactionManager"/>

    
    
    
    <jpa:repositories base-package="domain"
        entity-manager-factory-ref="entityManagerFactory">jpa:repositories>
beans>

 

xml 创建形式:

jdbc.user=root
jdbc.password=root
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost/jpatest?userSSL=true&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT 

5:方法测试

public class SpringTest {
    
    private ClassPathXmlApplicationContext context=null;
    
    {
        context=new ClassPathXmlApplicationContext("applicationContext.xml");
    }
    @Test
    public void testjap() {
        PersonRepository repository = context.getBean(PersonRepository.class);
        List id = repository.findById(1);
        for (Person person : id) {
            System.out.println(person);
        }
    }
    @Test
    public void testdataSources() throws SQLException {
        DataSource dataSource = context.getBean(DataSource.class);
        System.out.println(dataSource.getConnection());
    }
}

你可能感兴趣的:(Spring Data Jpa Maven 创建)