Spring+SpringMVC+MyBatis深入学习及搭建(九)——MyBatis和Spring整合
转载请注明出处: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整合的更多相关文章
- Spring+SpringMVC+MyBatis深入学习及搭建(十)——MyBatis逆向工程
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6973266.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(九)--My ...
- Spring+SpringMVC+MyBatis深入学习及搭建(二)——MyBatis原始Dao开发和mapper代理开发
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6869133.html 前面有写到Spring+SpringMVC+MyBatis深入学习及搭建(一)——My ...
- Spring+SpringMVC+MyBatis深入学习及搭建(三)——MyBatis全局配置文件解析
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6874672.html 前面有写到Spring+SpringMVC+MyBatis深入学习及搭建(二)——My ...
- Spring+SpringMVC+MyBatis深入学习及搭建(四)——MyBatis输入映射与输出映射
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6878529.html 前面有讲到Spring+SpringMVC+MyBatis深入学习及搭建(三)——My ...
- Spring+SpringMVC+MyBatis深入学习及搭建(七)——MyBatis延迟加载
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6953005.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(六)——My ...
- Spring+SpringMVC+MyBatis深入学习及搭建(八)——MyBatis查询缓存
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6956206.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(七)——My ...
- Spring+SpringMVC+MyBatis深入学习及搭建(六)——MyBatis关联查询
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6923464.html 前面有将到:Spring+SpringMVC+MyBatis深入学习及搭建(五)--动 ...
- Spring+SpringMVC+MyBatis深入学习及搭建(一)——MyBatis的基础知识
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6812311.html 1.对原生态jdbc程序中问题总结 1.1 jdbc程序 需求:使用jdbc查询mys ...
- Spring+SpringMVC+MyBatis深入学习及搭建(五)——动态sql
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6908763.html 前面有讲到Spring+SpringMVC+MyBatis深入学习及搭建(四)——My ...
随机推荐
- oracle调用array参数存储过程
declare -- Non-scalar parameters require additional processing files tyt_gas2014_search; ,); temp1 t ...
- js 数组方法总结
Array数组: length属性 可通过array.length增加或者减少数组的长度,如;array.length=4(数组长3,第四位为undefined),也可单纯获得长度.array[arr ...
- WPF 杂谈——入门介绍
对于WPF的技术笔者是又爱又恨.现在WPF的市场并不是很锦气.如果以WPF来吃饭的话,只怕会饿死在街头.同时现在向面WEB开发更是如火冲天.所以如果是新生的话,最好不要以WPF为主.做为选择性来学习一 ...
- 漏洞预警 | Apache Struts2 曝任意代码执行漏洞 (S2-045)
近日,Apache官方发布Apache Struts 2.3.5–2.3.31版本及2.5–2.5.10版本存在远程代码执行漏洞(CNNVD-201703-152 ,CVE-2017-5638)的紧急 ...
- VopSdk一个高逼格微信公众号开发SDK
一.我们的目标 分离基础参数和业务参数. 具有高重用和扩展性. 轻量级. 二.实现目标 (一)分离基础参数和业务参数 仔细分析所有接口,抽离出每个模块接口的公共参数. A.针对微信公众号所有接口分析( ...
- Hadoop集群
你可以用以下三种支持的模式中的一种启动Hadoop集群: 单机模式 伪分布式模式 完全分布式模式 单机模式的操作方法 默认情况下,Hadoop被配置成以非分布式模式运行的一个独立Java进程.这对调试 ...
- 构建你人生里的第一个 Laravel 项目
安装 Laravel 安装器 composer global require "laravel/installer" 创建项目 laravel new links 检查是否安装成功 ...
- UIView的属性
.alpha 设置视图的透明度.默认为1. // 完全透明 view.alpha = ; // 不透明 view.alpha = ; .clipsToBounds // 默认是NO,当设置为yes时, ...
- python 小程序—三级菜单—循环和字典练习
程序中利用多级字典来存储三级菜单, 通过一系列while循环和for循环,实现了三级菜单的查询,选择,退回上级菜单,退出程序几个功能. 缺点:程序语句过于重复,效率低. #-*-coding:utf- ...
- php的文件引用
最近研究公司代码时发现了set_include_path(dirname(__FILE__));这样一行代码,在网上查了些资料,才把这个方法的作用弄清楚. 首先,dirname(__FILE__)这个 ...