整合之前回忆一下spring和mybatis分别做了什么:

spring 通过注解/xml配置,实现AOP和DI

DI: 接口实现类中, 将接口私有化,从容器中读取,而不是new一个

UserDao userDao=new UserDaoImpl()
//改写成
private UserDao userDao

  

IoC:获取类时,从容器中获取

UserDao userDao = new UserDaoImpl();
//改成
UserDao userDao = applicationConfig.getBean("userDao");

  

AOP:将一些常用的操作,编写成切面,插入到程序中,通过监听实现

Before
After
...

 

mybatis 通过config.xml mapper.xml ,实体类,serviceImpl,来映射数据库数据,并且通过对实体类的进行serviceImpl的操作,来调用mapper内容,对数据库数据进行操作

class People{
private String name;
....getting/setting/toString;
}
class PeopleService{
private People people;
void addPeople(){
}
}
class PeopleServiceImpl{
private People people;
void addPeople(){
}
....
}
Peoplemapper.xml
insert xxxxxx config.xml
Peoplemapper.xml
jdbc
....

  

两者整合起来可以做什么,我还不是很清楚。。。

——————————————————————————————————————

整合

一/,导入mybatis-spring的包

		<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.1</version>
</dependency>

  

二/ 编辑applicationContent.xml,加入sqlSessionFactory

	<bean id="sqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref = "dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>

 mybatis-config.xml中environment部分可以注释掉了

<environments default="mysql">
<!-- 配置id为mysql的数据库环境 -->
<environment id = "mysql">
<transactionManager type="JDBC"/>
<dataSource type = "POOLED">
<property name = "driver" value = "${jdbc.driver}"/>
<property name = "url" value = "${jdbc.url}"/>
<property name = "username" value = "${jdbc.username}"/>
<property name = "password" value = "${jdbc.password}"/>
</dataSource>
</environment>
</environments>

  

三/ 与mybatis方法用的差不多。

  原理:创建DAO和DAOIMPL,在daoImpl中注入sqlSessionFactory,创建sqlSession来操作。

  提供:mybatis-spring包提供了SqlSessionTemplate和SqlSessionDaoSupport来实现。

  具体内容:

  1. SqlSessionTemplate:负责当前SqlSession和当前Spring事务时相关的,并管理SqlSession(关闭,提交,回滚)

  2. SqlSessionDaoSupport作为DAO的基类来使用,提供方法 getSqlSession()

四/代码实现

Customer,CustomerDao,customerMapper.xml,mybatis-config.xml没变化。

CustomerDaoImpl修改如下

public class CustomerImpl
extends SqlSessionDaoSupport
implements CustomerDao{
public Customer findCustomerById(Integer id){
return this.getSqlSession().selectOne("com.itheima.mapper.CustomerMapper.findCustomerById",id);
}
}

  

以上代码需要SqlSessionFactory来创建sqlSession,这里通过注入来实现,修改applicationContext.xml

	<bean id = "customerDao" class = "com.itheima.dao.impl.CustomerImpl">
<property name="sqlSessionFactory" ref ="sqlSessionFactory"/>
</bean>

 

以上代码将sqlSessionFactory注入到CustomerImpl中,这里的注入sqlSessionFactory是父类SqlSessionDaoSupport的一个私有属性。

  public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
if (this.sqlSessionTemplate == null || sqlSessionFactory != this.sqlSessionTemplate.getSqlSessionFactory()) {
this.sqlSessionTemplate = createSqlSessionTemplate(sqlSessionFactory);
}
}

  

(property元素用于调用bean示例中的setter方法,完成属性赋值)

用xml方式整合

原则

去除dao和daoImpl的class

在applicationContext.xml中增加bean

	<bean id="customerMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.itheima.mapper.CustomerMapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<!--可以自行给bean加property?-->
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<!--注入数据源 -->
<property name="dataSource" ref="dataSource" />
<!--指定核心配置文件位置 -->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>

  

在mapper.xml同路径下新增mapper.java

package com.itheima.mapper;

import com.itheima.po.Customer;;

public interface CustomerMapper {
public Customer findCustomerById(Integer id);
}

  

public class DaoTest {
@Test
public void findCustometByIdMapperTest(){
ApplicationContext act = new
ClassPathXmlApplicationContext("applicationContext.xml");
CustomerMapper customerMapper = act.getBean(CustomerMapper.class);
Customer customer = customerMapper.findCustomerById(1);
System.out.print(customer);
}
}

  

基于MapperScannerConfigurer的整合

不需要编写DaoImpl实现类,不需要添加DaoImpl的bean。

增加一个扫描mapper的路径,直接读

	<bean class ="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.itheima.mapper"/>
</bean>
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>

  

用mapperScannerConfigurer+事务+service处理数据(比用mapper多一层,用service做一些业务处理)

这边增删改都要用事务(具体是数据库的事务的ACID原则,不多讲)

@Service
@Transactional
public class CustomerServiceImpl implements CustomerService {
//注解注入CustomerMapper
@Autowired
private CustomerMapper customerMapper;
//添加客户
@Override
public void addCustomer(Customer customer) {
this.customerMapper.addCustomer(customer);
int i=1/0; //模拟添加操作后系统突然出现的异常问题
}
}

  

