Spring+Ibatis框架整合学习

阅读更多
      最近温习了一下J2EE框架里面的Spring+Ibatis的结合,写了一个DEMO,支持多数据源,主要用于以后学习第三方的分布式事务框架以及Spring本身支持的一些特性。这里只贴出主要代码供大家参考,具体代码请下载附件。
       1.  对象   User、Role     User.java  Role.java
      2.  Dao层  接口UserRoleBindingDao   实现UserRoleBindingDaoImpl
      UserRoleBindingDaoImpl.java
public class UserRoleBindingDaoImpl extends SqlMapClientDaoSupport implements UserRoleBindingDao{

	public void bindUserRole(User user, Role role) {
		long nextId = (Long)this.getSqlMapClientTemplate().queryForObject("selectId"); 
		Map map = new HashMap();
		map.put("id", nextId);
		map.put("userId", user.getUserId());
		map.put("roleId", role.getRoleId());
		this.getSqlMapClientTemplate().insert("bindUserRole", map);
		
	}

}

      3. Service层  接口UserRoleBindingService  实现UserRoleBindingServiceImpl
       UserRoleBindingServiceImpl.java
public class UserRoleBindingServiceImpl implements UserRoleBindingService{
	
	private UserService userService;
	private RoleService roleService;
    private UserRoleBindingDao userRoleBindingDao;
	
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public void setUserRoleBindingDao(UserRoleBindingDao userRoleBindingDao) {
		this.userRoleBindingDao = userRoleBindingDao;
	}

	public void bindUserRole(User user, Role role) {
		this.userService.saveUser(user);
		this.roleService.saveRole(role);
		this.userRoleBindingDao.bindUserRole(user, role);
	}

}

      4.  配置文件
        SQL映射文件  userRoleBinding.xml


    

	
          insert into t_user_role values (#id#,#userId#,#roleId#)
	
	
	

       所有SQL存取文件 sql-map-config.xml

    
	
	
	

       数据源配置文件   jdbc.properties
  jdbc1.driverClassName=oracle.jdbc.xa.client.OracleXADataSource
jdbc1.url=jdbc:oracle:thin:@localhost:1521:mgodb
jdbc1.username=user
jdbc1.password=user

jdbc2.driverClassName=oracle.jdbc.xa.client.OracleXADataSource
jdbc2.url=jdbc:oracle:thin:@localhost:1521:mgodb
jdbc2.username=role
jdbc2.password=role

       Bean配置文件  applicationContext-database.xml

        
            ${jdbc1.driverClassName}
        
        
            ${jdbc1.url}
        
        
            ${jdbc1.username}
        
        
            ${jdbc1.password}
        	
    
    
    
        
            ${jdbc2.driverClassName}
        
        
            ${jdbc2.url}
        
        
            ${jdbc2.username}
        
        
            ${jdbc2.password}
        	
    
    
    
        
	        
	            classpath:/dao/sql-map-config.xml
	        
        
    
    
    
		
			
				classpath*:jdbc/jdbc.properties
			
		
	

        applicationContext-dao.xml

	     
	     
	
	
	
	
	     
	
	
	
	    
	     
	

       applicationContext-service.xml

		
			
		
	
    
    
		
			
		
	
	
	
		
			
		
		
			
		
		
			
		
	

       applicationContext-beans.xml

	
    

      5.  日志输出组件log4j  log4j.xml





    
        
            
        
    

    

    	
    	
    	

    	
    	
    	
    	
    		
    	
    

	

		
    	
    	
    	

    	
    	
    	
    	
    		
    	
    
    
    
    
        
    

    
        
    
    
    
        
    

    
        
    
    
    
        
    
    
     
        
    
  
    
        
        
        
    
    

      6.  测试类,Service层进行调用测试  ServiceTest.java
public class ServiceTest extends TestCase{
	
	public UserRoleBindingService service ;

	public void setUp(){
		ApplicationContext ac = new ClassPathXmlApplicationContext("spring/applicationContext-beans.xml");
        this.service = (UserRoleBindingService)ac.getBean("userRoleBindingService");
	}
	
    public void testBindingUserRole(){
    	User user = new User();
        user.setUserId(7);
        user.setUserName("zwustudy");
        Role role = new Role();
        role.setRoleId(7);
        role.setRoleName("vip1");
        service.bindUserRole(user, role);
    }
}
 
     
  • spring.rar (9.3 MB)
  • 下载次数: 186

你可能感兴趣的:(spring,ibatis,框架,dao,java)