mybatis和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:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
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-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<!-- 开启包扫描 -->
<context:component-scan base-package="cn.dw.hy"></context:component-scan>

<!-- 配置mybatis -->
<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/test?useSSl=true&amp;useUnicode=true&amp;characterEncoding=utf8"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>

<!-- mybatis的工厂对象配置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="da" ref="datasource"></property>
<property name="mapperLocations" value="classpath:cn/dw/hy/mapper/xml/*.xml"></property>
<property name="typeAliasesPackage" value="cn.dw.hy.model"></property>
</bean>

<!-- mybatis跟spring的整合配置 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
<property name="basePackage" value="cn.dw.hy.mapper"></property>
</bean>

<!-- 註冊轉換器 使用的是jackons的轉換器 依賴:jsckson-core; jackson-databind -->
<mvc:annotation-driven/>
<bean id="aop" class="cn.dw.hy.util.AopClass" />
<!-- 指定aop的类 -->
<aop:config>
<!-- 告知spring切面类 -->
<aop:aspect ref="aop">
<aop:pointcut expression="execution(* cn.dw.hy.service.*.*(..))" id="pt"/>
<!-- 此处配置通知,通知分为前置、后置、环绕、异常
method:指定要切入的方法(通知)
aop:before:代表前置通知
<aop:before method="logs" pointcut="execution(* cn.dw.service.*.*(..))"/>
-->
<!--after-returning:只要正常结束,就会切入通知,一般用来实现日志操作 -->
<aop:after-returning method="logs" pointcut-ref="pt" />

<!-- 结束就会切入,类似finally主要用于释放资源
<aop:after method="logs" pointcut-ref="pt"/>
-->
<!-- 出现异常才会被切入,一般用来捕获异常信息 -->
<aop:after-throwing method="ex" throwing="e" pointcut-ref="pt"/>
</aop:aspect>
</aop:config>
</beans>

posted @ 2018-08-23 16:12  鱼丸1993  阅读(99)  评论(0编辑  收藏  举报