1. 程式人生 > >Spring整合mybatis和springmvc的配置

Spring整合mybatis和springmvc的配置

applicationContext.xml(Spring整合Mybatis配置)

<?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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">

<!--開啟service註解掃描-->
<context:component-scan base-package="top.biyenanhai.service"/>
  或者:
  <!--開啟註解掃描,掃描除Controller以外的註解-->
  <context:component-scan base-package="top.biyenanhai">
  <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
  </context:component-scan>

<!--dao層配置開始-->
<!--資料來源配置讀取-->
<context:property-placeholder location="classpath:jdbc.properties"/>

<!--Druid連線池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>

<!--配置spring整合mybatis框架的SqlSessionFactoryBean-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--
掃描pojo包,為實體類建立別名-->
        <property name="typeAliasesPackage" value="top.biyenanhai.pojo"/>
</bean>

<!--mapper掃描器,用於產生代理物件-->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="top.biyenanhai.mapper"/>
</bean>

  <!--dao層配置結束-->


<!--service配置開始-->
<!--事務管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>

<!--訊息通知-->
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="query*" read-only="true" />
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>

<!--AOP切入點-->
<aop:config>
<aop:pointcut id="pt1" expression="execution(* top.biyenanhai.service.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"/>
</aop:config>

<!--開啟事務註解驅動-->
<tx:annotation-driven/>
  <!--service配置結束-->

</beans>

springmvc.xml(springmvc的配置)
  
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
 http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans.xsd
 http://www.springframework.org/schema/mvc
 http://www.springframework.org/schema/mvc/spring-mvc.xsd
 http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context.xsd">

<!--開啟註解掃描-->
<context:component-scan base-package="top.biyenanhai">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>


<!--配置檢視解析器物件-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>


<!--過濾靜態資源-->
  <mvc:default-servlet-handler/>

<!--開啟SpringMVC註解的支援-->
<mvc:annotation-driven/>

</beans>