<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆 - 1, 文章 - 44, 評論 - 2, 引用 - 0
    數(shù)據(jù)加載中……

    Spring 的持久層封裝

    一、概述
    (一)基本概念
    1、數(shù)據(jù)訪問的關(guān)鍵技術(shù)
    ???? 我們可以將數(shù)據(jù)訪問分為兩個部分:一是獲得數(shù)據(jù)源;二是進行數(shù)據(jù)庫操作(增刪改查)。
    2、獲得數(shù)據(jù)源的幾種方法
    因為只是為了記錄一些關(guān)鍵的東西,以實用為主,因此沒有過多的考慮措辭和學(xué)術(shù)上的嚴謹。這里指的是在Java中怎么能取得和數(shù)據(jù)源(DataSource)的聯(lián)系,方法主要有傳統(tǒng)的在程序中硬編碼和通過XML注入。Spring提供三種XML注入:(1)使用Spring自帶的DriverManagerDataSource;(2)使用DBCP連接池(3)使用Tomcat提供的JNDI。其中(1)可以配合Hibernate、iBatis等ORM一起使用(在XML配置文檔中加入相應(yīng)的配置段)。
    (二)框架圖

    XML 注入數(shù)據(jù)源的三種方式
    Spring 自帶的DriverManagerDataSource
    DBCP 連接池
    Tomcat 的JNDI
    ?
    數(shù)據(jù)庫操作的兩種方式
    Spring JdbcTemplate
    使用 ORM 工具
    ?

    (三)何時使用什么
    ????????? 現(xiàn)在大多數(shù)輕量級開發(fā),都采用Hibernate作為持久層解決方案,因此可以作為首選。
    二、詳細
    (一)數(shù)據(jù)源注入
    1、使用Spring自帶的DriverManagerDataSource??
    配置文件
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    ?"http://www.springframework.org/dtd/spring-beans.dtd">
    <beans>
    ?? <! 設(shè) 定dataSource à
    ?? <bean id= dataSource ” class=” org.springframework.jdbc.datasource.DriverManagerDataSource ”>
    ????? <! —使用SQL Server 數(shù) 據(jù) à
    ?????? <property name= ”driverClassName”>
    ????????? <value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
    ?????? </property>
    ??????? <property name= ”url”>
    ????????? <value>jdbc:Microsoft:sqlserver://localhost:1433/stdb</value>
    ?????? </property>
    <property name= ”name”>
    ????????? <value>admin</value>
    ?????? </property>
    <property name= ”msg”>
    ????????? <value>admin</value>
    ?????? </property>
    ??? </bean>
    ?
    ??? <! —設(shè)定 transactionManager à
    ??? <bean id= transactionManager
    class= org.springframework.jdbc.datasource.DataSourceTransactionManager >
    ??????? <property name= dataSource >
    ??????????? <ref bean= dataSource />
    ??????? </property>
    ??? </bean>
    ?
    ?? <!—示例中DAO-->
    ?? <bean id= ”bookDAO” class=”com.bookDAO”>
    ??????? <property name= ”dataSource”>
    ??????????? <ref bean= ”dataSource”/>
    ??????? </property>
    ??????? <property name= ”transactionManager”>
    ??????????? <ref bean= ”transactionManager”>
    ??????? </property>
    ?? </bean>
    </beans>
    ?? 紅色部分顯示了所使用的類,就是用它來處理數(shù)據(jù)庫連接。
    2、使用DBCP連接池
    ??? 要在Spring中使用DBCP連接池,需要引入spring-framework-2.0-ml\lob\jakarta-commons文件夾中的commons-collections.jar、commons-dbcp.jar和commons-pool.jar。
    配置文件
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    ?"http://www.springframework.org/dtd/spring-beans.dtd">
    <beans>
    ?? <! 設(shè) 定dataSource à
    ?? <bean id= dataSource ” class=” org.apache.commons.dbcp.BasicDataSource ”>
    ????? <! —使用SQL Server 數(shù) 據(jù) à
    ?????? <property name= ”driverClassName”>
    ????????? <value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
    ?????? </property>
    ??????? <property name= ”url”>
    ????????? <value>jdbc:Microsoft:sqlserver://localhost:1433/stdb</value>
    ?????? </property>
    <property name= ”name”>
    ????????? <value>admin</value>
    ?????? </property>
    <property name= ”msg”>
    ????????? <value>admin</value>
    ?????? </property>
    ??? </bean>
    ?
    ??? <! —設(shè)定 transactionManager à
    ??? <bean id= transactionManager
    class= org.springframework.jdbc.datasource.DataSourceTransactionManager >
    ??????? <property name= dataSource >
    ??????????? <ref bean= dataSource />
    ??????? </property>
    ??? </bean>
    ?
    ?? <!—示例中DAO-->
    ?? <bean id= ”bookDAO” class=”com.bookDAO”>
    ??????? <property name= ”dataSource”>
    ??????????? <ref bean= ”dataSource”/>
    ??????? </property>
    ??????? <property name= ”transactionManager”>
    ??????????? <ref bean= ”transactionManager”>
    ??????? </property>
    ?? </bean>
    </beans>
    ?? 紅色部分為不同之處,可以看出,Spring為各種不同的數(shù)據(jù)源提供了統(tǒng)一的方式,下面看使用Tomcat提供的JNDI,有了一些不同。
    3、使用Tomcat提供的JNDI
    分兩步,第一步是配置Tomcat的server.xml;第二步是編寫Spring的配置文件。
    Tomcat 的server.xml
    <Context path=”/demo” reloadable=”true” docBase=”c: \eclipse\workspace\demo” workDir=” c: \eclipse\workspace\demo\work”>
    < Resource name=”jdbc/opendb” auth=”Container” type=”javax.sql.DataSource” factory=”org.apache.tomcat.dbcp.BasicDataSourceFactory” driverClassName=”com.microsoft.jdbc.sqlserver.SQLServerDriver” url=”jdbc:Microsoft:sqlserver://localhost:1433/stdb”
    <!— 設(shè)定用戶名-->
    name=”admin”
    <!— 設(shè)定密碼 à
    msg=”admin”
    < !--設(shè)定最大連接數(shù) à
    maxActive=”10000”
    < !--設(shè)定最大空閑時間 à
    maxldle=”10000”
    < !--設(shè)定最大等待時間 à
    maxWait=”10000”
    removeAbandoned=”true”
    removeAbandonedTimeout=”10”
    logAbandoned=”true”
    />
    </Context>
    Spring 配置文件
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    ?"http://www.springframework.org/dtd/spring-beans.dtd">
    <beans>
    ?? <! 設(shè) 定dataSource à
    ?? <bean id= dataSource ” class=” o rg.springframework.jndi.JndiObjectFactoryBean >
    ??? ?<property name=”jndiName”>
    ???????? <value>jdbc/opendb</value>
    ??? ? /* 以下信息在 server.xml 中已經(jīng)配置過了,不需要再配置了
    ?<! —使用SQL Server 數(shù) 據(jù) à
    ?????? <property name= ”driverClassName”>
    ????????? <value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
    ?????? </property>
    ??????? <property name= ”url”>
    ????????? <value>jdbc:Microsoft:sqlserver://localhost:1433/stdb</value>
    ?????? </property>
    <property name= ”name”>
    ????????? <value>admin</value>
    ?????? </property>
    <property name= ”msg”>
    ????????? <value>admin</value>
    ?????? </property> */
    ??? </bean>
    ?
    ?? ?<! —設(shè)定 transactionManager à
    ??? <bean id= transactionManager
    class= org.springframework.jdbc.datasource.DataSourceTransactionManager >
    ??????? <property name= dataSource >
    ??????????? <ref bean= dataSource />
    ??????? </property>
    ??? </bean>
    ?
    ?? <!—示例中DAO-->
    ?? <bean id= ”bookDAO” class=”com.bookDAO”>
    ??????? <property name= ”dataSource”>
    ??????????? <ref bean= ”dataSource”/>
    ??????? </property>
    ??????? <property name= ”transactionManager”>
    ??????????? <ref bean= ”transactionManager”>
    ??????? </property>
    ?? </bean>
    </beans>
    ?? 紅色部分為不同之處,可以看出,使用Tomcat提供的JNDI,有了一些不同。第一方式只是Spring包裝了一下Jdbc,跟直接使用Jdbc沒什么大的區(qū)別;后兩種采用了連接池技術(shù),比較好。我們也可以看出,三種方式配置基本相同,而作為使用數(shù)據(jù)源的類,調(diào)用方式相同,都是使用基本的依賴注入的方式。
    4、使用Hibernate
    配置文件
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    ?"http://www.springframework.org/dtd/spring-beans.dtd">
    <beans>
    ?? <! 設(shè) 定dataSource à
    ?? <bean id= dataSource ” class=”org.springframework.jdbc.datasource.DriverManagerDataSource”>
    ????? <! —使用SQL Server 數(shù) 據(jù) à
    ?????? <property name= ”driverClassName”>
    ????????? <value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
    ?????? </property>
    ??????? <property name= ”url”>
    ????????? <value>jdbc:Microsoft:sqlserver://localhost:1433/stdb</value>
    ?????? </property>
    <property name= ”name”>
    ????????? <value>admin</value>
    ????? ?</property>
    <property name= ”msg”>
    ????????? <value>admin</value>
    ?????? </property>
    ??? </bean>
    ?// 在第一種方法的基礎(chǔ)上加上 Hibernate 的配置段就 OK 了。
    ?<!— 使用 Hibernate sessionFactory à
    ? ?<bean id=” sessionFactory ” class=”org.springframework.orm.hibernate.LocalSessionFactoryBean”>
    ?????? <property name=”dataSource”>
    ??????????? <ref local=” dataSources ”>
    ?????? </property>
    ?? ????<property name=”mappingResources”>
    ???????? ???<list>
    ??????????????? <value>com/demo/bo/Book.hbm.xml</value>
    ???????????? </list>
    ?????? </property>
    ?????? <property name=”hibernateProperties”>
    ??????????? <props>
    ???????????????? <prop key=”hibernate.dialect”>
    ??????????????????????? net.sf.hibernate.dialect.SQLServerDialect
    ???????????????? </prop>
    <prop key=”hibernate.show_sql”>
    ????? true
    <prop>
    ??????????? </props>
    ??????? </property>
    ??? </bean>
    ?
    ??? <! —設(shè)定 transactionManager à
    ??? <bean id= transactionManager
    class= org.springframework.jdbc.datasource.DataSourceTransactionManager >
    ??????? <property name= sessionFactory >
    ??????????? <ref bean= sessionFactory />
    ??????? </property>
    ??? </bean>
    ?
    ?? <!—示例中DAO-->
    ?? <bean id= ”bookDAO” class=”com.demo.bo.Boo kDAO ”>
    ??????? <property name= sessionFactory ”>
    ??????????? <ref bean= sessionFactory ”/>
    ??????? </property>
    ??????? <property name= ”transactionManager”>
    ??????????? <ref bean= ”transactionManager”>
    ??????? </property>
    ?? </bean>
    </beans>
    ?? 紅色部分顯示了不同之處,有三點:1)、加入了<bean id=”sessionFactory”>段,引入Hibernate的sessionFactory;2)事務(wù)處理的屬性由原來的dataSource變成了sessionFactory;3)DAO引用從原來的dataSource變成了sessionFactory。其實就是說,在原來的datasouce之上加了Hibernate這一層,來處理相應(yīng)的數(shù)據(jù)訪問問題。因此在Spring中引入ORM是很容易的。
    ??Book.hbm.xmlBook.java是通過工具互相生成的,在這里不展示代碼了,而Book就是一個POJO
    4、使用iBatis
    配置文件
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    ?"http://www.springframework.org/dtd/spring-beans.dtd">
    <beans>
    ?? <! 設(shè) 定dataSource à
    ?? <bean id= dataSource ” class=”org.springframework.jdbc.datasource.DriverManagerDataSource”>
    ????? <! —使用SQL Server 數(shù) 據(jù) à
    ?????? <property name= ”driverClassName”>
    ????????? <value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
    ?????? </property>
    ??????? <property name= ”url”>
    ????????? <value>jdbc:Microsoft:sqlserver://localhost:1433/stdb</value>
    ?????? </property>
    <property name= ”name”>
    ????????? <value>admin</value>
    ?????? </property>
    <property name= ”msg”>
    ????????? <value>admin</value>
    ????? ?</property>
    ??? </bean>
    ?// 在第一種方法的基礎(chǔ)上加上 iBatis 的配置段就 OK 了。
    ?<!— 使用 iBatis à
    ? ?<bean id=” sqlMap ” class=”org.springframework.orm.ibatis.SqlMapClientFactoryBean”>
    ?????? <property name=”configLocation”>
    ??????????? <value>WEB/sqlMapConfig.xml</value>
    ?????? </property>
    ??? </bean>
    ??? <! —設(shè)定 transactionManager à
    ??? <bean id= transactionManager
    class= org.springframework.jdbc.datasource.DataSourceTransactionManager >
    ??????? <property name= dataSource >
    ??????????? <ref bean= dataSource />
    ??????? </property>
    ??? </bean>
    ?
    ?? <!—示例中DAO-->
    ??<bean id= ”bookDAO” class=”com.demo.bo.Boo kDAO ”>
    ??????? <property name= dataSource ”>
    ??????????? <ref bean= dataSource ”/>
    ??????? </property>
    ??????? <property name= ”transactionManager”>
    ??????????? <ref bean= ”transactionManager”>
    ??????? </property>
    ??? ????<property name=”sqlMap”>
    ???????????? <ref bean=”sqlMap”/>
    ??????? </property>
    ?? </bean>
    </beans>
    ?? 紅色部分顯示了不同之處,主要來看一下其與Hibernate的不同之處,1)同樣引入iBatis配置段,不過其mappingResources配置信息放在了sqlMapConfig.xml下,該文件放在WEB-INF下,示例代碼如下:
    <sqlMapConfig>
    ???? <sqlMap resource=”com/demo/bo/Book.xml”>
    </sqlMapConfig>
    而這其中的Book.xml文件類似于Hibernate中的Book.hbm.xml。
    ?2)事務(wù)處理配置段依然引用的是dataSource,而不是sqlMap。
    ?3)在DAO配置段,引用dataSource的同時,又加入了sqlMap引用。
    ?其PO對象——Book.java與Hibernate中相同。
    ?(二)數(shù)據(jù)庫操作
    1、使用JDBCTemplate
    ?
    import javax.sql.DataSource;
    import org.springframework.jdbc.core.*;
    import org.springframework.transaction.*;
    import org.springframework.dao.*;
    ?
    public class bookDAO{
    private DataSource dataSource依賴注入dataSource,管理數(shù)據(jù)庫;//
    private PlatformTransationManager transactionManager;//依賴注入管理事務(wù)
    ?
    public void setDataSource(DataSource dataSource){
    ??? this.dataSource=dataSource;
    }
    ?
    ???? public void setTransactionManager(PlatformTransationManager transactionManager){
    ???????? this. transactionManager= transactionManager;
    }
    ?
    public int create(String msg){
    ??? JdbcTemplate jdbcTemplate=new JdbcTemplate(dataSource);
    ??? jdbcTemplate.update(“INSERT INFO book VALUES(1,’gf’,’Mastering Spring’)”);
    }
    }
    配置文件
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    ?"http://www.springframework.org/dtd/spring-beans.dtd">
    <beans>
    ?? <! 設(shè) 定dataSource à
    ?? <bean id= ”dataSource” class=”org.springframework.jdbc.datasource.DriverManagerDataSource”>
    ????? <! —使用SQL Server 數(shù) 據(jù) à
    ?????? <property name= ”driverClassName”>
    ????????? <value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
    ?????? </property>
    ??????? <property name= ”url”>
    ????????? <value>jdbc:Microsoft:sqlserver://localhost:1433/stdb</value>
    ?????? </property>
    <property name= ”name”>
    ????????? <value>admin</value>
    ?????? </property>
    <property name= ”msg”>
    ????????? <value>admin</value>
    ?????? </property>
    ??? </bean>
    ?
    ??? <! —設(shè)定 transactionManager à
    ??? <bean id= transactionManager
    class= org.springframework.jdbc.datasource.DataSourceTransactionManager >
    ??????? <property name= dataSource >
    ??????????? <ref bean= dataSource />
    ??????? </property>
    ??? </bean>
    ?
    ?? <!—示例中DAO-->
    ??<bean id=”bookDAO” class=”com.bookDAO”>
    ??????? <property name= ”dataSource”>
    ??????????? <ref bean= ”dataSource”/>
    ??????? </property> ?
    ?? </bean>
    ?? <! —聲明式事務(wù)處理 - à
    ? ?<bean id= bookDAOProxy class= org.springframework.transaction.interceptor.Transation.ProxyFactoryBean >
    ??????? <property name= transacionManager >
    ??????????? <ref bean= transacionMaganer />
    ??????? </property>
    <property name= target >
    ???????? ???<ref bean= bookDAO />
    ??????? </property>
    <property name= transactionAttributes >
    ??????????? <props>
    ?????????????? <!-- 表示對 bookDAO 中的 create 方法進行事務(wù)處理,并指明當(dāng)前沒有事務(wù)就新建一個(用 PROPAGATION_REQUIRED 常量來表示的) à
    ??????????????? <prop key= create* >PROPAGATION_REQUIRED</prop>
    ??????????? </props>
    ??????? </property>?
    ?? </bean>
    </beans>
    ?
    最簡便、靈活的寫法:
    ?
    import javax.sql.DataSource;
    import org.springframework.jdbc.core.*;
    import org.springframework.transaction.*;
    import org.springframework.dao.*;
    ?
    public class bookDAO{
    private DataSource dataSource依賴注入dataSource,管理數(shù)據(jù)庫;//
    private PlatformTransationManager transactionManager;//依賴注入管理事務(wù)
    private String sql;
    ?
    public void setDataSource(DataSource dataSource){
    ??? this.dataSource=dataSource;
    }
    ?
    ???? public void setTransactionManager(PlatformTransationManager transactionManager){
    ???????? this. transactionManager= transactionManager;
    }
    ?
    public void setSql(String sql){
    ???? this.sql=sql;
    }
    ?
    public int create(String msg){
    ?/* JdbcTemplate jdbcTemplate=new JdbcTemplate(dataSource);
    ? ? jdbcTemplate.update( INSERT INFO book VALUES(1, ’gf’,’Mastering Spring’)”); */
    // 使用下述代碼代替上面注釋的內(nèi)容
    ?? jdbcTemplate.update(this.sql);
    }
    }
    配置文件
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    ?"http://www.springframework.org/dtd/spring-beans.dtd">
    <beans>
    ?? <! 設(shè) 定dataSource à
    ?? <bean id= ”dataSource” class=”org.springframework.jdbc.datasource.DriverManagerDataSource”>
    ????? <! —使用SQL Server 數(shù) 據(jù) à
    ?????? <property name= ”driverClassName”>
    ????????? <value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
    ?????? </property>
    ??????? <property name= ”url”>
    ????????? <value>jdbc:Microsoft:sqlserver://localhost:1433/stdb</value>
    ?????? </property>
    <property name= ”name”>
    ????????? <value>admin</value>
    ?????? </property>
    <property name= ”msg”>
    ????????? <value>admin</value>
    ?????? </property>
    ??? </bean>
    ?
    ??? <! —設(shè)定 transactionManager à
    ??? <bean id= transactionManager
    class= org.springframework.jdbc.datasource.DataSourceTransactionManager >
    ??????? <property name= dataSource >
    ???????? ???<ref bean= dataSource />
    ??????? </property>
    ??? </bean>
    ?
    ?? <!— 設(shè)定 jdbcTemplate à
    ??? <bean id=”jdbcTemplate” class=”org.springframework.jdbc.core.JdbcTemplate”>
    ??????? <property name=”dataSource”>
    ?????????????? <ref bean=”dataSource”>
    ??????? </property>
    ?? <!—示例中DAO-->
    ??<bean id=”bookDAO” class=”com.bookDAO”>
    ??????? <property name= ”dataSource”>
    ??????????? <ref bean= ”dataSource”/>
    ??????? </property>
    ???? ?//jdbcTemplate 屬性代替 處的代碼將dataSource注入到j(luò)dbcTemplate中
    ??????? <property name=”jdbcTemplate”>
    ???????????? <ref bean=”jdbcTemplate”>
    ??????? </property>
    ????? //sql 屬性代替 處代碼
    ??????? <property name=”sql”>
    ???????????? <value>INSERT INTO hello VALUES(1, gf , Mastering Spring )</value>
    ???????? </property>
    ?? </bean>
    ?? <! —聲明式事務(wù)處理 - à
    ? ?<bean id= bookDAOProxy class= org.springframework.transaction.interceptor.TransationProxyFactoryBean >
    ??????? <property name= transacionManager >
    ??????????? <ref bean= transacionMaganer />
    ??????? </property>
    <property name= target >
    ??????????? <ref bean= bookDAO />
    ??????? </property>
    <property name= transactionAttributes >
    ??????????? <props>
    ?????????????? <!-- 表示對 bookDAO 中的 create 方法進行事務(wù)處理,并指明當(dāng)前沒有事務(wù)就新建一個(用 PROPAGATION_REQUIRED 常量來表示的) à
    ??????????????? <prop key= create* >PROPAGATION_REQUIRED</prop>
    ??????????? </props>
    ??????? </property>?
    ?? </bean>
    </beans>
    ?
    ?
    2、使用Hibernate
    import javax.sql.DataSource;
    import org.springframework.jdbc.core.*;
    import org.springframework.transaction.*;
    import org.springframework.dao.*;
    import org.springframework.orm.*;
    ?
    public class bookDAO{
    //private DataSource dataSource; 依賴注入 dataSource ,管理數(shù)據(jù)庫
    private SessionFactory sessionFactory;
    private PlatformTransationManager transactionManager;//依賴注入管理事務(wù)
    ?
    /*public void setDataSource(DataSource dataSource){
    ??? this.dataSource=dataSource;
    }*/
    ?
    ???? public void setTransactionManager(PlatformTransationManager transactionManager){
    ???????? this. transactionManager= transactionManager;
    }
    ?
    public void setSessionFactory(DataSource sessionFactory){
    ???? this. sessionFactory = sessionFactory;
    }
    ?
    public int create(String msg){
    ?/* JdbcTemplate jdbcTemplate=new JdbcTemplate(dataSource);
    ? ? jdbcTemplate.update( INSERT INFO book VALUES(1, ’gf’,’Mastering Spring’)”); */
    ?// 下面這行替代
    ???? ?HibernateTemplate hibernateTemplate=new HibernateTemplate(sessionFactory);
    ?
    ????? Book book=new Book();
    ????? book.setId(1);
    ????? book.setAuthor(“gf”);
    ????? book.setName(“Mastering Spring”);
    ? // 下面這行替代
    ????? hibernateTemplate.saveOrUpdate(book);
    }
    ?
    sessionFactory 里引入了book.hbm.xml
    ?
    ?
    3、使用iBatis
    import javax.sql.DataSource;
    import org.springframework.jdbc.core.*;
    import org.springframework.transaction.*;
    import org.springframework.dao.*;
    import org.springframework.orm.*;
    ?
    public class bookDAO extends SqlMapClientDaoSupport{
    ???? private PlatformTransationManager transactionManager;
    ?
    ? ??? public void setTransactionManager(PlatformTransationManager transactionManager){
    ???????? this. transactionManager= transactionManager;
    }
    ?
    public int create(String msg){
    ??? ?? Book book=new Book();
    ????? book.setId(1);
    ????? book.setAuthor(“gf”);
    ????? book.setName(“Mastering Spring”);
    ??
    ???? getSqlMapClientTemplate().update(“ insertBook ”,book);
    }
    ?
    }
    ?
    配置文件——Book.xml
    該文件相當(dāng)于Hibernate中的book.hbm.xml
    ?
    <sqlMap namespace=”Book”>
    <typeAlias alias=”book” type=”com.demo.bo.Book”/>
    <insert id=”insertBook” parameterClass=”book”>
    ??? insert into book(id,author,name) values(#id#,#author#,#name#);
    </insert>
    </sqlMap>
    BookDAO中的insertBook和配置文件中的insertBook相匹配

    posted on 2007-03-15 09:40 ASONG 閱讀(1253) 評論(1)  編輯  收藏 所屬分類: JAVA

    評論

    # re: Spring 的持久層封裝  回復(fù)  更多評論   

    http://www.easy518.com
    主站蜘蛛池模板: 深夜免费在线视频| 亚洲精品乱码久久久久蜜桃| 一进一出60分钟免费视频| 成人免费看吃奶视频网站| 亚洲综合色区中文字幕| 免费一本色道久久一区| 亚洲中文字幕久久精品蜜桃| 野花高清在线电影观看免费视频| 亚洲三级在线免费观看| 成年人免费网站在线观看| 色欲色欲天天天www亚洲伊| 四虎影视在线永久免费看黄| 一级毛片免费播放男男| 久久99国产亚洲高清观看首页| 免费国产午夜高清在线视频| 亚洲精品自产拍在线观看动漫| 日韩人妻无码精品久久免费一| 久久久无码精品亚洲日韩按摩 | 国产亚洲一区二区三区在线| 全免费a级毛片免费看| 久久久久亚洲AV无码麻豆| 无人影院手机版在线观看免费| 亚洲国产欧美日韩精品一区二区三区 | 国产亚洲精品无码专区| 大地资源网高清在线观看免费| 337p日本欧洲亚洲大胆色噜噜| 免费看国产成年无码AV片| 激情无码亚洲一区二区三区| 精品亚洲一区二区三区在线观看 | 久久经典免费视频| 亚洲AV无码精品国产成人| 亚洲男人第一无码aⅴ网站| 免费h视频在线观看| 国产亚洲精aa在线看| 久久精品国产亚洲Aⅴ蜜臀色欲| 免费精品一区二区三区第35| 99999久久久久久亚洲| 亚洲乱码中文字幕手机在线| 在线免费中文字幕| 免费人成又黄又爽的视频在线电影| 亚洲成AV人片天堂网无码|