zoukankan      html  css  js  c++  java
  • Spring、mybaits整合

    mybatis.cfg.xml

    <!DOCTYPE configuration PUBLIC "-//mybatis.org/DTD Config 3.0//EN" 
        "http://mybatis.org/dtd/mybatis-3-config.dtd" >
    <configuration>
        <typeAliases>
            <package name="com.lovo.sm.beans"></package>
        </typeAliases>
    </configuration>
    

    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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/aop 
     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.0.xsd
     http://www.springframework.org/schema/tx
     http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
    
        <!-- 启动@AspectJ框架的支持,如果你的项目中,
        没有使用JAVA代码所写的切面,就不需要配置 -->
        <aop:aspectj-autoproxy/>
        <!-- 配置自动扫包规则 -->
        <context:component-scan base-package="com.lovo.sm">
    
        </context:component-scan>
    
        <!-- 配置数据源 DataSource -->
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <!-- &amp;在配置文件中代表& -->
            <property name="url" value="jdbc:mysql://localhost:3306/mybatis117?useUnicode=true&amp;characterEncoding=UTF-8"/>
            <property name="username" value="root"/>
            <property name="password" value="lovo"/>
        </bean>
    
        <!-- 配置Session工厂 -->
        <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <property name="configLocation" value="classpath:mybatis.cfg.xml"/>
        </bean>
    
        <!-- 将Mapper配置文件与Session进行关联 -->
        <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.lovo.sm.mapper"/>
            <property name="sqlSessionFactory" ref="sqlSessionFactoryBean"></property>
        </bean>
    
        <!-- 配置事务管理器 -->
        <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
    
        <!-- 事务管理方式一,适合于单数据库,使用注解的方式来管理事务 
        <tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>
        -->
    
    <!-- 事务管理方式二,采用springAOP来声明式的处理事务,
        声明式就是脱离你的JAVA代码,通常是声明在配置中 -->
        <!-- 声明一个通知 -->
        <tx:advice id="txAdvice" transaction-manager="dataSourceTransactionManager">
            <tx:attributes>
                <tx:method name="*" propagation="REQUIRED" read-only="true"></tx:method>        
                <tx:method name="save*" propagation="REQUIRED" read-only="false" rollback-for="Exception"></tx:method>
                <tx:method name="update*" propagation="REQUIRED" read-only="false" rollback-for="Exception"></tx:method>
                <tx:method name="delete*" propagation="REQUIRED" read-only="false" rollback-for="Exception"></tx:method>
                <tx:method name="find*" propagation="SUPPORTS" read-only="true"></tx:method>
                <tx:method name="get*" propagation="SUPPORTS" read-only="true"></tx:method>
                <tx:method name="search*" propagation="SUPPORTS" read-only="true"></tx:method>
            </tx:attributes>
        </tx:advice>
    
        <!-- 这里是配置AOP事务关联 -->
        <aop:config>
            <!-- 声明切入点 -->
            <aop:pointcut id="serviceMethod" expression="execution(* com.lovo.sm.service.impl.*ServiceImpl.*(..))"></aop:pointcut>
            <!-- 将我们定义的通知,交给AOP来关联 -->
            <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod"></aop:advisor>
        </aop:config>
    
    </beans>
    
  • 相关阅读:
    Hadoop 单机搭建 Scala安装
    Hadoop 单机搭建 Hbase单机模式搭建
    Hadoop 单机搭建 hadoop单机搭建
    Linux shell 重定向学习笔记
    转:SQLServer中的GROUPING,ROLLUP和CUBE
    ueditor getshell漏洞重现及分析
    SQLServer禁用、启用外键约束
    转:Sql Server中清空所有数据表中的记录
    EF结合SqlBulkCopy实现高效的批量数据插入 |EF插件EntityFramework.Extended实现批量更新和删除
    12种开源Web安全扫描程序
  • 原文地址:https://www.cnblogs.com/gjmfg/p/5374369.html
Copyright © 2011-2022 走看看