MyBatis

作者: LynnGuo | 来源:发表于2018-05-21 15:02 被阅读0次

    MyBatis 是支持普通 SQL 查询,存储过程和高级映射的优秀持久层框架。 MyBatis 消除了几乎所有的 JDBC 代码和参数的手工设置以及对结果集的检索。 MyBatis 可以使用简单的XML 或注解用于配置和原始映射,将接口和 Java 的 POJO( Plain Old Java Objects,普通的Java 对象)映射成数据库中的记录.

    配置文件

        <!-- 配置数据源 -->
        <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
            init-method="init" destroy-method="close">
            <property name="driverClassName" value="${driverClassName}"/>
            <property name="url" value="${jdbc_url}" />
            <property name="username" value="${jdbc_username}" />
            <property name="password" value="${jdbc_password}" />
    
            <!-- 初始化连接大小 -->
            <property name="initialSize" value="20" />
            <!-- 连接池最大使用连接数量 -->
            <property name="maxActive" value="2000" />
            <!-- 连接池最小空闲 -->
            <property name="minIdle" value="20" />
            <!-- 获取连接最大等待时间 -->
            <property name="maxWait" value="60000" />
    
            <property name="poolPreparedStatements" value="true" />
            <property name="maxPoolPreparedStatementPerConnectionSize" value="33" />
    
            <property name="validationQuery" value="${validationQuery}" />
            <property name="testOnBorrow" value="false" />
            <property name="testOnReturn" value="false" />
            <property name="testWhileIdle" value="true" />
    
            <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
            <property name="timeBetweenEvictionRunsMillis" value="60000" />
            <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
            <property name="minEvictableIdleTimeMillis" value="25200000" />
    
            <!-- 打开removeAbandoned功能 -->
            <!-- <property name="removeAbandoned" value="true" /> -->
            <!-- 1800秒,也就是30分钟 -->
            <!-- <property name="removeAbandonedTimeout" value="1800" /> -->
            <!-- 关闭abanded连接时输出错误日志 -->
            <!-- <property name="logAbandoned" value="true" /> -->
    
            <!-- 监控数据库 -->
            <!-- <property name="filters" value="stat" />
            <property name="filters" value="mergeStat" /> -->
        </bean>
    
    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="plugins">
                <bean class="sinosoft.framework.core.interceptor.PageInterceptor">
                    <!-- <property name="dialect" value="sqlserver" /> -->
                    <property name="dialect" value="oracle" />
                </bean>
            </property>
            <!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
            <property name="mapperLocations" value="classpath:sinosoft/**.xml" />
            <property name="configLocation" value="classpath:config/mybatis-configuration.xml"/>
        </bean>
    
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="sinosoft.framework;sinosoft.platform;sinosoft.project" />
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
        </bean>
    
    <!-- 配置事务管理器 -->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource" />
            <property name="globalRollbackOnParticipationFailure" value="false" />
        </bean>
    
        <!-- 注解方式配置事物 -->
        <!-- <tx:annotation-driven transaction-manager="transactionManager" /> -->
    
        <!-- 拦截器方式配置事物 -->
        <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                <tx:method name="add*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
                <tx:method name="append*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
                <tx:method name="insert*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
                <tx:method name="save*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
                <tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
                <tx:method name="modify*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
                <tx:method name="edit*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
                <tx:method name="delete*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
                <tx:method name="remove*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
                <tx:method name="repair" propagation="REQUIRED" rollback-for="java.lang.Exception" />
                <tx:method name="delAndRepair" propagation="REQUIRED" rollback-for="java.lang.Exception" />
                <tx:method name="sendWithResub" propagation="REQUIRED" rollback-for="java.lang.Exception" />
                <tx:method name="count*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
                <!-- 工作流 -->
                <tx:method name="claim*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
                <tx:method name="unclaim*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
                <tx:method name="delegate*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
                <tx:method name="complete*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
                <tx:method name="deployement*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
                <tx:method name="start*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
                <tx:method name="setSuspend*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
                <tx:method name="setTaskParam" propagation="REQUIRED" rollback-for="java.lang.Exception" />
                    
                <!-- select、query开头的方法,开启只读,提高数据库访问性能 -->  
                <tx:method name="select*" read-only="true"/>
                <tx:method name="query" read-only="true"/>
                <tx:method name="*" propagation="SUPPORTS" />
                <tx:method name="getNextId" propagation="REQUIRES_NEW" />
            </tx:attributes>
        </tx:advice>
        
        <aop:config>
            <aop:pointcut id="transactionPointcut" expression="execution(* sinosoft..*.*(..))" />
            <aop:pointcut id="maxNoPointcut" expression="execution(* sinosoft..MaxNoUtil.*(..))" />
            <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" order="1"/>      
            <aop:advisor pointcut-ref="maxNoPointcut" advice-ref="transactionAdvice" order="2"/>        
        </aop:config>
    

    相关文章

      网友评论

          本文标题:MyBatis

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