Spring+Mybatis 多数据源配置

     项目目录结构如下:


Spring+Mybatis 多数据源配置_第1张图片
 

 

      spring配置文件



	
		
			classpath:init-config.properties
		
	
	
	
	

	
	

	
		
		
		
		
		

		
		
		
		

		
		

		
		

		
		

		
		
		
		

		
		
		

		
		
	

	
	
		
		
	

	
		
		
	

	
	
		
		
	

	
	
	
	
	
	
	
	
	
	
	
		
		
		
		
		

		
		
		
		

		
		

		
		

		
		

		
		
		
		

		
		
		

		
		
	
	
	
		
		
	
	
	
		
		
	
	
	
		
		
	

	
	
	

 

 

      配置文件内容:

#数据库连接池设置
dataSource.driver=com.mysql.jdbc.Driver
dataSource.url=jdbc:mysql://localhost:3306/dwz
dataSource.username=root
dataSource.password=root

dataSource2.driver=oracle.jdbc.driver.OracleDriver
dataSource2.url=jdbc:oracle:thin:@192.168.10.43:1521:test
dataSource2.username=test
dataSource2.password=test

 

 

      Service类代码:

package org.zhuc.mybatis.service;

import org.springframework.transaction.annotation.Transactional;

/**
 * 基础mysql Service抽象类
 * 使用isap数据源及回滚
 * @author zhuc
 * @create 2013-3-11 下午4:27:33
 */
@Transactional(value = "isap", rollbackFor = Exception.class)
public abstract class BaseMySqlService {

}

 

package org.zhuc.mybatis.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zhuc.mybatis.domain.User;
import org.zhuc.mybatis.mapper.UserMapper;

/**
 * @author zhuc
 * @create 2013-3-11 下午1:19:03
 */
@Service("userService")
//@Transactional(value = "isap", rollbackFor = Exception.class)
public class UserService extends BaseMySqlService {

	@Autowired
	private UserMapper userMapper;

	/**
	 * @param id
	 * @return
	 */
	public User get(Integer id) {
		return userMapper.get(id);
	}

	/**
	 * @param id
	 * @return
	 */
	public User get2(Integer id) {
		return userMapper.get2(id);
	}

	/**
	 * @return
	 */
	public List findAll() {
		return userMapper.findAll();
	}

	/**
	 * @param user
	 * @throws Exception 
	 */
	public void insert(User user) throws Exception {
		userMapper.insert(user);
		throw new Exception("testException");
	}
}

 

package org.zhuc.mybatis.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.zhuc.mybatis.domain2.Flow;
import org.zhuc.mybatis.mapper2.FlowMapper;

/**
 * @author zhuc
 * @create 2013-3-11 下午1:19:03
 */
@Service("flowService")
@Transactional(value = "insurance", rollbackFor = Exception.class)
public class FlowService {

	@Autowired
	private FlowMapper flowMapper;

	/**
	 * @param id
	 * @return
	 */
	public Flow get(String id) {
		return flowMapper.get(id);
	}

}

 

 

      客户端测试类如下:

package spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.zhuc.mybatis.service.FlowService;
import org.zhuc.mybatis.service.UserService;

/**
 * @author zhuc
 * @create 2013-3-11 下午1:47:03
 */
public class MultiTest {

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		UserService userService = (UserService) ac.getBean("userService");
		System.out.println(userService.get(1));
		System.out.println(userService.get2(1));

		FlowService flowService = (FlowService) ac.getBean("flowService");
		System.out.println(flowService.get("94003d29-a7b0-42f0-839c-fa609b209ff1"));

		//		User user = new User();
		//		user.setId(100);
		//		user.setUserName("admin100");
		//		user.setPassword("password100");
		//		user.setTrueName("小李4");
		//		user.setCreateTime(new Date());
		//
		//		userService.insert(user);	//受事务管理,抛出Exception时将回滚  (rollbackFor)
	}

}

 

 

      输出结果:

2013-03-14 11:21:19 [main] DEBUG org.zhuc.mybatis.mapper.UserMapper.get - ooo Using Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@f6d64c5]
2013-03-14 11:21:19 [main] DEBUG org.zhuc.mybatis.mapper.UserMapper.get - ==>  Preparing: SELECT * from T_USER where id = ?
2013-03-14 11:21:19 [main] DEBUG org.zhuc.mybatis.mapper.UserMapper.get - ==> Parameters: 1(Integer)
User [id=1, userName=userName0, password=password0, roleId=3, trueName=姓名0, createTime=Mon Mar 04 14:17:31 CST 2013, modifyTime=Mon Mar 04 14:17:31 CST 2013]
2013-03-14 11:21:19 [main] DEBUG org.zhuc.mybatis.mapper.UserMapper.get2 - ooo Using Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@f6d64c5]
2013-03-14 11:21:19 [main] DEBUG org.zhuc.mybatis.mapper.UserMapper.get2 - ==>  Preparing: SELECT * from T_USER where id = ?
2013-03-14 11:21:19 [main] DEBUG org.zhuc.mybatis.mapper.UserMapper.get2 - ==> Parameters: 1(Integer)
User [id=1, userName=userName0, password=password0, roleId=3, trueName=姓名0, createTime=Mon Mar 04 14:17:31 CST 2013, modifyTime=Mon Mar 04 14:17:31 CST 2013]
2013-03-14 11:21:19 [main] DEBUG org.zhuc.mybatis.mapper2.FlowMapper.get - ooo Using Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@11742dfe]
2013-03-14 11:21:19 [main] DEBUG org.zhuc.mybatis.mapper2.FlowMapper.get - ==>  Preparing: SELECT * from T_FLOW where FLOW_ID = ?
2013-03-14 11:21:19 [main] DEBUG org.zhuc.mybatis.mapper2.FlowMapper.get - ==> Parameters: 94003d29-a7b0-42f0-839c-fa609b209ff1(String)
Flow [id=94003d29-a7b0-42f0-839c-fa609b209ff1, insuranceId=5a08abc4-463e-436d-b249-9954be487cdd, codeId=14, operatingTime=Mon Jan 21 16:47:12 CST 2013]

你可能感兴趣的:(spring,mybatis,java,数据库)