spring学习 六 spring与mybatis整合
在mybatis学习中有两种配置文件 :全局配置文件,映射配置文件。mybatis和spring整合,其实就是把mybatis中的全局配置文件的配置内容都变成一个spring容器的一个bean,让spring容器进行托管.因此整合过程就是把mybatis全局配置文件的内容整合到spring的配置文件中
(一)mybatis全局配置文件 :
根标签是<configuration>,
子标签包括:
<typeAliases>配置别名,
<environments> 配置数据库环境,
<mappers> 配置包扫描
<?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>
<!--
配置log4j
-->
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings> <!-- default的值是<environment>的id,代表当前要连接的数据库环境
【注】default的值一定要是一个<environment>,否则在获取SqlSession实例时会报空指针错误
-->
<environments default="mySqlJdbc">
<environment id="mySqlJdbc">
<!-- 事物标签,使用原生jdbc的事物 ,如果是MANAGERD表示交给其他容器管理-->
<transactionManager type="JDBC"></transactionManager>
<!-- 数据库连接池技术 -->
<dataSource type="POOLED">
<!-- property中的name的值,不能随便写,有规定的 -->
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://192.168.153.128:3306/mybaties?characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
<environment id="oracleJdbc">
<transactionManager type=""></transactionManager>
<dataSource type=""></dataSource>
</environment>
</environments> <!-- 加载XXXMapper.xml
如果采用代理开发,package批量指定xxxMapper.xml文件已经xxxMapper.java文件
-->
<mappers>
<mapper resource="com/xxx/mapper/FlowerMapper.xml"/>
</mappers>
</configuration>
根上面的配置文件,就可以推断出,spring中必须有
(1)用来描述数据库链接的基本配置内容,对应mybatis配置文件中的<dataSource>标签
(2)用来获取sqlSession的sqlSessionFactory,对应mybatis中的下面的代码
//加载配置文件
InputStream in = Resources.getResourceAsStream("mybatis.xml");
//使用工厂设计模式 【注意】以后sqlSessionFactory都会被spring 管理,不需要我们自己创建
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(in);
(3) 用来指定映射文件位置的配置内容,mybatis全局配置文件中的<mappers>标签
(二) 把mybatis全局配置文件整合进spring配置文件
第一步:整合数据库配置
在spring中使用了一个数据源封装类来替换mybatis全局配置文件中的<dataSource>标签,这个类就是org.springframework.jdbc.datasource.DriverManagerDataSource,在spring-jdbc.jar包中。在spring配置文件中做如下配置
<!--
数据源封装类
数据源就是用来封装数据库链接的,此处的类就是对JDBC中的DriverManager的封装
spring对jdbc的封装在spring-jdbc.jar包中
数据源封装类的作用就是代替mybatis中的<dataSource>标签里面的东西
-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="jdbc:mysql://192.168.153.128:3306/mybaties?characterEncoding=utf-8"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property> <!--
在DriverManagerDataSource类以及父类中,并没有任何一个driverClassName属性,
只是有一个public void setDriverClassName(String driverClassName)方法,
在使用<property>标签时,其实是根据name属性的值,来确定要调用的setter方法,name属性的值并不是类中的属性
-->
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
</bean>
第二步:把用来获取sqlSession的sqlSessionFactory注册到spring工厂
<!-- sqlSessionFactory ,注册sqlSession工厂-->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--dataSource的类型是DataSource是一个接口,上面配置的DriverManagerDataSouce类实现了DataSource接口 -->
<property name="dataSource" ref="dataSource"></property>
< !-- 为实体类起别名 -->
<property name="typeAliasesPackage" value="com.spring.pojo"></property>
</bean>
第三步:配置mybatis要扫描的映射文件的位置,spring在扫描basePackage指定的包时,会创建这个包下的所有接口的实现类的实例,并把这些实例注册到spring容器,且注册的名称为接口名的首字母小写
<!--
配置mybatis扫描的mapper包,是替代mybatis全局配置文件中的<mappers>标签
扫描了com.spring.mapper包下的所有的接口,并且会给这些接口提供实例化,并且实例化的bean命名为首字母小写的接口名
例如有一个接口名为AirportMapper,在spring中对应的bean的名称为airportMapper
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.airplan.mapper" ></property>
<property name="sqlSessionFactory" ref="sessionFactory"></property>
</bean>
经过上述三步就可以把mybatis需要的基本内容整合进spring
为了下面的代码测试,再在spring配置文件中配置一个bean
<bean name="airportService" class="com.airplan.service.impl.AirportServiceImpl">
<property name="airportMapper" ref="airportMapper"></property>
</bean>
(三)代码测试
public class TestClass {
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
String[] names=ac.getBeanDefinitionNames();
for (String string : names) {
System.out.println(string);
}
AirportService bean = ac.getBean("airportService",AirportService.class);
List<Airport> showAirports = bean.showAirport();
System.out.println(showAirports.size());
for (Airport airport : showAirports) {
System.out.println(airport);
}
}
}
spring-mybatis整合的第二种方式:mybatis配置文件与Spring配置文件分开写
spring配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
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-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 引入数据库配置文件 -->
<context:property-placeholder location="classpath:db.properties"/> <!--
注册数据库连接池
-->
<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> <!-- 注册sqlSessionFactoryBean --> <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:sqlMapConfig.xml"></property>
</bean> <!-- <bean id="userDao" class="com.caopeng.dao.UserDaoImpl">
<property name=""></property>
</bean> --> <!-- mapper 动态代理开发 <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="sqlSessionFactory" ref="sqlSessionFactoryBean" />
<property name="mapperInterface" value="com.caopeng.mapper.UserMapper"></property>
</bean> -->
<!-- mapper动态代理开发,开启扫描 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.caopeng.mapper"/>
</bean> </beans>
mybatis配置文件
<?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="com.xxx.bean" />
</typeAliases> <!--
指定xxxMapper.xml与xxxMapper.java文件,package方法指定
必须两种文件都在一个包下,且文件名一致
-->
<mappers>
<package name="com.caopeng.mapper"/>
</mappers> </configuration>
spring学习 六 spring与mybatis整合的更多相关文章
- Spring学习(六)-----Spring使用@Autowired注解自动装配
Spring使用@Autowired注解自动装配 在上一篇 Spring学习(三)-----Spring自动装配Beans示例中,它会匹配当前Spring容器任何bean的属性自动装配.在大多数情况下 ...
- Spring学习(十一)-----Spring使用@Required注解依赖检查
Spring学习(九)-----Spring依赖检查 bean 配置文件用于确定的特定类型(基本,集合或对象)的所有属性被设置.在大多数情况下,你只需要确保特定属性已经设置但不是所有属性.. 对于这种 ...
- Spring学习笔记--spring+mybatis集成
前言: 技术的发展, 真的是日新月异. 作为javaer, 都不约而同地抛弃裸写jdbc代码, 而用各种持久化框架. 从hibernate, Spring的JDBCTemplate, 到ibatis, ...
- Spring学习六(事物管理)
参考链接 http://www.mamicode.com/info-detail-1248286.html http://www.cnblogs.com/wangdaqian/archive/2017 ...
- Spring Cloud 学习 (六) Spring Cloud Config
在实际开发过程中,每个服务都有大量的配置文件,例如数据库的配置.日志输出级别的配置等,而往往这些配置在不同的环境中也是不一样的.随着服务数量的增加,配置文件的管理也是一件非常复杂的事 在微服务架构中, ...
- spring学习(三) ———— spring事务操作
前面一篇博文讲解了什么是AOP.学会了写AOP的实现,但是并没有实际运用起来,这一篇博文就算是对AOP技术应用的进阶把,重点是事务的处理. --wh 一.jdbcTemplate 什么是JdbcTem ...
- 学习笔记_J2EE_SSM_01_spring+springMVC+Mybatis整合_XML配置示例
spring+springMVC+Mybatis整合_XML配置示例 1.概述 spring+springMVC+Mybatis整合 XML配置方式 1.1 测试环境说明 名称 版本 备注 操作系统 ...
- MyBatis学习之SpringMvc和MyBatis整合
1. 整合流程 Dao层: 1. SqlMapConfig.xml,空文件即可,需要文件头. 2. applicationContext-dao.xml. a) 数据库连接池 b) SqlSessio ...
- springMVC学习(3)-springMVC和mybatis整合
一.需求:使用springmvc和mybatis完成商品列表查询. 二.整合思路:springMVC+mybaits的系统架构: 1步):整合dao层 mybatis和spring整合,通过sprin ...
随机推荐
- WAS 常见报错
1) An error occurred while deleting the server. ADMG0011E: An unexpected exception occurred com.ibm. ...
- NumPy 从已有的数组创建数组
NumPy 从已有的数组创建数组 本章节我们将学习如何从已有的数组创建数组. numpy.asarray numpy.asarray 类似 numpy.array,但 numpy.asarray 只有 ...
- 项目总结11:Centos部署JDK+Tomcat+MySQL文档(阿里云-网易云-华为云)
(如果不是root登陆,则输入:sudo su - 切换成root) 1.JDK安装 1-1-yum update (升级所有包同时也升级软件和系统内核) --安装中会有提示输入y就好(两个y,中 ...
- 项目总结01:JSP mysql SpringMvc下中国省市县三级联动下拉框
JSP mysql SpringMvc下中国省市县三级联动下拉框 关键词 JSP mysql数据库 SpringMvc ajax Controller层 Service层 中国地区 省 ...
- linux命令学习之:echo
echo命令用于在shell中打印shell变量的值,或者直接输出指定的字符串.linux的echo命令,在shell编程中极为常用, 在终端下打印变量value的时候也是常常用到的,因此有必要了解下 ...
- MySQL安装(windows版本)
1.下载.MySQL http://dev.mysql.com/downloads/mysql/ 下载windows的zip包,解压后,添加path路径bin, 系统环境变量->path-> ...
- thinkphp 查找表并返回结果
使用 M('devices')->where($data)-> execute($resql); 返回的只是int值,表示成功与否,并没有返回search 到的 record . $res ...
- shell加密工具shc的安装和使用
1) 工具说明 shell脚本是可读写的, 很有可能会泄露敏感信息, 如用户名/密码/路径/IP等. 同样在shell脚本运行时会也泄露敏感信息. shc是一个加密shell脚本的工具, 它的作用是把 ...
- Android Studio 发布 APK
打开发布设置窗口 打开Generate Signed APK...窗口,点击Create new... 打开Create New...窗口,创建一个Key,这个Key的相关信息一定要好好保存,因为以后 ...
- PAT 1018 锤子剪刀布(20)
1018 锤子剪刀布 (20)(20 分) 大家应该都会玩"锤子剪刀布"的游戏:两人同时给出手势,胜负规则如图所示: 现给出两人的交锋记录,请统计双方的胜.平.负次数,并且给出双方 ...