Spring Data Jpa Maven 创建

1:概要

今春ブートを使用して実用的なの例としては、ここでは春の見直しを構築するだけで基本的な枠組みを構築するために古い方法を使用します

 

2:POM

 

<プロジェクトの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 https://maven.apache.org/xsd/maven-4.0.0.xsd "> 
    <modelVersion> 4.0.0 </ modelVersion> 
    <groupIdを> com.dgw < /のgroupId> 
    <たartifactId> DGW </たartifactId> 
    <バージョン> 0.0.1-SNAPSHOT </バージョン> 

    <依存性> 
        <依存性> 
            <のgroupId> org.slf4j </のgroupId> 
            <たartifactId> SLF4J-API </たartifactId> 
            <バージョン> 1.7.26 </バージョン> 
        </依存関係>
        <依存> 
            <groupIdを>コモンズ・ログ</ groupIdを>
            <たartifactId>コモンズロギング</たartifactId>/のgroupId> 
            <たartifactId>ばねコア</たartifactId>
            <バージョン> 1.2 </バージョン> 
        </依存> 
        <依存性> 
            <のgroupId> org.springframework </のgroupId> 
            <たartifactId>ばねコア</たartifactId> 
            <バージョン> 5.2.2.RELEASE </バージョン> 
        </依存> 
        <依存性> 
            <のgroupId> org.springframework </のgroupId> 
            <たartifactId>スプリング-TX </たartifactId> 
            <バージョン> 5.2.2.RELEASE </バージョン> 
        </依存> 
        <依存性> 
            <のgroupId> org.springframework < / groupIdを> 
            <バージョン> 5.2.2.RELEASE </バージョン>
        </依存関係> 

        <依存> 
            <のgroupId> org.springframework </のgroupId> 
            <たartifactId>ばね豆</たartifactId> 
            <バージョン> 5.2.2.RELEASE </バージョン> 
        </依存関係> 
        <依存> 
            <groupIdを> org.springframework </ groupIdを> 
            <たartifactId>春</たartifactId> 
            <バージョン> 5.2.2.RELEASE </バージョン> 
            <タイプ> POM </タイプ> 
        </依存関係> 
        <! - - JPA - > 
        <依存> 
            <groupIdを> org.springframework.data </ groupIdを>
            <たartifactId>春・データ・コモンズ</たartifactId>
            <バージョン> 2.2.2.RELEASE </バージョン> 
        </依存関係> 
            <groupIdを> mysqlの</ groupIdを>
        <依存> 
            <groupIdを> org.springframework.data </ groupIdを>
            <たartifactId>春・データ-JPA </たartifactId> 
            <バージョン> 2.2.2.RELEASE </バージョン> 
        </依存関係> 
        <! - C3P0 - > 
        <依存> 
            <groupIdを> com.mchange </ groupIdを> 
            <たartifactId> C3P0 </たartifactId> 
            <バージョン> 0.9.5.2 </バージョン> 
        </依存> 
        <依存性> 
            <のgroupId> org.hibernate </のgroupId> 
            <たartifactId>休止状態コア</たartifactId> 
            <バージョン> 5.2.10 .Final </バージョン> 
        </依存関係> 
        <依存> 
            <たartifactId>のmysql-コネクタ-javaの</たartifactId> 
            <バージョン> 8.0.18 </バージョン>
        </依存> 
        <依存性> 
            <のgroupId> org.hibernate </のgroupId> 
            <たartifactId>休止状態-のEntityManager </たartifactId> 
            <バージョン> 5.2.10.Final </バージョン> 
        </依存> 
        <依存性> 
            <のgroupId> ORG .springframework </のgroupId> 
            <たartifactId>ばね試験</たartifactId> 
            <バージョン> 5.2.2.RELEASE </バージョン> 
            <スコープ>テスト</スコープ> 
        </依存> 
        <依存性> 
            <のgroupId> JUnitの</のgroupId > 
            <たartifactId> JUnitの</たartifactId> 
            <バージョン> 4.12 </バージョン>
            <スコープ>テスト</スコープ> 
        </依存> 
    </依存関係>
</プロジェクト>

 

 3:エンティティ

