一、准备工作:

1.1 添加相应的jar包依赖:

这里用到了两个jar包,一个是mybatis的,另一个是mybatis-spring的,代码如下:

1
2
3
4
5
6
7
8
9
10
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.2.2</version>
</dependency>
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>1.1.1</version>
</dependency>

1.2 spring中配置mybatis

要想在spring中使用mybatis,需要在spring的上下文中配置两个内容:SqlSessionFactory和至少一个数据映射器。SqlSessionFactory使用mybatis-spring包中的SqlSessionFactoryBean,使用SqlSessionTemplete实现SqlSession接口。代码如下:

1
2
3
4
5
6
7
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="configLocation" value="classpath:/mybatis/mapperConfig.xml"></property>
        <property name="dataSource" ref="dataSource"/>
</bean>

需要注意的是,SqlSessionFactory需要dataSource。

Mybatis添加到Spring的更多相关文章

  1. 【Mybatis】MyBatis之整合Spring(八)

    创建环境 系统:macOS Java:1.8 软件:eclipse,maven,mysql 创建步骤 本例:创建一个Maven项目(SpringMVC+Spring+Mybatis),页面上展示员工列 ...

  2. MyBatis 3 与 Spring 4 整合关键

    MyBatis 3 与 Spring 4 整合关键 MyBatis与Spring整合,首先需要一个Spring数据源.其次有两个关键,配置sqlSessionFactory时需要配置扫描sql映射xm ...

  3. MyBatis 学习-与 Spring 集成篇

    根据官方的说法,在 ibatis3,也就是 Mybatis3 问世之前,Spring3 的开发工作就已经完成了,所以 Spring3 中还是没有对 Mybatis3 的支持.因此由 Mybatis 社 ...

  4. mybatis添加记录时返回主键id

    参考:mybatis添加记录时返回主键id 场景 有些时候我们在添加记录成功后希望能直接获取到该记录的主键id值,而不需要再执行一次查询操作.在使用mybatis作为ORM组件时,可以很方便地达到这个 ...

  5. MyBatis之整合Spring

    MyBatis之整合Spring 整合思路: 1.SqlSessionFactory对象应该放到spring容器中作为单例存在 2.传统dao的开发方式中,应该从spring容器中获得sqlSessi ...

  6. MyBatis在非Spring环境下第三方DataSource设置-Druid篇

    首先在ITEye上面看到一个同标题文章,在此说明,此文并非转载自 http://iintothewind.iteye.com/blog/2069522 ,因为这篇文章根本就是错误的,照着上面做,工程可 ...

  7. 深入浅出mybatis之与spring集成

    目录 写在前面 详细配置 1.dataSource(数据源) 2.sqlSessionFactory(Session工厂) 3.Mapper(映射器) 4.TransactionManager(事务管 ...

  8. mybatis源码分析(8)-----事务(mybatis管理、spring管理)

    写在前面 接口:MyBatis的事务Transaction的接口有一下实现类 JdbcTransaction 由jdbc管理的事务(即利用Connection对象完成对事务的提交(commit()). ...

  9. 因此mybatis最好与spring集成起来使用

    单独使用mybatis是有很多限制的(比如无法实现跨越多个session的事务),而且很多业务系统本来就是使用spring来管理的事务,因此mybatis最好与spring集成起来使用. spring ...

随机推荐

  1. JS Date.parse() 函数详解

    Date.parse()函数用于分析一个包含日期的字符串,并返回该日期与 1970 年 1 月 1 日午夜之间相差的毫秒数. 该函数属于Date对象,所有主流浏览器均支持该函数. 语法 Date.pa ...

  2. go:channel(未完)

    注:1)以下的所有讨论建立在包含整形元素的通道类型之上,即 chan int 2)对于“<-”我的理解是,它可能是一个操作符(接收操作符),也  可能是类型的一部分(如“chan<- in ...

  3. Python初学者应了解的技巧

    交换变量 x = 6 y = 5 x, y = y, x print x >>> 5 print y >>> 6 if 语句在行内 print "Hell ...

  4. 1.vim常用方法

    vim编辑器 ASCII   vi: vim:   全屏,模式化   vim模式: 编辑模式(命令模式) 输入模式 末行模式   模式转换: 编辑-->输入 i,a,o (o:在当前光标所在行的 ...

  5. 一个简单的判断浏览器是否为IE9以下的方法

    if(!-[1,]){ //是IE placeHolderIE9_(); }else{ //非IE if(navigator.userAgent.indexOf("MSIE 9.0" ...

  6. ASP中Lable控件的定位问题

    问题:Lable控件的定位问题:找了好久都没找到可以将Lable控件定位的办法,网上说可以将修改position这个属性来实现定位,可是我始终没找到这个属性. (1)首先,在源代码中添加 style ...

  7. Linux 昨天时间

    今天date +%F昨天date -d yesterday +%F明天date -d tomorrow +%F七天前date -d "7 days ago" +%F

  8. vue.js 接收url参数

    1) 路由配置传参方式 在配置路由时 例如 "/firewall/authorize/:uid/:uname/:token" 页面url为 http://XXX.com/firew ...

  9. 假如 UNION ALL 里面的子句 有 JOIN ,那个执行更快呢

    比如: select id, name from table1 where name = 'x' union all select id, name from table2 where name =  ...

  10. eclipse工具常用快捷键总结

    1.  ctrl + shift + r  可以根据类名快速找到你所需要的类名称 2.  ctrl + shift + i  debug的时候可以查看代码的值 3.  ctrl + shift + f ...