mapper中增加对应的方法(略)

applicationContext中增加扫描包

<context:component-scan base-package="com.itheima.service" />

  

spring-mybatis+spring整合的更多相关文章

  1. struts2 + spring + mybatis 框架整合详细介绍

    struts2 + spring + mybatis  框架整合详细介绍 参考地址: https://blog.csdn.net/qq_22028771/article/details/5149898 ...

  2. SpringMvc+Spring+Mybatis+Maven整合

    一.建立数据库表,使用generator自动生成相关代码: /* SQLyog Ultimate v11.24 (32 bit) MySQL - 5.1.62-community : Database ...

  3. JavaWeb_(SpringMVC框架)测试SpringMVC&Spring&MyBatis三大整合

    搭建 SpringMVC&Spring&MyBatis三大整合 传送门 1.准备 测试搭建S pringMVC&Spring&MyBatis三大整合 用例   a)准备 ...

  4. SSM框架-----------SpringMVC+Spring+Mybatis框架整合详细教程

    1.基本概念 1.1.Spring Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One  ...

  5. maven+springmvc+spring+mybatis+velocity整合

      一.ssmm简介 ssmm是当下企业最常用的开发框架架构 maven:管理项目jar包,构建项目 spring:IOC容器,事务管理 springmvc:mvc框架 myBatis:持久层框架 v ...

  6. 1.springMVC+spring+Mybatis的整合思路

    SSM整合的过程:就是把一些东西交给spring管理,也就是添加配置文件的一个过程.那么有哪些东西我们要交给spring管理呢?大概有以下几个: 1.数据源(可配置数据库连接池) 2.SqlSessi ...

  7. 第一章 企业项目开发--maven+springmvc+spring+mybatis+velocity整合

    说明:本系列文章主要是对自己在一家大型互联网公司实习的过程中对所学知识的总结!参与的是实际中使用的上线项目. 代码的github地址:https://github.com/zhaojigang/ssm ...

  8. idea+springmvc+spring+mybatis+maven整合返回json数据webapi

    首先看一张目录结构图: : 创建步骤: 1.创建maven  webapp工程, 创建完后的目录结构为: 2.添加项目依赖(添加jar包) 需要的jar包: spring-webmvc, spring ...

  9. Spring+MyBatis+SpringMvc整合Demo

    客户关系管理系统demo 项目分析 该demo使用技术及环境:ssm+maven+bootstrap+jsp+mysql+idea+jdk1.8 需求:客户管理,实现客户列表分页显示如下图 项目开始 ...

  10. spring mybatis springmvc整合

    使用SSM(Spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没有记录SSM整合 ...

随机推荐

  1. Vue3 相比 vue2

    Vue3 使用Proxy替代了defineProperty. Proxy 相比于 defineProperty 的优势 Object.defineProperty() 的问题主要有三个: 不能监听数组 ...

  2. Noip2013(普及组) 车站分级

    题目描述 一条单向的铁路线上,依次有编号为 , , …, n 的 n 个火车站.每个火车站都有一个级别,最低为 级.现有若干趟车次在这条线路上行驶,每一趟都满足如下要求:如果这趟车次停靠了火车站 x, ...

  3. golang 切片和map查询比较

    package main import ( "fmt" "time" ) var testTimeSlice = []string{"aa" ...

  4. python map函数(23)

    截至到目前为止,其实我们已经接触了不少的python内置函数,而map函数也是其中之一,map函数是根据指定函数对指定序列做映射,在开发中使用map函数也是有效提高程序运行效率的办法之一. 一.语法定 ...

  5. Beginning Linux Programming 学习-chapter2-Shell programming-Pipes and Redirection

    "为了从事创造性工作,人类需要孤独,可是在孤独中,广义的人类仍存在于内心."--(德国)奥铿                                             ...

  6. [转帖]两张图看懂GDT、GDTR、LDT、LDTR的关系

    两张图看懂GDT.GDTR.LDT.LDTR的关系 2018-06-09 18:13:53 Six_666A 阅读数 2044更多 分类专栏: 深入理解linux内核   转自:http://ju.o ...

  7. git使用中的一些命令及心得

    Git 与 SVN 区别点: 1.Git 是分布式的,SVN 不是:这是 Git 和其它非分布式的版本控制系统,例如 SVN,CVS 等,最核心 的区别. 2.Git 把内容按元数据方式存储,而 SV ...

  8. Django之Hook函数

    Django之钩子Hook方法 局部钩子: 在Fom类中定义 clean_字段名() 方法,就能够实现对特定字段进行校验.(校验函数正常必须返回当前字段值) def clean_name(self): ...

  9. oracle经典查询语句

    1. select * from emp;   2. select empno, ename, job from emp;   3. select empno 编号, ename 姓名, job 工作 ...

  10. GPIO 输出—使用固件库点亮 LED

    编程要点 1. 使能 GPIO 端口时钟:  2. 初始化 GPIO 目标引脚为推挽输出模式:  3. 编写简单测试程序,控制 GPIO 引脚输出高.低电平. LED的电路图 过程: 1.拷贝一个库函 ...