Spring DaoのJDBC
Springが提供するDAO(データアクセスオブジェクト)サポートの主な目的は、JDBC、Hibernate、JDOなどの標準的な方法でさまざまなデータアクセステクノロジーの使用を促進することです。これらの永続性テクノロジーを簡単に切り替えることができるだけでなく、エンコード時にさまざまなテクノロジーで特定の例外を処理することを検討する必要もありません。
JDBC、JDO、Hibernateなどのさまざまなデータアクセステクノロジーの使用を一貫した方法で促進するために、Springは抽象的なDAOクラスを提供します。これらの抽象クラスは、現在使用しているデータアクセステクノロジーに関連するデータソースやその他の構成情報を取得できるいくつかの方法を提供します。
DAOサポートクラス:
JDBCDAOSUPPORT -JDBCデータアクセスオブジェクトのベースクラス。 DataSourceが必要であり、JDBCTEMPLATEがサブクラスに提供されます。
HibernatedAoSupport-冬眠データアクセスオブジェクトのベースクラス。 SessionFactoryが必要であり、サブクラスにHibernateTemplateが提供されます。また、HibernateTemplateを提供して直接的に初期化することを選択することもできます。これにより、後者の設定は、SessionFactory、Flushモード、例外翻訳者など、再利用できます。
JDODAOSUPPORT -JDOデータアクセスオブジェクトのベースクラス。 SubclassにJDoTemplateをセットアップおよび提供するには、PersistenceManagerFactoryが必要です。
JPADAOSUPPORT -JPAデータアクセスオブジェクトのベースクラス。 EntityManagerFactoryが必要であり、JPATEMPLATEがサブクラスに提供されます。
このセクションでは、主にJDBCDAOSUPPORTに対するSpringのサポートについて説明します。
これが例です:
ユーザーが存在する場合はテーブルをドロップします。 /*=============================================================== /*=============================================================================
パブリッククラスユーザー{private integer id;プライベート文字列名;民間整数年齢; public Integer getId(){return id; } public void setid(integer id){this.id = id; } public string getname(){return name; } public void setName(string name){this.name = name; } public Integer getage(){return age; } public void Setage(整数年齢){this.age = age; }} /** * Intellij Ideaによって作成されました。<br> * <b>ユーザー</b>:Leizhimin <br> * <b> date </b>:2008-4-22 15:34:36 <br> * <b> note </b>:DAOインターフェイス */パブリックインターフェイスiuserdao {ユーザーユーザーインサート(ユーザー);パブリックユーザーFind(整数ID); } javax.sql.datasourceをインポートします。 Java.sql.Connectionをインポートします。 java.sql.sqlexceptionをインポートします。 /*** Intellij Ideaによって作成されました。<br>* <b> note </b>:基本クラスDAO、データソースインジェクションを提供*/public class Basedao {private DataSource DataSource; public dataSource getDataSource(){return dataSource; } public void setDataSource(dataSource dataSource){this.dataSource = dataSource; } public Connection getConnection(){connection conn = null; try {conn = dataSource.getConnection(); } catch(sqlexception e){e.printstacktrace(); } return conn; }}/** * Intellijのアイデアによって作成された。<br> * <b>ユーザー</b>:Leizhimin <br> * <b> date </b>:2008-4-22 15:36:04 <br> * <b>:DAO実装 */パブリッククラスのユーザーダオ拡張basedao exprentations iuserdao {public jdbctemplate getjdbctemplate(){return new jdbctemplate(getDataSource()); } public void insert(user user){string name = user.getName(); int age = user.getage()。intvalue(); // jdbctemplate.update( "inserting into user(name、age)" // + "values( '" + name + "'、" + age + ")"); string sql = "ユーザー(名前、年齢)値(?、?)に挿入されます(?)"; getjdbctemplate()。update(sql、new object [] {name、age}); } public user find(integer id){list rows = getjdbctemplate()。queryforlist( "select * from user where id =" + id.intvalue()); iterator it = rows.iterator(); if(it.hasnext()){map usermap =(map)it.next(); integer i = new Integer(usermap.get( "id")。toString()); string name = usermap.get( "name")。toString(); integer age = new Integer(usermap.get( "age")。toString()); user user = new user(); user.setId(i); user.setname(name); user.setage(age);ユーザーを返します。 } nullを返します。 }}<?xml version = "1.0" encoding = "utf-8"?> <!doctype beans public "-// spring/dtd bean/en" "http://www.springframework.org/dtd/spring-beans.dtd" "> <beans =" bean ">"> < <value> com.mysql.jdbc.driver </value> </property> <property name = "url"> <value> jdbc:mysql:// localhost:3306/value> </property> <property name = "username"> <value> root> root> </property> <propertion </bean> <bean id = "Basedao" abstract = "true"> <プロパティ名= "dataSource"> <ref bean = "dataSource"/> </property> </bean> <bean id = "userdao" parent = "basedao"> </bean> </beans>
org.springframework.context.applicationcontextをインポートします。 import org.springframework.context.support.filesystemxmlapplicationcontext; /** * Intellij Ideaによって作成された。<br> * <b> user </b>:Leizhimin <br> * <b> date </b>:2008-4-22 15:59:18 <br> * <b> note </b>:クライアント */パブリッククラスSpringDaodemo {Public Static Main(String [] ArputionContextext Concation filesystemxmlapplicationContext( "d://_spring//src//com/lavasoft//springnote//ch05_jdbc03_temp//bean-jdbc-temp.xml"); user user = new user(); user.setname( "hahhah"); user.setage(new Integer(22)); iuserdao userdao =(iuserdao)context.getbean( "userdao"); userdao.insert(user); user = userdao.find(new Integer(1)); System.out.println( "name:" + user.getName()); }}
実行結果:
log4j:logger(org.springframework.core.collectionfactory)には、付録は見つかりません。 log4j:WARN LOG4Jシステムを適切に初期化してください。名前:JDBCTEMPLATEプロセスEXITコード0で終了します
春のダオの冬眠
HibernatedAoSupport-冬眠データアクセスオブジェクトのベースクラス。 SessionFactoryが必要であり、サブクラスにHibernateTemplateが提供されます。また、HibernateTemplateを提供して直接的に初期化することを選択することもできます。これにより、後者の設定は、SessionFactory、Flushモード、例外翻訳者など、再利用できます。
このセクションでは、主にHibernateTemplateに対するSpringのサポートについて説明します。
これが例です:
ユーザーが存在する場合はテーブルをドロップします。 /*=============================================================== /*=============================================================================
/*** Intellij Ideaによって作成されました。<br>* <b> note </b>:Hiberante Entity class*/public classユーザー{private integer id;プライベート文字列名;民間整数年齢; public Integer getId(){return id; } public void setid(integer id){this.id = id; } public string getname(){return name; } public void setName(string name){this.name = name; } public Integer getage(){return age; } public void Setage(整数年齢){this.age = age; }}<?xml version = "1.0" encoding = "utf-8"?> <!doctype hibernate-mapping public " - // hibernate/hibernateマッピングdtd 3.0 // en" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <higbernate> name = "com.lavasoft.springnote.ch06_hbm_02detx.user" table = "user"> <id name = "column =" id "> <generator/> </id> <財産名=" column = "name"/> <プロパティ名= "column =" age "/>
/** * Intellij Ideaによって作成されました。<br> * <b>ユーザー</b>:Leizhimin <br> * <b> date </b>:2008-4-23 15:37:43 <br> * <b> note </b>:DAOインターフェイス */パブリックインターフェイスiuserdao {ユーザーユーザーインサート(ユーザー);パブリックユーザーFind(整数ID); } import org.hibernate.sessionFactory; import org.springframework.orm.hibernate3.hibernateTemplate; /** * Intellij Ideaによって作成されました。<br> * <b>ユーザー</b>:Leizhimin <br> * <b> date </b>:2008-4-23 15:15:55 <br> * <b> note </b>:dao実装 */パブリッククラスユーザーダオ実装iuserdao public void setSessionFactory(SessionFactory SessionFactory){this.hibernateTemplate = new hibernateTemplate(sessionfactory); } public void insert(user user){hibernatetemplate.save(user); System.out.println( "保存されたユーザーオブジェクトのID:"+user.getId()); } public user find(integer id){user user =(user)hibernatetemplate.get(user.class、id);ユーザーを返します。 }}<?xml version = "1.0" encoding = "utf-8"?> <!doctype beans public " - // spring/dtd bean/en" "http://www.springframework.org/dtd/spring-beans.dtd" "> <beans> <bean =" datasource "> <jidclassname"> <value> com.mysql.jdbc.driver </value> </property> <property name = "url"> <value> jdbc:mysql:// localhost:3306/value> </property> <property name = "username"> <value> root> root> </property> <propertion </bean> <bean id = "sessionfactory" Destrow-method = "close"> <プロパティ名= "dataSource"> <ref bean = "datasource"/> </jppropert name = "mappingresources"> <balue> com/lavasoft/springnote/ch06_hbm_02protx/propertx/flut> </</</</</</</</</</</</flomic name = "hibernateProperties"> <props> <prop key = "hibernate.dialect"> org.hibernate.dialect.mysqldialect </prop> </props> </property> </bean> <bean id = "userdao"> <propertial name = "sessionfactory"/<
org.springframework.context.applicationcontextをインポートします。 import org.springframework.context.support.filesystemxmlapplicationcontext; /*** Intellij Ideaによって作成された。<br>* <b>注</b>:テストクラス、クライアント*/パブリッククラスSpringHibernatemo {public static void main(string [] args){ApplicationContext Context = new filesystemxmlapplicationContext( "d://_spring//src//com/lavasoft//springnote//ch06_hbm_02protx//bean-hbm_tx.xml"); // daoオブジェクトを作成しますiuserdao userdao =(iuserdao)context.getbean( "userdao"); user user = new user(); user.setName( "Caterpillar"); user.setage(new Integer(30)); userdao.insert(user); user = userdao.find(new Integer(1)); System.out.println( "name:" + user.getName()); }}
実行結果:
log4j:logger(org.springframework.core.collectionfactory)には、付録は見つかりません。 log4j:WARN LOG4Jシステムを適切に初期化してください。保存されたユーザーオブジェクトのID:18名:jdbctemplateプロセス終了コードで終了0