添加基础增删改差模板类

This commit is contained in:
winLsy
2018-02-04 16:48:39 +08:00
parent 712685d904
commit 4ff21938ea
8 changed files with 199 additions and 15 deletions
@@ -0,0 +1,19 @@
package com.micro.fast.common.dao;
public interface SsmMapper<T,ID> {
int deleteByPrimaryKey(ID id);
int insert(T record);
int insertSelective(T record);
T selectByPrimaryKey(ID id);
int updateByPrimaryKeySelective(T record);
int updateByPrimaryKeyWithBLOBs(T record);
int updateByPrimaryKey(T record);
}
@@ -0,0 +1,30 @@
package com.micro.fast.common.service;
import com.micro.fast.boot.starter.common.response.ServerResponse;
/**
* ssm框架service基础操作接口
* @author lsy
*/
public interface SsmService<T,ID> {
/**
* 增加
*/
ServerResponse add(T pojo);
/**
* 根据id查询
*/
ServerResponse getById(ID id);
/**
* 按条件分页查询
*/
ServerResponse getByCondition(T pojo);
/**
* 修改
*/
ServerResponse update(T pojo);
/**
* 根据id删除
*/
ServerResponse deleteById(ID id);
}
@@ -0,0 +1,66 @@
package com.micro.fast.common.service.impl;
import com.micro.fast.boot.starter.common.exception.SystemException;
import com.micro.fast.boot.starter.common.response.BaseConst;
import com.micro.fast.boot.starter.common.response.ServerResponse;
import com.micro.fast.common.dao.SsmMapper;
import com.micro.fast.common.service.SsmService;
import lombok.Setter;
import java.io.Serializable;
/**
* T表示的是实体类型,ID表示的是主键类型,DAO表示的是dao层的类型
* @author lsy
*/
@Setter
public abstract class SsmServiceImpl<T, ID extends Serializable, MAPPER extends SsmMapper<T,ID>> implements SsmService<T,ID>{
/**
* 这个属性一定要使用子类的覆盖
*/
private MAPPER mapper;
@Override
public ServerResponse add(T pojo) {
int insert = mapper.insertSelective(pojo);
if (insert!=1){
throw new SystemException(BaseConst.SystemResponse.DATABASE_INSERT_FAILURE);
}
return ServerResponse.successMsg("添加成功");
}
@Override
public ServerResponse getById(ID id)
{
T t = mapper.selectByPrimaryKey(id);
return ServerResponse.successData(t);
}
/**
* 抽象的,提示开发者条件化查询需要自定义实现
* @param pojo
* @return
*/
@Override
public abstract ServerResponse getByCondition(T pojo);
@Override
public ServerResponse update(T pojo) {
int i = mapper.updateByPrimaryKeySelective(pojo);
if (i<1){
ServerResponse.errorMsg("更新失败");
}
return ServerResponse.successMsg("更新成功");
}
@Override
public ServerResponse deleteById(ID id) {
int i = mapper.deleteByPrimaryKey(id);
if (i<1){
ServerResponse.errorMsg("删除失败");
}
return ServerResponse.successMsg("删除成功");
}
}
@@ -1,19 +1,16 @@
package com.micro.fast.upms.controller;
import com.micro.fast.boot.starter.common.exception.SystemException;
import com.micro.fast.boot.starter.common.response.BaseConst;
import com.micro.fast.boot.starter.common.response.ServerResponse;
import com.micro.fast.upms.dao.UpmsSystemMapper;
import com.micro.fast.upms.pojo.UpmsSystem;
import com.micro.fast.upms.service.UpmsSystemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
@@ -27,18 +24,44 @@ import javax.validation.Valid;
public class UpmsSystemController {
@Autowired
private UpmsSystemMapper upmsSystemMapper;
private UpmsSystemService<UpmsSystem,Integer> upmsSystemService;
@ApiOperation("添加系统")
@PostMapping
public ServerResponse addSystem(@Valid UpmsSystem upmsSystem,BindingResult errors) throws BindException {
upmsSystem.setCtime(System.currentTimeMillis());
upmsSystem.setStatus((byte)1);
//对参数的校验抛出异常
int i = upmsSystemMapper.insertSelective(upmsSystem);
if (i!=1){
throw new SystemException(BaseConst.SystemResponse.DATABASE_INSERT_FAILURE);
}
return ServerResponse.successMsg("添加系统成功");
return upmsSystemService.add(upmsSystem);
}
@ApiOperation("根据id查询系统详细信息")
@GetMapping("/{id}")
public ServerResponse getSystemById(
@PathVariable(value = "id") @NotBlank(message = BaseConst.BASEMSG_PREFIX+"请传入id") String id){
return upmsSystemService.getById(Integer.valueOf(id));
}
@ApiOperation("根据条件分页查询")
@GetMapping
public ServerResponse getSystemByCondition(){
return null;
}
@ApiOperation("修改系统信息")
@PutMapping("/{id}")
public ServerResponse updateSystem(UpmsSystem upmsSystem,
@NotBlank(message = BaseConst.BASEMSG_PREFIX+"请传入id") String id){
upmsSystem.setId(Integer.valueOf(id));
return upmsSystemService.update(upmsSystem);
}
@ApiOperation("根据id删除系统,多个使用逗号分割")
@DeleteMapping("/{id}")
public ServerResponse deleteSystem(
@NotBlank(message = BaseConst.BASEMSG_PREFIX+"请传入id") String id){
return upmsSystemService.deleteById(Integer.valueOf(id));
}
}
@@ -1,8 +1,10 @@
package com.micro.fast.upms.dao;
import com.micro.fast.common.dao.SsmMapper;
import com.micro.fast.upms.pojo.UpmsSystem;
public interface UpmsSystemMapper {
public interface UpmsSystemMapper extends SsmMapper<UpmsSystem,Integer> {
int deleteByPrimaryKey(Integer id);
int insert(UpmsSystem record);
@@ -0,0 +1,12 @@
package com.micro.fast.upms.service;
import com.micro.fast.common.service.SsmService;
import com.micro.fast.upms.pojo.UpmsSystem;
/**
* 系统管理接口
* @author lsy
*/
public interface UpmsSystemService<T,ID> extends SsmService<T,ID>{
}
@@ -0,0 +1,33 @@
package com.micro.fast.upms.service.impl;
import com.micro.fast.boot.starter.common.response.ServerResponse;
import com.micro.fast.common.service.impl.SsmServiceImpl;
import com.micro.fast.upms.dao.UpmsSystemMapper;
import com.micro.fast.upms.pojo.UpmsSystem;
import com.micro.fast.upms.service.UpmsSystemService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @author lsy
*/
@Service
public class UpmSystemServiceImpl extends SsmServiceImpl<UpmsSystem,Integer,UpmsSystemMapper> implements UpmsSystemService<UpmsSystem,Integer>,InitializingBean {
@Autowired
private UpmsSystemMapper mapper;
@Override
public ServerResponse getByCondition(UpmsSystem pojo) {
return null;
}
/**
* 在这个bean初始化完成后覆盖父类的mapper
*/
@Override
public void afterPropertiesSet() throws Exception {
super.setMapper(this.mapper);
}
}
@@ -8,7 +8,6 @@ import com.micro.fast.boot.starter.common.response.ServerResponse;
import com.micro.fast.upms.dao.UpmsUserMapper;
import com.micro.fast.upms.pojo.UpmsUser;
import com.micro.fast.upms.service.UpmsUserService;
import com.micro.fast.boot.starter.common.util.ExceptionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;