转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6964162.html

前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(八)——MyBatis查询缓存

1.整合思路

需要Spring通过单例方式管理SqlSessionFactory。

Spring和MyBatis整合生成代理对象,使用SqlSessionFactory创建SqlSession。(Spring和MyBatis整合自动完成)

持久层的mapper都需要由Spring进行管理。

2.整合环境

创建一个java工程(接近实际开发的工程结构)

jar包:

mybatis3.2.7的jar包

spring3.2.0的jar包

dbcp连接池

数据库驱动

mybatis和spring的整合包:早期ibatis和spring整合是由spring官方提供,mybatis和spring整合由mybatis提供。

3.spring配置文件

在applicationContext.xml配置sqlSessionFactory和数据源。SqlSessionFactory在mybatis和spring的整合包下。

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
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.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!-- 加载配置文件 -->
<context:property-placeholder location="classpath:db.properties"/> <!-- 数据库连接池,使用dbcp -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxActive" value="10"/>
<property name="maxIdle" value="5"/>
</bean> <!-- SqlSessionFactory配置 -->
<!-- 让Spring管理SqlSessionFactory使用mybatis和spring整合包中的 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据库连接池 -->
<property name="dataSource" ref="dataSource" />
<!-- 加载mybatis的全局配置文件 -->
<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
</bean> </beans>

4.Mapper编写的三种方法

4.1原始dao开发(和spring整合后)

4.1.1User.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace命名空间,作用就是对sql进行分类化的管理,理解为sql隔离
注意:使用mapper代理开发时,namespace有特殊作用 -->
<mapper namespace="test">
<!--在映射文件中配置很多sql语句 -->
<!--需求:通过id查询用户表的记录 -->
<!--id:标识映射文件中的sql,称为statement的id。将sql语句封装在mapperStatement的对象中,所有id称为Statement的id;
parameterType:指定输入参数的类型,这里指定int型;
#{}:表示一个占位符;
#{id}:其中id表示接收输入的参数,参数名称就是id,如果输入参数是简单类型,#{}中的参数名可以任意,可以是value或其它名称;
resultType:指定输出结果所映射的Java对象类型,select指定resultType表示将单条记录映射成Java对象。
-->
<select id="findUserById" parameterType="int" resultType="joanna.yan.po.User">
select * from user where id=#{value}
</select> </mapper>

在SqlMapConfig.xml中加载User.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>
<!--
批量别名的定义:
package:指定包名,mybatis会自动扫描包中的pojo类,自定义别名,别名就是类名(首字母大写或小写都可以) -->
<typeAliases>
<package name="joanna.yan.po"/>
</typeAliases> <mappers>
<mapper resource="sqlmap/User.xml"/>
<!-- 批量加载映射配置文件,mybatis自动扫描包下的mapper接口进行加载;
遵循一定的规范:需要将mapper接口类名和mapper.xml映射文件名称保持一致,且在一个目录中;
以上规范的前提是:使用的是mapper代理方法;
-->
<package name="joanna.yan.mapper"/>
</mappers>
</configuration>

4.1.2 dao(实现类继承SqlSessionDaoSupport)

public interface UserDao {
//根据id查询用户信息
public User findUserById(int id) throws Exception;
}

dao接口实现类需要注入SqlSessionFactory,通过spring进行注入。

这里用spring声明配置方式,配置dao的bean:让UserDaoImpl实现类继承SqlSessionDaoSupport。

public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao{

    @Override
public User findUserById(int id) throws Exception {
//继承SqlSessionDaoSupport,通过this.getSqlSession()得到sqlSession
SqlSession sqlSession=this.getSqlSession();
User user=sqlSession.selectOne("test.findUserById", id);
return user;
}
}

4.1.3配置dao

在applicationContext.xml中配置dao。

    <!-- 方法一:原始dao接口  -->
<bean id="userDao" class="joanna.yan.dao.UserDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>

4.1.4测试程序

public class UserDaoImplTest {
private ApplicationContext applicationContext; @Before
public void setUp(){
applicationContext=new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
} @Test
public void findUsetByIdTest() throws Exception{
UserDao userDao=(UserDao) applicationContext.getBean("userDao");
User user=userDao.findUserById(1);
System.out.println(user);
}
}

4.2mapper代理开发

4.2.1mapper.xml和mapper.java

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace命名空间,作用就是对sql进行分类化的管理,理解为sql隔离
注意:使用mapper代理开发时,namespace有特殊作用,namespace等于mapper接口地址 -->
<mapper namespace="joanna.yan.mapper.UserMapper">
<select id="findUserById" parameterType="int" resultType="user">
select * from user where id=#{value}
</select>
</mapper>
public interface UserMapper {
public User findUserById(int id) throws Exception;
}

4.2.2通过MapperFactoryBean创建代理对象

在applicationContext.xml中配置mapper。

    <!-- 方法二:mapper配置
MapperFactoryBean:根据mapper接口生成代理对象
-->
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="joanna.yan.mapper.UserMapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>

4.2.3测试程序

public class UserMapperTest {
private ApplicationContext applicationContext; @Before
public void setUp(){
applicationContext=new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
} @Test
public void findUsetByIdTest() throws Exception{
UserMapper userMapper=(UserMapper) applicationContext.getBean("userMapper");
User user=userMapper.findUserById(1);
System.out.println(user);
}
}

此方法的问题:需要针对每个mapper进行配置,麻烦。

