符:

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.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://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.xsdhttp://www.springframework.org/schema/txclasspath: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 文件。

啦啦啦

MyBatis -- Spring -- SqlSessionFactoryBean相关推荐

  1. Mybatis异常:java.lang.ClassNotFoundException: org.mybatis.spring.SqlSessionFactoryBean

    Mybatis异常:java.lang.ClassNotFoundException: org.mybatis.spring.SqlSessionFactoryBean 参考文章: (1)Mybati ...

  2. springMvc+mybatis+spring 整合 包涵整合activiti 基于maven

    2019独角兽企业重金招聘Python工程师标准>>> 最近自己独立弄一个activiti项目,写一下整合过程: 环境:jdk1.7 tomcat7.0 maven3.5  ecli ...

  3. Mybatis+Spring SqlSessionTemplate注入学习--1

    1.我们创建一个maven工程,创建的时候选择packageing 为war形式,也就是一个web工程 2.工程创建好之后,我们在pom.xml中引入相关的jar,要想使用Spring+mybatis ...

  4. MyBatis+Spring整合

    不多说,直接上案例! 1. 建立数据库+表 CREATE TABLE s_user(user_id INT AUTO_INCREMENT PRIMARY KEY,user_name VARCHAR(3 ...

  5. Maven+Struts2+MyBatis+Spring搭建教程

    教程目标: 在MyEclipse中搭建Struts2+MyBatis+Spring环境,用Maven管理项目,利用mybatis-gernerator插件生成部分代码 附带目标: struts2的自定 ...

  6. springmvc+mybatis+spring+redis

    只作参考,以防忘记使用! mybatis的配置文件: <?xml version="1.0" encoding="UTF-8" ?> <!DO ...

  7. MyBatis简介与配置MyBatis+Spring+MySql

    一.MyBatis简介与配置MyBatis+Spring+MySql 原文出自:http://limingnihao.iteye.com/blog/781671 MyBatis学习 之 一.MyBat ...

  8. 搭建eclipse版的ssm+maven+tk.mybatis+redis及mybatis+spring多数据源配置集成的demo

    前言:我这里搭建好eclipse版的ssm+maven+tk.mybatis+redis及mybatis+spring多数据源配置集成的demo.新手快速上手直接看demo. 最后处提供完整高质量de ...

  9. Maven+Mybatis+Spring配置

    第一步:配置pom.xml依赖 1.配置slfj+logback:  Java代码   <!-- Logging -->   <dependency>   <groupI ...

最新文章

  1. 文本类控件(EditView 的介绍)
  2. 特斯拉打响自动驾驶芯片反击战!
  3. 7大类卷积神经网络(CNN)创新综述
  4. python threading多线程计算
  5. keepalived 多实例
  6. PyTorch-模型可视化工具TorchSummary
  7. iOS学习之iOS沙盒(sandbox)机制和文件操作复习
  8. Chrome开发者工具关于网络请求的一个隐藏技能 1
  9. 某猪微店状元分销V2.0钻石版-全开源纯净安装版
  10. solaris linux nfs,solaris 10 nfs服务配置
  11. Laravel用post提交表单常见的两个错误
  12. 如何提高lstm的预测精度_如何提高失重秤的喂料精度?你需要了解这些!
  13. java 补位_Java 经典问题
  14. RandomCodeUtil随机数工具类,随机生成数字、字母、数字字母组合、中文姓名
  15. 网站服务器无返回数据,服务器无返回数据处理
  16. centos7安装禅道
  17. 解决谷歌浏览器自动填充表单
  18. 锐浪报表 Grid++Report PrintPreview 显示模式
  19. 【IDE工具】win10电脑设置保护眼睛色
  20. 百度360争推1TB永久网盘

热门文章

  1. 读书笔记-《增长黑客》-低成本、高效率的精准营销
  2. (0104)iOS开发之在Mac上用Charles给iPhone抓包
  3. (0072)iOS开发之UITableViewCell高度自适应探索--cell预估高度
  4. (0034) iOS 开发之UIView动画(过渡效果)
  5. JMeter断言介绍
  6. Effect Java 学习笔记-对象的创建与销毁
  7. vue-router 的重定向-redirect
  8. 加载静态文件,父模板的继承和扩展
  9. 网站下载器WebZip、Httrack及AWWWB.COM网站克隆器
  10. Android_注解+反射代替findViewById()