spring整合mybatis文件

文件框架:



spring的配置文件(mybatis配置文件里面为空,全部在spring中配置,但要保留mybatis配置文件,例如添加 mybatis 拦截器时)

<?xml version="1.0" encoding="UTF-8"?>
<beans  
    xmlns="http://www.springframework.org/schema/beans"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xmlns:p="http://www.springframework.org/schema/p"  
    xmlns:aop="http://www.springframework.org/schema/aop"   
    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-3.0.xsd   
    http://www.springframework.org/schema/tx   
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd   
    http://www.springframework.org/schema/aop    
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">  
    
   
	<!-- spring读取配置文件   ,以下读取多个文件 -->
	<bean id="common.propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="ignoreResourceNotFound" value="true" />
		<property name="ignoreUnresolvablePlaceholders" value="true" />
		<property name="fileEncoding" value="UTF-8" />
		<property name="locations">
			<list>
				<value>classpath:jdbc.properties</value>
				<!-- <value>classpath:log4j.properties</value> -->
			</list>
		</property>
	</bean>
  
<!-- 配置数据源-->  
    <bean id="DataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
	  	<property name="driverClassName" value="${jdbc.driver}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>  
	
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
	    <property name="dataSource" ref="DataSource" /> 
	    <!-- 添加mybatis配置文件 --> 
	    <property name="configLocation" value="classpath:mybatis-config.xml"></property> 
	     <!-- 自动扫描mapping.xml文件 -->  
        <property name="mapperLocations" value="classpath:com.test.dao.*.xml"></property>
	</bean>  
	
	<!-- DAO接口所在包名,Spring会自动查找其下的类 -->  
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
        <property name="basePackage" value="com.test.dao" />  
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>  
    </bean>
	
	
	<!-- 单个DAO接口手动租入 
	<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">  
	    <property name="mapperInterface" value="com.test.dao.UserMapper"></property>  
	    <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>  
	</bean> 
	-->
	 
</beans>  

测试文件:

package com.test.test;

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

import com.test.dao.UserMapper;
import com.test.model.User;
import com.test.service.UserService;

public class TestUser {
	
	private UserMapper userDao;
	
	 	@Before
	    public void setUp() throws Exception {
	 		ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-commons.xml");
	 		userDao= ctx.getBean(UserMapper.class);
	    }
	 	
	 	@Test
	 	public void run(){
	 		System.out.println("-------------------------------------");
	 		User user = this.userDao.queryUserById(3);
	 		System.out.println(user);
	 		System.out.println("-------------------------------------");
	 	}

}

其他和mybatis整合mysql相同  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值