注意:将applicationContext.xml拆分出一个applicationContext-dao.xml,此文件中只配置dao

<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 1 加载properties文件 --> <context:property-placeholder location="classpath:jdbcinfo.properties"/> <!-- 2 配置数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> <property name="user" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!-- 3 配置hibernate SessionFactory 注意:这里使用的是hibernate5,要看你使用的jar包是什么版本的,每个人不一样。--> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <!-- 3.1 配置数据源 * <property name="属性名" ref="另一个bean引用"> name 必须是对象的setter方法推断获得,setDataSource(...), 去掉set DataSource ,首字母小写 dataSource * ref 其他bean引用 <bean id=""> 可以任意,一般情况与上面属性名称相同。 --> <property name="dataSource" ref="dataSource"></property> <!-- 3.2 配置hibernate其他属性 * 在hibernate.cfg.xml 配置文件 “hibernate.dialect” 和 “dialect” 等效的 * 在spring配置文件中,必须使用“hibernate.dialect” --> <property name="hibernateProperties"> <props> <!-- 方言 --> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> <!-- 显示sql语句 --> <prop key="hibernate.show_sql">true</prop> <!-- 式化sql语句 --> <prop key="hibernate.format_sql">true</prop> </props> </property> <!-- 3.3 加载映射文件 com/wuhao/ssh_dao/Student/domain/Student.hbm.xml com/wuhao/ssh_dao/xxx/domain/xxx.hbm.xml com/wuhao/ssh_dao/*/domain/*.hbm.xml <property name="mappingLocations" value="classpath:com/wuhao/ssh_dao/domain/*.hbm.xml"></property> --> <property name="mappingLocations" value="classpath:com/wuhao/ssh_dao/domain/Student.hbm.xml"></property> </bean> <!-- hibernate中通过sessionFactory创建得session对对象进行操作,spring提供一个hibernateTemplate进行操作,跟spring中的jdbcTemplate一样, 继承HibernateDaoSupport后,注入SessionFactory即可,在dao层就不用在写hibernateTemplate的set方法了。 --> <bean id="studentDao" class="com.wuhao.ssh_dao.dao.impl.StudentDaoImpl"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> </beans>
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-60366-2.html
豆子都是找大口塑料瓶子装着