myBatis系列之一:搭建开发环境
myBatis系列之二:以接口方式交互数据
myBatis系列之三:增删改查
myBatis系列之四:关联数据的查询
myBatis系列之六:与SpringMVC集成
myBatis系列之七:事务管理
Spring作为基础框架,可以集成后端框架,如Hibernate,MyBatis等。
前面是介绍单独使用MyBatis的,大致逻辑是:
sqlSessionFactory <- configuration file (包括数据库连接配置)
IXxxMapper <- sqlSession <- sqlSessionFactory
<- mapper interface <- mapper xml
得到IxxMapper后,就可以调用其方法进行数据交互了。
和Spring集成时,上面的这些对象需要作为bean来管理:
dataSource bean <- 数据库连接配置
sqlSessionFactory bean <- dataSource
<- configuration file
userMapper bean <- sqlSessionFactory
<- mapper interface
1. 在pom.xml中加入依赖:
<properties>
<mybatis.spring.version>1.2.1</mybatis.spring.version>
<dbcp.version>1.4</dbcp.version>
<spring.version>3.1.2.RELEASE</spring.version>
</properties>
<dependencies>
<dependency><!-- mybatis的几个对象包装成spring的bean -->
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis.spring.version}</version>
</dependency>
<dependency><!-- spring上下文用spring-jdbc连接数据库 -->
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency><!-- dataSource是BasicDataSource的实例 -->
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>${dbcp.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
2. 在类路径下创建beans-da.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"><!-- 数据库连接bean -->
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/hbatis?characterEncoding=utf8" />
<property name="username" value="root" />
<property name="password" value="123456" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!-- sqlSessionFactory bean -->
<property name="dataSource" ref="dataSource" /><!-- 数据源 -->
<property name="configLocation" value="classpath:Configuration.xml" /><!-- 配置文件 -->
</bean>
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"><!-- user映射bean-->
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
<property name="mapperInterface" value="com.john.hbatis.mapper.IUserMapper" /><!-- 映射接口 -->
</bean>
</beans>
3. 测试类:
@ContextConfiguration(locations = { "classpath:beans-da.xml" })
public class SpringIntegrationTest extends AbstractTestNGSpringContextTests {
private static final Logger log = LoggerFactory.getLogger(SpringIntegrationTest.class);
@Resource
IUserMapper mapper;
@Test
public void queryTest() {
User user = mapper.getUserById(1);
log.info("Name: {}, address: {}", user.getName(), user.getAddress());
}
}
参考:
http://www.yihaomen.com/article/java/309.htm