(可以跑通的,有数据库的,mchange+ aop )springmvc+jdbctemplate+mchange+multidatasource

阅读更多

 

 基于 上篇文章  mybatis +springmvc+ mchange+multidatasource 没有跑通 , 一直没有找到 问题所在,现在 用 jdbctemplate 是可以跑通的 ,,,,

 

 

 

 

 

(可以跑通的,有数据库的,mchange+ aop )springmvc+jdbctemplate+mchange+multidatasource 

下载:  http://download.csdn.net/download/knight_black_bob/9245103

 

 


(可以跑通的,有数据库的,mchange+ aop )springmvc+jdbctemplate+mchange+multidatasource_第1张图片
 applicationcontext.xml





	
	
  
    
		
	

	
		
	


	  
	   
		
		
		
		
		
		
		
		
		
		
		
	

	 

	  
	    
		
		
		
		
		
		
		
		
		
		
		
	
    
     
		
			
				
				
			
		
		
	  

	 

	
		
			
			
			
			
		
	


      
          
              
          
     
   

 

 

springMvc-servlet.xml



	
	 
	 

	
	
       
          
     
	 
	
	
	
	 
	

 

 

 

web.xml



	

	
	
		/back/jsp/main.jsp
	


	
		contextConfigLocation
		classpath:applicationContext*.xml
	
	
		org.springframework.web.context.ContextLoaderListener
	




	
	
		springMVC
		org.springframework.web.servlet.DispatcherServlet
		
			contextConfigLocation
			classpath:springMVC-servlet.xml
		
		1
	
	
		springMVC
		*.do
	



 

 

 

 

 

DatabaseContextHolder。java

 

 

package com.baoy.dynamic;

public class DatabaseContextHolder {

	private static final ThreadLocal contextHolder = new ThreadLocal();  
	  
    public static void setCustomerType(String customerType) { 
        contextHolder.set(customerType);  
    }  
  
    public static String getCustomerType() {  
        return contextHolder.get();  
    }  
  
    public static void clearCustomerType() {  
        contextHolder.remove();  
    }  
	    
}

 

 

DataSourceInterceptor.java

 

package com.baoy.dynamic;
 
   
public class DataSourceInterceptor {   
    public void setDataSourceOne() {  
        DatabaseContextHolder.setCustomerType("dataSourceOne");  
    }  
       
    public void setDataSourceTwo() {  
        DatabaseContextHolder.setCustomerType("dataSourceTwo");  
    }   
}  

 

DynamicDataSource.java

package com.baoy.dynamic;
 
 

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
 

public class DynamicDataSource  extends AbstractRoutingDataSource{  
  
	  
    @Override  
    protected Object determineCurrentLookupKey() {   
        return DatabaseContextHolder.getCustomerType();   
    }
 
    
}  

 

 

UserDaoImpl.java

package com.baoy.oneweb.daoimpl;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper; 
import org.springframework.stereotype.Repository;

import com.baoy.common.bean.User;
import com.baoy.oneweb.dao.UserDao; 

@Repository
public class UserDaoImpl   implements UserDao {
	

	 @Autowired 
	 private JdbcTemplate jdbcTemplate;
	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
	
	@SuppressWarnings("rawtypes")
	private class UserRowMapper implements RowMapper {

//		userId,userName,password,tel,sex,description
		@Override
		public User mapRow(ResultSet rs, int i) throws SQLException {
			User user = new User(); 
			user.setUserId(rs.getInt("userId"));
			user.setUserName(rs.getString("userName"));
			user.setPassword(rs.getString("password"));
			user.setTel(rs.getString("tel"));
			user.setSex(rs.getString("sex"));
			user.setDescription(rs.getString("description"));
			return user;
		}
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List allUser() {
		String sql = "select * from t_user ";
		return   this.getJdbcTemplate().query(sql, new UserRowMapper());
	}

	@Override
	public void delUser(int userId) {
		String sql = "delete from t_user where userId ='"+userId+"'"; 
	    this.getJdbcTemplate().update(sql );
		
	}

	@Override
	public User user(int userId) {
		String sql = "select * from t_user where  userId ='"+userId+"'"; 
		List userList = this.getJdbcTemplate().query(sql, new UserRowMapper());
		if (userList !=null && userList.size() > 0 ) {
			return userList.get(0);
		}else{
			return null;	
		}
	}

