zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    paddlepaddle训练网络的基本流程二(进阶Paddle-detection框架)
    paddlepaddle训练网络的基本流程一(入门示例)
    redis常用操作命令
    mysql基础命令
    使用Gunicorn和nginx进行Flask服务部署
    如何测试(八)抖音 如何测试?
    全(十九)Jmeter 请求 之 遇到 cookie、token 处理方式(使用 正则表达式提取器 获取 然后在引用)
    全(十八)Jmeter 请求元件 之 JSON PATH 提取 响应结果
    全(十七)Jmeter 请求元件 之 正则表达式提取器 提取 响应结果、foreach循环控制器
    全(十六)Jmeter 请求 之 正则表达式
  • 原文地址:https://www.cnblogs.com/qin1993/p/12022904.html
Copyright © 2011-2022 走看看