MyBatis之整合Spring
MyBatis之整合Spring
整合思路:
1、SqlSessionFactory对象应该放到spring容器中作为单例存在
2、传统dao的开发方式中,应该从spring容器中获得sqlSession对象
3、Mapper代理形式中,应该从spring容器中直接获得mapper的代理对象
4、数据库的连接以及数据库连接池事务管理都交给spring容器来完成
整合需要的jar包
1、spring的jar包
2、mybatis的jar包
3、spring+mybatis的整合包
4、mysql的数据库驱动jar包
5、数据库连接池的jar包
配置sqlMapConfig.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>
<!-- 设置别名 -->
<typeAliases>
<!-- 2.指定扫描包,会把包内所有的类都设置别名,别名的名称就是类名,大小写不敏感 -->
<package name="deep.mybatis.pojo"/>
</typeAliases>
</configuration>
配置applicationContext.xml核心配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.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> <!-- Mybatis的工厂 -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 核心配置文件的位置 -->
<property name="configLocation" value="classpath:sqlMapConfig.xml"/>
</bean> </beans>
配置资源文件db.properties文件
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456
配置log4j.properties日志资源文件
#Global logging configuration
log4j.rootLogger=DEBUG,stdout
#Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p[%t]-%m%n
配置UserDaoImpl
<!-- Dao -->
<bean id="userDao" class="deep.mybatis.dao.UserDaoImpl">
<!-- 其实这个工厂并没有注入到UserDaoImp这个实现类里面,而是注入到了实现类的父类里去了 -->
<property name="sqlSessionFactory" ref="sqlSessionFactoryBean"/>
</bean>
package deep.mybatis.dao; import org.mybatis.spring.support.SqlSessionDaoSupport; /**
* 原始dao开发
* @author DeepSleeping
*
*/
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao{ //声明工厂 public void insertUser(){
this.getSqlSession().insert("");
}
}
配置Mapper动态代理开发
applicationContext.xml
<!-- Mapper动态代理开发 -->
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<!-- 有工厂才能有session,有session才能有session.getMapper获得实现类 -->
<property name="sqlSessionFactory" ref="sqlSessionFactoryBean"/>
<property name="mapperInterface" value="deep.mybatis.mapper.UserMapper"/> </bean>
package deep.mybatis.mapper; import deep.mybatis.pojo.User; public interface UserMapper { public User findByUserId(Integer id);
}
<?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"> <mapper namespace="deep.mybatis.mapper.UserMapper"> <!-- 通过ID查询一个用户 -->
<select id="findByUserId" parameterType="Integer" resultType="User">
select * from account where id = #{v}
</select>
</mapper>
package deep.mybatis.junit; import static org.junit.Assert.*; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import deep.mybatis.mapper.UserMapper;
import deep.mybatis.pojo.User; public class JunitTest { @Test
public void testmapper() throws Exception {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
/*ac.getBean(UserMapper.class);*/
UserMapper userMapper = (UserMapper) ac.getBean("userMapper");
User user = userMapper.findByUserId(27);
System.out.println(user);
}
}
配置Mapper动态代理(增强版)
上面的方式还是有弊端,给mybatis提供接口的时候,必须得指定到具体类。我们可以用mybatis中的另一个mapper包里的功能,扫描基本包下的所有
<!-- Mapper动态代理开发(增强版) 扫描 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 不需要指定工厂了,因为会自动去工厂中找到工厂bean -->
<!-- 配置基本包(扫描包下的所有的) -->
<property name="basePackage" value="deep.mybatis.mapper"></property>
</bean>
MyBatis之整合Spring的更多相关文章
- 【Mybatis】MyBatis之整合Spring(八)
创建环境 系统:macOS Java:1.8 软件:eclipse,maven,mysql 创建步骤 本例:创建一个Maven项目(SpringMVC+Spring+Mybatis),页面上展示员工列 ...
- Mybatis整合Spring
根据官方的说法,在ibatis3,也就是Mybatis3问世之前,Spring3的开发工作就已经完成了,所以Spring3中还是没有对Mybatis3的支持.因此由Mybatis社区自己开发了一个My ...
- 框架整合——Spring与MyBatis框架整合
Spring整合MyBatis 1. 整合 Spring [整合目标:在spring的配置文件中配置SqlSessionFactory以及让mybatis用上spring的声明式事务] 1). 加入 ...
- Mybatis整合Spring -- typeAliasesPackage
Mybatis整合Spring 根据官方的说法,在ibatis3,也就是Mybatis3问世之前,Spring3的开发工作就已经完成了,所以Spring3中还是没有对Mybatis3的支持. 因此由M ...
- 160330、Mybatis整合Spring
转自csdn文章 http://haohaoxuexi.iteye.com/blog/1843309 Mybatis整合Spring 根据官方的说法,在ibatis3,也就是Mybatis3问世之前, ...
- MyBatis 学习-与 Spring 集成篇
根据官方的说法,在 ibatis3,也就是 Mybatis3 问世之前,Spring3 的开发工作就已经完成了,所以 Spring3 中还是没有对 Mybatis3 的支持.因此由 Mybatis 社 ...
- SSM框架-----------SpringMVC+Spring+Mybatis框架整合详细教程
1.基本概念 1.1.Spring Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One ...
- mybatis入门_一对多,多对多映射以及整合spring框架
一.一对多映射. 1.1 一对多映射之根据多的一方关联查询一的一方 示例:查询出具体的订单信息,同时也查询出来订单的用户信息. 引入的订单表如下所示: 框选出来的为具体的外键. 订单的Pojo类如下所 ...
- 1.springMVC+spring+Mybatis的整合思路
SSM整合的过程:就是把一些东西交给spring管理,也就是添加配置文件的一个过程.那么有哪些东西我们要交给spring管理呢?大概有以下几个: 1.数据源(可配置数据库连接池) 2.SqlSessi ...
随机推荐
- 并发的核心:CAS 是什么?Java8是如何优化 CAS 的?
大家可能都听说说 Java 中的并发包,如果想要读懂 Java 中的并发包,其核心就是要先读懂 CAS 机制,因为 CAS 可以说是并发包的底层实现原理. 今天就带大家读懂 CAS 是如何保证操作的原 ...
- eShopOnContainers 知多少[9]:Ocelot gateways
引言 客户端与微服务的通信问题永远是一个绕不开的问题,对于小型微服务应用,客户端与微服务可以使用直连的方式进行通信,但对于对于大型的微服务应用我们将不得不面对以下问题: 如何降低客户端到后台的请求数量 ...
- 实战经验丨CTF中文件包含的技巧总结
站在巨人的肩头才会看见更远的世界,这是一篇技术牛人对CTF比赛中文件包含的内容总结,主要是对一些包含点的原理和特征进行归纳分析,并结合实际的例子来讲解如何绕过,全面细致,通俗易懂,掌握这个新技能定会让 ...
- 基于 websocket 实现的 im 实时通讯案例
分享利用 redis 订阅与发布特性,巧妙的现实高性能im系统.为表诚意,先贴源码地址:https://github.com/2881099/im 下载源码后的运行方法: 运行环境:.NETCore ...
- Asp.Net Core 轻松学-经常使用异步的你,可能需要看看这个文章
前言 事情的起因是由于一段简单的数据库连接代码引起,这段代码从语法上看,是没有任何问题:但是就是莫名其妙的报错了,这段代码极其简单,就是打开数据库连接,读取一条记录,然后立即更新到数据库中.但是,惨痛 ...
- visual studio code .net 开发
Visual Studio确实是相当好用,各种简化操作什么的简直不要太舒服.但其容量太大,有时不是很方便,所以今天简单介绍一下另一个工具--Visual Studio Code. 虽然相比于老大哥Vi ...
- 软件开发架构介绍||OSI七层协议之物理层、数据链路层、网络层、传输层(mac地址、ip协议、断开协议、tcp协议之三次握手四次挥手)
一.网络编程 软件开发架构 C/S架构 C:客户端 想体验服务的时候才会去找服务端体验服务 S:服务端 24小时不间断的提供服务,即时监听,随时待命 B/S架构 B:浏览器 想体验服务的时候 ...
- jQuery --- 第四期 (jQuery动效)
学习笔记 1.jQuery动画的淡入淡出 <!doctype html> <html> <head> <meta charset="utf-8&qu ...
- flex 布局实现固定头部和底部,中间滚动布局
关键词:display: flex,flex: 1, overflow-y: scroll; 实现:head 和footer 固定,中间body多了滚动,少了撑满: head和footer宽度根据内 ...
- 全球第一免费开源ERP Odoo工业互联网生产制造功能模块术语解析
物料清单 物料清单(BoM)用于描述物料.每种物料的数量.以及制造某一产品所需的步骤.由于行业和成品性质的不同,同一个文件可能有不同的命名.例如,在制药行业中,可以使用术语“处方”. 周期 产品周期是 ...