4.3通过MapperScannerConfigurer进行mapper扫描(建议使用)

在applicationContext.xml中配置mapper批量扫描。

    <!-- 方法三:mapper批量扫描
从mapper包中扫描出mapper接口,自动创建代理对象并且在spring容器中注册
遵循规范:将mapper.java和mapper.xml映射文件名称保持一致,且在一个目录中。
自动扫描出来的mapper的bean的id为mapper类名(首字母小写)
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 指定扫描的包名
如果扫描多个包,每个包中间使用半角逗号分隔
-->
<property name="basePackage" value="joanna.yan.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>

此时,SqlMapConfig.xml中批量加载joanna.yan.mapper下mapper的配置就可以省略掉了。

测试程序:

@Test
public void findUsetByIdTest() throws Exception{
UserMapper userMapper=(UserMapper) applicationContext.getBean("userMapper");
User user=userMapper.findUserById(1);
System.out.println(user);
}

如果此文对您有帮助,微信打赏我一下吧~

Spring+SpringMVC+MyBatis深入学习及搭建(九)——MyBatis和Spring整合的更多相关文章

  1. Spring+SpringMVC+MyBatis深入学习及搭建(十)——MyBatis逆向工程

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6973266.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(九)--My ...

  2. Spring+SpringMVC+MyBatis深入学习及搭建(二)——MyBatis原始Dao开发和mapper代理开发

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6869133.html 前面有写到Spring+SpringMVC+MyBatis深入学习及搭建(一)——My ...

  3. Spring+SpringMVC+MyBatis深入学习及搭建(三)——MyBatis全局配置文件解析

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6874672.html 前面有写到Spring+SpringMVC+MyBatis深入学习及搭建(二)——My ...

  4. Spring+SpringMVC+MyBatis深入学习及搭建(四)——MyBatis输入映射与输出映射

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6878529.html 前面有讲到Spring+SpringMVC+MyBatis深入学习及搭建(三)——My ...

  5. Spring+SpringMVC+MyBatis深入学习及搭建(七)——MyBatis延迟加载

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6953005.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(六)——My ...

  6. Spring+SpringMVC+MyBatis深入学习及搭建(八)——MyBatis查询缓存

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6956206.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(七)——My ...

  7. Spring+SpringMVC+MyBatis深入学习及搭建(六)——MyBatis关联查询

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6923464.html 前面有将到:Spring+SpringMVC+MyBatis深入学习及搭建(五)--动 ...

  8. Spring+SpringMVC+MyBatis深入学习及搭建(一)——MyBatis的基础知识

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6812311.html 1.对原生态jdbc程序中问题总结 1.1 jdbc程序 需求:使用jdbc查询mys ...

  9. Spring+SpringMVC+MyBatis深入学习及搭建(五)——动态sql

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6908763.html 前面有讲到Spring+SpringMVC+MyBatis深入学习及搭建(四)——My ...

随机推荐

  1. oracle调用array参数存储过程

    declare -- Non-scalar parameters require additional processing files tyt_gas2014_search; ,); temp1 t ...

  2. HDU 5556 最大独立集

    这题主要有了中间的一些连通块的限制,不太好直接用二分图最大独立集做.考虑到图比较小,可以作补图求最大团来求解. #include <iostream> #include <vecto ...

  3. Linux-进程描述(4)之进程优先级与进程创建执行

    进程优先级 进程cpu资源分配就是指进程的优先权(priority).优先权高的进程有优先执行权利. 权限与优先级.权限(privilege)是指在多用户计算机系统的管理中,某个特定的用户具有特定的系 ...

  4. Hadoop - 操作练习之单机配置 - Hadoop2.8.0/Ubuntu16.04

    系统版本 anliven@Ubuntu1604:~$ uname -a Linux Ubuntu1604 4.8.0-36-generic #36~16.04.1-Ubuntu SMP Sun Feb ...

  5. JavaEE开发之SpringMVC中的自定义消息转换器与文件上传

    上篇博客我们详细的聊了<JavaEE开发之SpringMVC中的静态资源映射及服务器推送技术>,本篇博客依然是JavaEE开发中的内容,我们就来聊一下SpringMVC中的自定义消息转发器 ...

  6. jQuery修炼心得-DOM节点的删除

    要移除页面上节点是开发者常见的操作,jQuery提供了几种不同的方法用来处理这个问题. 1.empty empty 顾名思义,清空方法,但是与删除又有点不一样,因为它只移除了 指定元素中的所有子节点. ...

  7. webService 下得 拦截

    当我们 对webservice 接口的 拦截 更具权限  来 判断 是否可以调用  一下是我的 一个demo 首先 我们写一个 拦截类 import javax.xml.soap.SOAPExcept ...

  8. [luoguP2912] [USACO08OCT]牧场散步Pasture Walking(lca)

    传送门 水题. 直接倍增求lca. x到y的距离为dis[x] + dis[y] - 2 * dis[lca(x, y)] ——代码 #include <cstdio> #include ...

  9. 微信小程序框架

    框架 小程序开发框架的目标是通过尽可能简单.高效的方式让开发者可以在微信中开发具有原生 APP 体验的服务. 框架提供了自己的视图层描述语言 WXML 和 WXSS,以及基于 JavaScript 的 ...

  10. selenium+python

    最近在学习selenium自动化测试,但是一直遇到一个问题,总是打不开指定的网址,今天突然成功了, 主要原因是因为selenium版本太低的缘故,所以只需要在终端输入:pip install -U s ...