MapperFactoryBean是mybati-spring团队提供的一个用于根据mapper接口生成mapper对象的类。

在spring配置文件中可以配置以下参数:

1.mapperInterface:用于指定接口

2.sqlSessionFactory:用于指定SqlSessionFactory。

3.sqlSessionTemplate:用于指定SqlSessionTemplate。如果和sqlSessionFactory同时配置,则只会启用sqlSessionTemplate。

例如:

/*
* 客户持久化类
*/ public class Customer {
private Integer id;
private String username;
private String jobs;
private String phone;
    setter/getter
    toString()
}
CustomerMapper接口
public interface CustomerMapper {
public Customer findCustomerById(Integer id);
}
映射文件:namespace命名空间有包名加接口名
<mapper namespace="com.itheima.po.mapper.CustomerMapper">
<select id="findCustomerById" parameterType="Integer" resultType="customer">
select * from t_customer where id=#{id}
</select>
</mapper>
在applicationContext.xml中的配置
<!--基于MapperFactoryBean开发-->
<bean id="customerMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.itheima.po.mapper.CustomerMapper"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
在mybatis-config.xml中的配置
<!--引入mapper文件位置-->
<mappers>
<mapper resource="com/itheima/po/mapper/CustomerMapper.xml"/>
</mappers>
测试
public class FindCustomerByIdTest {
@Test
public void findCustomerById() {
ApplicationContext applicationContex = new ClassPathXmlApplicationContext("applicationContext.xml");
CustomerMapper customerMapper = applicationContex.getBean(CustomerMapper.class);
Customer customer = customerMapper.findCustomerById(1);
System.out.println(customer);
}
}
结果:
Customer{id=1, username='jason', jobs='java', phone='123456'}
使用基于mapperFactoryBean来使mybatis和spring的整合开发与传统的Dao方式开发相比,比较简单,没有重复代码,出错也更容易发现。 使用mapper接口编程需要遵守以下编程规范:
mapper接口编程方式只需要编写一个接口,然后由mybatis框架根据接口的定义创建接口的动态代理对象。
1.mapper的接口名称和对应的mapper.xml映射文件的名称必须一致。
2.mapper.xml文件中的namespace与mapper接口的类路径相同(也就是接口文件和映射文件需要放在一个包中)。
3.mapper接口中的方法名和mapper.xml中定义的每个执行语句的id相同。
4.mapper接口中方法的输入参数类型要和mapper.xml中定义的每个sql的parameterType的类型相同。
5.mapper接口方法的输出参数类型要和mapper.xml中定义的每个sql的resultType的类型相同。 使用mapper接口开发虽然简单,但是有个问题就是,当接口过多,会导致映射文件也过多,这样配置文件就会显得比较杂乱和臃肿。
这就要采用另外一种方式整合。基于MapperScannerConfigurer的整合可以解决这个问题。
												