@Table(名= "JPA_PERSONS" 
@Entity 
パブリック クラス人{ 

    プライベート整数ID。
    プライベート文字列lastNameの。

    プライベート文字列のメール。
    プライベート日の誕生。
    
    @GeneratedValue 
    @Id 
    パブリック整数のgetId(){
         戻りID。
    }

 

メインリポジトリを達成します

パブリック インターフェース PersonRepositoryが延びリポジトリ<人、整数> { 
    リストの<person> findById(整数ID)。
}

 

 

4:applicationContext.xmlを設定

<?= "1.0"エンコード= "UTF-8" XMLバージョン?> 
< のxmlns = "http://www.springframework.org/schema/beans" 
    のxmlns:XSI = "http://www.w3.org/2001 / XMLスキーマ・インスタンス" 
    のxmlns:コンテキスト= "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。 
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0。
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd」> 

    <! - 配置自动扫描的包- > 
    < コンテキスト:コンポーネントスキャンベースパッケージ= "ドメイン" > </ コンテキスト:コンポーネント・スキャン>  

    <! - 1.配置数据源- > 
    < 文脈:プロパティプレースホルダ位置= "クラスパス:db.properties" /> 

    < ID = "データソース" 
        クラス= "com.mchange.v2.c3p0.ComboPooledDataSource" > 
        < プロパティ= "ユーザー"= "$ {jdbc.user}"> </ プロパティ> 
        < プロパティ= "パスワード" = "$ {jdbc.password}" > </ プロパティ>     
        < プロパティ= "driverClass" = "$ {jdbc.driverClass}" > </ プロパティ> 
        < プロパティ= "jdbcUrlと" = "$ {jdbc.jdbcUrl}" > </ プロパティ> 
        
        <! - 配置其他属性- > 
    </ > 

    <! -2.配置JPA的のEntityManagerFactory - > 
    < ビーンID = "のEntityManagerFactory" 
        クラス= "org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" > 
        < プロパティ= "データソース" REF = "データソース" > </ プロパティ> 
        < プロパティ= "jpaVendorAdapter" > 
            < 豆のクラス= "org.springframework.orm。 jpa.vendor.HibernateJpaVendorAdapter」> </ > 
        </ プロパティ> 
        < プロパティ= "packagesToScan" = "ドメイン" > </ プロパティ> 
        < プロパティ = "jpaProperties" > 
            < 小道具> 
                <! - 2つのキャッシュコヒーレンシ- > 
                <! -   
                <支えるキー= "hibernate.cache.region.factory_class"> org.hibernate.cache.ehcache.EhCacheRegionFactory </小道具> 
                <小道具キー= "net.sf.ehcache.configurationResourceName"> Ehcacheの-hibernate.xml </小道具> 
                - > 
                <! - 生成された列のデータテーブルマッピング戦略- > 
                < 小道具キー= "hibernate.ejb.naming_strategy" > org.hibernate.cfg。ImprovedNamingStrategy </ 小道具> 
                <! -休止基本属性- > 
                < 小道具キー= "hibernate.dialect" >org.hibernate.dialect.MySQL5InnoDBDialect </ 小道具> 
                < 小道具キー= "hibernate.show_sql" ></ 小道具> 
                < 小道具キー= "hibernate.format_sql" ></ 小道具> 
                < 小道具キー= "hibernate.hbm2ddl。オート」>更新</ 小道具> 
            </ 小道具> 
        </ プロパティ> 
    </ > 

    <! - 3.配置事务管理器- > 
    <豆のID= "トランザクションマネージャー" 
        クラス= "org.springframework.orm.jpa.JpaTransactionManager" > 
        < プロパティ= "のEntityManagerFactory" REF = "のEntityManagerFactory" > </ プロパティ>     
    </ > 

    <! - 4.設定トランザクションサポートの注釈- > 
    < TX:アノテーション・ドリブン・トランザクションマネージャ- = "のTransactionManager" /> 

    <! - 5.設定SpringData - > 
    <! - 追加JPAのネームスペース- > 
    <! -ベース・パッケージ:扫描リポジトリビーン所在的パッケージ- > 
    < JPA:リポジトリベースパッケージ= "ドメイン" 
        エンティティ・マネージャの工場-REF = "のEntityManagerFactory" > </ JPA:リポジトリ> 
</ >

 

XMLフォームの作成:

jdbc.user =ルート
jdbc.password =ルート
jdbc.driverClass =はcom.mysql.jdbc.Driver 
jdbc.jdbcUrl = JDBCます。mysql:// localhost /をjpatest userSSL =真&useUnicode =真&characterEncoding = UTF8 &serverTimezone = GMT 

5:試験方法

パブリック クラスSpringTest { 
    
    プライベート ClassPathXmlApplicationContextコンテキスト= nullを
    
    { 
        コンテキスト = 新しい ClassPathXmlApplicationContext( "applicationContext.xmlを" )。
    } 
    @Test 
    公共 ボイドtestjap(){ 
        PersonRepositoryリポジトリ = context.getBean(PersonRepository。クラス)。
        リストの<person> ID = repository.findById(1 )。
        (人物の人物:ID){ 
            System.out.printlnは(人)。
        } 
    } 
    @Test 
    公衆 ボイド testdataSources()スローSQLExceptionが{ 
        データソースデータソース = context.getBean(データソース。クラス)。
        System.out.println(dataSource.getConnection())。
    } 
}

おすすめ

転載: www.cnblogs.com/dgwblog/p/12121696.html