yxhxj2006

          常用鏈接

          統計

          最新評論

          #

          Hibernate實現有兩種配置,xml配置與注釋配置

               摘要: hibernate實現有兩種配置,xml配置與注釋配置。 (1):xml配置:hibernate.cfg.xml (放到src目錄下)和實體配置類:xxx.hbm.xml(與實體為同一目錄中) <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC  &nbs...  閱讀全文

          posted @ 2012-06-30 10:00 奮斗成就男人 閱讀(49848) | 評論 (4)編輯 收藏

          hibernate查詢方法對比

          HQL查詢

          HQL是hibernate自己的一套查詢語言,于SQL語法不同,具有跨數據庫的優點。示例代碼:

          [java] 
          static void query(String name){ 
            Session s=null; 
            try{ 
             s=HibernateUtil.getSession(); 
              
             //from后面是對象,不是表名 
             String hql="from Admin as admin where admin.aname=:name";//使用命名參數,推薦使用,易讀。 
             Query query=s.createQuery(hql); 
             query.setString("name", name); 
              
             List<Admin> list=query.list(); 
              
             for(Admin admin:list){ 
              System.out.println(admin.getAname()); 
             } 
            }finally{ 
             if(s!=null) 
             s.close(); 
            } 
           } 
          適用情況:常用方法,比較傳統,類似jdbc。缺點:新的查詢語言,適用面有限,僅適用于Hibernate框架。


          對象化查詢Criteria方法:

          [java] 
          static void cri(String name,String password){ 
            Session s=null; 
            try{ 
             s=HibernateUtil.getSession(); 
              
             Criteria c=s.createCriteria(Admin.class); 
             c.add(Restrictions.eq("aname",name));//eq是等于,gt是大于,lt是小于,or是或 
             c.add(Restrictions.eq("apassword", password)); 
              
             List<Admin> list=c.list(); 
             for(Admin admin:list){ 
              System.out.println(admin.getAname()); 
             } 
            }finally{ 
             if(s!=null) 
             s.close(); 
            } 
           } 
          適用情況:面向對象操作,革新了以前的數據庫操作方式,易讀。缺點:適用面較HQL有限。


          動態分離查詢DetachedCriteria

          [java] 
          static List dc(DetachedCriteria dc) { 
           
            Session s = HibernateUtil.getSession(); 
            Criteria c = dc.getExecutableCriteria(s); 
            List rs = c.list(); 
            s.close(); 
            return rs; 
           } 

          [java]
          DetachedCriteria dc = DetachedCriteria.forClass(User.class); 
            int id = 1; 
            if (id != 0) 
             dc.add(Restrictions.eq("id", id)); 
            Date age = new Date(); 
            if (age != null) 
             dc.add(Restrictions.le("birthday", age)); 
            List users = dc(dc); 
            System.out.println("離線查詢返回結果:" + users); 

          適用情況:面向對象操作,分離業務與底層,不需要字段屬性攝入到Dao實現層。  缺點:適用面較HQL有限。


          例子查詢

          [java]
          static List example(User user) { 
            Session s = HibernateUtil.getSession(); 
            List<User> users = s.createCriteria(User.class).add( 
              Example.create(user)).list(); 
            // List<User> 
            // users2=s.createCriteria(User.class).add((Example.create(user)).ignoreCase()) 
            // .createCriteria("child").add((Example.create(user))).list(); 
            return users; 
           } 
          適用情況:面向對象操作。   缺點:適用面較HQL有限,不推薦。


          sql查詢

          [java]
          static List sql() { 
           
            Session s = HibernateUtil.getSession(); 
            Query q = s.createSQLQuery("select * from user").addEntity(User.class); 
            List<User> rs = q.list(); 
            s.close(); 
            return rs; 
           } 
          適用情況:不熟悉HQL的朋友,又不打算轉數據庫平臺的朋友,萬能方法   缺點:破壞跨平臺,不易維護,不面向對象。


          命名查詢

          [java]
          static List namedQuery(int id) { 
            Session s = HibernateUtil.getSession(); 
            Query q = s.getNamedQuery("getUserById"); 
            q.setInteger("id", id); 
            return q.list(); 
           } 

          [html]
          <?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> 
              <class name="com.sy.vo.User" table="user" catalog="news"> 
               
            
           
              </class> 
              <!-- 命名查詢:定義查詢條件 --> 
              <query name="getUserById"> 
               <![CDATA[from User where id=:id]]> 
              </query> 
              <!-- 命名查詢中使用sql,不推薦使用,影響跨數據庫 
              <sql-query name="getUserById2"> 
               <![CDATA[select * from User where ]]> 
              </sql-query> --> 
          </hibernate-mapping>

          posted @ 2012-06-29 13:34 奮斗成就男人 閱讀(906) | 評論 (0)編輯 收藏

          Spring配置文件總結

          首先來看一個標準的Spring配置文件 applicationContext.xml

          <?xml version="1.0" encoding="UTF-8"?>
          <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
              http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
              http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
           default-autowire="byName" default-lazy-init="true">

           <!-- 配置數據源 -->
           <bean id="dataSource"
            class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName">
             <value>com.mysql.jdbc.Driver</value>
            </property>
            <property name="url">
             <value>
              jdbc:mysql://localhost/ssh?characterEncoding=utf-8
             </value>
            </property>
            <property name="username">
             <value>root</value>
            </property>
            <property name="password">
             <value>123</value>
            </property>
           </bean>

           <!--配置SessionFactory -->
           <bean id="sessionFactory"
            class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <property name="dataSource">
             <ref bean="dataSource" />
            </property>
            <property name="mappingResources">
             <list>
              <value>com/ssh/pojo/User.hbm.xml</value>
             </list>
            </property>
            <property name="hibernateProperties">
             <props>
              <prop key="hibernate.show_sql">true</prop>
             </props>
            </property>
           </bean>
           
           <!-- 事務管理 -->
           <bean id="transactionManager"
            class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory">
             <ref bean="sessionFactory" />
            </property>
           </bean>
           
           <!-- hibernateTemplate -->
           <bean id="hibernateTemplate"
            class="org.springframework.orm.hibernate3.HibernateTemplate">
            <property name="sessionFactory">
             <ref bean="sessionFactory" />
            </property>
           </bean>

           <!-- 配置數據持久層 -->
           <bean id="userDao"
            class="com.ssh.dao.impl.UserDaoImpl">
            <property name="hibernateTemplate" ref="hibernateTemplate"></property>
           </bean>
           

           <!-- 配置業務邏輯層 -->
           <bean id="userService"
            class="com.ssh.service.impl.UserServiceImpl">
            <property name="userDao" ref="userDao"></property>
           </bean>
           

           <!-- 配置控制層 -->
           <bean id="UserAction"
            class="com.ssh.action.UserAction"  scope="prototype">
            <property name="userService" ref="userService"></property>
           </bean>
            <!-- 配置pojo -->
           <bean id="User" class="com.ssh.pojo.User" scope="prototype"/>
          </beans>

           

          ////////////////////////////////////////////////     下面是詳解:  ////////////////////////////////////////////////////////////////////////

          1.基本配置:
          <?xml version="1.0" encoding="UTF-8"?>
          <beans
           xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
                              http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                              http://www.springframework.org/schema/context
                              http://www.springframework.org/schema/context/spring-context-2.5.xsd
                              ">


          <context:component-scan base-package="com.persia">
          <!-- 開啟組件掃描 -->
          </context:component-scan>

          <context:annotation-config>
          <!--開啟注解處理器-->
          </context:annotation-config>

          <!-- 使用注解,省去了propertity的xml配置,減少xml文件大小 -->
          <bean id="personServiceAnno" class="com.persia.PersonServiceAnnotation"></bean>
          <bean id="personDaoBeanAnno" class="com.persia.PersonDaoBean"></bean>
          <bean id="personDaoBeanAnno2" class="com.persia.PersonDaoBean"></bean>

          <!-- 自動注解 -->
          <bean id="personServiceAutoInject" class="com.persia.PersonServiceAutoInject" autowire="byName"></bean>


          <bean id="personService" class="com.persia.PersonServiceBean">
          <!-- 由spring容器去創建和維護,我們只要獲取就可以了 -->
          </bean>

          <bean id="personService2" class="com.persia.PersonServiceBeanFactory" factory-method="createInstance" lazy-init="true"
                init-method="init"  destroy-method="destory">
          <!-- 靜態工廠獲取bean -->
          </bean>

          <bean id="fac" class="com.persia.PersonServiceBeanInsFactory"></bean>
          <bean id="personService3" factory-bean="fac" factory-method="createInstance" scope="prototype">
          <!-- 實例工廠獲取bean,先實例化工廠再實例化bean-->
          </bean>


          <!-- ref方式注入屬性 -->
          <bean id="personDao" class="com.persia.PersonDaoBean"></bean>
          <bean id="personService4" class="com.persia.PersonServiceBean">
            <property name="personDao" ref="personDao"></property>
          </bean>

          <!-- 內部bean方式注入 -->
          <bean id="personService5" class="com.persia.PersonServiceBean">
            <property name="personDao">
               <bean class="com.persia.PersonDaoBean"></bean>
            </property>
            <property name="name" value="persia"></property>
            <property name="age" value="21"></property>
           
            <property name="sets">
              <!-- 集合的注入 -->
               <set>
                 <value>第一個</value>
                 <value>第二個</value>
                 <value>第三個</value>
               </set>
            </property>
           
            <property name="lists">
              <!-- 集合的注入 -->
              <list>
                  <value>第一個l</value>
                 <value>第二個l</value>
                 <value>第三個l</value>
              </list>
             
            </property>
           
            <property name="properties">
              <props>
                <prop key="key1">value1</prop>
                <prop key="key2">value2</prop>
                <prop key="key3">value3</prop>
              </props>
            </property>
           
            <property name="map">
             <map>
                <entry key="key1" value="value-1"></entry>
                <entry key="key2" value="value-2"></entry>
                <entry key="key3" value="value-3"></entry>
             </map>
            </property>
          </bean>

          <bean id="personService6" class="com.persia.PersonServiceBean">
             <constructor-arg index="0" value="構造注入的name" ></constructor-arg>
             <!-- 基本類型可以不寫type -->
             <constructor-arg index="1" type="com.persia.IDaoBean" ref="personDao">
             </constructor-arg>
          </bean>

          </beans>2.開啟AOP:
          <?xml version="1.0" encoding="UTF-8"?>
          <beans
           xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
                              http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                               http://www.springframework.org/schema/aop
                              http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                              http://www.springframework.org/schema/context
                              http://www.springframework.org/schema/context/spring-context-2.5.xsd
                             ">

          <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
          <bean id="myInterceptor" class="com.persia.service.MyInterceptor"></bean>
          <bean id="personServiceImpl" class="com.persia.service.impl.PersonServiceImpl"></bean>
          </beans>AOP的xml版本<?xml version="1.0" encoding="UTF-8"?>
          <beans
           xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
                              http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                               http://www.springframework.org/schema/aop
                              http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                              http://www.springframework.org/schema/context
                              http://www.springframework.org/schema/context/spring-context-2.5.xsd
                             ">

          <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

          <bean id="personService" class="com.persia.service.impl.PersonServiceImpl"></bean>
          <bean id="aspectBean" class="com.persia.service.MyInterceptor"></bean>

          <aop:config>
           <aop:aspect id="myaop" ref="aspectBean">
           <aop:pointcut id="mycut" expression="execution(* com.persia.service.impl.PersonServiceImpl.*(..))"/>
           <aop:pointcut id="argcut" expression="execution(* com.persia.service.impl.PersonServiceImpl.*(..)) and args(name)"/> 
           <aop:before pointcut-ref="mycut" method="doAccessCheck"  />
           <aop:after-returning pointcut-ref="mycut" method="doAfterReturning"/>
             <aop:after-throwing pointcut-ref="mycut" method="doThrowing"/>
             <aop:after pointcut-ref="argcut" method="doAfter" arg-names="name"/>
           <aop:around pointcut-ref="mycut" method="arround"/>
           </aop:aspect>
           
          </aop:config>

          </beans>3.開啟事務和注解:
          <?xml version="1.0" encoding="UTF-8"?>
          <beans
           xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
                              http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                              http://www.springframework.org/schema/context
                              http://www.springframework.org/schema/context/spring-context-2.5.xsd
                              http://www.springframework.org/schema/aop
                              http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                              http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
                             ">

          <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
                            
          <!-- 配置數據源 -->  
            <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
              <property name="driverClassName" value="com.mysql.jdbc.Driver"/>  
              <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=utf-8"/>  
              <property name="username" value="root"/>  
              <property name="password" value=""/>  
               <!-- 連接池啟動時的初始值 -->  
               <property name="initialSize" value="1"/>  
               <!-- 連接池的最大值 -->  
               <property name="maxActive" value="500"/>  
               <!-- 最大空閑值.當經過一個高峰時間后,連接池可以慢慢將已經用不到的連接慢慢釋放一部分,一直減少到maxIdle為止 -->  
               <property name="maxIdle" value="2"/>  
               <!--  最小空閑值.當空閑的連接數少于閥值時,連接池就會預申請去一些連接,以免洪峰來時來不及申請 -->  
               <property name="minIdle" value="1"/>  
            </bean> 
            
            <!-- 配置事務管理器-->  
           <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
              <property name="dataSource" ref="dataSource"/>  
            </bean> 
            <!-- 配置業務bean -->
              <bean id="personService" class="com.persia.service.impl.PersonServiceImpl">
              <property name="ds" ref="dataSource"></property>
            </bean>
            
            <!-- 采用@Transactional注解方式來使用事務 -->  
            <tx:annotation-driven transaction-manager="txManager"/> 


          </beans>XML版本:

          <?xml version="1.0" encoding="UTF-8"?>
          <beans
           xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
                              http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                              http://www.springframework.org/schema/context
                              http://www.springframework.org/schema/context/spring-context-2.5.xsd
                              http://www.springframework.org/schema/aop
                              http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                              http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
                             ">

          <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
                            
          <!-- 配置數據源 -->  
            <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
              <property name="driverClassName" value="com.mysql.jdbc.Driver"/>  
              <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=utf-8"/>  
              <property name="username" value="root"/>  
              <property name="password" value=""/>  
               <!-- 連接池啟動時的初始值 -->  
               <property name="initialSize" value="1"/>  
               <!-- 連接池的最大值 -->  
               <property name="maxActive" value="500"/>  
               <!-- 最大空閑值.當經過一個高峰時間后,連接池可以慢慢將已經用不到的連接慢慢釋放一部分,一直減少到maxIdle為止 -->  
               <property name="maxIdle" value="2"/>  
               <!--  最小空閑值.當空閑的連接數少于閥值時,連接池就會預申請去一些連接,以免洪峰來時來不及申請 -->  
               <property name="minIdle" value="1"/>  
            </bean> 
            
          <!-- 配置事務管理器 -->
           <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
              <property name="dataSource" ref="dataSource"/>  
            </bean> 
            <!-- 配置業務bean -->
             <bean id="personService" class="com.persia.service.impl.PersonServiceImpl">
              <property name="ds" ref="dataSource"></property>
            </bean>
           
           
              <!-- 使用XML來使用事務管理--> 
          <aop:config> 
              <!-- 配置一個切面,和需要攔截的類和方法 -->  
              <aop:pointcut id="transactionPointcut" expression="execution(* com.persia.service..*.*(..))"/> 
              <aop:advisor advice-ref="txAdvice" pointcut-ref="transactionPointcut"/> 
          </aop:config>
          <!-- 配置一個事務通知 -->   
          <tx:advice id="txAdvice" transaction-manager="txManager"> 
                <tx:attributes>
                <!-- 方法以get開頭的,不使用事務 -->
                  <tx:method name="get*" read-only="true" propagation="NOT_SUPPORTED"/>
                <!-- 其他方法以默認事務進行 -->
                  <tx:method name="*"/> 
                </tx:attributes> 
          </tx:advice> 
            
           
          </beans>4.SSH:
          <?xml version="1.0" encoding="UTF-8"?>
          <beans
           xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
                              http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                              http://www.springframework.org/schema/context
                              http://www.springframework.org/schema/context/spring-context-2.5.xsd
                              http://www.springframework.org/schema/aop
                              http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                              http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
                             ">


           <!-- 配置數據源 -->  
            <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
              <property name="driverClassName" value="com.mysql.jdbc.Driver"/>  
              <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=utf-8"/>  
              <property name="username" value="root"/>  
              <property name="password" value=""/>  
               <!-- 連接池啟動時的初始值 -->  
               <property name="initialSize" value="1"/>  
               <!-- 連接池的最大值 -->  
               <property name="maxActive" value="500"/>  
               <!-- 最大空閑值.當經過一個高峰時間后,連接池可以慢慢將已經用不到的連接慢慢釋放一部分,一直減少到maxIdle為止 -->  
               <property name="maxIdle" value="2"/>  
               <!--  最小空閑值.當空閑的連接數少于閥值時,連接池就會預申請去一些連接,以免洪峰來時來不及申請 -->  
               <property name="minIdle" value="1"/>  
            </bean> 
           
            <!-- 配置hibernate的sessionFactory -->
          <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
           <property name="dataSource"><ref bean="dataSource" /></property>
            <property name="mappingResources">
                <list>
                  <value>com/persia/model/Person.hbm.xml</value>
                </list>
             </property>
            
               <!-- 1.首先在sessionFactory里面配置以上3條設置 -->
                  <!-- 2.然后得在類路徑下面添加一個ehcache.xml的緩存配置文件 -->
                  <!-- 3.最后在要使用緩存的實體bean的映射文件里面配置緩存設置 -->
                       <!--使用二級緩存-->
                       <!-- 不使用查詢緩存,因為命中率不是很高 -->
                       <!-- 使用Ehcache緩存產品 --> 
            <property name="hibernateProperties">
                <value>
                    hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
                    hibernate.hbm2ddl.auto=update
                    hibernate.show_sql=false
                    hibernate.format_sql=false
                    hibernate.cache.use_second_level_cache=true
                          hibernate.cache.use_query_cache=false
                       hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
                </value>
                </property>
          </bean>

          <!-- 配置Spring針對hibernate的事務管理器 -->
          <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
              <property name="sessionFactory" ref="sessionFactory"/>
          </bean>

          <!-- 配置使用注解的方式來使用事務 --> 
          <tx:annotation-driven transaction-manager="txManager"/>

          <!-- 使用手工配置的注解方式來注入bean -->
          <context:annotation-config></context:annotation-config>

          <!--定義要注入的業務bean -->
          <bean id="personService" class="com.persia.service.impl.PersonServiceImpl"></bean>

          <!--將Struts的action交給Spring容器來管理 -->
          <bean name="/person/list" class="com.persia.struts.PersonListAction">
          <!--1.這里要求name和struts-config里面的action的path名稱一致,因為id不允許有特殊字符-->
          <!--2.還得在Struts-config文件里面添加Spring的請求處理器,該處理器會根據action的path屬性到Spring容器里面尋找這個bean,若找到了則用這個bean來處理用戶的請求-->
          <!--3.然后去掉action的type標簽和值(可選),當Spring處理器找不到該bean時,才會使用Struts的action-->
          <!--4.最后在action里面使用Spring的注入方式來注入業務bean-->
          </bean>

          <bean name="/person/manage" class="com.persia.struts.PersonManageAction"></bean>
          </beans>5.SSH2:
          <?xml version="1.0" encoding="UTF-8"?>
          <beans
           xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
                              http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                              http://www.springframework.org/schema/context
                              http://www.springframework.org/schema/context/spring-context-2.5.xsd
                              http://www.springframework.org/schema/aop
                              http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                              http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
                             ">


           <!-- 配置數據源 -->  
            <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
              <property name="driverClassName" value="com.mysql.jdbc.Driver"/>  
              <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=utf-8"/>  
              <property name="username" value="root"/>  
              <property name="password" value=""/>  
               <!-- 連接池啟動時的初始值 -->  
               <property name="initialSize" value="1"/>  
               <!-- 連接池的最大值 -->  
               <property name="maxActive" value="500"/>  
               <!-- 最大空閑值.當經過一個高峰時間后,連接池可以慢慢將已經用不到的連接慢慢釋放一部分,一直減少到maxIdle為止 -->  
               <property name="maxIdle" value="2"/>  
               <!--  最小空閑值.當空閑的連接數少于閥值時,連接池就會預申請去一些連接,以免洪峰來時來不及申請 -->  
               <property name="minIdle" value="1"/>  
            </bean> 
           
            <!-- 配置hibernate的sessionFactory -->
          <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
           <property name="dataSource"><ref bean="dataSource" /></property>
            <property name="mappingResources">
                <list>
                  <value>com/persia/model/Person.hbm.xml</value>
                </list>
             </property>
            
               <!-- 1.首先在sessionFactory里面配置以上3條設置 -->
                  <!-- 2.然后得在類路徑下面添加一個ehcache.xml的緩存配置文件 -->
                  <!-- 3.最后在要使用緩存的實體bean的映射文件里面配置緩存設置 -->
                       <!--使用二級緩存-->
                       <!-- 不使用查詢緩存,因為命中率不是很高 -->
                       <!-- 使用Ehcache緩存產品 --> 
            <property name="hibernateProperties">
                <value>
                    hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
                    hibernate.hbm2ddl.auto=update
                    hibernate.show_sql=false
                    hibernate.format_sql=false
                    hibernate.cache.use_second_level_cache=true
                          hibernate.cache.use_query_cache=false
                       hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
                </value>
                </property>
          </bean>

          <!-- 配置Spring針對hibernate的事務管理器 -->
          <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
              <property name="sessionFactory" ref="sessionFactory"/>
          </bean>

          <!-- 配置使用注解的方式來使用事務 --> 
          <tx:annotation-driven transaction-manager="txManager"/>

          <!-- 使用手工配置的注解方式來注入bean -->
          <context:annotation-config></context:annotation-config>

          <!--定義要注入的業務bean -->
          <bean id="personService" class="com.persia.service.impl.PersonServiceImpl"></bean>

          <!--注入Struts 2的action -->
          <bean id="personList" class="com.persia.struts2.action.PersonListAction"></bean>
          </beans>6.SSJ:
          <?xml version="1.0" encoding="UTF-8"?>
          <beans
           xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
                              http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                              http://www.springframework.org/schema/context
                              http://www.springframework.org/schema/context/spring-context-2.5.xsd
                              http://www.springframework.org/schema/aop
                              http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                              http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
                             ">


          <!-- 使用手工配置的注解方式來注入bean -->
          <context:annotation-config></context:annotation-config>

          <!-- 1.配置Spring集成JPA -->
          <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
                <property name="persistenceUnitName" value="SpringJPAPU"/>
          </bean>

          <!--2.配置Spring針對JPA的事務 -->
              <bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
               <property name="entityManagerFactory" ref="entityManagerFactory"/>
          </bean>

          <!--3.開啟事務注解 -->
          <tx:annotation-driven transaction-manager="txManager"/>
           
          <!--以上3個Spring集成JPA的配置,在web項目先添加Spring支持,后添加JPA支持時會自動生成 -->

          <!-- 配置業務bean -->
          <bean id="personService" class="com.persia.service.impl.PersonServiceImpl"></bean>

          <!-- 配置Struts的action -->
          <bean name="/person/list" class="com.persia.struts.PersonListAction"/>
          <bean name="/person/manage" class="com.persia.struts.PersonManageAction"/>
          </beans>

           

          posted @ 2012-06-28 11:14 奮斗成就男人 閱讀(157) | 評論 (0)編輯 收藏

          web.xml 中的listener、 filter、servlet 加載順序及其詳解 .

               摘要: 在項目中總會遇到一些關于加載的優先級問題,近期也同樣遇到過類似的,所以自己查找資料總結了下,下面有些是轉載其他人的,畢竟人家寫的不錯,自己也就不重復造輪子了,只是略加點了自己的修飾。         首先可以肯定的是,加載順序與它們在 web.xml 文件中的先后順序無關。即不會因為 filter 寫在 listener 的前...  閱讀全文

          posted @ 2012-06-27 16:37 奮斗成就男人 閱讀(233) | 評論 (0)編輯 收藏

          HttpClient 學習整理

               摘要: HttpClient 是我最近想研究的東西,以前想過的一些應用沒能有很好的實現,發現這個開源項目之后就有點眉目了,令人頭痛的cookie問題還是有辦法解決滴。在網上整理了一些東西,寫得很好,寄放在這里。HTTP 協議可能是現在 Internet 上使用得最多、最重要的協議了,越來越多的 Java 應用程序需要直接通過 HTTP 協議來訪問網絡資源。雖然在 JDK 的 java.net 包中已經提供...  閱讀全文

          posted @ 2012-06-19 18:25 奮斗成就男人 閱讀(224) | 評論 (0)編輯 收藏

          java后臺用post方式提交參數

           public static void main(String[] args) {
            URL url = null;
            HttpURLConnection httpurlconnection = null;
            try {
             url = new URL("    httpurlconnection = (HttpURLConnection) url.openConnection();
             httpurlconnection.setDoInput(true);
             httpurlconnection.setDoOutput(true);

             httpurlconnection.setRequestMethod("POST");
             httpurlconnection.setRequestProperty("Content-Type",
               "application/x-www-form-urlencoded");

             String username = "ip=192.168.0.1";
             httpurlconnection.getOutputStream().write(username.getBytes());

             httpurlconnection.getOutputStream().flush();
             httpurlconnection.getOutputStream().close();
             int code = httpurlconnection.getResponseCode();
             System.out.println("code    " + code);

             if (code == 200) {

              String cookie = httpurlconnection.getHeaderField("Set-Cookie ");
              System.out.println(cookie);
              // httpurlconnection.setRequestProperty( "Cookie", cookie);

              DataInputStream in = new DataInputStream(httpurlconnection
                .getInputStream());
              int len = in.available();
              byte[] by = new byte[len];
              in.readFully(by);
              String rev = new String(by);
              System.out.println(rev);
              in.close();
             }
            } catch (Exception e) {
             e.printStackTrace();
            } finally {
             if (httpurlconnection != null) {
              httpurlconnection.disconnect();
             }
            }
           }

          posted @ 2012-06-19 18:20 奮斗成就男人 閱讀(3306) | 評論 (1)編輯 收藏

          僅列出標題
          共23頁: First 上一頁 15 16 17 18 19 20 21 22 23 
          主站蜘蛛池模板: 成武县| 于田县| 昭觉县| 太仓市| 梅河口市| 丹棱县| 西峡县| 县级市| 德惠市| 枞阳县| 浠水县| 堆龙德庆县| 桂东县| 香格里拉县| 延川县| 万山特区| 宜宾市| 光山县| 昔阳县| 闸北区| 托里县| 福贡县| 吉水县| 通州区| 浏阳市| 新干县| 巴青县| 绩溪县| 清远市| 深水埗区| 普格县| 开化县| 德格县| 伊金霍洛旗| 临澧县| 青海省| 丰原市| 昌吉市| 正安县| 惠来县| 桃源县|