|
MapperFactoryBean
数据映射器接口可以按照如下做法加入到 Spring 中
<bean id="userMapper" class="org.mybatis.spring.MapperFactoryBean">
<property name="mapperInterface" value="com.plus88548886.mapper.UserMapper">
</property>
<property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean>
MapperFactoryBean 创建的代理类实现了 UserMapper 接口,并且注入到应用程序中。 因为代理创建在运行时环境中 ,那么指定的映射器必须是一个接口,而 不是一个具体的实现类。
如果 UserMapper 有一个对应的 MyBatis 的 XML 映射器文件, 如果 XML 文件在类路径的 位置和映射器类相同时, 它会被 MapperFactoryBean 自动解析。 没有必要在 MyBatis 配置文 件 中 去 指 定 映 射 器 , 除 非 映 射 器 的 XML 文 件 在 不 同 的 类 路 径 下 。 可 以 参 考 SqlSessionFactoryBean 的 configLocation 属性配置 XML 文件。
package com.plus88548886.dao.impl;
import com.plus88548886.mapper.UserMapper;
import com.plus88548886.model.User;
public class UserDaoImpl {
private UserMapper userMapper;
public UserMapper getUserMapper() {
return userMapper;
}
public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
}
public User selectById(int id){
return userMapper.selectById(id);
}
}
MapperFactoryBean可以用映射器接口方法上定义注解和XML两种方法实现
使用注解的示例请看Z-Test1.rar
使用xml的示例请看Z-Test3.rar,需要特别说明的是,使用xml 方式,可以把XML放在映射器接口同一路径下,因为 MapperFactoryBean 自动解析在映射器相同路径下的xml文件
上面这种适用于方法内不需要有其它的用到SqlSession的操作,下面一种方法
注入SqlSessionTemplate,不需要映射器接口,也就是说mapper类
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:db.properties</value>
</list>
</property>
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass">
<value>${driverClass}</value>
</property>
<property name="jdbcUrl">
<value>${jdbcUrl}</value>
</property>
<property name="user">
<value>${user}</value>
</property>
<property name="password">
<value>${password}</value>
</property>
<property name="maxPoolSize">
<value>20</value>
</property>
<property name="minPoolSize">
<value>2</value>
</property>
<property name="initialPoolSize">
<value>2</value>
</property>
<!-- 设置数据库连接池的连接的最大空闲时间,单位为秒 -->
<property name="maxIdleTime">
<value>20</value>
</property>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis.xml" />
<!-- <property name="mapperLocations" value="classpath*:com/plus88548886/model/*.xml" /> -->
<!-- 如果在配置了配置了<mappers>就不需要配mapperLocations了 -->
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
<bean id="userMapperDaoImpl" class="com.plus88548886.dao.impl.UserMapperDaoImpl">
<property name="sqlSession" ref="sqlSession" />
</bean>
<!-- 数据库的事务管理器配置 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 使用annotation定义数据库事务,这样可以在类或方法中直接使用@Transactional注解来声明事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
下面提供全部测试代码,user表很简单就三个字段,id,username,password,请自行创建 |
|
|