文字的内容很少,直接上代码。欢迎有需要的朋友参考和学习,或者更好的方法和逻辑,可以加小编一起讨论。
1 创建的实体继承BaseEntity
/**
* Entity基类
*
* @author
*/
public class BaseEntity implements Serializable
{
private static final long serialVersionUID = 1L;
/** 搜索值 */
@JsonIgnore
@TableField(exist = false)
private String searchValue;
/** 创建者 */
private String createBy;
/** 创建时间 */
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date createTime;
/** 更新者 */
private String updateBy;
/** 更新时间 */
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date updateTime;
/** 备注 */
@TableField(exist = false)
private String remark;
/** 请求参数 */
@TableField(exist = false)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private Map<String, Object> params;
public String getSearchValue()
{
return searchValue;
}
public void setSearchValue(String searchValue)
{
this.searchValue = searchValue;
}
public String getCreateBy()
{
return createBy;
}
public void setCreateBy(String createBy)
{
this.createBy = createBy;
}
public Date getCreateTime()
{
return createTime;
}
public void setCreateTime(Date createTime)
{
this.createTime = createTime;
}
public String getUpdateBy()
{
return updateBy;
}
public void setUpdateBy(String updateBy)
{
this.updateBy = updateBy;
}
public Date getUpdateTime()
{
return updateTime;
}
public void setUpdateTime(Date updateTime)
{
this.updateTime = updateTime;
}
public String getRemark()
{
return remark;
}
public void setRemark(String remark)
{
this.remark = remark;
}
public Map<String, Object> getParams()
{
if (params == null)
{
params = new HashMap<>();
}
return params;
}
public void setParams(Map<String, Object> params)
{
this.params = params;
}
}
1.1 编写的实体类继承extends BaseEntity
@Data
@ApiModel("备注")
@TableName("表明")
public class 编写的实体类 extends BaseEntity {}
2 基类Service
/**
* Title: com.common.business.service.base
* Description:基类Service
* Copyright: Copyright (c) 2024
* Company:
*
* @author : 作者
* @version : 版本
* @date :时间
*/
public interface BaseService<T> {
/**
* @description : 新增
* @author : 作者
* @date : 时间
* @return :int
**/
AjaxResult save(T entity);
/**
* @description : 修改
* @author : 作者
* @date : 时间
* @return :int
**/
AjaxResult update(T entity);
/**
* @description : 删除
* @author : 作者
* @date : 时间
* @return :int
**/
int delete(T entity);
/**
* @description : 查询单条
* @author : 作者
* @date : 时间
* @return :T
**/
T getOne(T entity);
/**
* @description : 根据id查询单条
* @author : 作者
* @date : 时间
* @return :T
**/
T getOneById(Long id);
/**
* @description : 查询多条
* @author : 作者
* @date : 时间
* @return :List<T>
**/
List<T> getList(T entity);
/**
* @description : 分页查询多条
* @author : 作者
* @date : 时间
* @return :IPage<T>
**/
IPage<T> getListByPage(Page page, T entity);
}
3 通用数据处理控制层
/**
* 通用数据处理控制层
*
* @author 作者
*/
public class BaseController
{
protected final Logger logger = LoggerFactory.getLogger(this.getClass());
/**
* 将前台传递过来的日期格式的字符串,自动转化为Date类型
*/
@InitBinder
public void initBinder(WebDataBinder binder)
{
// Date 类型转换
binder.registerCustomEditor(Date.class, new PropertyEditorSupport()
{
@Override
public void setAsText(String text)
{
setValue(DateUtils.parseDate(text));
}
});
}
/**
* 响应请求分页数据
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
protected TableDataInfo getDataTable(IPage<?> list)
{
TableDataInfo rspData = new TableDataInfo();
rspData.setCode(HttpStatus.SUCCESS);
rspData.setRows(list.getRecords());
rspData.setMsg(I18nUtil.getMessage("query.success"));
rspData.setTotal(list.getTotal());
return rspData;
}
/**
* 返回成功
*/
public AjaxResult success()
{
return AjaxResult.success();
}
/**
* 返回成功消息
*/
public AjaxResult success(String message)
{
return AjaxResult.success(message);
}
/**
* 返回成功消息
*/
public AjaxResult success(Object data)
{
return AjaxResult.success(data);
}
/**
* 返回失败消息
*/
public AjaxResult error()
{
return AjaxResult.error();
}
/**
* 返回失败消息
*/
public AjaxResult error(String message)
{
return AjaxResult.error(message);
}
/**
* 返回警告消息
*/
public AjaxResult warn(String message)
{
return AjaxResult.warn(message);
}
/**
* 响应返回结果
*
* @param rows 影响行数
* @return 操作结果
*/
protected AjaxResult toAjax(int rows)
{
return rows > 0 ? AjaxResult.success() : AjaxResult.error();
}
/**
* 响应返回结果
*
* @param result 结果
* @return 操作结果
*/
protected AjaxResult toAjax(boolean result)
{
return result ? success() : error();
}
}
4 父类控制层
/**
* Title: com.common.core.web.controller
* Description:父类控制层
* Copyright: Copyright (c)
* Company:
*
* @author :作者
* @version :版本
* @date :时间
*/
public abstract class ParentController<T,Service extends BaseService<T>> extends BaseController {
@Autowired
Service service;
/**
* @description : 新增
* @author : 作者
* @date : 时间
* @param t
* @return :com.common.core.web.domain.AjaxResult
**/
@ApiOperation(value = "添加", notes = "添加")
@Log(title = "添加", businessType = BusinessType.INSERT)
@RequiresPermissions("add")
@PostMapping(value = "/insert")
public AjaxResult create(@Validated @RequestBody T t) {
return service.save(t);
}
/**
* @description : 分页列表查询
* @author : 作者
* @date : 时间
* @param page
* @param t
* @return :com.common.core.web.page.TableDataInfo
**/
@ApiOperation(value = "分页列表查询", notes = "分页列表查询")
@RequiresPermissions("list")
@GetMapping("/list")
public TableDataInfo list(Page page, T t) {
return getDataTable(service.getListByPage(page,t));
}
/**
* @description : 修改
* @author : 作者
* @date : 时间
* @param t
* @return :com.common.core.web.domain.AjaxResult
**/
@ApiOperation(value = "修改", notes = "修改")
@Log(title = "修改", businessType = BusinessType.UPDATE)
@RequiresPermissions("edit")
@PostMapping("/edit")
public AjaxResult edit(@Validated @RequestBody T t) {
return service.update(t);
}
/**
* @description : 通过id查询
* @author : 作者
* @date : 时间
* @param id
* @return :org.springframework.http.ResponseEntity<com.common.core.response.ServerResponse<T>>
**/
@ApiOperation(value = "通过id查询", notes = "通过id查询")
@GetMapping("/info/{id}")
public ResponseEntity<ServerResponse<T>> info(@PathVariable Long id) {
return ResponseEntity.ok().body(ServerResponseEntity.success(service.getOneById(id)));
}
5 写自己的service层逻辑
/**
* Title: com.common.business.service.afterSale
* Description:写自己的service层逻辑
* Copyright: Copyright (c)
* Company:
*
* @author :作者
* @version :版本
* @date :时间
*/
public interface **Service extends BaseService<实体类> {
}
6 写自己的控制层逻辑
/**
* Title: com.app.controller.afterSale
* Description:写自己的控制层逻辑
* Copyright: Copyright (c)
* Company:
*
* @author :作者
* @version :版本
* @date :时间
*/
@Api(tags = "说明")
@RestController
@RequestMapping("请求地址符")
@RequiresPermissions("权限控制符")
public class xxxController extends ParentController<实体类, **Service> {
}
觉得写的还可以的朋友,请点点赞,非常感谢!