0
点赞
收藏
分享

微信扫一扫

Spring AOP 注解配置

颜路在路上 2022-02-19 阅读 68

1.添加依赖

2. 配置Spring配置文件

 3.声明注解配置

 4.通过注解 Service调用Dao

@Service
public class BookServiceImpl implements BookService {
    @Autowired
    private BookDao bookDao;
    @Override
    public void queryBookById(int bookId) {
        Book book = bookDao.selectBookById(bookId);
        System.out.println(book);
    }
}

Spring整合Mybatis,步骤

  • IoC支持:SpringIoC可以为MyBatis完成DataSource、SqlSessionFactory、SqlSession以及DAO对象的创建

  • AOP支持:使用Spring提供的事务管理类,基于AOP完成对MyBatis数据库操作的事务管理

第一步:配置Mybatis-conf.xml和mapper

第二步:mybatis.conf.xml:不用配置?为什么  -->交给Spring容器管理mapper

 第三步:写完xxxMapper.xml后编写 Spring配置文件

添加依赖(context 、aspects)

 第四步:在resources目录创建spring配置文件applicationContext.xml

<?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"
       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:annotation-config/>
    <context:component-scan base-package="com.qfedu"/>
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
    
</beans>

详细配置信息 

    <context:annotation-config/>
<!--    使用注解-->
    <context:component-scan base-package="com.zdq"/>
<!--    声明注解扫描范围-->
    <aop:aspectj-autoproxy/>
<!--声明基于注解的aop代理配置-->
    <context:property-placeholder location="druid.properties"/>
    <!--    引入属性文件-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${druid.driver}"/>
        <property name="url" value="${druid.url}"/>
        <property name="username" value="${druid.username}"/>
        <property name="password" value="${druid.password}"/>
        <property name="initialSize" value="${druid.pool.init}"/>
        <property name="maxActive" value="${druid.pool.maxActive}"/>
        <property name="minIdle" value="${druid.pool.minIdle}"/>
        <property name="maxWait" value="${druid.pool.timeout}"/>
    </bean>
    <!--spring创建mybayissqlsessionfactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 配置数据源    -->
        <property name="dataSource" ref="dataSource"/>
        <!-- MyBatis主配置文件 -->
        <property name="configLocation" value="mybatis-conf.xml"/>
        <!-- 映射文件 -->
        <property name="mapperLocations" value="classpath:mappers/*Mapper.xml"/>
        <!-- 配置实体类的包名,自定进行别名定义    -->
        <property name="typeAliasesPackage" value="com.zdq.Entity"/>
    </bean>

    <!-- MapperScannerConfigurer 扫描包中的daoMapper接口 给spring指定mapper工厂-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/><!-- spring创建mybayissqlsessionfactory-->
        <property name="basePackage" value="com.zdq.Dao"/>
        <!--        读取包中所有的dao接口 创建所有dao对象放到spring容器中;默认id就是首字母小写-->
    </bean>
<!--以上是spring ioc配置-->
<!--    加载事务管理-->
    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--2.配置事务管理策略(引入tx命名空间)
        isolation:配置事务的隔离级别
        propagation:配置事务的传播机制
        -->
<!--    <tx:advice id="txAdvice" transaction-manager="transactionManager">-->
<!--        <tx:attributes>-->
<!--            <tx:method name="add*" isolation="REPEATABLE_READ" propagation="REQUIRED"/>-->
<!--            <tx:method name="delete*" isolation="REPEATABLE_READ" propagation="REQUIRED"/>-->
<!--            <tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED"/>-->
<!--            <tx:method name="get*" isolation="REPEATABLE_READ" propagation="SUPPORTS"/>-->
<!--            <tx:method name="list*" isolation="REPEATABLE_READ" propagation="SUPPORTS"/>-->
<!--            <tx:method name="select*" isolation="REPEATABLE_READ" propagation="SUPPORTS"/>-->
<!--        </tx:attributes>-->
<!--    </tx:advice>-->
    <!-- 3.aop配置-->
<!--    <aop:config>-->
<!--        <aop:pointcut id="service_crud" expression="execution(* com.zdq.Service.Impl.*ServiceImpl.*(..))"/>-->
<!--        <aop:advisor advice-ref="txAdvice" pointcut-ref="service_crud"/>-->
<!--    </aop:config>-->
    <!--  2.声明事务采用注解配置  -->
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

第五步:添加Spring-Mybatis依赖和spring-jdbc、druid

 第六步:创建druid.properties

 第七步:在application.xml配置读取druid配置信息

第八步:在Spring的配置文件中配置创建SqlSessionFactory对象:让spring创建sqlsession工厂对象

第九步:MapperScannerConfigurer用于创建DAO/Mapper接口对象存放到Spring容器

 十:在serviceImpl中加入spring注解

 十一:引入Spring Test

添加依赖

 

举报

相关推荐

0 条评论