在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>