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

12 篇文章 0 订阅
3 篇文章 0 订阅

整合主要是将数据源和映射等交给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);
	}

}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值