resources.prioperties文件中有
db.driverClass=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/数据库名
db.username=root
db.password=123
db.maxPoolSize=50
在spring配置文件中配置
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:resources.properties</value> </list> </property> </bean> 才能让spring认识resources.prioperties这个文件,以便后面的读取 <!-- 数据源配置,这里使用c3p0数据库连接池--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${db.driverClass}" /> <property name="jdbcUrl" value="${db.url}" /> <property name="user" value="${db.username}" /> <property name="password" value="${db.password}" /> <property name="maxPoolSize" value="${db.maxPoolSize}" /> </bean> spring读取resources.properties这个文件,进行数据源配置 <!-- 读取数据库配置,设置sessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <!-- 注入连接池对象 --> <property name="dataSource" ref="dataSource"></property> <!-- hibernate常用配置 --> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="current_session_context_class">thread</prop> </props> </property> <!-- hibernate映射配置 --> <property name="mappingLocations"> <list> <value>classpath:com/xxx/bean/user/*.hbm.xml</value> </list> </property> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 需要执行事务管理的方法 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="save*" propagation="REQUIRED" /> ... <tx:method name="*" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <!-- aop设置 --> <aop:config> <aop:pointcut expression="execution(* com.xxx.service.user.*.* (..))"id="userpointcut" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="userpointcut" /> ... </aop:config> 接下来就是日常的dao、service、action配置了