mapper:.....存放XXMapper以及实体类的对应表的关系的xml文件
public class DataSourceContextHolder {
private static final ThreadLocal contextHolder = new ThreadLocal();
public static void setDbType(String dbType) {
contextHolder.set(dbType);
}
public static String getDbType() {
return ((String) contextHolder.get());
}
public static void clearDbType() {
contextHolder.remove();
}
}
import java.sql.SQLFeatureNotSupportedException;
import java.util.logging.Logger;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DynamicDataSource extends AbstractRoutingDataSource {
@Override
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
return null;
}
@Override
protected Object determineCurrentLookupKey() {
return DataSourceContextHolder. getDbType();
}
}
property>
property>
property>
property>
property>
property>
property>
property>
property>
public class User {
private int userId;
private int compId;
private String euserName;
private String euserPhone;
private int euserStatus;
private String eAddTime;
private String eLeaveTime;
private String eUserNameCn;
private int cUserId;
private String scDown;
private String userDesc;
public int getUserId() {
return userId;
}
public void setUserId( int userId) {
this. userId = userId;
}
public int getCompId() {
return compId;
}
public void setCompId( int compId) {
this. compId = compId;
}
public String getEuserName() {
return euserName;
}
public void setEuserName(String euserName) {
this. euserName = euserName;
}
public String getEuserPhone() {
return euserPhone;
}
public void setEuserPhone(String euserPhone) {
this. euserPhone = euserPhone;
}
public int getEuserStatus() {
return euserStatus;
}
public void setEuserStatus( int euserStatus) {
this. euserStatus = euserStatus;
}
public String geteAddTime() {
return eAddTime;
}
public void seteAddTime(String eAddTime) {
this. eAddTime = eAddTime;
}
public String geteLeaveTime() {
return eLeaveTime;
}
public void seteLeaveTime(String eLeaveTime) {
this. eLeaveTime = eLeaveTime;
}
public String geteUserNameCn() {
return eUserNameCn;
}
public void seteUserNameCn(String eUserNameCn) {
this. eUserNameCn = eUserNameCn;
}
public int getcUserId() {
return cUserId;
}
public void setcUserId( int cUserId) {
this. cUserId = cUserId;
}
public String getScDown() {
return scDown;
}
public void setScDown(String scDown) {
this. scDown = scDown;
}
public String getUserDesc() {
return userDesc;
}
public void setUserDesc(String userDesc) {
this. userDesc = userDesc;
}
}
public interface UserMapper {
public User getUser(String euserPhone);
}
public interface UserService {
public User getUser(String euserPhone);
}
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.baimi.routerweb.datasource.DataSourceContextHolder;
import com.baimi.routerweb.datasource.DataSourceType;
import com.baimi.routerweb.entity.User;
import com.baimi.routerweb.mapper.UserMapper;
import com.baimi.routerweb.service.UserService;
@Service
public class UserServiceImpl implements UserService {
@Resource(name = "userMapper")
private UserMapper userMapper;
@Override
public User getUser(String euserPhone) {
DataSourceContextHolder. setDbType(DataSourceType.SOURCE_MOP);
return userMapper.getUser(euserPhone);
}
}
//切换数据库
DataSourceContextHolder. setDbType(DataSourceType. SOURCE_MOP);
//从数据库中得到该用户的数据
User user= userService.getUser(userId);
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.baimi.routerweb.common.Constant;
import com.baimi.routerweb.common.ErrorHandle;
import com.baimi.routerweb.datasource.DataSourceContextHolder;
import com.baimi.routerweb.datasource.DataSourceType;
import com.baimi.routerweb.entity.User;
import com.baimi.routerweb.service.UserService;
import com.baimi.routerweb.util.HttpUtil;
@Controller
public class MainController {
@Resource(name = "userServiceImpl")
private UserService userService;
@RequestMapping("/login.do")
public String login(HttpServletRequest request, HttpServletResponse response) {
// 获取客户端传过来的code的值
String code = request.getParameter( "code");
if (code == null || "".equals(code)) {
return ErrorHandle.getError(ErrorHandle.INVALID_TOKEN, "invalid token");
}
// 从微信得到token的值
JSONObject objToken = HttpUtil.httpRequest(Constant.URL_WEIXIN_TOKEN, "GET", null);
if (objToken == null || "".equals(objToken)) {
return ErrorHandle.getError(ErrorHandle.SERVICE_UNRESPONSE, "service unresponse");
}
if (!objToken.has( "access_token")) {
return ErrorHandle.getError(ErrorHandle.INVALID_TOKEN, "invalid token");
}
//根据token和code得到UserId
JSONObject objUser = HttpUtil.httpRequest(Constant.URL_GET_USERID,
"POST", "access_token=" + objToken.getString("access_token" )+ "&code=" + code + "&agentid=1");
if(objUser== null|| "".equals(objUser)){
return ErrorHandle.getError(ErrorHandle.SERVICE_UNRESPONSE, "service unresponse");
}
if(!objUser.has( "UserId")){
return ErrorHandle.getError(ErrorHandle.INVALID_TOKEN, "invalid token");
}
String userId=objUser.getString( "UserId");
//切换数据库
DataSourceContextHolder. setDbType(DataSourceType.SOURCE_MOP);
//从数据库中得到该用户的数据
User user= userService.getUser(userId);
if(user!= null){
JSONObject rtObj = new JSONObject();
rtObj.put( "userId", String.valueOf(user.getUserId()));
return rtObj.toString();
}
return ErrorHandle.getError(ErrorHandle.INVALID_USER,"invalid user");
}
}