Spring和MyBatis的整合

 

1. Spring和各个框架的整合

Spring目前是JavaWeb开发中最终的框架,提供一站式服务,可以其他各个框架整合集成

Spring整合方案

1.1. SSH

Ssh是早期的一种整合方案

Struts2 : Web层框架

Spring : 容器框架

Hibernate : 持久层框架

2. SSM

主流的项目架构的三大框架(相对其他框架而言,最优秀)

SpringMVC : spring自己家的 Web层框架,spring的一个模块

Spring :容器框架

MyBatis :持久层框架

3. Spring与MyBatis整合

3.1. 集成思路

实际开发,使用Maven项目,直接引入项项目在Maven仓库中的坐标即可

学习阶段: 手动导入jar包,从零开始集成(巩固基础知识)

3.2. 创建java项目

3.3. 准备集成相关jar包

3.3.1. Spring依赖包

3.3.2. Mybatis依赖包

3.3.3. MyBatis和Spring框架集成的桥梁包

Spring自己并没有集成MyBatis框架,需要MyBatis自己来集成,所以需要自己提供Spring框架集成的桥梁包

如果我们使用的mybatis3.4.4 不能直接使用mybatis内置的 桥梁包版本,版本比较低,无法正常运行,需要单独下载一个比价高的版本

3.3.4. 数据库驱动包和连接池

3.3.5. Mybatis支持的日志包log4j

3.4. 项目集成需要各种配置文件

3.5. 完成项目层与层之间spring对象的创建和依赖关系的维护

3.5.1. Service层

package cn.zj.mybatis.service.impl;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import cn.zj.mybatis.mapper.UserMapper;

import cn.zj.mybatis.pojo.User;

import cn.zj.mybatis.service.UserService;

@Service

public class UserServiceImpl implements UserService {

/*

* 问题: UserMapper 代理对象如何创建?

* 答 :使用 SqlSession 操作对象创建 !

*

* 问题 : SqlSession 对象如何创建?

*

* 答 : SqlSessionFactory 工厂对象创建?

*

* 问题: SqlSessionFactory 对象如何创建

*

* 1,和Spring框架集成之前

*  MyBatis框架自己读取配置文件中的相关配置去创建

* 2, 和Spring框架集成之后

*  交个Spring容器来创建

* 问题: 如何在Spring框架中配置,创建出来SqlSessionFactory对象?

*  mybatis和spring集成的类查阅 桥梁包

*  org.mybatis.spring.SqlSessionFactoryBean 创建 SqlSessionFactory

*

*/

@Autowired

private UserMapper mapper;

@Override

public int insert(User user) {

return mapper.insert(user);

}

@Override

public User selectByPrimaryKey(Integer id) {

System.out.println(mapper);

return mapper.selectByPrimaryKey(id);

}

}

3.5.2. 测试代码

package cn.zj.mybatis.test;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.test.context.ContextConfiguration;

import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import cn.zj.mybatis.pojo.User;

import cn.zj.mybatis.service.UserService;

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration("classpath:applicationContext.xml")

public class UserServiceTest {

@Autowired

private UserService service;

@Test

public void testInsert() {

User user = new User(null, "乔峰", "qiaofeng", 30);

int row = service.insert(user);

System.out.println(row);

}

@Test

public void testSelectByPrimaryKey() {

User user = service.selectByPrimaryKey(8);

System.out.println(user);

}

}

3.5.3. applicationContext配置文件的配置

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:p="http://www.springframework.org/schema/p"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

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

        http://www.springframework.org/schema/aop

        http://www.springframework.org/schema/aop/spring-aop.xsd

        http://www.springframework.org/schema/tx

        http://www.springframework.org/schema/tx/spring-tx.xsd

        ">

<!-- 设置注解配置包扫描位置 -->

<context:component-scan base-package="cn.zj.mybatis"/>

</beans>

3.6. MyBatis 框架SqlSessionFactory对象的创建

* 问题: UserMapper 代理对象如何创建?

* 答 :使用 SqlSession 操作对象创建 !

*

* 问题 : SqlSession 对象如何创建?

*

* 答 : SqlSessionFactory 工厂对象创建?

*

* 问题: SqlSessionFactory 对象如何创建

*

* 1,和Spring框架集成之前

*  MyBatis框架自己读取配置文件中的相关配置去创建

* 2, 和Spring框架集成之后

*  交个Spring容器来创建

* 问题: 如何在Spring框架中配置,创建出来SqlSessionFactory对象?

*  mybatis和spring集成的类查阅 桥梁包

*  org.mybatis.spring.SqlSessionFactoryBean 创建 SqlSessionFactory

*

*/

创建MyBatis框架工厂对象的 类在mybatis-spring1.2.1.jar 桥梁包中的

org.mybatis.spring.SqlSessionFactoryBean 类 如下图

