本文配置是建立在criteria完整配置使用经验2(集成配置)基础上进行基础的。今天主要来说说关于criteria的配置如何集成更方便使用。
一、hibernate.cfg.xml和hibernate.cfg.xml的集成配置
前文说到使用criteria就一定要用到hibernate.cfg.xml和hibernate.cfg.xml两个文件。但是实际上我们项目的里实体类的可达几十个,总不能一个一个配置过去吧,所以这里考虑将hibernate.cfg.xml和hibernate.cfg.xml两个文件集成配置到application,如下所示:
<!-- 数据库连接池 -->
<bean id="dataSourceForFact" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="oracle.jdbc.driver.OracleDriver" />
<property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:orcl" />
<property name="user" value="scott" />
<property name="password" value="oracle" />
<property name="acquireIncrement" value="3" />
<property name="initialPoolSize" value="3" />
<property name="minPoolSize" value="2" />
<property name="maxPoolSize" value="75" />
<property name="maxIdleTime" value="600" />
<property name="idleConnectionTestPeriod" value="900" />
<property name="maxStatements" value="100" />
<property name="numHelperThreads" value="10" />
</bean>
<!-- hibernate -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<!-- 集成hibernate.cfg.xml -->
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop>
<prop key="hibernate.jdbc.fetch_size">${hibernate.jdbc.fetch_size}</prop>
<prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
<prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}</prop>
<prop key="javax.persistence.validation.mode">${javax.persistence.validation.mode}</prop>
<prop key="hibernate.connection.SetBigStringTryClob">true</prop>
</props>
</property>
<!-- 集成hbm.xml ,这里通过注解扫描实体类,而不用在hbm.xml中申明实体类了-->
<property name="packagesToScan" value="ffcs.cn" />
</bean>
调用:
Resource resource = new ClassPathResource("ffcs/cn/system/applicationContext.xml");
BeanFactory factory = new XmlBeanFactory(resource);
SessionFactory sessionFactory = (SessionFactory) factory.getBean("sessionFactory");
Session s = sessionFactory.openSession();
s.beginTransaction();
Criteria c = s.createCriteria(Product.class);
二、 使用工厂模式创建sessionFactory‘
如上面所示,我们每次使用criteria都需要重新创建sessionFactory,封装性较差,所以这里可以考虑使用工厂模式创建sessionFactory。
- xml配置
由于使用时候报错,提示需要用到事务的支持,所以这里配置下事务
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
<property name="dataSource" ref="dataSourceForFact" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
- 父类设置
package ffcs.cn.common.dao;
import javax.annotation.Resource;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
@Component
@Transactional
public class SupperDao {
@Autowired
protected SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public Session getSession() {
System.out.println(sessionFactory);
return sessionFactory.getCurrentSession();
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public SupperDao(final SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public SupperDao() {
super();
}
}
- 子类调用
package ffcs.cn.peam.card.dao;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;
import ffcs.cn.common.dao.SupperDao;
import ffcs.cn.peam.card.domain.Card;
@SuppressWarnings("unchecked")
@Repository
public class CardDao extends SupperDao {
public String getdata() {
Session session = this.getSession();
Criteria c = session.createCriteria(Card.class);
List<Card> ps = c.list();
for (Card p : ps) {
System.out.println(p.getUser());
}
return null;
}
}