您的位置:首页 > 编程语言 > Java开发

ssm框架学习02--mybatis和spring的整合

2016-05-14 18:52 821 查看
整合主要是将数据源和映射等交给spring来管理,准备工作需要spring和mybatis的整合包,这里采用mybatis的第二种mapper的开发方式

mapper接口,映射文件,pojo类和之前的编写一样,这里不再列举

首先来看看sqlmapconfig.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.zcj.ssm.po"/>
</typeAliases>
</configuration>
这里数据源配置已经被拿走了,并且mapper映射的管理也没有了(因为我们使用在spring中进行扫描,可以实现不用为每一个mapper注册,更加的简洁)

接下来是spring对mybatis的整合文件

ApplicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" 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.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<!-- 加载数据库配置文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置数据源,这里使用dbcp -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}"></property>
<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>
<!-- 创建sessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"></property>
</bean>
<!-- 使用mapper扫描来配置,需要注意这种方式需要映射文件和接口在同名并且同一目录 -->
<!-- mapper扫描器,注意下面的名字要用 sqlSessionFactoryBeanName,对于单个配置的mapper使用org.mybatis.spring.mapper.MapperFactoryBean-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 扫描包路径,如果需要扫描多个包,中间使用半角逗号隔开 -->
<property name="basePackage" value="com.zcj.ssm.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>

</beans>
需要注意的是mapper扫描器中第二个property的name,需要特别注意,不然会使得最上面的db.properti失效

最后给出整合之后单元测试的代码

package com.zcj.mapper;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zcj.po.User;

public class UserMapperTest {
private ApplicationContext applicationContext;
@Before
public void setUp() throws Exception {
applicationContext =new ClassPathXmlApplicationContext("classpath:spring/ApplicationContext.xml");
}

@Test
public void testSelectUserById() {
UserMapper userMapper = (UserMapper) applicationContext.getBean("userMapper");
User user = userMapper.selectUserById(1);
System.out.println(user);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: