base基础类
文章目录
这些设置可以做到减少一些基本信息的配置。与util类似,减少代码的重复使用。通过继承类,实现一些共有的操作。
controller层
BaseController.java
/**
* 设置BaseController
* Created by ppliang on 2020/11/30 15:55
*/
public class BaseController {
@ModelAttribute
public void preHandler(HttpServletRequest request){
//设置上下文的路径,也可以理解为项目编译后的路径 localhost:8080/(项目名)/
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(Object result){
ResultInfo resultInfo= new ResultInfo();
resultInfo.setResult(result);
return resultInfo;
}
}
前端界面就可直接获取项目路径:
common.ftl
这是前端的基本信息配置,直接获取资源文件,不用通过本文件路径去找。
<link rel="icon" href="${ctx}/images/favicon.ico">
<link rel="stylesheet" href="${ctx}/lib/layui-v2.5.5/css/layui.css" media="all">
<link rel="stylesheet" href="${ctx}/css/layuimini.css" media="all">
<link rel="stylesheet" href="${ctx}/lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
<link rel="stylesheet" href="${ctx}/css/formSelects-v4.css" media="all">
<link rel="stylesheet" href="${ctx}/css/public.css" media="all">
<script src="${ctx}/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="${ctx}/js/lay-config.js" charset="utf-8"></script>
<script type="text/javascript">
var ctx="${ctx}";
</script>
ResultInfo.java
后端回复前端的数据,封装在这个类中。
@Data
public class ResultInfo {
//约定200 为返回数据正确
private Integer code=200;
private String msg="success";
private Object result;
public ResultInfo() {
}
public ResultInfo(Object result){
this.result=result;
}
public ResultInfo(String result){
this.code=500;
this.msg=result;
}
public ResultInfo(String msg,Object result){
this.code=500;
this.msg=msg;
this.result=result;
}
}
service的基础类
基本的增删改查方法
BaseService.java
/**
* T :表类型
* ID:主键
* Created by ppliang on 2020/11/30 16:10
*/
@SuppressWarnings("all")
public abstract class BaseService<T,ID> {
@Autowired
private BaseDao<T,ID> baseMapper;
/**
* 添加记录返回行数
* @param entity
* @return
*/
public Integer insertSelective(T entity) throws DataAccessException {
return baseMapper.insertSelective(entity);
}
/**
* 添加记录返回主键
* @param entity
* @return
*/
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;
}
}
/**
* 批量添加
* @param entities
* @return
*/
public Integer insertBatch(List<T> entities) throws DataAccessException{
return baseMapper.insertBatch(entities);
}
/**
* 根据id 查询详情
* @param id
* @return
*/
public T selectByPrimaryKey(ID id) throws DataAccessException{
return baseMapper.selectByPrimaryKey(id);
}
/**
* 多条件查询
* @param baseQuery
* @return
*/
public List<T> selectByParams(BaseQuery baseQuery) throws DataAccessException{
return baseMapper.selectByParams(baseQuery);
}
/**
* 更新单条记录
* @param entity
* @return
*/
public Integer updateByPrimaryKeySelective(T entity) throws DataAccessException{
return baseMapper.updateByPrimaryKeySelective(entity);
}
/**
* 批量更新
* @param entities
* @return
*/
public Integer updateBatch(List<T> entities) throws DataAccessException{
return baseMapper.updateBatch(entities);
}
/**
* 删除单条记录
* @param id
* @return
*/
public Integer deleteByPrimaryKey(ID id) throws DataAccessException{
return baseMapper.deleteByPrimaryKey(id);
}
/**
* 逻辑批量删除
* @param ids
* @return
*/
public void deleteBatch(ID[] ids) throws DataAccessException{
AssertUtil.isTrue(ids==null||ids.length<1,"删除异常");
AssertUtil.isTrue(baseMapper.deleteBatch(ids)!=ids.length, "删除异常");
}
//多表查询的语句
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;
}
}
dao层的基础配置
分页的基础配置
BaseQuery.java
@Data
public class BaseQuery {
//默认值
private Integer page=1;
private Integer limit=10;
}
BaseDao.java
public interface BaseDao<T,ID>{
/**
* 添加记录返回行数
* @param entity
* @return
*/
public Integer insertSelective(T entity) throws DataAccessException;
/**
* 添加记录返回主键
* @param entity
* @return
*/
public Integer insertHasKey(T entity) throws DataAccessException;
/**
* 批量添加
* @param entities
* @return
*/
public Integer insertBatch(List<T> entities) throws DataAccessException;
/**
* 根据id 查询详情
* @param id
* @return
*/
public T selectByPrimaryKey(ID id) throws DataAccessException;
/**
* 多条件查询
* @param baseQuery
* @return
*/
public List<T> selectByParams(BaseQuery baseQuery) throws DataAccessException;
/**
* 更新单条记录 有值更新
* @param entity
* @return
*/
public Integer updateByPrimaryKeySelective(T entity) throws DataAccessException;
/**
* 批量更新
* @param entities
* @return
*/
public Integer updateBatch(List<T> entities) throws DataAccessException;
/**
* 删除单条记录
* @param id
* @return
*/
public Integer deleteByPrimaryKey(ID id) throws DataAccessException;
/**
* 批量删除
* @param ids
* @return
*/
public Integer deleteBatch(ID[] ids) throws DataAccessException;
/**
* 更新通过id数据 全部更新
* @param entity
* @return
*/
Integer updateByPrimaryKey(T entity);
}
建议结合逆向工程使用。