	@Override
	public void updateUser(User user) {
		String sql = "update t_user set userName=?,password=?,tel=?,sex=?,description=?";
		  this.getJdbcTemplate().update(sql, new Object[] {
					 user.getUserName(),
					 user.getPassword(),
					 user.getTel(),
					 user.getSex(),
					 user.getDescription()
			 });
	}

	@Override
	public void addUser(User user) {
		String sql = "insert into t_user(userName,password,tel,sex,description) values (?,?,?,?,?)";
		 this.getJdbcTemplate().update(sql, new Object[] {
				 user.getUserName(),
				 user.getPassword(),
				 user.getTel(),
				 user.getSex(),
				 user.getDescription()
		 });
	}


}

 service

package com.baoy.oneweb.service;

import java.util.List; 

import com.baoy.common.bean.User; 
 public interface UserService {
 
   public List allUser();

   public void delUser(int userId);
   public User user(int userId);

   public void updateUser(User user);

   public void addUser(User user);

}

 

 

 

action

package com.baoy.oneweb.action;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.baoy.common.bean.User;
import com.baoy.oneweb.service.UserService;
 

@Controller
public class UserController {

       
      @Autowired
      UserService userService;

      @RequestMapping(value = "/userView.do")
      public String userView(ModelMap modelMap,String pageNo, String choice, HttpSession session){
            List userList = userService.allUser();  
           modelMap.put("userList", userList);
           return "back/jsp/user/userView";
     }

      @RequestMapping(value = "/userDel{userId}.do")
      public String userDel(@PathVariable("userId")int userId ,ModelMap modelMap,String pageNo, String choice, HttpSession session){
             userService.delUser(userId);
             String message1="删除成功";
             String message2="请返回……";
             String  url="userView.do";
             modelMap.put("message1", message1);
             modelMap.put("message2", message2);
             modelMap.put("url", url);
            return "infomationShow";
      }

      @RequestMapping(value ="/userGoAdd.do")
             public String userGoAdd(ModelMap modelMap,String pageNo, String choice, HttpSession session){
             return "back/jsp/user/userAdd";
      }

      @RequestMapping(value = "/userAdd.do",method=RequestMethod.POST)
      public String userAdd(User form,ModelMap modelMap,String pageNo, String choice, HttpSession session){
             userService.addUser(form);
             String message1="添加成功";
             String message2="请返回……";
             String  url="userView.do";
             modelMap.put("message1", message1);
             modelMap.put("message2", message2);
            modelMap.put("url", url);
             return "infomationShow";
      }

      @RequestMapping(value = "/userGoUpdate{userId}.do")
      public String userGoUpdate(@PathVariable("userId")int userId ,ModelMap modelMap,String pageNo, String choice, HttpSession session){
            User user = userService.user(userId);
           modelMap.put("user", user);
           return "back/jsp/user/userUpdate";
      }

      @RequestMapping(value = "/userUpdate.do",method=RequestMethod.POST)
      public String userUpdate(User form,ModelMap modelMap,String pageNo, String choice, HttpSession session){
             userService.updateUser(form);
             String message1="修改成功";
             String message2="请返回……";
             String  url="userView.do";
             modelMap.put("message1", message1);
             modelMap.put("message2", message2);
             modelMap.put("url", url);
             return "infomationShow";
      }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

捐助开发者

在兴趣的驱动下,写一个免费的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。


(可以跑通的,有数据库的,mchange+ aop )springmvc+jdbctemplate+mchange+multidatasource_第2张图片(可以跑通的,有数据库的,mchange+ aop )springmvc+jdbctemplate+mchange+multidatasource_第3张图片(可以跑通的,有数据库的,mchange+ aop )springmvc+jdbctemplate+mchange+multidatasource_第4张图片
 
 
 谢谢您的赞助,我会做的更好!

 

 

  • (可以跑通的,有数据库的,mchange+ aop )springmvc+jdbctemplate+mchange+multidatasource_第5张图片
  • 大小: 8.8 KB
  • 查看图片附件

你可能感兴趣的:(springmvc,jdbctemplate,mchange,multidatasource,spring)