springMVC+spring+hibernate基础配置文件

来源:互联网 发布:minecraft java 编辑:程序博客网 时间:2024/06/05 08:33

一,配置springMVC

<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-4.3.xsd 
     http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-4.3.xsd
     http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd 
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">


<!-- 自动扫描controller包下的所有类,使其认为spring mvc的控制器 -->
<context:component-scan base-package="cn.cque.controller" />

<!-- spring mvc不能处理的请求交给Tomcat -->
<mvc:default-servlet-handler/>


<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<mvc:annotation-driven/>


<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp" />
</bean>
</beans>


二 。 配置spring与hibernate的整合

<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-4.3.xsd 
     http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-4.3.xsd
     http://www.springframework.org/schema/aop
  http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
  
  <!-- 开启注解功能 -->
<context:annotation-config />
<!-- 指定jdbc配置文件的位置 -->
  <context:property-placeholder location="classpath:jdbc.properties" />
<!-- 指定扫描dao包 -->
<context:component-scan base-package="cn.cque.dao.impl" />
<!-- 是定扫描service包 -->
<context:component-scan base-package="cn.cque.service" />


<!-- 数据库连接池,配置数据源 -->
<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.name}" />
<property name="password" value="${jdbc.password}" />
<property name="driverClassName" value="${jdbc.DriverClass}" />


</bean>


<!-- spring与hibernate整合配置 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource" />
<!-- 扫描实体类包 -->
<property name="packagesToScan">
<list>
<value>cn.cque.entity</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="current_session_context_class">thread</prop>
<prop key="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</prop>
</props>
</property>
</bean>

<!-- hibernate事物管理 -->
<bean id="txManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<!-- 切入规则 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<!-- add开头的方法的事物传播规则 -->
<tx:method name="add*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>


<!-- aop配置 -->
<aop:config>
<!-- 切入点 ,所有的事物方法为事物方法-->
<aop:pointcut id="businessService"
expression="execution(public * cn.cque.service..*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="businessService" />
</aop:config>
</beans>









原创粉丝点击