spring和mybatis的整合开发(基于MapperFactoryBean的整合开发(方便简单不复杂))的更多相关文章

  1. spring和mybatis的整合开发(基于MapperScannerConfigurer的整合开发(适用于复杂项目,接口较多的情况))

    在实际项目中,Dao层会包含很多接口,这样会导致spring配置文件过于臃肿.这时就需要采用扫描包的形式来配置mybaits中的映射器. 采用MapperScannerConfigurer来实现. M ...

  2. Spring+SpringMvc+Mybatis框架集成搭建教程三(框架整合测试程序开发)

    框架整合测试程序开发 (1).在mysql数据库中创建t_user表,sql语句如下 CREATE TABLE `t_user` ( `id` bigint(20) NOT NULL AUTO_INC ...

  3. java:Mybatis框架2(基于mapper接口的开发,多种查询,复合类型查询,resultMap定义,多表联查,sql片段)

    1.mybatis02: mybatis-config.xml: <?xml version="1.0" encoding="UTF-8"?> &l ...

  4. php 微信公众号+微商城开发 基于Thinkphp3.2框架开发

    说明:本教程是自己自学+自己的理解+扩展(包括学习过程中遇到的一些问题) 参考教程:麦子学院--李忠益--http://www.maiziedu.com/u/70409/ 微盟: http://www ...

  5. spring和mybatis的整合开发(传统Dao开发方式)

    spring和mybatis整合开发有三种整合方式1.传统DAO方式的开发整合(现在基本上不会用这种方式了,不推荐使用这种方式),2.mapper接口方式的开发整合(基于MapperFactoryBe ...

  6. 【SpringMVC学习04】Spring、MyBatis和SpringMVC的整合

    前两篇springmvc的文章中都没有和mybatis整合,都是使用静态数据来模拟的,但是springmvc开发不可能不整合mybatis,另外mybatis和spring的整合我之前学习mybati ...

  7. (转)SpringMVC学习(四)——Spring、MyBatis和SpringMVC的整合

    http://blog.csdn.net/yerenyuan_pku/article/details/72231763 之前我整合了Spring和MyBatis这两个框架,不会的可以看我的文章MyBa ...

  8. 搭建Spring + SpringMVC + Mybatis框架之三(整合Spring、Mybatis和Spring MVC)

    整合Spring和SpringMVC 之前已经整合了spring和mybatis,现在在此基础上整合SSM. 项目目录: 思路:SpringMVC的配置文件独立,然后在web.xml中配置整合. (1 ...

  9. SSM框架——Spring+SpringMVC+Mybatis的搭建教程

    一:概述 SSM框架在项目开发中经常使用到,相比于SSH框架,它在仅几年的开发中运用的更加广泛. Spring作为一个轻量级的框架,有很多的拓展功能,最主要的我们一般项目使用的就是IOC和AOP. S ...

随机推荐

  1. Spark-RDD之Partition源码分析

    概要 Spark RDD主要由Dependency.Partition.Partitioner组成,Partition是其中之一.一份待处理的原始数据会被按照相应的逻辑(例如jdbc和hdfs的spl ...

  2. java获取真实的IP地址工具类

    在实际项目中,有调用微信支付完成支付功能,在微信支付的请求参数中需要传递一个本机的ip地址,java代码运行环境目前为windows10以及centos7. 以下为获取ip地址工具类: package ...

  3. STL中的set使用方法详细!!!!

    1.关于set C++ STL 之所以得到广泛的赞誉,也被很多人使用,不只是提供了像vector, string, list等方便的容器,更重要的是STL封装了许多复杂的数据结构算法和大量常用数据结构 ...

  4. SpringCloud(10)使用Spring Cloud OAuth2和JWT保护微服务

    采用Spring Security AOuth2 和 JWT 的方式,避免每次请求都需要远程调度 Uaa 服务.采用Spring Security OAuth2 和 JWT 的方式,Uaa 服务只验证 ...

  5. 关于hibernate查询结果类的封装

    实际应用中,我们查询的结果有时候会需要其他的类或者是一个新的包装类,即希望映射到一个DTO(即使hibernate早在很久就不推荐使用..)但我还是说一下吧 如 我有这样子的两个类 //get和set ...

  6. 简单的JQuery完美拖拽

    首先导入jq库,最好是1.0版本的.调用函数时,传入要拖拽元素的id值. function drag(sel){ $div = $("#"+sel); $div.mousedown ...

  7. 其他综合-使用Xshell远程连接管理Linux实践

    使用Xshell远程连接管理Linux实践 1. Xshell整体优化 1)点击 工具 ,然后选择 选项 2)在 常规 选项中,下面的存放路径根据个人爱好修改(可选默认) 3)在 更新 选项中,将 √ ...

  8. 在CENTOS上源码搭建LNMP环境

    前言 1.操作前提: CentOS Linux release 7.5.1804: sudo用户(需要root权限): 2.需要安装的组件: nginx稳定版:nginx-1.14.0: MariaD ...

  9. 前端js区域上下拖拽

    先说说需求吧,网页内又上下两个区域,需要做到的功能是,第一个区域A底部的边可以进行拖拽使得区域变大或变小,同时第二个区域B跟着拖动的变化进行自适应. 思路: 1.使用一个假的div定义为那条可进行拖拽 ...

  10. 安全工具acunetix使用

    今天来主要介绍了安全测试工具AWVS(acunetix web vulnerability scanner)的使用 1)  安装包的下载地址:https://github.com/jiyanjiao/ ...