spring配置文件接收

< aop:config >

    <aop:aspectj-autoproxy />
    <aop:config>
        <aop:pointcut id="appService" expression="execution(* com.test.appliance.business.admin.service.*.*(..))" />

        <aop:advisor advice-ref="txAdvice" pointcut-ref="appService" />
    </aop:config>

execution(* com.test.appliance.business.admin.service. * . *(. .) )” />
    注释:
         表示在com.test.appliance.business.admin.service包下所有类的方法
         第一个* 表示返回值类型
         第二个*表示所有的类
         第三个* 表示类的所有方法
         括号里面的..代表所有的参数
 

别名 < property name=” typeAliasesPackage ” value=” ” />

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />

        <!--***每个项目不一样*** -->
        <property name="typeAliasesPackage" value="com.test.appliance.commons.base.module.admin.model" />
        <!--***每个项目不一样*** -->

        <property name="mapperLocations">
            <list>
                <value>classpath:/mapper/*Mapper.xml</value>
            </list>
        </property>
        <property name="plugins">
            <array>
                <bean class="com.test.appliance.commons.base.utils.jdbc.PagePlugin">
                    <property name="properties">
                        <value>pageSqlId=.*Page.*</value>
                    </property>
                </bean>
            </array>
        </property>
    </bean>

<引用>
         在Mybatis中, 所有的操作都是基于SqlSession操作的, SqlSession是由SessionFactory来产生的, SessionFactory是由SessionFactoryBuileder产生的. 但是Mybatis-Spring是基于SessionFactoryBean的, 在使用Mybatis-Spring的时候, 我们也需要SqlSession, 而且这个SqlSession是内嵌在程序中, 一般不需要我们直接访问.
      SqlSession也是由SessionFactory来产生的, 但是Mybatis-Spring给我们封装了一个SessionFactoryBean, 在这个bean里面还是通过SessionFactoryBuidder来建立对应的SessionFactory,进而获取到对应的SqlSession,
 
通过SessionFactoryBean 我们可以通过对其指定一些属性来提供Mybatis的一些配置信息, 所以需要在Spring的配置文件中定义一个SessionFactoryBean
这里写图片描述

  1. 在定义SqlSessionFactory的时候, dataSource是必须指定的, 它表示用于连接数据库的数据源
  2. mapperLocations: 表示我们的mapper文件存放的位置, 当mapper文件跟对应的mapper接口处于同一位置的时候可以不用指定该属性的值
  3. configLocation: 同于指定Mybaits的配置文件的位置, 如果指定了该属性, 那么会以配置文件的内容作为配置信息构建SqlSessionFactoryBuilder, 但是后续指定的内容会覆盖该配置文件里面指定的对应内容
  4. typeAliasesPackage: 一般对应我们的实体类所在的包,这个时候会自动取对应包中类的名称(首字母小写)作为别名, 多个package之间可以用逗号或者分号来进行间隔.
    注意: value值一定要是包的全名
    <property name="typeAliasesPackage" value="com.test.appliance.commons.base.module.admin.model" />
  1. typeAliases: 用来指定别名, 指定了该属性后,Mybatis会把这个类型的短名作为这个类型的别名, 前提是该类上没有@Alias注解, 否则将使用该注解对应的值作为此种类型的别名
    注意: value值一定要是类的全限定名
  2. plugins: 用来指定Mybatis的Interceptor(拦截器)
  3. typeHandlersPackage : 用来指定typeHandler所在的包, 如果指定了该属性,SqlSessionFactoryBean会自动把该包下面的类注册成为对应的typeHandler. 多个package之间可以用逗号或者分号进行分割
  4. typeHandlers: 标识typeHandler
     

spring事务

    //配置spring的事务管理者PlatformTransactionManager 
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    //开启事务控制
    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />

* < tx: advice> *
 
         注解解释: 该标签是用来指定不同的事务
标签内设置id和transaction-manager属性 , id是advice 的标识, 而transaction-manager 则必须引用一个PlatformTransactionManager
      除了这两个属性外, 还可以通过 < tx:attributes /> 标签所创建的通知的行为.并且只可以用< tx:method >作为子元素
下面是 < tx:method > 的不同属性的介绍
这里写图片描述

    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="select*" read-only="true" />
            <tx:method name="find*" read-only="true" />
            <tx:method name="get*" read-only="true" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="del*" propagation="REQUIRED" />
            <tx:method name="*" />
        </tx:attributes>
    </tx:advice>

         < tx:method name=”save*”>:表示将拦截以save开头的方法,被拦截的方法将应用配置的事务属性:propagation=”REQUIRED”表示传播行为是Required,isolation=”READ_COMMITTED”表示隔离级别是提交读;
         < tx:method name=”*”>:表示将拦截其他所有方法,被拦截的方法将应用配置的事务属性:propagation=”REQUIRED”表示传播行为是Required,isolation=”READ_COMMITTED”表示隔离级别是提交读,read-only=”true”表示事务只读;

—>spring配置文件还有很多, 先写这么多,待补充

一起学习,共同进步

猜你喜欢

转载自blog.csdn.net/weixin_39297312/article/details/79270394