博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
(可以跑通的,有数据库的,mchange+ aop )springmvc+jdbctemplate+mchange+multidatasource...
阅读量:5955 次
发布时间:2019-06-19

本文共 8762 字,大约阅读时间需要 29 分钟。

 

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

 

 

 

 

 

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

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

 

 

 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; @Repositorypublic 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; @Controllerpublic 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捐助),没钱捧个人场,谢谢各位。

 
 
 谢谢您的赞助,我会做的更好!

 

 

转载地址:http://raoxx.baihongyu.com/

你可能感兴趣的文章
sybase_isql命令
查看>>
kernel.sem信号量参数调优,以及ipcs信号量队列查询
查看>>
理解嵌入式开发中的一些硬件相关的概念
查看>>
ceph的读写性能测试
查看>>
access_token is invalid or not latest hint
查看>>
H3C设备之 EASY NAT
查看>>
Linux常用命令参考手册02
查看>>
linux 编写shell管理脚本01。2
查看>>
Emmet 文档下载,所有快捷键总结
查看>>
通过EmbeddedServletContainerCustomizer接口调优Tomcat
查看>>
hdu2000——ASCII码排序
查看>>
spring配置线程池
查看>>
Ubuntu 16.04 安裝chrome
查看>>
开发了个 Flipper 调试工具的 Flutter 版本 SDK,让 Flutter 应用调试起来更容易
查看>>
08.实例方法和类方法的区别与及工厂方法
查看>>
mysql 备份
查看>>
内核编译
查看>>
继承和泛型
查看>>
exchange2010 取消OWA内更改密码选项
查看>>
Reverse digits of an integer.
查看>>