package com.xxxx.base;
import org.springframework.web.bind.annotation.ModelAttribute;
import javax.servlet.http.HttpServletRequest;
public class BaseController {
@ModelAttribute
public void preHandler(HttpServletRequest request){
request.setAttribute("ctx", request.getContextPath());
}
public ResultInfo success(){
return new ResultInfo();
}
public ResultInfo success(String msg){
ResultInfo resultInfo= new ResultInfo();
resultInfo.setMsg(msg);
return resultInfo;
}
public ResultInfo success(String msg,Object result){
ResultInfo resultInfo= new ResultInfo();
resultInfo.setMsg(msg);
resultInfo.setResult(result);
return resultInfo;
}
}
package com.xxxx.base;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@SuppressWarnings("all")
public abstract class BaseService<T,ID> {
@Autowired
private BaseMapper<T,ID> baseMapper;
public Integer insertSelective(T entity) throws DataAccessException{
return baseMapper.insertSelective(entity);
}
public ID insertHasKey(T entity) throws DataAccessException{
baseMapper.insertHasKey(entity);
try {
return (ID) entity.getClass().getMethod("getId").invoke(entity);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public Integer insertBatch(List<T> entities) throws DataAccessException{
return baseMapper.insertBatch(entities);
}
public T selectByPrimaryKey(ID id) throws DataAccessException{
return baseMapper.selectByPrimaryKey(id);
}
public List<T> selectByParams(BaseQuery baseQuery) throws DataAccessException{
return baseMapper.selectByParams(baseQuery);
}
public Integer updateByPrimaryKeySelective(T entity) throws DataAccessException{
return baseMapper.updateByPrimaryKeySelective(entity);
}
public Integer updateBatch(List<T> entities) throws DataAccessException{
return baseMapper.updateBatch(entities);
}
public Integer deleteByPrimaryKey(ID id) throws DataAccessException{
return baseMapper.deleteByPrimaryKey(id);
}
public Integer deleteBatch(ID[] ids) throws DataAccessException{
return baseMapper.deleteBatch(ids);
}
public Map<String, Object> queryByParamsForTable(BaseQuery baseQuery) {
Map<String,Object> result = new HashMap<String,Object>();
PageHelper.startPage(baseQuery.getPage(),baseQuery.getLimit());
PageInfo<T> pageInfo =new PageInfo<T>(selectByParams(baseQuery));
result.put("count",pageInfo.getTotal());
result.put("data",pageInfo.getList());
result.put("code",0);
result.put("msg","");
return result;
}
}
package com.xxxx.base;
import org.springframework.dao.DataAccessException;
import java.util.List;
public interface BaseMapper<T,ID> {
public Integer insertSelective(T entity) throws DataAccessException;
public Integer insertHasKey(T entity) throws DataAccessException;
public Integer insertBatch(List<T> entities) throws DataAccessException;
public T selectByPrimaryKey(ID id) throws DataAccessException;
public List<T> selectByParams(BaseQuery baseQuery) throws DataAccessException;
public Integer updateByPrimaryKeySelective(T entity) throws DataAccessException;
public Integer updateBatch(List<T> entities) throws DataAccessException;
public Integer deleteByPrimaryKey(ID id) throws DataAccessException;
public Integer deleteBatch(ID[] ids) throws DataAccessException;
}
package com.xxxx.base;
public class BaseQuery {
private Integer page=1;
private Integer limit=10;
public Integer getPage() {
return page;
}
public void setPage(Integer page) {
this.page = page;
}
public Integer getLimit() {
return limit;
}
public void setLimit(Integer limit) {
this.limit = limit;
}
}
package com.xxxx.base;
public class ResultInfo {
private Integer code=200;
private String msg="success";
private Object result;
public Object getResult() {
return result;
}
public void setResult(Object result) {
this.result = result;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}