深入理解MyBatis-Spring中间件(spring/mybatis整合)
转:http://blog.csdn.net/fqz_hacker/article/details/53485833
Mybatis-Spring
1.应用
- public interface UserMapper {
- int createUser(@Param("user") User user);
- }
然后,定义对应的mybatis xml文件,
- <?xml version="1.0" encoding="utf-8" ?>
- <!--PUBLIC后面跟着的可以用于验证文档结构的 DTD 系统标识符和公共标识符。-->
- <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="com.fqz.mybatis.dao.UserMapper"><!--namespace是必须的,指向对应的java interface,要把包名写全,此例中为com.fqz.mybatis.dao.UserMapper -->
- <resultMap id="User" type="User">
- <id property="id" column="id"/>
- <result property="name" column="name"/>
- <result property="sex" column="sex"/>
- <result property="mobile" column="mobile"/>
- </resultMap>
- <insert id="createUser" parameterType="User" useGeneratedKeys="true" keyColumn="id" keyProperty="user.id">
- INSERT INTO
- User
- (name,sex,mobile)
- VALUES
- (#{user.name},#{user.sex},#{user.mobile})
- </insert>
- </mapper>
紧接着,添加配置文件,命名为spring-dao.mxl,
- <?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"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context.xsd">
- <context:component-scan base-package="com.fqz.mybatis"/>
- <!-- Data Source -->
- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
- <property name="driverClass" value="com.mysql.jdbc.Driver"/>
- <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/fqz"/>
- <property name="user" value="root"/>
- <property name="password" value="abcd1234"/>
- <!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数 -->
- <property name="acquireIncrement" value="5"></property>
- <!-- 初始连接池大小 -->
- <property name="initialPoolSize" value="10"></property>
- <!-- 连接池中连接最小个数 -->
- <property name="minPoolSize" value="5"></property>
- <!-- 连接池中连接最大个数 -->
- <property name="maxPoolSize" value="20"></property>
- </bean>
- <!-- 扫描对应的XML Mapper -->
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <!-- 数据源 -->
- <property name="dataSource" ref="dataSource"></property>
- <!-- 别名,它一般对应我们的实体类所在的包,这个时候会自动取对应包中不包括包名的简单类名作为包括包名的别名。多个package之间可以用逗号或者分号等来进行分隔。 -->
- <property name="typeAliasesPackage" value="com.fqz.mybatis.entity"></property>
- <!-- sql映射文件路径,它表示我们的Mapper文件存放的位置,当我们的Mapper文件跟对应的Mapper接口处于同一位置的时候可以不用指定该属性的值。 -->
- <property name="mapperLocations" value="classpath*:mybatis/*.xml"></property>
- </bean>
- <!-- 扫描对应的Java Mapper -->
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="basePackage" value="com.fqz.mybatis.dao"/>
- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
- </bean>
- </beans>
- @Service
- public class UserServiceImpl implements UserService {
- @Resource
- UserMapper userMapper;
- public int createUser(UserDTO userDTO) {
- User user = new User();
- BeanUtils.copyProperties(userDTO,user,new String[]{"id"});
- int row = userMapper.createUser(user);//插入返回值为作用的记录数;生成的主键已经被赋值到user对象上
- if(row >= 1)
- return user.getId();
- return -1;
- }
- public UserDTO getUserById(Integer id) {
- return null;
- }
- }
完成接口定义、mybatis xml定义和配置文件,就可以直接使用接口来操作数据库,
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations = {"classpath*:spring/spring-*.xml"})
- public class TestUser{
- private static UserService userService;
- @BeforeClass
- public static void beforeClass(){
- ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/spring-dao.xml");
- userService = context.getBean(UserService.class);
- }
- @Test
- public void testCreatUser(){
- UserDTO userDTO = new UserDTO();
- userDTO.setName("your name");
- userDTO.setSex(true);
- userDTO.setMobile("12134232211");
- Integer userId = userService.createUser(userDTO);
- System.out.println(userId);
- System.out.println(userDTO.getId());
- }
- }
2. 原理
从UserServiceImpl实现类中可以看出,服务类直接使用的UserMapper接口来操作数据库,而UserMapper接口没有对应的实现
类,这一切都是由spring-mybatis库通过动态代理实现的,接下来分析下它的实现原理,先由SqlSessionFactoryBean生成
SQLSessionFactory和并扫描接口,为接口生成动态代理
1. SqlSessionFactoryBean配置
- public class SqlSessionFactoryBean implements FactoryBean<SqlSessionFactory>, InitializingBean
SqlSessionFactoryBean
实现了FactoryBean和InitializingBean,因此会首先执行afterPropertiesSet()方法,然后根据
getObject()方法返回的对象生产Spring
Bean,这里生产的是SqlSessionFactory类型的对象,在afterPropertiesSet方法中,执行了
buildSqlSessionFactory方法来初始化SqlSessionFactory对象,来看看其中的一段,
- if (!isEmpty(this.mapperLocations)) {
- for (Resource mapperLocation : this.mapperLocations) {
- if (mapperLocation == null) {
- continue;
- }
- try {
- XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(mapperLocation.getInputStream(),
- configuration, mapperLocation.toString(), configuration.getSqlFragments());
- xmlMapperBuilder.parse();
- }
进入XmlMapperBuilder.parse方法,
- private void bindMapperForNamespace() {
- String namespace = builderAssistant.getCurrentNamespace();
- if (namespace != null) {
- Class<?> boundType = null;
- try {
- boundType = Resources.classForName(namespace);
- } catch (ClassNotFoundException e) {
- //ignore, bound type is not required
- }
- if (boundType != null) {
- if (!configuration.hasMapper(boundType)) {
- // Spring may not know the real resource name so we set a flag
- // to prevent loading again this resource from the mapper interface
- // look at MapperAnnotationBuilder#loadXmlResource
- configuration.addLoadedResource("namespace:" + namespace);
- configuration.addMapper(boundType);
- }
- }
- }
- }
namespace指定了mybatis dao接口的路径,通过configuration.addMapper(boundType)将接口的动态代理委托给MapperRegistry,MapperRegistry中通过MapperFactory生成动态代理,
- public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
- final MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory<T>) knownMappers.get(type);
- if (mapperProxyFactory == null) {
- throw new BindingException("Type " + type + " is not known to the MapperRegistry.");
- }
- try {
- return mapperProxyFactory.newInstance(sqlSession);
- } catch (Exception e) {
- throw new BindingException("Error getting mapper instance. Cause: " + e, e);
- }
- }
- public <T> boolean hasMapper(Class<T> type) {
- return knownMappers.containsKey(type);
- }
- public <T> void addMapper(Class<T> type) {
- if (type.isInterface()) {
- if (hasMapper(type)) {
- throw new BindingException("Type " + type + " is already known to the MapperRegistry.");
- }
- boolean loadCompleted = false;
- try {
- knownMappers.put(type, new MapperProxyFactory<T>(type));
- // It's important that the type is added before the parser is run
- // otherwise the binding may automatically be attempted by the
- // mapper parser. If the type is already known, it won't try.
- MapperAnnotationBuilder parser = new MapperAnnotationBuilder(config, type);
- parser.parse();
- loadCompleted = true;
- } finally {
- if (!loadCompleted) {
- knownMappers.remove(type);
- }
- }
- }
- }
MapperProxyFactory中使用Proxy.newProxyInstance来生成动态代理
- protected T newInstance(MapperProxy<T> mapperProxy) {
- return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);
- }
- public T newInstance(SqlSession sqlSession) {
- final MapperProxy<T> mapperProxy = new MapperProxy<T>(sqlSession, mapperInterface, methodCache);
- return newInstance(mapperProxy);
- }
MapperProxy为动态代理的处理类,实际上将SqlSession操作db的过程封装在了MapperMethod类中,
- public MapperProxy(SqlSession sqlSession, Class<T> mapperInterface, Map<Method, MapperMethod> methodCache) {
- this.sqlSession = sqlSession;
- this.mapperInterface = mapperInterface;
- this.methodCache = methodCache;
- }
- @Override
- public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
- if (Object.class.equals(method.getDeclaringClass())) {
- try {
- return method.invoke(this, args);
- } catch (Throwable t) {
- throw ExceptionUtil.unwrapThrowable(t);
- }
- }
- final MapperMethod mapperMethod = cachedMapperMethod(method);
- return mapperMethod.execute(sqlSession, args);
- }
简单看下结构,
总结,SqlSessionFactoryBean实际上对应的是SqlSessionFactory类,它会扫描sql
xml文件,并对接口创建动态代理,将接口类的Class和动态代理关系保存在SqlSessionFactory中,这仅仅是完成了动态代理的生成,而
动态代理在哪里被使用到,怎么使用,这些都是由MapperScannerConfigurer完成,接下来看看
MapperScannerConfigurer都做了些什么?
2. MapperScannerConfigurer
接口所在package,之前也说过SqlSessionFactoryBean实际上对应的是SqlSessionFactory,它可以提供
Mapper接口的动态代理类,而Mapper所在package提供了扫描的路径,在扫描过程中,会把每个Mapper接口对应到一个
MapperFactoryBean,MapperFactoryBean实际上对应的是动态代理类,这一切也就说通了,下面来看看源码,
factory初始化的时候,被调用到,调用的方法为postProcessBeanDefinitionRegistry,因此看看
postProcessBeanDefinitionRegistry的源码,
- public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) {
- if (this.processPropertyPlaceHolders) {
- processPropertyPlaceHolders();
- }
- ClassPathMapperScanner scanner = new ClassPathMapperScanner(registry);
- scanner.setAddToConfig(this.addToConfig);
- scanner.setAnnotationClass(this.annotationClass);
- scanner.setMarkerInterface(this.markerInterface);
- scanner.setSqlSessionFactory(this.sqlSessionFactory);
- scanner.setSqlSessionTemplate(this.sqlSessionTemplate);
- scanner.setSqlSessionFactoryBeanName(this.sqlSessionFactoryBeanName);
- scanner.setSqlSessionTemplateBeanName(this.sqlSessionTemplateBeanName);
- scanner.setResourceLoader(this.applicationContext);
- scanner.setBeanNameGenerator(this.nameGenerator);
- scanner.registerFilters();
- scanner.scan(StringUtils.tokenizeToStringArray(this.basePackage, ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS));
- }
调用了ClassPathMapperScanner的scan()方法,
- public int scan(String... basePackages) {
- int beanCountAtScanStart = this.registry.getBeanDefinitionCount();
- doScan(basePackages);
- // Register annotation config processors, if necessary.
- if (this.includeAnnotationConfig) {
- AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry);
- }
- return (this.registry.getBeanDefinitionCount() - beanCountAtScanStart);
- }
scan
方法又调用了doScan方法,看看ClassPathMapperScanner的doScan方法,Spring会首先把需要实例化的bean加载的
BeanDefinitionHolder的集合中,doScan方法,就是添加mybatis
mapper接口的bean定义到BeanDefinitionHolder集合,
- public Set<BeanDefinitionHolder> doScan(String... basePackages) {
- Set<BeanDefinitionHolder> beanDefinitions = super.doScan(basePackages);
- if (beanDefinitions.isEmpty()) {
- logger.warn("No MyBatis mapper was found in '" + Arrays.toString(basePackages) + "' package. Please check your configuration.");
- } else {
- processBeanDefinitions(beanDefinitions);
- }
- return beanDefinitions;
- }
- private void processBeanDefinitions(Set<BeanDefinitionHolder> beanDefinitions) {
- GenericBeanDefinition definition;
- for (BeanDefinitionHolder holder : beanDefinitions) {
- definition = (GenericBeanDefinition) holder.getBeanDefinition();
- if (logger.isDebugEnabled()) {
- logger.debug("Creating MapperFactoryBean with name '" + holder.getBeanName()
- + "' and '" + definition.getBeanClassName() + "' mapperInterface");
- }
- // the mapper interface is the original class of the bean
- // but, the actual class of the bean is MapperFactoryBean
- definition.getConstructorArgumentValues().addGenericArgumentValue(definition.getBeanClassName()); // issue #59
- definition.setBeanClass(this.mapperFactoryBean.getClass());//将其bean Class类型设置为mapperFactoryBean,放入BeanDefinitions
- definition.getPropertyValues().add("addToConfig", this.addToConfig);
- boolean explicitFactoryUsed = false;
- if (StringUtils.hasText(this.sqlSessionFactoryBeanName)) {
- definition.getPropertyValues().add("sqlSessionFactory", new RuntimeBeanReference(this.sqlSessionFactoryBeanName));
- explicitFactoryUsed = true;
- } else if (this.sqlSessionFactory != null) {
- definition.getPropertyValues().add("sqlSessionFactory", this.sqlSessionFactory);
- explicitFactoryUsed = true;
- }
- if (StringUtils.hasText(this.sqlSessionTemplateBeanName)) {
- if (explicitFactoryUsed) {
- logger.warn("Cannot use both: sqlSessionTemplate and sqlSessionFactory together. sqlSessionFactory is ignored.");
- }
- definition.getPropertyValues().add("sqlSessionTemplate", new RuntimeBeanReference(this.sqlSessionTemplateBeanName));
- explicitFactoryUsed = true;
- } else if (this.sqlSessionTemplate != null) {
- if (explicitFactoryUsed) {
- logger.warn("Cannot use both: sqlSessionTemplate and sqlSessionFactory together. sqlSessionFactory is ignored.");
- }
- definition.getPropertyValues().add("sqlSessionTemplate", this.sqlSessionTemplate);
- explicitFactoryUsed = true;
- }
- if (!explicitFactoryUsed) {
- if (logger.isDebugEnabled()) {
- logger.debug("Enabling autowire by type for MapperFactoryBean with name '" + holder.getBeanName() + "'.");
- }
- definition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE);
- }
- }
- }
那MapperFactoryBean究竟又做了什么呢,看源码,
- MapperFactoryBean<T> extends SqlSessionDaoSupport implements FactoryBean
通过集成SqlSessionDaoSupport获得SqlSessionFactory,通过实现FactoryBean,生产动态代理对象,
- @Override
- public T getObject() throws Exception {
- return getSqlSession().getMapper(this.mapperInterface);
- }
一
切到这里就已经很显而易见了,Mapper接口对应的Spring
Bean实际上就是getSqlSession().getMapper(this.mapperInterface)返回的动态代理,每次装配
Mapper接口时,就相当于装配了此接口对应的动态代理,这样就顺利成章的被代理成功了。
深入理解MyBatis-Spring中间件(spring/mybatis整合)的更多相关文章
- Spring、Spring MVC、MyBatis整合文件配置详解
原文 http://www.cnblogs.com/wxisme/p/4924561.html 主题 MVC模式MyBatisSpring MVC 使用SSM框架做了几个小项目了,感觉还不错是时候总 ...
- spring、spring mvc、mybatis框架整合基本知识
学习了一个多月的框架知识了,这两天很想将它整合一下.网上看了很多整合案例,基本都是基于Eclipse的,但现在外面公司基本都在用Intellij IDEA了,所以结合所学知识,自己做了个总结,有不足之 ...
- 转载 Spring、Spring MVC、MyBatis整合文件配置详解
Spring.Spring MVC.MyBatis整合文件配置详解 使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用法最好还是看官方文档. ...
- Spring、Spring MVC、MyBatis整合文件配置详解2
使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用法最好还是看官方文档. Spring:http://spring.io/docs MyBatis ...
- Spring、Spring MVC、MyBatis 整合文件配置详解
使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用法最好还是看官方文档. Spring:http://spring.io/docs MyBatis ...
- MyBatis原理,Spring、SpringBoot整合MyBatis
1. MyBatis概述 MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可 ...
- spring mvc与mybatis与maven+mysql框架整合
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"% ...
- Spring MVC Spring MyBatis 整合 - 快速上手
我个人比较喜欢写注释,在工作中对注释的重要性看的也比较高,所以大部分文字都在注释中,代码外的文字会写的偏少,关键能懂就行 先看一下整合后的工程目录(单工程,多工程以后会采用maven) 5个packa ...
- 搭建Spring + SpringMVC + Mybatis框架之二(整合Spring和Mybatis)
整合Spring和Mybatis 首先给出完整的项目目录: (1)引入项目需要的jar包 使用http://maven.apache.org作为中央仓库即可. Spring核心包,mybatis核心包 ...
- MyBatis 3 与 Spring 4 整合关键
MyBatis 3 与 Spring 4 整合关键 MyBatis与Spring整合,首先需要一个Spring数据源.其次有两个关键,配置sqlSessionFactory时需要配置扫描sql映射xm ...
随机推荐
- 懒汉式单例要加volatile吗
private static volatile Something instance = null; public static Something getInstance() { if (insta ...
- mapreduce求平均数
1. 现有某电商关于商品点击情况的数据文件,表名为goods_click,包含两个字段(商品分类,商品点击次数),分隔符“ ”,由于数据很大,所以为了方便统计我们只截取它的一部分数据,内容如下 ...
- Java-IO读写文件简单操作
Java中读写文件的关键在于节点流和处理流的的选取上,而节点流和处理流又分为面向字节.面向字符两种处理模式,因此,需要根据实际的情况选择合适的处理模式,一般而言,往往尽量尝试使用面向字符的处理模式,如 ...
- 使用codesmith无法连接mysql问题
最近研究codesmith的用法,遇到了如题的问题,记录一下解决的方法. 1.问题描述: 在codesmith中选择MySQLSchemaProvider并连接数据库时,会报以下错误: Test fa ...
- CAD安装失败怎样卸载CAD 2017?错误提示某些产品无法安装
AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...
- UGUI EventSystem.current.IsPointerOverGameObject(),判断是否进入了UI上
EventSystem.current.IsPointerOverGameObject(); //返回一个布尔值,进入了UI上就返回true,用的时候要 using UnityEngine.Event ...
- oracle简单命令
1.cmd 中 sqlplus /nolog 2.SQL> conn sys/password as sysdba
- 修改jar包bug的方式
第一种方式 1. 直接在项目同样的包名里面新建同样的class,会优先jar包的class加载,等同于覆盖. 第二种方式 2. 拿到第一步打包后的jar或者war,找到相应的java类的.class文 ...
- JDK一键安装,配置环境
人懒,就得“多干活”! 正常安装JDK 的话,它会修改系统的path 变量,加入自己的调用路径(jre) 这样,我们刚安装好后,就可直接调用java –verbose 查看jre 安装的目录了. ( ...
- 修改model,映射到表中
1. 当使用EF code first创建数据表后,数据库中会自动创建一个名为:dbo.__MigrationHistory的系统数据表, 如果尚未启用数据库迁移功能,那么每次在应用程序运行时,都会对 ...