• MyBatis-Spring


    Mybatis-spring 用于帮助你将 MyBatis 代码无缝地整合到 Spring 中。
    Spring 将会加载必要的 MyBatis 工厂类和 session 类
    提供一个简单的方式来注入 MyBatis 数据映射器和 SqlSession 到业务层的 bean 中。
    方便集成spring事务
    翻译 MyBatis 的异常到 Spring 的 DataAccessException 异常(数据访问异常)中。

    添加依赖 

        <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>2.0.0</version>
        </dependency>

    配置SqlSessionFactoryBean
    配置MapperScannerConfigurer
    配置事务

    <?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:c="http://www.springframework.org/schema/c"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:cache="http://www.springframework.org/schema/cache" xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:redisson="http://redisson.org/schema/redisson" 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-4.3.xsd
                            http://www.springframework.org/schema/cache
                            http://www.springframework.org/schema/cache/spring-cache.xsd
                            http://www.springframework.org/schema/tx 
                              http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
                              http://redisson.org/schema/redisson
                              http://redisson.org/schema/redisson/redisson.xsd
                              http://www.springframework.org/schema/aop
                            http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
    
    
    
        <context:property-placeholder location="classpath:db.properties"
            ignore-unresolvable="true" />
    
        <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
            init-method="init" destroy-method="close">
            <!-- 基本属性 url、user、password -->
            <property name="driverClassName" value="${jdbc_driver}" />
            <property name="url" value="${jdbc_url}" />
            <property name="username" value="${jdbc_username}" />
            <property name="password" value="${jdbc_password}" />
    
            <!-- 配置初始化大小、最小、最大 -->
            <property name="initialSize" value="1" />
            <property name="minIdle" value="1" />
            <property name="maxActive" value="20" />
    
            <!-- 配置获取连接等待超时的时间 -->
            <property name="maxWait" value="60000" />
    
            <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
            <property name="timeBetweenEvictionRunsMillis" value="60000" />
    
            <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
            <property name="minEvictableIdleTimeMillis" value="300000" />
    
            <property name="validationQuery" value="SELECT 'x'" />
            <property name="testWhileIdle" value="true" />
            <property name="testOnBorrow" value="false" />
            <property name="testOnReturn" value="false" />
    
            <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
            <property name="poolPreparedStatements" value="true" />
            <property name="maxPoolPreparedStatementPerConnectionSize"
                value="20" />
            <!-- 配置监控统计拦截的filters -->
            <property name="filters" value="stat" />
        </bean>
    
    
        <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="typeAliasesPackage" value="com.enjoylearning.mybatis.entity" />
            <property name="mapperLocations" value="classpath*:sqlmapper/*.xml" />
        </bean>
        <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.enjoylearning.mybatis.mapper" />
        </bean>
    
    
    
        <!-- (事务管理)transaction manager -->
        <bean id="transactionManager"
            class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource" />
            <qualifier value="transactionManager" />
        </bean>
    
        <tx:annotation-driven transaction-manager="transactionManager" />
    
    
        <context:component-scan base-package="com.*">
        </context:component-scan>
    
    </beans>
  • 相关阅读:
    微信小程序设置底部导航栏目方法
    微信小程序四(设置底部导航)
    微信小程序三(设置页面标题)
    error while loading shared libraries: libpcre.so.0的解决办法(转)
    mysql无法启动问题的解决方案:mysql.sock重启不自动生成,mysqld_safe启动报错
    使用snmp+mrtg监控CPU、流量、磁盘空间、内存
    lvs之dr技术的学习与实践
    lvs之nat技术的学习与实践
    lvs之ip-tun(ip隧道)技术的学习与实践
    CentOS 6.4下Squid代理服务器的安装与配置【转】
  • 原文地址:https://www.cnblogs.com/qin1993/p/12022904.html
Copyright © 2020-2023  润新知