1 将hibernate集成到spring里去,并使用注释的方式的一种配置如下:
<?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"> <context:annotation-config/> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="org.gjt.mm.mysql.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&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> <!-- 下面是用hiberanate的二级缓存的配置,先生成sessionFactory,再将数据源(上面配好的),注入到这里的name=dataSource这里来--> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- 下面完成hiberate的配置映射文件,它可以有多条的映射文件列表 --> <property name="mappingResources"> <list> <value>bean/person/Customer.hbm.xml</value> </list> </property> <!-- 下面是对hibernate的属性的设置 --> <property name="hibernateProperties"> <value> hibernate.dialect=org.hibernate.dialect.MySQL5Dialect hibernate.hbm2ddl.auto=update hibernate.show_sql=true hibernate.format_sql=true hibernate.cache.use_second_level_cache=true<!-- 使用二级缓存 --> hibernate.cache.use_query_cache=false hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider </value> </property> </bean> <!-- 下面是配置事务管理器 --> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/><!-- 将上面的(ref="sessionFactory")交由事务管理器(name="sessionFactory")来管理 --> </bean> <!-- 下面是打开注解的支持,并设置了事务管理器 (上面配置的事务管理器)--> <tx:annotation-driven transaction-manager="txManager"/> <!-- 下面将bean交给spring容器管理,就可以使用依赖注入的方式来引用bean --> <bean id="customerService" class="service.impl.CustomerServiceBean"/> </beans>
2 将实体类的每个hibernate配置文件放到实体类的同一包下的spring配置如下:
<?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"> <context:annotation-config/> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="org.gjt.mm.mysql.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&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> <!-- 下面是用hiberanate的二级缓存的配置,先生成sessionFactory,再将数据源(上面配好的),注入到这里的name=dataSource这里来--> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- 下面完成hiberate的配置映射文件,它可以有多条的映射文件列表 --> <property name="annotatedClasses"> <list> <value>entity.Student</value> </list> </property> <!-- 下面是对hibernate的属性的设置 --> <property name="hibernateProperties"> <value> hibernate.dialect=org.hibernate.dialect.MySQL5Dialect hibernate.hbm2ddl.auto=update hibernate.show_sql=true hibernate.format_sql=true hibernate.cache.use_second_level_cache=true<!-- 使用二级缓存 --> hibernate.cache.use_query_cache=false hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider </value> </property> </bean> <!-- 下面是配置事务管理器 --> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/><!-- 将上面的(ref="sessionFactory")交由事务管理器(name="sessionFactory")来管理 --> </bean> <!-- 下面是打开注解的支持,并设置了事务管理器 (上面配置的事务管理器)--> <tx:annotation-driven transaction-manager="txManager"/> <!-- 下面将bean交给spring容器管理,就可以使用依赖注入的方式来引用bean --> <!-- 注入时用的是接口,而这里会指定接口所用的实现类 --> <bean id="studentServer" class="server.impl.StudentServerImpl"/> </beans>
这里需要注意的是当用java项目来运行spring及hibernate的集成来操作数据库时,要用下面的方式来获取server,放入服务器就由服务器来完成,直接用注释来做就行了。
//实例化spring容器,这里不用@Resource注解来做,是因为这里没放到服务器就可以运行,当把项目放入到服务器中,那就可以直接用注解的简便方式来引用spring配好的Bean
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
//用容器获取一个bean,这些bean要在配置文件里有的
studentServer = (StudentServer)applicationContext.getBean("studentServer");