Spring+SpringMVC+mybatis已经成为当前最流行的java开发框架组合,为此mybatis团队开发了Mybatis-Spring项目用于spring整合mybatis,在开发中主要有如下几处需要配置的地方:
- 在spring配置数据源(c3p0,druid…)
- 配置SqlSessionFactory对象,用于生成SqlSession获取Mapper代理对象(由spring管理,消除样板代码)
- 配置Mapper,使用包扫描的方式注册Mapper接口生成的代理对象为容器中的Bean,用于操作数据库
- 事务管理,与spring整合之后交由spring进行声明式事务管理(@Transactional)
在Mybatis-Spring项目中提供了SqlSessionFactoryBean类用于配置mybatis,这个对象有很多属性,囊括了mybatis核心配置文件的所有配置项,其中常用属性为:
- configLocation:用于指定mybatis的核心配置文件位置
- dataSource:指定数据源
mybatis中的其他复杂配置项建议放在mybatis-config.xml中去配置,这样更加清晰,例如批量配置pojo的别名和批量加载mapper.xml映射文件等。mybatis-config.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>
<package name="com.znker.domain" />
</typeAliases>
<!-- 使用包扫描机制批量加载有所映射文件,指定包名,mybatis会扫描并加载其中的所有的mapper.xml映射文件 -->
<mappers>
<package name="com.znker.repository" />
</mappers>
</configuration>
与spring整合还有一个问题:为mapper接口的生成代理对象并注册为容器中的Bean作为DAO操作数据库。此时同样可以使用注解扫描的方式,Mybatis-Spring项目中提供了MapperScannerConfigurer对象用于扫描指定的包并将带有@Repository
注解的代理对象批量注册为容器中的Bean,MapperScannerConfigurer对象有三个重要的属性需要配置:
- basePackage:指定让spring逐层扫描的包路径,多个包之间用逗号隔开
- SqlSessionFactoryBeanName:指定容器中的配置的SqlSessionFactoryBean对象的id
- annotationClass:告知spring添加了指定注解的mapper代理类注册为容器中的Bean(@Repository)
可以创建一个配置文件spring-dao.xml用于配置数据源以及整合mybatis:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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">
<!-- 加载属性文件,获取数据库连接参数 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="driverClass" value="${jdbc.driverClass}"/>
<property name="url" value="${jdbc.url}"/>
</bean>
<!-- SqlSessionFactoryBean用于生成SqlSessionFactory对象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource"/>
<!-- 加载mybatis的核心配置文件 -->
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
</bean>
<!-- 扫描并注册Mapper接口生成的代理对象为Bean,用于操作数据库 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 指定需要扫描的包路径,spring会逐层扫描该包及其子包下的mapper接口 -->
<property name="basePackage" value="com.znker.repository"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 指定标注了@Repository注解的mapper接口代理对象注册为spring容器中的Bean -->
<property name="annotationClass" value="org.springframework.stereotype.Repository"/>
</bean>
</beans>
在spring容器配置文件applicationContext.xml中导入mybatis-spring.xml的配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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">
<!-- 引入spring-dao.xml的配置项 -->
<import resource="classpath:spring/spring-dao.xml"/>
<!-- 扫描注册注册Controller,Service组件 -->
<context:component-scan base-package="com.znker.controller, com.znker.service.impl"/>
</beans>