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

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

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

    My-java-spark

    BlogJava 首頁 新隨筆 聯(lián)系 聚合 管理
      5 Posts :: 0 Stories :: 4 Comments :: 0 Trackbacks

       Spring為應用程序提供一個容器, 為應用程序的管理帶來了方便. 它與hibernate的結(jié)合, 形成一個完整的后臺體系, 也是當今應用開發(fā)流行的做法. 奮斗了一個晚上, 終于把hibernate3與spring整合了起來, hibernate2.x和hibernate3與spring的結(jié)合稍有不同, 關(guān)鍵是引入的spring的包的不同, 下面我會標識出來.

    Spring 的配置文件applicationContext.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "spring" "../../../lib/spring-beans.dtd" >
    <beans default-autowire="no" default-dependency-check="none" default-lazy-init="false">

    <!-- 
       配置數(shù)據(jù)源
       注意: 用org.apache.commons.dbcp.BasicDataSource, 要引入 apache commons 
       的commons-collections-3.1.jar, commons-dbcp-1.2.1.jar, commons-pool-1.2.jar三個包
     
    -->
     
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
      
    <property name="driverClassName">
       
    <value>org.gjt.mm.mysql.Driver</value>
      
    </property>
      
    <property name="url">
       
    <value>jdbc:mysql://localhost/sparkcrm</value>
      
    </property>
      
    <property name="username">
       
    <value>root</value>
      
    </property>
      
    <property name="password">
       
    <value>1111</value>
      
    </property>
     
    </bean>

     
    <!-- 配置sessionFactory, 注意這里引入的包的不同  -->
     
    <bean id="sessionFactory"
      class
    ="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
      
    <property name="dataSource">
       
    <ref local="dataSource" />
      
    </property>
      
    <property name="mappingResources">
       
    <list>
         
    <value>com/sparkcrm/schema/entities/Lead.hbm.xml</value>
        
    </list>
      
    </property>
      
    <property name="hibernateProperties">
       
    <props>
        
    <prop key="hibernate.dialect">
         org.hibernate.dialect.MySQLDialect
        
    </prop>
        
    <prop key="hibernate.show_sql">true</prop>
       
    </props>
      
    </property>
     
    </bean>
     
      <!-- 配置transactionManager, 注意這里引入的包的不同  -->
     
    <bean id="transactionManager" 
      class
    ="org.springframework.orm.hibernate3.HibernateTransactionManager">
      
    <property name="sessionFactory">
       
    <ref local="sessionFactory" />
      
    </property>
     
    </bean>

    <--事務代理在這里配置, 這里省略了 -->

     
    <bean id="leadDAO" class="com.sparkcrm.schema.dao.LeadDao">
      
    <property name="sessionFactory">
       
    <ref local="sessionFactory" />
      
    </property>
     
    </bean>

    </beans>



    一個示例的hibernate的映射文件

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC 
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
    >
        
    <hibernate-mapping package="com.sparkcrm.schema.entities">
          
       <!-- 我在這里用了hibernate的動態(tài)模型(dynamic models) , 沒用pojo-->
        
    <class entity-name="Lead" table="Lead">
            
    <id name="id" column="id" type="string">
                
    <generator class="uuid.hex"/>
            
    </id>
            
    <property name="companyName" type="string"/>
            
    <property name="topic" type="string"/>
            
    <property name="contactName" type="string"/>
        
    </class>
    </hibernate-mapping>


    DAO代碼:

    import java.util.Map;
    /**
       * DAO接口
       */
    public interface IDAO {

        String create(Map
    <String, Object> map);
        
        
    void update(Map<String, Object> map);
        
        Map
    <String, Object> delete(String id);
        
        boolean share(String id, String userId, 
    int rights);
        
        boolean assign(String id, String userId);
    }



    import java.util.Map;

    import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

    import com.sparkcrm.schema.IDAO;
    import com.sparkcrm.schema.Schema;
    import com.sparkcrm.schema.metadata.Lead;
    /**
        *一個示例的DAO實現(xiàn), 繼承HibernateDaoSupport, 用spring帶來的管理session等的便利
        */
    public
     class LeadDao extends HibernateDaoSupport implements IDAO {

        
    public String create(Map<String, Object> map) {
            getHibernateTemplate().saveOrUpdate(Schema.LEAD, map);
            
    return (String) map.get(Lead.ID);
        }


        public
     void update(Map<String, Object> map) {
            
        }


        public
     Map<String, Object> delete(String id) {
            
    return null;
        }


        public
     boolean share(String id, String userId, int rights) {
            
    return false;
        }


        public
     boolean assign(String id, String userId) {
            
    return false;
        }


    }

    示意性的測試代碼:

    import java.sql.Timestamp;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.Map;

    import junit.framework.TestCase;

    import org.springframework.context.support.ClassPathXmlApplicationContext;

    import com.sparkcrm.schema.IDAO;

    public class testLeadDAO extends TestCase {
        
        ClassPathXmlApplicationContext ctx 
    = null;
        
        
    public void setUp(){
            ctx 
    = new ClassPathXmlApplicationContext("applicationContext.xml");
        }

        
        
    public void testCreateLead(){
             IDAO leadDao 
    = (IDAO) ctx.getBean("leadDAO");

            Map
    <String, Object> map = new HashMap<String, Object>();
            map.put(
    "companyName""Spark Ltd.");
            map.put(
    "topic""This is a Good Lead!");
            map.put(
    "contactName""abcd");
            
            String id 
    = leadDao.create(map);
            System.
    out.println(id);
        }

    }

    posted on 2005-09-14 23:16 spark 閱讀(3764) 評論(1)  編輯  收藏

    評論

    # re: Hibernate3與spring的整合應用 2008-09-03 15:57
    中華民國  回復  更多評論
      


    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導航:
     
    主站蜘蛛池模板: 久久久久亚洲国产AV麻豆| 亚洲国产天堂在线观看| 亚洲AV无码一区二区乱子仑| 波多野结衣免费在线| 亚洲精品高清国产麻豆专区| 18禁黄网站禁片免费观看不卡 | 亚洲AV中文无码乱人伦在线视色| 亚洲heyzo专区无码综合| 国产无遮挡吃胸膜奶免费看视频| 亚洲AV无码男人的天堂| 免费A级毛片无码A∨男男| 一级a性色生活片久久无少妇一级婬片免费放 | 无码精品人妻一区二区三区免费| 亚洲国产综合精品中文字幕| 一本岛v免费不卡一二三区| 亚洲第一AAAAA片| 最近中文字幕免费完整| 亚洲黄页网在线观看| 日韩精品视频免费网址| 成人精品综合免费视频| 亚洲国产精品无码久久一线| 无码人妻丰满熟妇区免费| 亚洲国产福利精品一区二区| 蜜桃精品免费久久久久影院| 人人爽人人爽人人片A免费| 亚洲精品无码专区久久久| 免费国产99久久久香蕉| avtt天堂网手机版亚洲| 亚洲嫩草影院在线观看| a级黄色毛片免费播放视频| 亚洲卡一卡2卡三卡4卡无卡三| 久久福利资源网站免费看| 亚洲JIZZJIZZ妇女| 亚洲国产a∨无码中文777 | 国产精品美女自在线观看免费| 色吊丝性永久免费看码| 久久久久亚洲精品天堂| 免费v片在线观看无遮挡| 国产成人免费视频| 色欲aⅴ亚洲情无码AV蜜桃| 亚洲人成网7777777国产|