spring-context.xml

JDBC连接池
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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">
    <context:property-placeholder location="classpath:db.properties"/>
    <!--连接池-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <property name="driverClassName" value="${driver}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
    </bean>

    <!--SqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--连接池-->
        <property name="dataSource" ref="dataSource"/>
        <!--mapper文件-->
        <property name="mapperLocations" value="classpath:mapper/*-mapper.xml"/>
        <!--别名-->
        <property name="typeAliasesPackage" value="entity"/>
        <!--关联mybatis核心配置-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>

    <!--MapperScannerConfigurer-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--工厂中只有一个SqlSessionFactory此配置可省略-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <property name="basePackage" value="dao"/>
    </bean>

    <!--service-->
    <bean id="userService" class="service.UserServiceImpl">
        <property name="userDAO" ref="userDAO"/>
    </bean>

</beans>

mybatis-context.xml文件配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">

<!--MyBatis配置-->
<configuration>
    
    <settings>
        <!--mybatis-config.xml开启全局缓存-->
        <setting name="cacheEnabled" value="true"/>
    </settings>
    
    <plugins>
        <!--分页插件-->
        <!-- com.github.pagehelper为PageHelper类所在包名 -->
        <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
    </plugins>
</configuration>

AOP配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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/aop
                           http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 目标 -->
    <bean id="userService" class="com.service.UserServiceImpl"></bean>
    <!-- Advice -->
    <bean id="before2005" class="com.advice.MyBefore"/>

    <!-- 组装 -->
    <aop:config>
        <!-- pointcut:切入点
             execution表达式: [修饰符] 返回值 包.类.方法名(参数表)
             切入点表达式  execution(* com.service.UserServiceImpl.*(..))
             切入点表达式  execution(* com.service.UserServiceImpl.insertUser(..))
        -->
        <aop:pointcut id="pc2005" expression="execution(* com.service.UserServiceImpl.*(..))"/>
        <aop:pointcut id="pc2006" expression="execution(* com.service.UserServiceImpl.insertUser(..))"/>
        <aop:pointcut id="pc2007" expression="execution(* com.service.UserServiceImpl.updateUser(..))"/>
        <!-- 组装 -->
        <aop:advisor advice-ref="before2005" pointcut-ref="pc2006"/>
        <aop:advisor advice-ref="before2005" pointcut-ref="pc2007"/>
    </aop:config>
</beans>
<!--
   class XXProxy implements UserService{
        private UserService userService;
        User queryUserById(Integer id){
            //System.out.println("before~~~~");
            userService.queryById(id);
        }
        int insertUser(User user){
            System.out.println("before~~~~");
            userService.insertUser(user);
        }
        int updateUser(User user){
            System.out.println("before~~~~");
            userService.updateUser(user);
        }
   }
-->

spring-context-tx.xml 事务管理
  • 文件头部添加如下代码:
    xmlns:tx=“http://www.springframework.org/schema/tx”
    xsi:schemaLocation=“http://www.springframework.org/schema/tx
                                       http://www.springframework.org/schema/tx/spring-tx.xsd”
    <!-- 事务管理器 -->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- 事务Advice -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <!--<tx:method name="queryDeptById" read-only="true"/>-->
            <!--
               ACID  原子性  一致性  隔离性  持久性
               1> 隔离级别
                  小到大过程中,共享变少,阻塞变多,安全性提高,并发性变差
               2> 传播行为
                  required
                  supports
             -->
            <tx:method name="query*" propagation="SUPPORTS"/>
            <tx:method name="*Dept" read-only="false" isolation="DEFAULT" propagation="REQUIRED" timeout="-1" rollback-for="Exception"/>
            <!--<tx:method name="save*" read-only="false"/>
            <tx:method name="delete*" read-only="false"/>
            <tx:method name="update*" read-only="false"/>-->
        </tx:attributes>
    </tx:advice>

    <!-- 编织 -->
    <aop:config>
        <aop:pointcut id="pc" expression="execution(* com..DepartmentServiceImpl.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pc"/>
    </aop:config>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值