zoukankan      html  css  js  c++  java
  • MyBatis -- Spring -- SqlSessionFactoryBean

    符:

    Mybatis-config.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd" >
    <configuration>
        <!-- 配置mybatis的多个运行环境,default默认运行环境,必须同 运行环境的id相同 -->
        <environments default="e1">
            <!-- 配置1个运行环境,id 唯一标识 -->
            <environment id="e1">
                <!-- 配置事务管理策略,type类型,其中JDBC代表 使用JDBC中的事务管理 -->
                <!-- <transactionManager type="JDBC"></transactionManager> -->
                <!-- 数据的连接信息,type类型,POOLED代表  使用mybatis自带的连接池 -->
                <dataSource type="POOLED">
                    <property name="driver" value="oracle.jdbc.OracleDriver"/>
                    <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"/>
                    <property name="username" value="hr"/>
                    <property name="password" value="hr"/>
                </dataSource>
            </environment>
            <environment id="mysql">
                <!-- 事务管理,采用JDBC管理方式 -->
                <transactionManager type="JDBC"></transactionManager>
                <!-- 连接数据库的参数配置 -->
                <dataSource type="POOLED">
                    <property name="driver" value="com.mysql.jdbc.Driver"/>
                    <property name="url" value="jdbc:mysql://localhost:3306/dataBase?characterEcoding=utf-8"/>
                    <property name="username" value="root"/>
                    <property name="password" value="123"/>
                </dataSource>
            </environment>
    
        </environments>
        
        <!-- 注册映射文件 -->
        <mappers>
            <mapper resource="day1/AccountDaoImpl.xml"/>
        </mappers>
        
    </configuration>

    beans.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:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        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/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.0.xsd
                             http://www.springframework.org/schema/aop 
                             http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
        
        <!-- 1.扫描包 service,dao -->
        <context:component-scan base-package="cn.itcast.mybatis.service,cn.itcast.mybatis.dao"/>
        
        <!-- 2.加载jdbc.properties -->
        <context:property-placeholder location="classpath:jdbc.properties"/>
        
        <!-- 3.数据源 datasource 自带数据库连接池 -->
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="${jdbc.driverclass}"/>
            <property name="url" value="${jdbc.url}"/>
            <property name="username" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/>
        </bean>
        
        <!-- 4.SqlSessionFactory工厂 factoryBean -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <!-- 怎么和mybatis整合,关联Mybatis,关联映射文件 -->
            <property name="configLocation" value="classpath:sqlMapConfig.xml"/>
            <property name="mapperLocations" value="classpath:cn/itcast/mybatis/mapper/*.xml"/>
        </bean>
        
        <!-- 5.事务 -->
        <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"/>
        </bean>
        
        <tx:advice id="txAdvice" transaction-manager="txManager">
            <tx:attributes>
                <tx:method name="save*" propagation="REQUIRED"/>
                <tx:method name="insert*" propagation="REQUIRED"/>
                <tx:method name="update*" propagation="REQUIRED"/>
                <tx:method name="delete*" propagation="REQUIRED"/>
                
                <tx:method name="*" read-only="true"/>
            </tx:attributes>
        </tx:advice>
        
        <aop:config>
            <aop:pointcut expression="execution(* cn.itcast.mybatis.service.*.*(..))" id="txPointCut"/>
            <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
        </aop:config>
        
                         
    </beans>

    app-datasource.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:context="http://www.springframework.org/schema/context" 
        xmlns:p="http://www.springframework.org/schema/p"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="    
              http://www.springframework.org/schema/beans 
              classpath:/org/springframework/beans/factory/xml/spring-beans-4.1.xsd 
              http://www.springframework.org/schema/context 
              classpath:/org/springframework/context/config/spring-context-4.1.xsd
              http://www.springframework.org/schema/tx
              classpath:org/springframework/transaction/config/spring-tx-4.1.xsd"
        default-lazy-init="false">
    
        <!-- <context:property-placeholder location="classpath:spring/exportParam.properties" /> -->
    
        <bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
            <property name="poolProperties">
                <bean class="org.apache.tomcat.jdbc.pool.PoolProperties">
                    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
                    <property name="url" value="${jdbc.url}" />
                    <property name="username" value="${jdbc.user}" />
                    <property name="password" value="${jdbc.password}" />
                    <!-- Register the pool with JMX. In order for the connection pool object to create the MBean. -->
                    <property name="jmxEnabled" value="true" />
                    <!-- The indication of whether objects will be validated by the idle object evictor. -->
                    <property name="testWhileIdle" value="true" />
                    <!-- The indication of whether objects will be validated before being borrowed from the pool. -->
                    <property name="testOnBorrow" value="false" />
                    <property name="testOnReturn" value="false" />
                    <property name="initialSize" value="${jdbc.initialPoolSize}" />
                    <property name="maxActive" value="${jdbc.maxActive}" />
                    <property name="maxWait" value="${jdbc.maxWait}" />
                    <property name="minIdle" value="${jdbc.minIdle}" />
                    <property name="maxIdle" value="${jdbc.maxIdle}" />
                    <property name="maxAge" value="60000" />
                    <!-- The number of milliseconds to sleep between runs of the idle connection validation/cleaner thread. -->
                    <property name="timeBetweenEvictionRunsMillis" value="15000" />
                    <!-- The minimum amount of time an object may sit idle in the pool before it is eligible for eviction. -->
                    <property name="minEvictableIdleTimeMillis" value="60000" />
                    <property name="removeAbandoned" value="true" />
                    <property name="removeAbandonedTimeout" value="30" />
                    <property name="validationQuery" value="SELECT 1" />
                    <property name="validationInterval" value="30000" />
                </bean>
            </property>
        </bean>
    
        <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="mapperLocations" value="classpath:mybatis/mapper/ibs/*/*Dao.xml"></property>
        </bean>
        
        <bean name="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.xindatai.ibs.*.dao" />
        </bean>
        
    
        <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource" />
        </bean>
        <!-- enable transaction annotation support -->
        <tx:annotation-driven transaction-manager="txManager" />
    
    </beans>

    啦啦啦-----------------------------------------------------------------------------------------------------------------------------------------------

    SqlSessionFactoryBean

    在基本的 MyBatis 中,session 工厂可以使用 SqlSessionFactoryBuilder 来创建。而在 MyBatis-spring 中,则使用 SqlSessionFactoryBean 来替代。

    Setup

    要创建工厂 bean,放置下面的代码在 Spring 的 XML 配置文件中:

        <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
        </bean>

    要注意 SqlSessionFactoryBean 实现了 Spring 的 FactoryBean 接口,这就说明了由 Spring 最终创建的 bean 不是 SqlSessionFactoryBean 本身,  而是工厂类的 getObject()返回的方法的结果。这种情况下,Spring 将会在应用启动时为你创建 SqlSessionFactory 对象,然后将它以 SqlSessionFactory 为名来存储。在 Java 中, 相同的代码是:

    SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
    SqlSessionFactory sessionFactory = factoryBean.getObject();

    在一般的 MyBatis-Spring 用法中, 你不需要直接使用 SqlSessionFactoryBean 或和其对 应的 SqlSessionFactory。相反,session 工厂将会被注入到 MapperFactoryBean 或其它扩 展了 SqlSessionDaoSupport 的 DAO中。

    属性

    SqlSessionFactory 有一个单独的必须属性,就是 JDBC 的 DataSource。这可以是任意 的 DataSource,其配置应该和其它 Spring 数据库连接是一样的。

    一个通用的属性是 configLocation,它是用来指定 MyBatis 的 XML 配置文件路径的。 如果基本的 MyBatis 配置需要改变, 那么这就是一个需要它的地方。 通常这会是<settings> 或<typeAliases>的部分。

    要注意这个配置文件不需要是一个完整的 MyBatis 配置。确切地说,任意环境,数据源 和 MyBatis 的事务管理器都会被忽略。SqlSessionFactoryBean 会创建它自己的,使用这些 值定制 MyBatis 的 Environment 时是需要的。

    如果 MyBatis 映射器 XML 文件在和映射器类相同的路径下不存在,那么另外一个需要 配置文件的原因就是它了。使用这个配置,有两种选择。第一是手动在 MyBatis 的 XML 配 置文件中使用<mappers>部分来指定类路径。第二是使用工厂 bean 的 mapperLocations 属 性。

    mapperLocations 属性使用一个资源位置的 list。 这个属性可以用来指定 MyBatis 的 XML 映射器文件的位置。 它的值可以包含 Ant 样式来加载一个目录中所有文件, 或者从基路径下 递归搜索所有路径。比如:

        <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="mapperLocations" value="classpath:mybatis/mapper/ibs/*/*Dao.xml"></property>
        </bean>

    这会从类路径下加载在 mybatis.mapper.ibs 包和它的子包中所有的 MyBatis 映射器 XML 文件。

    啦啦啦

  • 相关阅读:
    VMware Workstation 6.0 正式版公布
    KMyMoney:全体理财好管家
    Bugzilla 3.0 公布
    Brightside:切换工作区的小东西
    QTM-Blogging 客户端
    MDF2ISO-将 MDF 转换为 ISO
    Yakuake 2.8 beta1
    Red Hat 的 Liberation 字体
    Dictman:有效的词典呆板人
    digiKam 0.9.2 Beta 1
  • 原文地址:https://www.cnblogs.com/ClassNotFoundException/p/6820323.html
Copyright © 2011-2022 走看看