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>
  • 相关阅读:
    flask数据库操作
    flask之--钩子,异常,上下文,flask-script,模板,过滤器,csrf_token
    Flask项目出现html文件无法自动补全
    pandas强化练习(美国交警开放的数据)
    flask初识
    爬取实时变化的 WebSocket 数据(转载)
    pep8规范
    模拟登陆
    关于在scrapy中使用xpath
    Java基础/利用fastjson序列化对象为JSON
  • 原文地址:https://www.cnblogs.com/qin1993/p/12022904.html
Copyright © 2011-2022 走看看