bean1.java
パッケージ豆。
パブリッククラスbean1 {
文字列名= "123123";
パブリック文字列のgetName(){
戻り名。
}
公共ボイドのsetName(文字列名){
this.name =名。
}
@Override
パブリック文字列のtoString(){
リターン"bean1 [NAME =" +名+ "]"。
}
}
bean2.java
パッケージ豆。 パブリック クラスbean2 { 文字列名 = "0" ; INT ID = 1 。 公共 ボイドsetBean1(bean1 bean1){ のSystem.out.println(bean1)。 この .nameの= bean1.getName()。 } 公共 ボイドのsetName(文字列名){ この .nameの= 名前。 } パブリック文字列のgetName(){ 戻り名。 } 公共 ボイド SETID(int型のID){ この .ID = ID。 } パブリック INT のgetId(){ 戻りID。 } @Override パブリック文字列のtoString(){ リターン "bean2 [NAME =" +名+ "]" 。 } }
WebDao.java
パッケージDAO。 輸入org.hibernate.SessionFactory。 輸入org.springframework.context.ApplicationContext; 輸入org.springframework.context.support.ClassPathXmlApplicationContext; 輸入org.springframework.orm.hibernate4.HibernateTemplate; 輸入org.springframework.orm.hibernate4.support.HibernateDaoSupport; 輸入bean.bean2。 パブリック クラス WebDaoが延びHibernateDaoSupportを{ パブリック WebDao(){ System.out.printlnは( "DAO" )。 } 公共 ボイド(bean2 bean2)保存{ System.out.println(bean2)。 HibernateTemplateのHibernateTemplate = この.getHibernateTemplate()。 System.out.println( "のHibernateTemplate:" + のHibernateTemplate)。 getHibernateTemplate()setCheckWriteOperations(偽); この.getHibernateTemplate()セーブ(bean2)。 getHibernateTemplate()フラッシュ(); System.out.println( "保存成功!" ); } 公共の 最終的な ボイドsetMySessionFactory(SessionFactoryのSF){ } }
bean2.hbm.xml
<?xmlのバージョン= "1.0"エンコード= "UTF-8"> <!DOCTYPE hibernate- PUBLICマッピング " - //休止/休止状態のマッピングDTD 3.0 // EN" 「http://www.hibernate.org/dtdを/hibernate-mapping-3.0.dtd「> <! -エンティティ構成テーブルのオブジェクトとの関係- > < -属性のパッケージペナルティ要素のname属性内の完全なクラス名を書くために、すべての必要性を袋を埋める:!、あなたがすることができ、直接クラス名の書き込みエッセイ- > <-Hibernateマッピングのためのパッケージ変更 =「豆」> <! - クラス要素:エンティティとテーブルの間の対応関係を設定する 完全なクラス名:名 表:データベーステーブル名を - > < クラス =名"bean2"表= "test_bean"> <名ID = "ID"> <発電機のクラス = "ネイティブ"> </ジェネレータ> </ ID> <プロパティ名= "名前"> </ property>の </ クラス > </休止状態マッピング>
applicationContext.xmlを
<?xmlのバージョン= "1.0"エンコード= "UTF-8"> <豆のxmlns = "http://www.springframework.org/schema/beans" のxmlns:コンテキスト = "http://www.springframework.org /スキーマ/コンテキスト"のxmlns:P = "http://www.springframework.org/schema/p" のxmlns:AOP = "http://www.springframework.org/schema/aop"のxmlns:TX ="のhttp: //www.springframework.org/schema/tx " のxmlns:XSI = "http://www.w3.org/2001/XMLSchema-instance" のxsi:schemaLocationの =" http://www.springframework.org/schema/豆http://www.springframework.org/schema/beans/spring-beans-4.0.xsd ます。http:// www.springframework.org/schema/context ます。http://www.springframework。ORG /スキーマ/コンテキスト/春コンテキスト-4.0.xsd のhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx のhttp://www.springframework .ORG /スキーマ/ TX /春-TX-4.0.xsd のhttp:// www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd 「> <Bean名= "データソース" クラス = "com.mchange.v2.c3p0.ComboPooledDataSource"> <プロパティ名= "jdbcUrlと"値= "JDBC:MySQLの:///テスト"> </ property>の <プロパティ名=」 driverClass」値= "はcom.mysql.jdbc.Driver"> </ property>の <プロパティ名= "ユーザー"値= "ルート"> </ property>の <プロパティ名= "パスワード"値= "ルート"> </ property>の </豆> <Bean名= "SessionFactoryの" クラス = "org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <プロパティ名= "データソース" REF = "データソース"> </ property>の <プロパティ名= "hibernateProperties"> <小道具> < ! -必选配置- > <小道具キー= "hibernate.connection.driver_class">はcom.mysql.jdbc.Driver </小道具> <小道具キー= "hibernate.connection.url"> JDBCます。mysql:// /試験</小道具> <プロパキー= "hibernate.connection.username">ルート</小道具> <プロパキー= "hibernate.connection.password">ルート</小道具> >ルート</小道具> <! -オプション- > <小道具キー= "hibernate.dialect"> org.hibernate.dialect.MySQLDialect </小道具> <小道具キー= "hibernate.show_sql"> trueに </小道具> <小道具キー= "hibernate.format_sqlを"> trueに </小道具> <小道具キー=「hibernate.hbm2ddl.auto」>更新</小道具> </小道具> </プロパティ> <! -パスのORMは、春にはパッケージを自動的に読み込みます導入ORMメタデータ指定されたパッケージのメタデータすべての設定- > <プロパティ名= "mappingDirectoryLocations"値= "CLASSPATH:"> </プロパティ> </豆> <Bean名= "Bean1" クラス = "bean.bean1"> </豆> <Bean名= "bean2" クラス = "bean.bean2"> <プロパティ名= "bean1" REF = "bean1"> </ property>の </ビーン> <Bean名= "beanDao" クラス = "dao.WebDao"> <プロパティ名= "SessionFactoryの" REF = "SessionFactoryの"> </ property>の</豆> </豆>
hibernate.cfg.xmlの
<?xml version = "1.0"エンコード= "UTF-8"?> <DOCTYPE hibernate-!コンフィグレーションPUBLIC " - //休止/ Hibernate構成DTD 3.0 // EN" 「http://www.hibernate.org/dtd /hibernate-configuration-3.0.dtd "> <休止状態-設定> の<session-工場> <! - #hibernate.dialect org.hibernate.dialect.MySQLDialect #hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect #hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect #hibernate.connection.driver_classはcom.mysql.jdbc.Driver #hibernate.connection.url JDBCます。mysql:// /テスト #1 hibernate.connection。ユーザ名ギャビン #1 hibernate.connection。 パスワード- > <! -データベース駆動型- > <プロパティ名= "hibernate.connection.driver_class">はcom.mysql.jdbc.Driver </プロパティ> <! -データベースのURL - > <プロパティ名= "hibernate.connection.url"> JDBC:MySQLの:// /テスト</プロパティ> <! -データベースのユーザ名- > <プロパティ名= "hibernate.connection.username">ルート</プロパティ> < ! -データベース接続パスワード- > <プロパティ名=「hibernate.connection.password」>ルート</プロパティ> <! - データベースの方言 異なるデータベース、SQL構文が若干異なっている方言は、指定された休止フレームワークを可能にします。 SQL文を生成するときに、データベースの方言のために生成される。 SQL99標準:チェックするデータベース・テーブルへのDDL定義言語の追加および削除 DCL制御言語権事務を 操作言語DML CRUDの 注意:MYSQL選択方言、最短方言を選択し - > 、<プロパティ名= "hibernate.dialect"> org.hibernate.dialect.MySQLDialect </プロパティ> <! - #hibernate.show_sqlをtrueに hibernate.format_sql#trueに - > <! -コンソールに出力生成されたSQL文の休止状態になる- > <プロパティ名= "hibernate.show_sql"> trueにする </プロパティ> <! -生成されたSQL文を休止します(文法インデント)の書式設定- > <プロパティ名=「hibernate.format_sqlを」> trueに </プロパティ> <! - ##自動スキーマのエクスポート自動エクスポートテーブル構造に自動的にテーブルを構築します。 各時間フレームテーブルを構築するために自動実行を作成hibernate.hbm2ddl.auto#新しいテーブルが前の表でカバーされます作成し、テーブルのデータは、(使用テスト開発環境を)失われます... #作成hibernate.hbm2ddl.auto - 自動ドロップテーブルの構成。すべてのフレームは、両端を実行するには、すべてのテーブルを削除します。(開発環境を使用してテスト) 自動的にテーブルを生成するために#hibernate.hbm2ddl.auto更新(推奨)。もはやすでに存在する場合は発生しません。テーブルが変更された場合。自動更新テーブル(任意のデータを消去しない)。 #hibernate.hbm2ddl.auto検証チェックはありません自動的にテーブルがデータベース内のすべてのテーブルのチェックを開始します生成し、正しいチェックが...失敗している > - <プロパティ名=「hibernate.hbm2ddl.auto」>更新</プロパティ> <マッピングリソース=「bean2.hbm.xmlを「> </マッピング> </セッション・ファクトリー> </ Hibernateは、設定>
TempTest.java
パッケージart_test。 輸入org.junit.Test; 輸入org.springframework.context.ApplicationContext; 輸入org.springframework.context.support.ClassPathXmlApplicationContext; 輸入bean.bean2。 輸入dao.WebDao。 パブリック クラスTempTest { 公共 ボイドtestSessionFactory(){ ApplicationContextの交流 = 新しい ClassPathXmlApplicationContext( "ApplicationContextの-dao.xml" )。 // するSystem.out.println( "AC:" + AC); オブジェクトビーン= ac.getBean( "のSessionFactory" )。 System.out.println( "豆:" +豆); } 公共 ボイドtestBean(){ ApplicationContextの交流 = 新しい ClassPathXmlApplicationContext( "applicationContext.xmlを" )。 // するSystem.out.println( "AC:" + AC); bean2 bean2 =(bean2)ac.getBean( "bean2" )。 System.out.println( "bean2:" + bean2)。 bean2.setName( "試験" )。 // WebDao beandao =新しいWebDao(); // ClassPathXmlApplicationContextリソース=新ClassPathXmlApplicationContext( "applicationContext.xmlを"); WebDao DAO =(WebDao)ac.getBean( "beanDao" )。 System.out。); dao.save(bean2)。 } パブリック 静的 ボイドメイン(文字列[]引数){ TempTest tempTest = 新しいTempTest()。 tempTest.testBean(); } }
要約:
図1は、MySQLデータベースの最初にテーブルを作成するには
IDが2タグテーブルには主キーで、プロパティが共通の属性です
3つのDAOを自動的に注入される、新しい作成するために使用することはできません
hibernate.cfg.xmlの<マッピング資源= "bean2.hbm.xml"> </マッピング>を導入することによって確立される</プロパティ>:<プロパティ名= "mappingDirectoryLocations" 値= "クラスパス">で4 applicationContext.xmlをデータベースのマッピング