3.6.1. 配置文件

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:p="http://www.springframework.org/schema/p"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

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

        http://www.springframework.org/schema/aop

        http://www.springframework.org/schema/aop/spring-aop.xsd

        http://www.springframework.org/schema/tx

        http://www.springframework.org/schema/tx/spring-tx.xsd

        ">

<!-- 配置读取 db.properties 数据库配置文件 -->

<context:property-placeholder location="classpath:db.properties"/>

<!-- 配置数据源连接池 -->

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">

<property name="driverClassName" value="${jdbc.driverClassName}"/>

<property name="url" value="${jdbc.url}"/>

<property name="username" value="${jdbc.username}"/>

<property name="password" value="${jdbc.password}"/>

<property name="maxActive" value="${jdbc.maxActive}"/>

</bean>

<!--

配置MyBatis框架的 SqlSessionFactoryBean 类,创建

SqlSessionFactory 工厂对象

-->

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

<!-- 1.注入数据源 -->

<property name="dataSource" ref="dataSource"/>

<!-- 2.配置映射文件 -->

<property name="mapperLocations">

<array>

<!-- <value>classpath:cn/zj/mybatis/mapper/UserMapper.xml</value> -->

<!-- 可以使用通配符 * 读取 目录下面所有的配置文件 -->

<value>classpath:cn/zj/mybatis/mapper/*Mapper.xml</value>

</array>

</property>

<!-- 3. 配置别名使用包扫描 -->

<property name="typeAliasesPackage" value="cn.zj.mybatis.pojo"/>

<!-- 4.读取mybat-config.xml配置文件,此配置文件可能还会配一些mybatis框架的

其他个性化配置

实际项目开发可能不用配置

-->

<property name="configLocation" value="classpath:mybatis-config.xml"/>

</bean>

</beans>

3.7. 创建MyBatis的Mapper接口的代理对象

使用桥梁包 org.mybatis.spring.mapper.MapperFactoryBean<T> 创建 UserMapper代理对象

此种方式每一个Mapper接口需要单独配置,如果Mapper过多,创建Mapper可能造成配置代码过多

<!-- 创建UserMapper代理对象-创建单个Mapper对象

使用桥梁包 org.mybatis.spring.mapper.MapperFactoryBean<T> 创建 UserMapper代理对象

-->

<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">

<!-- 注入SqlSessionFacotry对象 -->

<property name="sqlSessionFactory" ref="sqlSessionFactory"/>

<!-- 注入UserMapper接口类型:底层创建UserMapper的代理对象 -->

<property name="mapperInterface" value="cn.zj.mybatis.mapper.UserMapper"/>

</bean>

3.8. 使用包扫描创建MyBatis的Mapper接口的代理对象

<!-- 批量创建Mapper代理对象 ,使用包扫描创建Mapper代理对象

使用桥梁包

org.mybatis.spring.mapper.MapperScannerConfigurer

-->

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

<!-- 配置需要创建Mapper接口代理对象对应的包 -->

<property name="basePackage" value="cn.zj.mybatis.mapper"/>

<!-- 配置SqlSessionFactoryBean 的名称,不是引用 -->

<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>

</bean>

3.9. MyBatis的事务管理器的配置

一般开发,事务的管理都会使用aop切入到业务层

<!-- 配置事务管理器 -->

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<!-- 注入数据源 -->

<property name="dataSource" ref="dataSource"/>

</bean>

<!-- spring事务配置 -->

<tx:advice id="txAdvice" transaction-manager="transactionManager">

<!-- 事务属性配置 -->

<tx:attributes>

<!-- DQL :查询操作,配置只读事务 -->

<tx:method name="get*" read-only="true" isolation="REPEATABLE_READ"  propagation="REQUIRED"/>

<tx:method name="select*" read-only="true" isolation="REPEATABLE_READ"  propagation="REQUIRED"/>

<tx:method name="find*" read-only="true" isolation="REPEATABLE_READ"  propagation="REQUIRED"/>

<tx:method name="query*" read-only="true" isolation="REPEATABLE_READ"  propagation="REQUIRED"/>

<!-- 其他 SQL :非只读事务 -->

<tx:method name="*" read-only="false" isolation="REPEATABLE_READ"  propagation="REQUIRED"/>

</tx:attributes>

</tx:advice>

<!-- 配置AOP 切入事务 -->

<aop:config>

<!-- 切入点 -->

<aop:pointcut expression="execution(* cn.zj.mybatis.service..*.*(..))" id="pt"/>

<!-- 切面 -->

<aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>

</aop:config>

Spring和MyBatis的集成的更多相关文章

  1. Spring+SpringMvc+Mybatis框架集成搭建教程

    一.背景 最近有很多同学由于没有过SSM(Spring+SpringMvc+Mybatis , 以下简称SSM)框架的搭建的经历,所以在自己搭建SSM框架集成的时候,出现了这样或者那样的问题,很是苦恼 ...

  2. Spring MVC 学习总结(十)——Spring+Spring MVC+MyBatis框架集成(IntelliJ IDEA SSM集成)

    与SSH(Struts/Spring/Hibernate/)一样,Spring+SpringMVC+MyBatis也有一个简称SSM,Spring实现业务对象管理,Spring MVC负责请求的转发和 ...

  3. Spring MVC 学习总结(六)——Spring+Spring MVC+MyBatis框架集成

    与SSH(Struts/Spring/Hibernate/)一样,Spring+SpringMVC+MyBatis也有一个简称SSM,Spring实现业务对象管理,Spring MVC负责请求的转发和 ...

  4. Spring+Spring MVC+MyBatis框架集成

    目录 一.新建一个基于Maven的Web项目 二.创建数据库与表 三.添加依赖包 四.新建POJO实体层 五.新建MyBatis SQL映射层 六.JUnit测试数据访问 七.完成Spring整合My ...

  5. Spring+SpringMvc+Mybatis框架集成搭建教程二(依赖配置及框架整合)

    依赖导入以及框架整合 (1).打开项目的pom.xml文件,声明依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" x ...

  6. spring mvc+mybatis+maven集成tkmapper+pagehelper

    <!-- maven tkmapper引入--> <dependency> <groupId>tk.mybatis</groupId> <arti ...

  7. (通用Mapper、分页,批量插入,一分钟接入)spring mvc+mybatis+maven集成tkmapper+pagehelper

    <!-- maven tkmapper引入--> <dependency> <groupId>tk.mybatis</groupId> <arti ...

  8. Spring+SpringMvc+Mybatis框架集成搭建教程一(项目创建)

    一.框架搭建环境 Spring 4.2.6.RELEASE SpringMvc 4.2.6.RELEASE Mybatis 3.2.8 Maven 3.3.9 Jdk 1.7 Idea 15.04 二 ...

  9. Spring+SpringMvc+Mybatis框架集成搭建教程四(项目部署及测试)

    在IDEA中将项目部署到本地Tomcat下进行运行并验证整合结果 (1).点击如下图所示的下拉按钮,弹出Edit Configurations...后点击该项. (2).跳出如下界面后,点击红框内的& ...

随机推荐

  1. 每天一点点之vue框架开发 - vue中使用vue-router切换页面时自动滚动到顶部的方法

    1. 在main.js入口文件中写入 //路由跳转后,页面回到顶部 router.afterEach(() => { document.body.scrollTop = 0; document. ...

  2. unique constraint(PD.HSI_RIGHT) violated

    插入时报错,原因:唯一约束重复了.... 查看表中有唯一约束的列是不是已经有值了 果然,唯一约束有两条重复的记录. ------------------------------------------ ...

  3. jQuery元素的左右移动

    1.下载jQuery,并导入:https://blog.csdn.net/weixin_44718300/article/details/88746796 2.代码实现: <!DOCTYPE h ...

  4. SpringMVC错误,商品添加信息HTTP Status 400 – Bad Request

    记录一个自己在做商品信息显示与传递数据的时候出现的错误, HTTP Status 400 – Bad Request Type Status Report Description The server ...

  5. URAL_1146/uva_108 最大子矩阵 DP 降维

    题意很简单,给定一个N*N的大矩阵,求其中数值和最大的子矩阵. 一开始找不到怎么DP,没有最优子结构啊,后来聪哥给了我思路,化成一维,变成最大连续和即可.为了转化成一维,必须枚举子矩阵的宽度,通过预处 ...

  6. spring boot 环境配置(profile)切换

    Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...

  7. 201771010123汪慧和《面向对象程序设计Java》第十七周实验总结

    一.理论部分 1.多线程并发执行中的问题 ◆多个线程相对执行的顺序是不确定的. ◆线程执行顺序的不确定性会产生执行结果的不确定性. ◆在多线程对共享数据操作时常常会产生这种不确定性. 2.线程的同步 ...

  8. python yield 使用示例

    1.yield由于创建迭代器 def deal(): tmp = [] for i in range(20): tmp.append(i) if i % 4 == 0: yield tmp tmp = ...

  9. 51nod 1267:4个数和为0 哈希

    1267 4个数和为0 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题  收藏  关注 给出N个整数,你来判断一下是否能够选出4个数,他们的和为0,可以则输出&qu ...

  10. 转 SQL 的数据库 架构规范 之 58到家数据库30条军规解读

    军规适用场景:并发量大.数据量大的互联网业务 军规:介绍内容 解读:讲解原因,解读比军规更重要 一.基础规范 (1)必须使用InnoDB存储引擎 解读:支持事务.行级锁.并发性能更好.CPU及内存缓存 ...