美文网首页
spring-mybatis-配置文件

spring-mybatis-配置文件

作者: blank_white | 来源:发表于2020-06-23 10:51 被阅读0次

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" 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">

<!--    自动生成代理类-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!--    注解类的位置-->
    <context:component-scan base-package="com.spyouth"/>
<!--    导入其他 bean spring 配置文件-->
    <import resource="classpath:spring-*.xml"/>
<!--    导入配置 name=value -->
    <context:property-placeholder location="classpath:JDBC-config.properties"/>
<!--    阿里巴巴 德鲁伊 数据源,数据库连接池-->
    <bean id="myDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    <!--    创建 sqlSessionFactory 对象-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--        指定数据源-->
        <property name="dataSource" ref="myDataSource"/>
        <!--        指定配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>

    <!--    用于自动生成 mybatis getMapper 返回的dao对象-->
    <bean id="scannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--        指定 sqlSessionFactory 对象名称-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <!--        指定 dao 接口所在目录-->
        <property name="basePackage" value="com.spyouth.dao"/>
    </bean>

<!--    声明事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="myDataSource"/>
    </bean>
<!--    事务注解驱动-->
    <tx:annotation-driven transaction-manager="transactionManager"/>

<!--    配置文件配置事务 声明事务方法,指定事务属性-->
    <tx:advice id="advice" transaction-manager="transactionManager">
        <tx:attributes>
<!--            方法名可使用通配符-->
            <tx:method name="bugGoods" propagation="REQUIRED" isolation="DEFAULT"
                       rollback-for="com.spyouth.ex.GoodsNotEnoughException,java.lang.NullPointerException"/>
            <tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT"
                       rollback-for="java.lang.RuntimeException"/>
            <tx:method name="insert*" propagation="REQUIRED" isolation="DEFAULT"
                       rollback-for="java.lang.RuntimeException"/>
            <tx:method name="*" propagation="SUPPORTS" read-only="true" />
        </tx:attributes>
    </tx:advice>
<!--    配置 aop 关联上面的-->
    <aop:config >
        <aop:pointcut id="servicePt" expression="execution(* *..service..*.*(..))"/>
        <aop:advisor advice-ref="advice" pointcut-ref="servicePt"/>
    </aop:config>


    <bean id="studentService" class="com.spyouth.service.StudentServiceImpl" autowire="byName"/>
    <bean id="buyGoodsService" class="com.spyouth.service.BuyGoodsServiceImpl2" autowire="byName"/>

</beans>

mybatis-config.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">
<configuration>

    <settings>
<!--        延迟加载-->
        <setting name="lazyLoadingEnabled" value="true"/>
        <setting name="aggressiveLazyLoading" value="false"/>
<!--        二级缓存,除了主配文件外,还要在需要二级缓存的 dao 配置文件中添加 <cache/> 标签 -->
        <setting name="cacheEnabled" value="true"/>
    </settings>

<!--    实体类别名-->
    <typeAliases>
        <package name="com.spyouth.beans"/>
    </typeAliases>

    <mappers>
<!--        自动为包下面的dao注册 mapper-->
        <package name="com.spyouth.dao"/>
    </mappers>

</configuration>

相关文章

  • spring-mybatis-配置文件

    applicationContext.xml mybatis-config.xml

  • 配置文件使用实例

    从配置文件读取内容进行实例化 读取配置文件的类 配置文件 使用配置文件

  • springboot(2)配置文件与整合其他技术

    4.配置文件: 4.1 SpringBoot配置文件类型:4.2 yml配置文件简介与语法: 4.3配置文件与...

  • URXVT

    配置文件 生成简明的配置文件 生成有详细说明的配置文件 [1] 示例配置文件 .Xresource / .X...

  • Spring Boot

    配置文件 默认配置文件:application.properties 推荐配置文件:application.yml...

  • Nginx + Uwsgi的基础配置

    Nginx 配置文件: Uwsgi配置文件:

  • linux rabbitmq 修改端口号

    查找配置文件位置: 拷贝配置文件到 /etc/rabbitmq 目录下: 修改配置文件:

  • python配置文件读取

    1、ConfigParser读取配置文件模块 配置文件 [people]为section 2、读取配置文件

  • Hexo+Next的摸索

    区分站点配置文件和主题配置文件 站点配置文件路径:blog/_config.yml主题配置文件路径:blog/th...

  • 环境变量

    环境变量配置文件 查看环境变量 set source 配置文件 等同于 . 配置文件重新读取配置文件/etc...

网友评论

      本文标题:spring-mybatis-配置文件

      本文链接:https://www.haomeiwen.com/subject/zzlowhtx.html