myBatis系列之五:与Spring3集成
Mybatis技术学习,更多知识请访问https://www.itkc8.com
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中加入依赖:
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代码
- <?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. 测试类:
Java代码
- @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());
- }
- }
Mybatis技术学习,更多知识请访问https://www.itkc8.com
参考:
http://www.yihaomen.com/article/java/309.htm