• HibernateDaoSupport类的底层中hql操作使用


    spring的ApplicationContex.xml 中配置 sql 查询方法:

    加载数据源的两种方式:

    <!--方式一:使用 c3p0 连接池 加载数据源  -->
    	<bean id="propertyConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">	
    		<property name="locations">   
                <list>
                    <value>/WEB-INF/jdbc.properties</value>
                </list>
            </property>
    	</bean>
    	<!--c3p0 连接池-->
    	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    		<property name="driverClass">
    			<value>${hibernate.connection.driver_class}</value>
    		</property>
    		<property name="jdbcUrl">
    			<value>${hibernate.connection.url}</value>
    		</property>
    			<property name="user">
    		 		<value>${hibernate.connection.username}</value>
    			</property>
    		<property name="password">
    			<value>${hibernate.connection.password}</value>
    		</property>
    		
    		<!-- 连接池中保留的最小连接数. -->
    		<property name="minPoolSize">
    			<value>5</value>
    		</property>
    		<!-- 连接池中保留的最大连接数。Default: 15 -->
    		<property name="maxPoolSize">
    		 <value>100</value>
    		</property>
    		<!-- 初始化时获得的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
    		<property name="initialPoolSize">
    		 <value>5</value>
    		</property>
    		<!-- 最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
    		<property name="maxIdleTime">
    		 <value>60</value>
    		</property>
    		<!-- 当连接池中的连接耗尽的时候c3p0一次同时获得的连接数。Default: 3 -->
    		<property name="acquireIncrement">
    		 <value>3</value>
    		</property>
    		
    		<!-- JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements
    		  属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。
    		  假如maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->
    		<property name="maxStatements">
    			<value>0</value>
    		</property>
    
    		<!-- 每60秒检查所有连接池中的空闲连接。Default: 0 -->		
    		<property name="idleConnectionTestPeriod">
    			<value>60</value>
    		</property>
    		
    		<!-- 定义在从数据库获得新连接失败后反复尝试的次数。Default: 30 -->
    		<property name="acquireRetryAttempts">
    			<value>30</value>
    		</property>
    		
    		<!-- 获得连接失败将会引起所有等待连接池来获得连接的线程抛出异常。但是数据源仍有效
    		  保留,并在下次调用getConnection()的时候继续尝试获得连接。假如设为true,那么在尝试
    		  获得连接失败后该数据源将申明已断开并永久关闭。Default: false-->
    		<property name="breakAfterAcquireFailure">
    			<value>true</value>
    		</property>
    		
    		<!-- 因性能消耗大请只在需要的时候使用它。假如设为true那么在每个connection提交的
    		  时候都将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable		
    		  等方法来提升连接测试的性能。Default: false -->
    		<property name="testConnectionOnCheckout">
    			<value>false</value>
    		</property>
    	</bean>
    
    	<!--create sessionFactory-->
    	<bean id="sessionFactory"  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    		<property name="dataSource" ref="dataSource"></property>
    		<!-- config hibernate Field -->
    		<property name="hibernateProperties">
    		<props>
    			<prop key="hibernate.dialect"> ${hibernate.dialect}</prop>
    			<prop key="hibernate.generate_statistics">true</prop>
    			<prop key="hibernate.jdbc.fetch_size"> ${hibernate.jdbc.fetch_size}</prop>
    			<prop key="hibernate.jdbc.batch_size"> ${hibernate.jdbc.batch_size}</prop>
                        <prop key="hibernate.current_session_context_class">thread</prop>
                        <prop key="hibernate.bytecode.use_reflection_optimizer">false</prop>
                        <prop key="hibernate.query.substitutions">true</prop>
                        <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                        <prop key="hibernate.max_fetch_depth">3</prop> 
                        <prop key="hibernate.hibernate.use_outer_join">true</prop> 
                        <prop key="hibernate.jdbc.batch_size">50</prop>
                    
                        <!-- prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
                        <prop key="hibernate.cache.use_second_level_cache">true</prop>                                              
                        <prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}</prop -->
                    
                        <!-- 自动增加新表,重新启动服务时执行该命令,应用时务必注释此句(本机使用)  -->
                        <!-- prop key="hibernate.hbm2ddl.auto">update</prop -->
    		</props>
    		</property>
    		<!--mapping hibernate model po class-->
    		<property name="mappingDirectoryLocations">
    			<list>
    				<value>/WEB-INF/classes/com/pojo/po</value>				
    			</list>
    		</property>
    		
    		<!--  object have  same identity-->
    		<property name="eventListeners">
    			<map>
    				<entry key="merge">
    					<bean class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener"/>
    				</entry>
    			</map>
    		</property>
    		<!-- clob blob -->
    		<property name="lobHandler" ref="myHandler"></property>
    	</bean>
    	
    	<!-- hql 的查询注入,xml方式注解,service注入到此 -->
    	<bean id="baseDao" class="com.future.cms.daoimpl.BaseDaoImpl">
    		<property name="sessionFactory" ref="sessionFactory"></property>
    	</bean>
    
    dbcp 数据源配置,单连接 
    <!--方式二: dbcp 数据源配置,单连接 -->
    	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
    		<property name="url" value="jdbc:mysql://localhost:3306/police" />
    		<property name="username" value="root" />
    		<property name="password" value="root" />
    	</bean>

    底层的dao 要继承 HibernateDaoSupport 

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

    class  BaseDaoImpl extends HibernateDaoSupport implements BaseDao 
    

    常用使用方法:

    		this.getHibernateTemplate().save(obj);
    		this.getHibernateTemplate().merge(obj);
    		this.getHibernateTemplate().saveOrUpdateAll(entities);
    		this.getHibernateTemplate().delete(object); //
    		this.getHibernateTemplate().deleteAll(entities); //Collection entities
    		this.getHibernateTemplate().setCacheQueries(true); //查询设置二级缓存
    		list =this.getHibernateTemplate().find(hql, paramValues); //String hql, Object[] paramValues
    		obj = this.getHibernateTemplate().get(c, serializable);
    		obj = this.getHibernateTemplate().load(c, serializable);
    

     

    1、 继承了HibernateDaoSupport类的类获取session时,已不可用SessionFactory.OpenSessioon的形式来获 取Session了,由于HibernateDaoSupport本身已有获取session的方法getSession(),所以直接用Session se=this.getSession();来获取,

    2、在依据hql获取用户信息时,继承了HibernateDaoSupport类的类中不能在使用Query类了,而是用List<Ssh> list = this.getHibernateTemplate().find(hql);形式来获取实体类集合

     

      

      

  • 相关阅读:
    处理MySQL的ibdata1文件过大问题
    关于mysql启动问题---mysqld_safe mysqld from pid file * ended
    mysql数据库指定ip远程访问
    关于access_log 日志文件以及ip、uv和pv的定义
    nginx配置及内核优化详解
    LN : leetcode 746 Min Cost Climbing Stairs
    LN : leetcode 684 Redundant Connection
    LN : leetcode 730 Count Different Palindromic Subsequences
    LN : leetcode 516 Longest Palindromic Subsequence
    LN : leetcode 215 Kth Largest Element in an Array
  • 原文地址:https://www.cnblogs.com/estellez/p/4569135.html
Copyright © 2020-2023  润新知