1、项目结构
根据“开源项目zheng”拆分出来的切换数据源核心代码,中间有少许改动。
2、代码功能划分
Java部分
Annotation部分
package com.vivebest.annotation; import java.lang.annotation.*; /** * 初始化继承BaseService的service *Created by shuzheng on 2017/2/11. */ @Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface BaseService { }
Controller部分
package com.vivebest.controller; import com.vivebest.entity.UpmsLogExample; importcom.vivebest.service.CustomerService; import org.springframework.beans.factory.annotation.Autowired; importorg.springframework.stereotype.Controller; importorg.springframework.web.bind.annotation.PathVariable; importorg.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; importorg.springframework.web.bind.annotation.ResponseBody; @Controller//标明为控制层 @RequestMapping("/customer")//相当于路径 public class CustomerController{ @Autowired private CustomerService customerService; /** * 测试查询1 */ @RequestMapping(value ="/queryLog", method =RequestMethod.GET) @ResponseBody public Object queryLog(){ return customerService.countByExample(new UpmsLogExample()); } /** * 测试查询2 */ @RequestMapping(value ="/queryLogB", method =RequestMethod.GET) @ResponseBody public Object queryLogB(){ return customerService.countByExampleB(new UpmsLogExample()); } /** * 测试事物 */ @RequestMapping(value ="/deleteLog/{logId}", method =RequestMethod.GET) @ResponseBody public Object deleteLog(@PathVariable("logId") Integer logId){ UpmsLogExample upmsLogExample = new UpmsLogExample(); UpmsLogExample.Criteria upmsLogExampleCriteria = upmsLogExample.createCriteria(); upmsLogExampleCriteria.andLogIdEqualTo(logId); return customerService.deleteByExample(upmsLogExample); } /** * 测试事物2 */ @RequestMapping(value ="/deleteLogB/{logId}", method =RequestMethod.GET) @ResponseBody public Object deleteLogB(@PathVariable("logId") IntegerlogId){ UpmsLogExample upmsLogExample = new UpmsLogExample(); UpmsLogExample.Criteria upmsLogExampleCriteria =upmsLogExample.createCriteria(); upmsLogExampleCriteria.andLogIdEqualTo(logId); return customerService.deleteByExampleB(upmsLogExample); } }
Dao部分
package com.vivebest.dao; import com.vivebest.entity.UpmsLogExample; public interface UpmsLogMapper { long countByExample(UpmsLogExample example); int deleteByExample(UpmsLogExample example); long countByExampleB(UpmsLogExample example); int deleteByExampleB(UpmsLogExample example); }
Entity部分
-----------------------
package com.vivebest.entity; import java.io.Serializable; public class UpmsLog implementsSerializable { /** * 编号 * * @mbg.generated */ private Integer logId; /** * 操作描述 * * @mbg.generated */ private String description; /** * 操作用户 * * @mbg.generated */ private String username; /** * 操作时间 * * @mbg.generated */ private Long startTime; /** * 消耗时间 * * @mbg.generated */ private Integer spendTime; /** * 根路径 * * @mbg.generated */ private String basePath; /** * URI * * @mbg.generated */ private String uri; /** * URL * * @mbg.generated */ private String url; /** * 请求类型 * * @mbg.generated */ private String method; /** * 用户标识 * * @mbg.generated */ private String userAgent; /** * IP地址 * * @mbg.generated */ private String ip; /** * 权限值 * * @mbg.generated */ private String permissions; private String parameter; private String result; private static final long serialVersionUID = 1L; public Integer getLogId() { return logId; } public void setLogId(Integer logId) { this.logId = logId; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Long getStartTime() { return startTime; } public void setStartTime(Long startTime) { this.startTime = startTime; } public Integer getSpendTime() { return spendTime; } public void setSpendTime(Integer spendTime) { this.spendTime = spendTime; } public String getBasePath() { return basePath; } public void setBasePath(String basePath) { this.basePath = basePath; } public String getUri() { return uri; } public void setUri(String uri) { this.uri = uri; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getMethod() { return method; } public void setMethod(String method) { this.method = method; } public String getUserAgent() { return userAgent; } public void setUserAgent(String userAgent) { this.userAgent = userAgent; } public String getIp() { return ip; } public void setIp(String ip) { this.ip = ip; } public String getPermissions() { return permissions; } public void setPermissions(String permissions) { this.permissions = permissions; } public String getParameter() { return parameter; } public void setParameter(String parameter) { this.parameter = parameter; } public String getResult() { return result; } public void setResult(String result) { this.result = result; } @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append(getClass().getSimpleName()); sb.append(" ["); sb.append("Hash = ").append(hashCode()); sb.append(", logId=").append(logId); sb.append(", description=").append(description); sb.append(", username=").append(username); sb.append(", startTime=").append(startTime); sb.append(", spendTime=").append(spendTime); sb.append(", basePath=").append(basePath); sb.append(", uri=").append(uri); sb.append(", url=").append(url); sb.append(", method=").append(method); sb.append(", userAgent=").append(userAgent); sb.append(", ip=").append(ip); sb.append(", permissions=").append(permissions); sb.append(", parameter=").append(parameter); sb.append(", result=").append(result); sb.append("]"); return sb.toString(); } @Override public boolean equals(Object that) { if (this == that) { return true; } if (that == null) { return false; } if (getClass() != that.getClass()) { return false; } UpmsLog other = (UpmsLog) that; return (this.getLogId() == null ? other.getLogId() == null :this.getLogId().equals(other.getLogId())) && (this.getDescription() == null ? other.getDescription() ==null : this.getDescription().equals(other.getDescription())) && (this.getUsername() == null ? other.getUsername() == null :this.getUsername().equals(other.getUsername())) && (this.getStartTime() == null ? other.getStartTime() == null :this.getStartTime().equals(other.getStartTime())) && (this.getSpendTime() == null ? other.getSpendTime() == null :this.getSpendTime().equals(other.getSpendTime())) && (this.getBasePath() == null ? other.getBasePath() == null :this.getBasePath().equals(other.getBasePath())) && (this.getUri() == null ? other.getUri() == null :this.getUri().equals(other.getUri())) && (this.getUrl() == null ? other.getUrl() == null :this.getUrl().equals(other.getUrl())) && (this.getMethod() == null ? other.getMethod() == null :this.getMethod().equals(other.getMethod())) && (this.getUserAgent() == null ? other.getUserAgent() == null :this.getUserAgent().equals(other.getUserAgent())) && (this.getIp() == null ?other.getIp() == null : this.getIp().equals(other.getIp())) && (this.getPermissions() == null ? other.getPermissions() ==null : this.getPermissions().equals(other.getPermissions())) && (this.getParameter() == null ? other.getParameter() == null :this.getParameter().equals(other.getParameter())) && (this.getResult() == null ? other.getResult() == null :this.getResult().equals(other.getResult())); } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((getLogId() == null) ? 0 :getLogId().hashCode()); result = prime * result + ((getDescription() == null) ? 0 :getDescription().hashCode()); result = prime * result + ((getUsername() == null) ? 0 :getUsername().hashCode()); result = prime * result + ((getStartTime() == null) ? 0 :getStartTime().hashCode()); result = prime * result + ((getSpendTime() == null) ? 0 : getSpendTime().hashCode()); result = prime * result + ((getBasePath() == null) ? 0 :getBasePath().hashCode()); result = prime * result + ((getUri() == null) ? 0 :getUri().hashCode()); result = prime * result + ((getUrl() == null) ? 0 :getUrl().hashCode()); result = prime * result + ((getMethod() == null) ? 0 :getMethod().hashCode()); result = prime * result + ((getUserAgent() == null) ? 0 :getUserAgent().hashCode()); result = prime * result + ((getIp() == null) ? 0 : getIp().hashCode()); result = prime * result + ((getPermissions() == null) ? 0 :getPermissions().hashCode()); result = prime * result + ((getParameter() == null) ? 0 :getParameter().hashCode()); result = prime * result + ((getResult() == null) ? 0 :getResult().hashCode()); return result; } }
-----------------------
package com.vivebest.entity; import java.io.Serializable; import java.util.ArrayList; import java.util.List; public class UpmsLogExample implements Serializable { protected String orderByClause; protected boolean distinct; protected ListoredCriteria; private static final long serialVersionUID = 1L; public UpmsLogExample() { oredCriteria = new ArrayList (); } public void setOrderByClause(String orderByClause) { this.orderByClause = orderByClause; } public String getOrderByClause() { return orderByClause; } public void setDistinct(boolean distinct) { this.distinct = distinct; } public boolean isDistinct() { return distinct; } public List getOredCriteria() { return oredCriteria; } public void or(Criteria criteria) { oredCriteria.add(criteria); } public Criteria or() { Criteria criteria = createCriteriaInternal(); oredCriteria.add(criteria); return criteria; } public Criteria createCriteria() { Criteria criteria = createCriteriaInternal(); if (oredCriteria.size() == 0) { oredCriteria.add(criteria); } return criteria; } protected Criteria createCriteriaInternal() { Criteria criteria = new Criteria(); return criteria; } public void clear() { oredCriteria.clear(); orderByClause = null; distinct = false; } protected abstract static class GeneratedCriteria implementsSerializable { protected List criteria; protected GeneratedCriteria() { super(); criteria = new ArrayList (); } public boolean isValid() { return criteria.size() > 0; } public List getAllCriteria() { return criteria; } public List getCriteria() { return criteria; } protected void addCriterion(String condition) { if (condition == null) { throw newRuntimeException("Value for condition cannot be null"); } criteria.add(new Criterion(condition)); } protected void addCriterion(String condition, Object value, Stringproperty) { if (value == null) { throw newRuntimeException("Value for " + property + " cannot benull"); } criteria.add(new Criterion(condition, value)); } protected void addCriterion(String condition, Object value1, Objectvalue2, String property) { if (value1 == null || value2 == null) { throw newRuntimeException("Between values for " + property + " cannot benull"); } criteria.add(new Criterion(condition, value1, value2)); } public Criteria andLogIdIsNull() { addCriterion("log_id is null"); return (Criteria) this; } public Criteria andLogIdIsNotNull() { addCriterion("log_id is not null"); return (Criteria) this; } public Criteria andLogIdEqualTo(Integer value) { addCriterion("log_id =", value, "logId"); return (Criteria) this; } public Criteria andLogIdNotEqualTo(Integervalue) { addCriterion("log_id <>", value, "logId"); return (Criteria) this; } public Criteria andLogIdGreaterThan(Integer value) { addCriterion("log_id >", value, "logId"); return (Criteria) this; } public Criteria andLogIdGreaterThanOrEqualTo(Integer value) { addCriterion("log_id >=", value, "logId"); return (Criteria) this; } public Criteria andLogIdLessThan(Integer value) { addCriterion("log_id <", value, "logId"); return (Criteria) this; } public Criteria andLogIdLessThanOrEqualTo(Integer value) { addCriterion("log_id <=", value, "logId"); return (Criteria) this; } public Criteria andLogIdIn(List values) { addCriterion("log_id in", values, "logId"); return (Criteria) this; } public Criteria andLogIdNotIn(List values) { addCriterion("log_id not in", values, "logId"); return (Criteria) this; } public Criteria andLogIdBetween(Integer value1, Integer value2) { addCriterion("log_id between", value1, value2,"logId"); return (Criteria) this; } public Criteria andLogIdNotBetween(Integer value1, Integer value2) { addCriterion("log_id not between", value1, value2,"logId"); return (Criteria) this; } public Criteria andDescriptionIsNull() { addCriterion("description is null"); return (Criteria) this; } public Criteria andDescriptionIsNotNull() { addCriterion("description is not null"); return (Criteria) this; } public Criteria andDescriptionEqualTo(String value) { addCriterion("description =", value, "description"); return (Criteria) this; } public Criteria andDescriptionNotEqualTo(String value) { addCriterion("description <>", value,"description"); return (Criteria) this; } public Criteria andDescriptionGreaterThan(String value) { addCriterion("description >", value,"description"); return (Criteria) this; } public Criteria andDescriptionGreaterThanOrEqualTo(String value) { addCriterion("description >=", value,"description"); return (Criteria) this; } public Criteria andDescriptionLessThan(String value) { addCriterion("description <", value,"description"); return (Criteria) this; } public Criteria andDescriptionLessThanOrEqualTo(String value) { addCriterion("description <=", value,"description"); return (Criteria) this; } public Criteria andDescriptionLike(String value) { addCriterion("description like", value,"description"); return (Criteria) this; } public Criteria andDescriptionNotLike(String value) { addCriterion("description not like", value,"description"); return (Criteria) this; } public Criteria andDescriptionIn(List values) { addCriterion("description in", values,"description"); return (Criteria) this; } public Criteria andDescriptionNotIn(List values) { addCriterion("description not in", values,"description"); return (Criteria) this; } public Criteria andDescriptionBetween(String value1, String value2) { addCriterion("description between", value1, value2,"description"); return (Criteria) this; } public Criteria andDescriptionNotBetween(String value1, String value2) { addCriterion("description not between", value1, value2,"description"); return (Criteria) this; } public Criteria andUsernameIsNull() { addCriterion("username is null"); return (Criteria) this; } public Criteria andUsernameIsNotNull() { addCriterion("username is not null"); return (Criteria) this; } public Criteria andUsernameEqualTo(String value) { addCriterion("username =", value, "username"); return (Criteria) this; } public Criteria andUsernameNotEqualTo(String value) { addCriterion("username <>", value,"username"); return (Criteria) this; } public Criteria andUsernameGreaterThan(String value) { addCriterion("username >", value, "username"); return (Criteria) this; } public Criteria andUsernameGreaterThanOrEqualTo(String value) { addCriterion("username >=", value, "username"); return (Criteria) this; } public Criteria andUsernameLessThan(String value) { addCriterion("username <", value, "username"); return (Criteria) this; } public Criteria andUsernameLessThanOrEqualTo(String value) { addCriterion("username <=", value, "username"); return (Criteria) this; } public Criteria andUsernameLike(String value) { addCriterion("username like", value, "username"); return (Criteria) this; } public Criteria andUsernameNotLike(String value) { addCriterion("username not like", value,"username"); return (Criteria) this; } public Criteria andUsernameIn(List values) { addCriterion("username in", values, "username"); return (Criteria) this; } public Criteria andUsernameNotIn(List values) { addCriterion("username not in", values, "username"); return (Criteria) this; } public Criteria andUsernameBetween(String value1, String value2) { addCriterion("username between", value1, value2,"username"); return (Criteria) this; } public Criteria andUsernameNotBetween(String value1, String value2) { addCriterion("username not between", value1, value2,"username"); return (Criteria) this; } public Criteria andStartTimeIsNull() { addCriterion("start_time is null"); return (Criteria) this; } public Criteria andStartTimeIsNotNull() { addCriterion("start_time is not null"); return (Criteria) this; } public Criteria andStartTimeEqualTo(Long value) { addCriterion("start_time =", value, "startTime"); return (Criteria) this; } public Criteria andStartTimeNotEqualTo(Long value) { addCriterion("start_time <>", value,"startTime"); return (Criteria) this; } public Criteria andStartTimeGreaterThan(Long value) { addCriterion("start_time >", value, "startTime"); return (Criteria) this; } public CriteriaandStartTimeGreaterThanOrEqualTo(Long value) { addCriterion("start_time >=", value,"startTime"); return (Criteria) this; } public Criteria andStartTimeLessThan(Long value) { addCriterion("start_time <", value, "startTime"); return (Criteria) this; } public Criteria andStartTimeLessThanOrEqualTo(Long value) { addCriterion("start_time <=", value,"startTime"); return (Criteria) this; } public Criteria andStartTimeIn(List values) { addCriterion("start_time in", values, "startTime"); return (Criteria) this; } public Criteria andStartTimeNotIn(List values) { addCriterion("start_timenot in", values, "startTime"); return (Criteria) this; } public Criteria andStartTimeBetween(Long value1, Long value2) { addCriterion("start_time between", value1, value2,"startTime"); return (Criteria) this; } public Criteria andStartTimeNotBetween(Long value1, Long value2) { addCriterion("start_time not between", value1, value2,"startTime"); return (Criteria) this; } public Criteria andSpendTimeIsNull() { addCriterion("spend_time is null"); return (Criteria) this; } public Criteria andSpendTimeIsNotNull() { addCriterion("spend_time is not null"); return (Criteria) this; } public Criteria andSpendTimeEqualTo(Integer value) { addCriterion("spend_time =", value, "spendTime"); return (Criteria) this; } public Criteria andSpendTimeNotEqualTo(Integer value) { addCriterion("spend_time <>", value,"spendTime"); return (Criteria) this; } public Criteria andSpendTimeGreaterThan(Integer value) { addCriterion("spend_time >", value, "spendTime"); return (Criteria) this; } public Criteria andSpendTimeGreaterThanOrEqualTo(Integer value) { addCriterion("spend_time >=", value,"spendTime"); return (Criteria) this; } public Criteria andSpendTimeLessThan(Integer value) { addCriterion("spend_time <", value, "spendTime"); return (Criteria) this; } public Criteria andSpendTimeLessThanOrEqualTo(Integer value) { addCriterion("spend_time <=", value,"spendTime"); return (Criteria) this; } public Criteria andSpendTimeIn(List values) { addCriterion("spend_time in", values, "spendTime"); return (Criteria) this; } public Criteria andSpendTimeNotIn(List values) { addCriterion("spend_time not in", values,"spendTime"); return (Criteria) this; } public Criteria andSpendTimeBetween(Integer value1, Integer value2) { addCriterion("spend_time between", value1, value2,"spendTime"); return (Criteria) this; } public Criteria andSpendTimeNotBetween(Integer value1, Integer value2) { addCriterion("spend_time not between", value1, value2,"spendTime"); return (Criteria) this; } public Criteria andBasePathIsNull() { addCriterion("base_path is null"); return (Criteria) this; } public Criteria andBasePathIsNotNull() { addCriterion("base_path is not null"); return (Criteria) this; } public Criteria andBasePathEqualTo(String value) { addCriterion("base_path =", value, "basePath"); return (Criteria) this; } public Criteria andBasePathNotEqualTo(String value) { addCriterion("base_path <>", value,"basePath"); return (Criteria) this; } public Criteria andBasePathGreaterThan(String value) { addCriterion("base_path >", value, "basePath"); return (Criteria) this; } public Criteria andBasePathGreaterThanOrEqualTo(String value) { addCriterion("base_path >=", value, "basePath"); return (Criteria) this; } public Criteria andBasePathLessThan(String value) { addCriterion("base_path <", value, "basePath"); return (Criteria) this; } public Criteria andBasePathLessThanOrEqualTo(String value) { addCriterion("base_path <=", value, "basePath"); return (Criteria) this; } public Criteria andBasePathLike(String value) { addCriterion("base_path like", value, "basePath"); return (Criteria) this; } public Criteria andBasePathNotLike(String value) { addCriterion("base_path not like", value,"basePath"); return (Criteria) this; } public Criteria andBasePathIn(List values) { addCriterion("base_path in", values, "basePath"); return (Criteria) this; } public Criteria andBasePathNotIn(List values) { addCriterion("base_path not in", values,"basePath"); return (Criteria) this; } public Criteria andBasePathBetween(String value1, String value2) { addCriterion("base_path between", value1, value2,"basePath"); return (Criteria) this; } public Criteria andBasePathNotBetween(String value1, String value2) { addCriterion("base_path not between", value1, value2,"basePath"); return (Criteria) this; } public Criteria andUriIsNull() { addCriterion("uri is null"); return (Criteria) this; } public Criteria andUriIsNotNull() { addCriterion("uri is not null"); return (Criteria) this; } public Criteria andUriEqualTo(String value) { addCriterion("uri =", value, "uri"); return (Criteria) this; } public Criteria andUriNotEqualTo(String value) { addCriterion("uri <>", value, "uri"); return (Criteria) this; } public Criteria andUriGreaterThan(String value) { addCriterion("uri >", value, "uri"); return (Criteria) this; } public Criteria andUriGreaterThanOrEqualTo(String value) { addCriterion("uri >=", value, "uri"); return (Criteria) this; } public Criteria andUriLessThan(String value) { addCriterion("uri <", value, "uri"); return (Criteria) this; } public Criteria andUriLessThanOrEqualTo(String value) { addCriterion("uri <=", value, "uri"); return (Criteria) this; } public Criteria andUriLike(String value) { addCriterion("uri like", value, "uri"); return (Criteria) this; } public Criteria andUriNotLike(String value) { addCriterion("uri not like", value, "uri"); return (Criteria) this; } public Criteria andUriIn(List values) { addCriterion("uri in", values, "uri"); return (Criteria) this; } public Criteria andUriNotIn(List values) { addCriterion("uri not in", values, "uri"); return (Criteria) this; } public Criteria andUriBetween(String value1, String value2) { addCriterion("uri between", value1, value2, "uri"); return (Criteria) this; } public Criteria andUriNotBetween(String value1, String value2) { addCriterion("uri not between", value1, value2,"uri"); return (Criteria) this; } public Criteria andUrlIsNull() { addCriterion("url is null"); return (Criteria) this; } public Criteria andUrlIsNotNull() { addCriterion("url is not null"); return (Criteria) this; } public Criteria andUrlEqualTo(String value) { addCriterion("url =", value, "url"); return (Criteria) this; } public Criteria andUrlNotEqualTo(String value) { addCriterion("url <>", value, "url"); return (Criteria) this; } public Criteria andUrlGreaterThan(String value) { addCriterion("url >", value, "url"); return (Criteria) this; } public Criteria andUrlGreaterThanOrEqualTo(String value) { addCriterion("url >=", value, "url"); return (Criteria) this; } public Criteria andUrlLessThan(String value) { addCriterion("url <", value, "url"); return (Criteria) this; } public Criteria andUrlLessThanOrEqualTo(String value) { addCriterion("url <=", value, "url"); return (Criteria) this; } public Criteria andUrlLike(String value) { addCriterion("url like", value, "url"); return (Criteria) this; } public Criteria andUrlNotLike(String value) { addCriterion("url not like", value, "url"); return (Criteria) this; } public Criteria andUrlIn(List values) { addCriterion("url in", values, "url"); return (Criteria) this; } public Criteria andUrlNotIn(List values) { addCriterion("url not in", values, "url"); return (Criteria) this; } public Criteria andUrlBetween(String value1, String value2) { addCriterion("url between", value1, value2, "url"); return (Criteria) this; } public Criteria andUrlNotBetween(String value1, String value2) { addCriterion("url not between", value1, value2,"url"); return (Criteria) this; } public Criteria andMethodIsNull() { addCriterion("method is null"); return (Criteria) this; } public Criteria andMethodIsNotNull() { addCriterion("method is not null"); return (Criteria) this; } public Criteria andMethodEqualTo(String value) { addCriterion("method =", value, "method"); return (Criteria) this; } public Criteria andMethodNotEqualTo(String value) { addCriterion("method <>", value, "method"); return (Criteria) this; } public Criteria andMethodGreaterThan(String value) { addCriterion("method >", value, "method"); return (Criteria) this; } public Criteria andMethodGreaterThanOrEqualTo(String value) { addCriterion("method >=", value, "method"); return (Criteria) this; } public Criteria andMethodLessThan(String value) { addCriterion("method <", value, "method"); return (Criteria) this; } public Criteria andMethodLessThanOrEqualTo(String value) { addCriterion("method <=", value, "method"); return (Criteria) this; } public Criteria andMethodLike(String value) { addCriterion("method like", value, "method"); return (Criteria) this; } public Criteria andMethodNotLike(String value) { addCriterion("method not like", value, "method"); return (Criteria) this; } public Criteria andMethodIn(List values) { addCriterion("method in", values, "method"); return (Criteria) this; } public Criteria andMethodNotIn(List values) { addCriterion("method not in", values, "method"); return (Criteria) this; } public Criteria andMethodBetween(String value1, String value2) { addCriterion("method between", value1, value2,"method"); return (Criteria) this; } public Criteria andMethodNotBetween(String value1, String value2) { addCriterion("method not between", value1, value2,"method"); return (Criteria) this; } public Criteria andUserAgentIsNull() { addCriterion("user_agent is null"); return (Criteria) this; } public Criteria andUserAgentIsNotNull() { addCriterion("user_agent is not null"); return (Criteria) this; } public Criteria andUserAgentEqualTo(String value) { addCriterion("user_agent =", value, "userAgent"); return (Criteria) this; } public Criteria andUserAgentNotEqualTo(String value) { addCriterion("user_agent <>", value,"userAgent"); return (Criteria) this; } public Criteria andUserAgentGreaterThan(String value) { addCriterion("user_agent >", value, "userAgent"); return (Criteria) this; } public Criteria andUserAgentGreaterThanOrEqualTo(String value) { addCriterion("user_agent >=", value,"userAgent"); return (Criteria) this; } public Criteria andUserAgentLessThan(String value) { addCriterion("user_agent <", value, "userAgent"); return (Criteria) this; } public Criteria andUserAgentLessThanOrEqualTo(String value) { addCriterion("user_agent <=", value,"userAgent"); return (Criteria) this; } public Criteria andUserAgentLike(String value) { addCriterion("user_agent like", value, "userAgent"); return (Criteria) this; } public Criteria andUserAgentNotLike(String value) { addCriterion("user_agent not like", value,"userAgent"); return (Criteria) this; } public Criteria andUserAgentIn(List values) { addCriterion("user_agent in", values, "userAgent"); return (Criteria) this; } public Criteria andUserAgentNotIn(List values) { addCriterion("user_agent not in", values,"userAgent"); return (Criteria) this; } public Criteria andUserAgentBetween(String value1, String value2) { addCriterion("user_agent between", value1, value2,"userAgent"); return (Criteria) this; } public Criteria andUserAgentNotBetween(String value1, String value2) { addCriterion("user_agent not between", value1, value2,"userAgent"); return (Criteria) this; } public Criteria andIpIsNull() { addCriterion("ip is null"); return (Criteria) this; } public Criteria andIpIsNotNull() { addCriterion("ip is not null"); return (Criteria) this; } public Criteria andIpEqualTo(String value) { addCriterion("ip =", value, "ip"); return (Criteria) this; } public Criteria andIpNotEqualTo(String value) { addCriterion("ip <>", value, "ip"); return (Criteria) this; } public Criteria andIpGreaterThan(String value) { addCriterion("ip >", value, "ip"); return (Criteria) this; } public Criteria andIpGreaterThanOrEqualTo(String value) { addCriterion("ip >=", value, "ip"); return (Criteria) this; } public Criteria andIpLessThan(String value) { addCriterion("ip <", value, "ip"); return (Criteria) this; } public Criteria andIpLessThanOrEqualTo(String value) { addCriterion("ip <=", value, "ip"); return (Criteria) this; } public Criteria andIpLike(String value) { addCriterion("ip like", value, "ip"); return (Criteria) this; } public Criteria andIpNotLike(String value) { addCriterion("ip not like", value, "ip"); return (Criteria) this; } public Criteria andIpIn(List values) { addCriterion("ip in", values, "ip"); return (Criteria) this; } public Criteria andIpNotIn(List values) { addCriterion("ip not in", values, "ip"); return (Criteria) this; } public Criteria andIpBetween(String value1, String value2) { addCriterion("ip between", value1, value2, "ip"); return (Criteria) this; } public Criteria andIpNotBetween(String value1, String value2) { addCriterion("ip notbetween", value1, value2, "ip"); return (Criteria) this; } public Criteria andPermissionsIsNull() { addCriterion("permissions is null"); return (Criteria) this; } public Criteria andPermissionsIsNotNull() { addCriterion("permissions is not null"); return (Criteria) this; } public Criteria andPermissionsEqualTo(String value) { addCriterion("permissions =", value, "permissions"); return (Criteria) this; } public Criteria andPermissionsNotEqualTo(String value) { addCriterion("permissions <>", value,"permissions"); return (Criteria) this; } public Criteria andPermissionsGreaterThan(String value) { addCriterion("permissions >", value,"permissions"); return (Criteria) this; } public Criteria andPermissionsGreaterThanOrEqualTo(String value) { addCriterion("permissions>=", value, "permissions"); return (Criteria) this; } public Criteria andPermissionsLessThan(String value) { addCriterion("permissions <", value,"permissions"); return (Criteria) this; } public Criteria andPermissionsLessThanOrEqualTo(String value) { addCriterion("permissions <=", value,"permissions"); return (Criteria) this; } public Criteria andPermissionsLike(String value) { addCriterion("permissions like", value,"permissions"); return (Criteria) this; } public Criteria andPermissionsNotLike(String value) { addCriterion("permissions not like", value, "permissions"); return (Criteria) this; } public Criteria andPermissionsIn(List values) { addCriterion("permissions in", values,"permissions"); return (Criteria) this; } public Criteria andPermissionsNotIn(List values) { addCriterion("permissions not in", values,"permissions"); return (Criteria) this; } public Criteria andPermissionsBetween(String value1, String value2) { addCriterion("permissions between", value1, value2,"permissions"); return (Criteria) this; } public Criteria andPermissionsNotBetween(String value1, String value2) { addCriterion("permissions not between", value1, value2,"permissions"); return (Criteria) this; } } public static class Criteria extends GeneratedCriteria implementsSerializable { protected Criteria() { super(); } } public static class Criterion implements Serializable { private String condition; private Object value; private Object secondValue; private boolean noValue; private boolean singleValue; private boolean betweenValue; private boolean listValue; private String typeHandler; public String getCondition() { return condition; } public Object getValue() { return value; } public Object getSecondValue() { return secondValue; } public boolean isNoValue() { return noValue; } public boolean isSingleValue() { return singleValue; } public boolean isBetweenValue() { return betweenValue; } public boolean isListValue() { return listValue; } public String getTypeHandler() { return typeHandler; } protected Criterion(Stringcondition) { super(); this.condition = condition; this.typeHandler = null; this.noValue = true; } protected Criterion(String condition, Object value, String typeHandler){ super(); this.condition = condition; this.value = value; this.typeHandler = typeHandler; if (value instanceof List>) { this.listValue = true; } else { this.singleValue = true; } } protected Criterion(String condition, Object value) { this(condition, value, null); } protected Criterion(String condition, Object value, Object secondValue,String typeHandler) { super(); this.condition = condition; this.value = value; this.secondValue = secondValue; this.typeHandler = typeHandler; this.betweenValue = true; } protected Criterion(String condition, Object value, Object secondValue){ this(condition, value, secondValue, null); } } }
------------------------
Service部分
-----------------------------
package com.vivebest.service; import com.vivebest.entity.UpmsLog; import com.vivebest.entity.UpmsLogExample; import com.vivebest.util.BaseService; public interface CustomerService extendsBaseService{ long countByExampleB(UpmsLogExample example); int deleteByExampleB(UpmsLogExample example); }
-------------------------------
package com.vivebest.service.impl; import com.vivebest.annotation.BaseService; import com.vivebest.dao.UpmsLogMapper; import com.vivebest.entity.UpmsLog; import com.vivebest.entity.UpmsLogExample; importcom.vivebest.service.CustomerService; import com.vivebest.util.BaseServiceImpl; importorg.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service @BaseService public class CustomerServiceImpl extendsBaseServiceImplimplementsCustomerService{ @Autowired private UpmsLogMapper upmsLogMapper; @Override public long countByExampleB(UpmsLogExample example) { return upmsLogMapper.countByExampleB(example); } @Override public int deleteByExampleB(UpmsLogExample example) { return upmsLogMapper.deleteByExampleB(example); } }
------------------------------
Util部分
---------------------------
package com.vivebest.util; import com.vivebest.annotation.BaseService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.ApplicationListener; importorg.springframework.context.event.ContextRefreshedEvent; import java.lang.reflect.Method; import java.util.Map; /** *spring容器初始化完成事件 *Created by shuzheng on 2017/1/7. */ public class ApplicationContextListenerimplements ApplicationListener{ private static Logger _log =LoggerFactory.getLogger(ApplicationContextListener.class); @Override public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent){ // root application context if(null == contextRefreshedEvent.getApplicationContext().getParent()) { _log.debug(">>>>> spring初始化完毕<<<<<"); // spring初始化完毕后,通过反射调用所有使用BaseService注解的initMapper方法 Map baseServices =contextRefreshedEvent.getApplicationContext().getBeansWithAnnotation(BaseService.class); for(Object service : baseServices.values()) { _log.debug(">>>>> {}.initMapper()",service.getClass().getName()); try { Method initMapper =service.getClass().getMethod("initMapper"); initMapper.invoke(service); } catch (Exception e) { _log.error("初始化BaseService的initMapper方法异常",e); e.printStackTrace(); } } // 系统入口初始化 Map baseInterfaceBeans =contextRefreshedEvent.getApplicationContext().getBeansOfType(BaseInterface.class); for(Object service : baseInterfaceBeans.values()) { _log.debug(">>>>> {}.init()",service.getClass().getName()); try { Method init =service.getClass().getMethod("init"); init.invoke(service); } catch (Exception e) { _log.error("初始化BaseInterface的init方法异常",e); e.printStackTrace(); } } } } }
----------------------------
package com.vivebest.util; /** * 系统接口 *Created by ZhangShuzheng on 2017/6/13. */ public interface BaseInterface { //系统初始化 voidinit(); }
---------------------------
package com.vivebest.util; import org.apache.ibatis.annotations.Param; import java.util.List; /** *BaseService接口 *Created by ZhangShuzheng on 2017/01/07. */ public interface BaseService{ intcountByExample(Example example); intdeleteByExample(Example example); intdeleteByPrimaryKey(Integer id); intinsert(Record record); intinsertSelective(Record record); List selectByExampleWithBLOBs(Example example); List selectByExample(Example example); List selectByExampleWithBLOBsForStartPage(Example example, Integer pageNum, IntegerpageSize); List selectByExampleForStartPage(Example example, Integer pageNum, IntegerpageSize); List selectByExampleWithBLOBsForOffsetPage(Example example, Integer offset, Integerlimit); List selectByExampleForOffsetPage(Example example, Integer offset, Integer limit); RecordselectFirstByExample(Example example); RecordselectFirstByExampleWithBLOBs(Example example); RecordselectByPrimaryKey(Integer id); intupdateByExampleSelective(@Param("record") Record record,@Param("example") Example example); intupdateByExampleWithBLOBs(@Param("record") Record record,@Param("example") Example example); intupdateByExample(@Param("record") Record record, @Param("example")Example example); intupdateByPrimaryKeySelective(Record record); intupdateByPrimaryKeyWithBLOBs(Record record); intupdateByPrimaryKey(Record record); intdeleteByPrimaryKeys(String ids); voidinitMapper(); }
----------------------------
package com.vivebest.util; import com.github.pagehelper.PageHelper; import org.apache.commons.lang.StringUtils; import org.apache.ibatis.annotations.Param; importjava.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.ParameterizedType; import java.util.List; /** * 实现BaseService抽象类 *Created by ZhangShuzheng on 2017/01/07. */ public abstract classBaseServiceImplimplementsBaseService { publicMapper mapper; @Override publicint countByExample(Example example) { try{ DynamicDataSource.setDataSource(DataSourceEnum.SLAVE.getName()); MethodcountByExample =mapper.getClass().getDeclaredMethod("countByExample",example.getClass()); Objectresult = countByExample.invoke(mapper, example); returnInteger.parseInt(String.valueOf(result)); }catch (IllegalAccessException e) { e.printStackTrace(); }catch (InvocationTargetException e) { e.printStackTrace(); }catch (NoSuchMethodException e) { e.printStackTrace(); } DynamicDataSource.clearDataSource(); return0; } @Override publicint deleteByExample(Example example) { try{ DynamicDataSource.setDataSource(DataSourceEnum.MASTER.getName()); MethoddeleteByExample = mapper.getClass().getDeclaredMethod("deleteByExample",example.getClass()); Objectresult = deleteByExample.invoke(mapper, example); returnInteger.parseInt(String.valueOf(result)); }catch (IllegalAccessException e) { e.printStackTrace(); }catch (InvocationTargetException e) { e.printStackTrace(); }catch (NoSuchMethodException e) { e.printStackTrace(); } DynamicDataSource.clearDataSource(); return0; } @Override publicint deleteByPrimaryKey(Integer id) { try{ DynamicDataSource.setDataSource(DataSourceEnum.MASTER.getName()); MethoddeleteByPrimaryKey =mapper.getClass().getDeclaredMethod("deleteByPrimaryKey",id.getClass()); Objectresult = deleteByPrimaryKey.invoke(mapper, id); returnInteger.parseInt(String.valueOf(result)); }catch (IllegalAccessException e) { e.printStackTrace(); }catch (InvocationTargetException e) { e.printStackTrace(); }catch (NoSuchMethodException e) { e.printStackTrace(); } DynamicDataSource.clearDataSource(); return0; } @Override publicint insert(Record record) { try{ DynamicDataSource.setDataSource(DataSourceEnum.MASTER.getName()); Methodinsert = mapper.getClass().getDeclaredMethod("insert",record.getClass()); Objectresult = insert.invoke(mapper, record); returnInteger.parseInt(String.valueOf(result)); }catch (IllegalAccessException e) { e.printStackTrace(); }catch (InvocationTargetException e) { e.printStackTrace(); }catch (NoSuchMethodException e) { e.printStackTrace(); } DynamicDataSource.clearDataSource(); return0; } @Override publicint insertSelective(Record record) { try{ DynamicDataSource.setDataSource(DataSourceEnum.MASTER.getName()); MethodinsertSelective = mapper.getClass().getDeclaredMethod("insertSelective",record.getClass()); Objectresult = insertSelective.invoke(mapper, record); returnInteger.parseInt(String.valueOf(result)); }catch (IllegalAccessException e) { e.printStackTrace(); }catch (InvocationTargetException e) { e.printStackTrace(); }catch (NoSuchMethodException e) { e.printStackTrace(); } DynamicDataSource.clearDataSource(); return0; } @Override publicList selectByExampleWithBLOBs(Example example) { try{ DynamicDataSource.setDataSource(DataSourceEnum.SLAVE.getName()); MethodselectByExampleWithBLOBs =mapper.getClass().getDeclaredMethod("selectByExampleWithBLOBs",example.getClass()); Objectresult = selectByExampleWithBLOBs.invoke(mapper, example); return(List ) result; }catch (IllegalAccessException e) { e.printStackTrace(); }catch (InvocationTargetException e) { e.printStackTrace(); }catch (NoSuchMethodException e) { e.printStackTrace(); } DynamicDataSource.clearDataSource(); returnnull; } @Override publicList selectByExample(Example example) { try{ DynamicDataSource.setDataSource(DataSourceEnum.SLAVE.getName()); MethodselectByExample = mapper.getClass().getDeclaredMethod("selectByExample",example.getClass()); Objectresult = selectByExample.invoke(mapper, example); return(List ) result; }catch (IllegalAccessException e) { e.printStackTrace(); }catch (InvocationTargetException e) { e.printStackTrace(); }catch (NoSuchMethodException e) { e.printStackTrace(); } DynamicDataSource.clearDataSource(); returnnull; } @Override publicList selectByExampleWithBLOBsForStartPage(Example example, IntegerpageNum, Integer pageSize) { try{ DynamicDataSource.setDataSource(DataSourceEnum.SLAVE.getName()); MethodselectByExampleWithBLOBs =mapper.getClass().getDeclaredMethod("selectByExampleWithBLOBs",example.getClass()); PageHelper.startPage(pageNum,pageSize, false); Objectresult = selectByExampleWithBLOBs.invoke(mapper, example); return(List ) result; }catch (IllegalAccessException e) { e.printStackTrace(); }catch (InvocationTargetException e) { e.printStackTrace(); }catch (NoSuchMethodException e) { e.printStackTrace(); } DynamicDataSource.clearDataSource(); returnnull; } @Override publicList selectByExampleForStartPage(Example example, IntegerpageNum, Integer pageSize) { try{ DynamicDataSource.setDataSource(DataSourceEnum.SLAVE.getName()); MethodselectByExample =mapper.getClass().getDeclaredMethod("selectByExample",example.getClass()); PageHelper.startPage(pageNum,pageSize, false); Objectresult = selectByExample.invoke(mapper, example); return(List ) result; }catch (IllegalAccessException e) { e.printStackTrace(); }catch (InvocationTargetException e) { e.printStackTrace(); }catch (NoSuchMethodException e) { e.printStackTrace(); } DynamicDataSource.clearDataSource(); returnnull; } @Override publicList selectByExampleWithBLOBsForOffsetPage(Example example,Integer offset, Integer limit) { try{ DynamicDataSource.setDataSource(DataSourceEnum.SLAVE.getName()); MethodselectByExampleWithBLOBs =mapper.getClass().getDeclaredMethod("selectByExampleWithBLOBs",example.getClass()); PageHelper.offsetPage(offset,limit, false); Objectresult = selectByExampleWithBLOBs.invoke(mapper, example); return(List ) result; }catch (IllegalAccessException e) { e.printStackTrace(); }catch (InvocationTargetException e) { e.printStackTrace(); }catch (NoSuchMethodException e) { e.printStackTrace(); } DynamicDataSource.clearDataSource(); returnnull; } @Override publicList selectByExampleForOffsetPage(Example example, Integeroffset, Integer limit) { try{ DynamicDataSource.setDataSource(DataSourceEnum.SLAVE.getName()); MethodselectByExample = mapper.getClass().getDeclaredMethod("selectByExample",example.getClass()); PageHelper.offsetPage(offset,limit, false); Objectresult = selectByExample.invoke(mapper, example); return(List ) result; }catch (IllegalAccessException e) { e.printStackTrace(); }catch (InvocationTargetException e) { e.printStackTrace(); }catch (NoSuchMethodException e) { e.printStackTrace(); } DynamicDataSource.clearDataSource(); returnnull; } @Override publicRecord selectFirstByExample(Example example) { try{ DynamicDataSource.setDataSource(DataSourceEnum.SLAVE.getName()); MethodselectByExample =mapper.getClass().getDeclaredMethod("selectByExample",example.getClass()); List result = (List ) selectByExample.invoke(mapper, example); if(null != result && result.size() > 0) { returnresult.get(0); } }catch (IllegalAccessException e) { e.printStackTrace(); }catch (InvocationTargetException e) { e.printStackTrace(); }catch (NoSuchMethodException e) { e.printStackTrace(); } DynamicDataSource.clearDataSource(); returnnull; } @Override publicRecord selectFirstByExampleWithBLOBs(Example example) { try{ DynamicDataSource.setDataSource(DataSourceEnum.SLAVE.getName()); MethodselectByExampleWithBLOBs =mapper.getClass().getDeclaredMethod("selectByExampleWithBLOBs",example.getClass()); List result = (List ) selectByExampleWithBLOBs.invoke(mapper, example); if(null != result && result.size() > 0) { returnresult.get(0); } }catch (IllegalAccessException e) { e.printStackTrace(); }catch (InvocationTargetException e) { e.printStackTrace(); }catch (NoSuchMethodException e) { e.printStackTrace(); } DynamicDataSource.clearDataSource(); returnnull; } @Override publicRecord selectByPrimaryKey(Integer id) { try{ DynamicDataSource.setDataSource(DataSourceEnum.SLAVE.getName()); MethodselectByPrimaryKey =mapper.getClass().getDeclaredMethod("selectByPrimaryKey",id.getClass()); Objectresult = selectByPrimaryKey.invoke(mapper, id); return(Record) result; }catch (IllegalAccessException e) { e.printStackTrace(); }catch (InvocationTargetException e) { e.printStackTrace(); }catch (NoSuchMethodException e) { e.printStackTrace(); } DynamicDataSource.clearDataSource(); returnnull; } @Override publicint updateByExampleSelective(@Param("record") Record record,@Param("example") Example example) { try{ DynamicDataSource.setDataSource(DataSourceEnum.MASTER.getName()); Method updateByExampleSelective =mapper.getClass().getDeclaredMethod("updateByExampleSelective",record.getClass(), example.getClass()); Objectresult = updateByExampleSelective.invoke(mapper, record, example); returnInteger.parseInt(String.valueOf(result)); }catch (IllegalAccessException e) { e.printStackTrace(); }catch (InvocationTargetException e) { e.printStackTrace(); }catch (NoSuchMethodException e) { e.printStackTrace(); } DynamicDataSource.clearDataSource(); return0; } @Override publicint updateByExampleWithBLOBs(@Param("record") Record record,@Param("example") Example example) { try{ DynamicDataSource.setDataSource(DataSourceEnum.MASTER.getName()); MethodupdateByExampleWithBLOBs = mapper.getClass().getDeclaredMethod("updateByExampleWithBLOBs",record.getClass(), example.getClass()); Objectresult = updateByExampleWithBLOBs.invoke(mapper, record, example); returnInteger.parseInt(String.valueOf(result)); }catch (IllegalAccessException e) { e.printStackTrace(); }catch (InvocationTargetException e) { e.printStackTrace(); }catch (NoSuchMethodException e) { e.printStackTrace(); } DynamicDataSource.clearDataSource(); return0; } @Override publicint updateByExample(@Param("record") Record record,@Param("example") Example example) { try{ DynamicDataSource.setDataSource(DataSourceEnum.MASTER.getName()); MethodupdateByExample =mapper.getClass().getDeclaredMethod("updateByExample",record.getClass(), example.getClass()); Objectresult = updateByExample.invoke(mapper, record, example); returnInteger.parseInt(String.valueOf(result)); }catch (IllegalAccessException e) { e.printStackTrace(); }catch (InvocationTargetException e) { e.printStackTrace(); }catch (NoSuchMethodException e) { e.printStackTrace(); } DynamicDataSource.clearDataSource(); return0; } @Override publicint updateByPrimaryKeySelective(Record record) { try{ DynamicDataSource.setDataSource(DataSourceEnum.MASTER.getName()); MethodupdateByPrimaryKeySelective =mapper.getClass().getDeclaredMethod("updateByPrimaryKeySelective",record.getClass()); Objectresult = updateByPrimaryKeySelective.invoke(mapper, record); returnInteger.parseInt(String.valueOf(result)); }catch (IllegalAccessException e) { e.printStackTrace(); }catch (InvocationTargetException e) { e.printStackTrace(); }catch (NoSuchMethodException e) { e.printStackTrace(); } DynamicDataSource.clearDataSource(); return0; } @Override publicint updateByPrimaryKeyWithBLOBs(Record record) { try{ DynamicDataSource.setDataSource(DataSourceEnum.MASTER.getName()); MethodupdateByPrimaryKeyWithBLOBs = mapper.getClass().getDeclaredMethod("updateByPrimaryKeyWithBLOBs",record.getClass()); Objectresult = updateByPrimaryKeyWithBLOBs.invoke(mapper, record); returnInteger.parseInt(String.valueOf(result)); }catch (IllegalAccessException e) { e.printStackTrace(); }catch (InvocationTargetException e) { e.printStackTrace(); }catch (NoSuchMethodException e) { e.printStackTrace(); } DynamicDataSource.clearDataSource(); return0; } @Override publicint updateByPrimaryKey(Record record) { try{ DynamicDataSource.setDataSource(DataSourceEnum.MASTER.getName()); MethodupdateByPrimaryKey =mapper.getClass().getDeclaredMethod("updateByPrimaryKey",record.getClass()); Objectresult = updateByPrimaryKey.invoke(mapper, record); returnInteger.parseInt(String.valueOf(result)); }catch (IllegalAccessException e) { e.printStackTrace(); }catch (InvocationTargetException e) { e.printStackTrace(); }catch (NoSuchMethodException e) { e.printStackTrace(); } DynamicDataSource.clearDataSource(); return0; } @Override publicint deleteByPrimaryKeys(String ids) { try{ if(StringUtils.isBlank(ids)) { return0; } DynamicDataSource.setDataSource(DataSourceEnum.MASTER.getName()); String[]idArray = ids.split("-"); int count = 0; for(String idStr : idArray) { if(StringUtils.isBlank(idStr)) { continue; } Integerid = Integer.parseInt(idStr); MethoddeleteByPrimaryKey =mapper.getClass().getDeclaredMethod("deleteByPrimaryKey",id.getClass()); Objectresult = deleteByPrimaryKey.invoke(mapper, id); count+= Integer.parseInt(String.valueOf(result)); } returncount; }catch (IllegalAccessException e) { e.printStackTrace(); }catch (InvocationTargetException e) { e.printStackTrace(); }catch (NoSuchMethodException e) { e.printStackTrace(); } DynamicDataSource.clearDataSource(); return0; } @Override publicvoid initMapper() { this.mapper= SpringContextUtil.getBean(getMapperClass()); } /** * 获取类泛型class * @return */ publicClass getMapperClass() { return(Class ) ((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0]; } }
---------------------------
package com.vivebest.util; /** * 多数据源枚举 *Created by ZhangShuzheng on 2017/1/15. */ public enum DataSourceEnum { //主库 MASTER("masterDataSource",true), //从库 SLAVE("slaveDataSource",false),; //数据源名称 privateString name; //是否是默认数据源 privateboolean master; DataSourceEnum(Stringname, boolean master) { this.name= name; this.master= master; } publicString getName() { returnname; } publicvoid setName(String name) { this.name= name; } publicboolean isMaster() { returnmaster; } publicvoid setMaster(boolean master) { this.master= master; } publicString getDefault() { StringdefaultDataSource = ""; for(DataSourceEnum dataSourceEnum : DataSourceEnum.values()) { if(!"".equals(defaultDataSource)) { break; } if(dataSourceEnum.master) { defaultDataSource= dataSourceEnum.getName(); } } returndefaultDataSource; } }
----------------------------
package com.vivebest.util; import org.slf4j.Logger; import org.slf4j.LoggerFactory; importorg.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; /** * 动态数据源(数据源切换) *Created by ZhangShuzheng on 2017/1/15. */ public class DynamicDataSource extendsAbstractRoutingDataSource { privatefinal static Logger _log = LoggerFactory.getLogger(DynamicDataSource.class); privatestatic final ThreadLocalcontextHolder = newThreadLocal<>(); @Override protectedObject determineCurrentLookupKey() { StringdataSource = getDataSource(); _log.info("当前操作使用的数据源:{}",dataSource); returndataSource; } /** * 设置数据源 * @param dataSource */ publicstatic void setDataSource(String dataSource) { contextHolder.set(dataSource); } /** * 获取数据源 * @return */ publicstatic String getDataSource() { StringdataSource = contextHolder.get(); //如果没有指定数据源,使用默认数据源 if(null == dataSource) { DynamicDataSource.setDataSource(DataSourceEnum.MASTER.getDefault()); } returncontextHolder.get(); } /** * 清除数据源 */ publicstatic void clearDataSource() { contextHolder.remove(); } }
---------------------------
package com.vivebest.util; importorg.springframework.beans.BeansException; importorg.springframework.context.ApplicationContext; importorg.springframework.context.ApplicationContextAware; import java.util.Map; /** * 资源文件读取工具 * *@author shuzheng *@date 2016年10月15日 */ public class SpringContextUtil implementsApplicationContextAware { privatestatic ApplicationContext context = null; privateSpringContextUtil() { super(); } @Override publicvoid setApplicationContext(ApplicationContext applicationContext) throwsBeansException { context= applicationContext; } /** * 根据名称获取bean * @param beanName * @return */ publicstatic Object getBean(String beanName) { returncontext.getBean(beanName); } /** * 根据bean名称获取指定类型bean * @param beanName bean名称 * @param clazz 返回的bean类型,若类型不匹配,将抛出异常 */ publicstaticT getBean(String beanName, Class clazz) { returncontext.getBean(beanName, clazz); } /** * 根据类型获取bean * @param clazz * @return */ publicstatic T getBean(Class clazz) { Tt = null; Map map = context.getBeansOfType(clazz); for(Map.Entry entry : map.entrySet()) { t= entry.getValue(); } returnt; } /** * 是否包含bean * @param beanName * @return */ publicstatic boolean containsBean(String beanName) { returncontext.containsBean(beanName); } /** * 是否是单例 * @param beanName * @return */ publicstatic boolean isSingleton(String beanName) { returncontext.isSingleton(beanName); } /** * bean的类型 * @param beanName * @return */ publicstatic Class getType(String beanName) { returncontext.getType(beanName); } }
----------------------------