Generate.POJOs.groovy生成实体类
package $class.PackageName;
import com.bocloud.bsm.cmdb.entity.${class.className};
import com.bocloud.common.model.Param;
import com.bocloud.database.core.intf.GenericDao;
import java.util.List;
import java.util.Map;
/**
* ${class.className}Dao
*
* @author jiaohongtao
* @version v1.0
* @since $TIME
*/
public interface ${class.className}Dao extends GenericDao<${class.className}, Long> {
/**
* 翻页查询
*
* @param page 第几页
* @param rows 第几行
* @param params params
* @param sorter 简要内容
*/
List<${class.className}> list(int page, int rows, List<Param> params, Map<String, String> sorter) throws Exception;
/**
* 查询所有
*/
List<${class.className}> listAll() throws Exception;
/**
* 根据id查询
*
* @param id id
*/
${class.className} query(Long id) throws Exception;
/**
* 根据id删除
*
* @param id id
* @param userId 操作人
*/
boolean delete(Long id, Long userId) throws Exception;
/**
* 查询总数
*
* @param params params
*/
int count(List<Param> params) throws Exception;
}
package $class.PackageName;
import com.bocloud.database.core.intf.impl.JdbcGenericDao;
import com.bocloud.bsm.cmdb.entity.${class.className};
import com.bocloud.bsm.cmdb.dao.intf.${class.className}Dao;
import com.bocloud.common.model.Param;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.utils.SqlHelper;
import org.springframework.stereotype.Repository;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* ${class.className}Dao实现
*
* @author jiaohongtao
* @version v1.0
* @since $TIME
*/
@Repository
public class ${class.className}DaoImpl extends JdbcGenericDao<${class.className}, Long> implements ${class.className}Dao {
@Override
public List<${class.className}> list(int page, int rows, List<Param> params, Map<String, String> sorter) throws Exception {
String sql = "select a.* from ${class.className} a where a.is_deleted = 0";
sql = SqlHelper.buildRawSql(sql, page, rows, params, sorter, "a");
Map<String, Object> param = SqlHelper.getParam(params);
return this.list(${class.className}.class, sql, param);
}
@Override
public List<${class.className}> listAll() throws Exception {
String sql = "select * from ${class.className} where is_deleted = 0";
return this.list(${class.className}.class, sql);
}
@Override
public ${class.className} query(Long id) throws Exception {
String sql = "select * from ${class.className} where is_deleted = 0 and id = :id";
Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
List<${class.className}> list = this.list(${class.className}.class, sql, params);
return list.isEmpty() ? null : list.get(0);
}
@Override
public boolean delete(Long id, Long userId) throws Exception {
String sql = "update ${class.className} set is_deleted = 1, gmt_modify = :gmtModify, mender_id = :menderId where id = :id";
Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
params.put("gmtModify", new Date());
params.put(Common.MENDER_ID, userId);
return this.execute(sql, params) > 0;
}
@Override
public int count(List<Param> params) throws Exception {
String sql = "select count(*) from ${class.className} a where a.is_deleted = 0";
sql = SqlHelper.buildRawSql(sql, params, null, "a");
Map<String, Object> param = SqlHelper.getParam(params);
return this.countQuery(sql, param).intValue();
}
}
package $class.PackageName;
import com.bocloud.bsm.cmdb.entity.${class.className};
import com.bocloud.common.model.Param;
import com.bocloud.common.model.BocloudResult;
import java.util.List;
import java.util.Map;
/**
* ${class.className}Service
*
* @author jiaohongtao
* @version v1.0
* @since $TIME
*/
public interface ${class.className}Service {
/**
* 翻页查询所有
*
* @param page 第几页
* @param rows 第几行
* @param params params
* @param sorter 分类
* @param simple 是否只取简要
*/
BocloudResult list(int page, int rows, List<Param> params, Map<String, String> sorter, Boolean simple) throws Exception;
/**
* 查询所有
*/
BocloudResult listAll() throws Exception;
/**
* 添加
*
* @param entity 修改对象
* @param userId 操作者
*/
BocloudResult create(${class.className} entity, Long userId) throws Exception;
/**
* 修改
*
* @param id id
* @param entity 修改对象
* @param userId 修改人
*/
BocloudResult modify(Long id, ${class.className} entity, Long userId) throws Exception;
/**
* 删除
*
* @param id id
* @param userId 删除人
*/
BocloudResult remove(Long id, Long userId) throws Exception;
/**
* 详情
*
* @param id id
*/
BocloudResult detail(Long id) throws Exception;
}
package $class.PackageName;
import com.bocloud.bsm.cmdb.dao.common.BeanCopyUtil;
import com.bocloud.bsm.cmdb.dao.intf.${class.className}Dao;
import com.bocloud.bsm.cmdb.entity.${class.className};
import com.bocloud.bsm.cmdb.service.intf.${class.className}Service;
import com.bocloud.common.model.BocloudResult;
import com.bocloud.common.model.GridBean;
import com.bocloud.common.model.Param;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.GridHelper;
import com.bocloud.service.lock.BocloudLock;
import com.bocloud.service.lock.LockFactory;
import com.google.common.collect.Maps;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.Assert;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* ${class.className}Service实现
*
* @author jiaohongtao
* @version v1.0
* @since $TIME
*/
@Service
public class ${class.className}ServiceImpl implements ${class.className}Service {
private static final Logger logger = LoggerFactory.getLogger(${class.className}ServiceImpl.class);
@Autowired
private LockFactory lockFactory;
@Autowired
private ${class.className}Dao originDao;
@Override
public BocloudResult list(int page, int rows, List<Param> params, Map<String, String> sorter, Boolean simple) throws Exception {
params = null == params ? new ArrayList<>() : params;
sorter = null == sorter ? Maps.newHashMap() : sorter;
sorter.putIfAbsent("gmtCreate", Common.ONE);
rows = simple ? Integer.MAX_VALUE : rows;
int total = originDao.count(params);
List<${class.className}> list = originDao.list(page, rows, params, sorter);
GridBean gridBean;
if (simple) {
gridBean = new GridBean(1, 1, total, list);
} else {
gridBean = GridHelper.getBean(page, rows, total, list);
}
return new BocloudResult(true, gridBean, "查询成功");
}
@Override
public BocloudResult listAll() throws Exception {
return new BocloudResult(true, originDao.listAll(), "查询成功");
}
@Override
@Transactional(rollbackFor = Exception.class)
public BocloudResult create(${class.className} entity, Long userId) throws Exception {
entity.setCreatorMender(userId);
originDao.save(entity);
return new BocloudResult(true, entity, "添加成功");
}
@Override
@Transactional(rollbackFor = Exception.class)
public BocloudResult modify(Long id, ${class.className} entity, Long userId) throws Exception {
String path = ${class.className}.class.getSimpleName() + "_" + id;
BocloudLock lock = lockFactory.getBLock(path);
Assert.isTrue(lock.acquire(10, TimeUnit.SECONDS), "获取分布式锁失败!");
try {
${class.className} queryEntity = originDao.query(id);
if (null == queryEntity) {
logger.warn("${class.className} data not exist");
return BocloudResult.FAILED("数据不存在!");
}
BeanCopyUtil.copyProperties(entity, queryEntity);
queryEntity.setMenderId(userId);
originDao.update(queryEntity);
return new BocloudResult(true, queryEntity, "修改成功");
} catch (Exception e) {
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
logger.error("修改失败:", e);
return BocloudResult.FAILED("修改失败");
} finally {
lock.release();
}
}
@Override
@Transactional(rollbackFor = Exception.class)
public BocloudResult remove(Long id, Long userId) throws Exception {
String path = ${class.className}.class.getSimpleName() + "_" + id;
BocloudLock lock = lockFactory.getBLock(path);
Assert.isTrue(lock.acquire(10, TimeUnit.SECONDS), "获取分布式锁失败!");
try {
${class.className} entity = originDao.query(id);
if (null == entity) {
return BocloudResult.FAILED("数据不存在");
}
originDao.delete(id, userId);
return new BocloudResult(true, "删除成功");
} catch (Exception e) {
logger.error("移除失败:", e);
return BocloudResult.FAILED("删除失败");
} finally {
lock.release();
}
}
@Override
public BocloudResult detail(Long id) throws Exception {
${class.className} entity = originDao.query(id);
return entity == null ? BocloudResult.FAILED("数据不存在") : new BocloudResult(true, entity, "获取详情成功");
}
}
package $class.PackageName;
import com.alibaba.fastjson.JSONObject;
import com.bocloud.bsm.cmdb.entity.${class.className};
import com.bocloud.bsm.cmdb.service.intf.${class.className}Service;
import com.bocloud.common.annotations.Log;
import com.bocloud.common.enums.BoCloudService;
import com.bocloud.common.model.BocloudResult;
import com.bocloud.common.model.Param;
import com.bocloud.common.model.RequestUser;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.JSONTools;
import com.bocloud.common.utils.ResultTools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
/**
* ${class.className}Controller
*
* @author jiaohongtao
* @version 1.0
* @since $TIME
*/
@RestController
@RequestMapping("/${class.className}")
public class ${class.className}Controller {
private static final Logger logger = LoggerFactory.getLogger(${class.className}Controller.class);
@Autowired
private ${class.className}Service originService;
/**
* 翻页查询所有
*
* @param page 第几页
* @param rows 第几行
* @param params params
* @param sorter 分类
* @param simple 是否只取简要
*/
@Log(module = BoCloudService.Cmdb, target = "${class.className}", operation = "列表查询")
@RequestMapping(value = "/list", method = {RequestMethod.POST})
public BocloudResult list(@RequestParam(value = Common.PAGE, required = false) Integer page,
@RequestParam(value = Common.ROWS, required = false) Integer rows,
@RequestParam(value = Common.PARAMS, required = false) String params,
@RequestParam(value = Common.SORTER, required = false) String sorter,
@RequestParam(value = Common.SIMPLE, required = false) Boolean simple) {
try {
@SuppressWarnings("unchecked")
Map<String, String> sorterMap = JSONObject.parseObject(sorter, HashMap.class);
return originService.list(page, rows, JSONObject.parseArray(params, Param.class), sorterMap, simple);
} catch (Exception e) {
logger.error("${class.className} list fail:", e);
return BocloudResult.FAILED("查询失败");
}
}
/**
* 查询所有
*/
@Log(module = BoCloudService.Cmdb, target = "${class.className}", operation = "查询所有")
@RequestMapping(value = "/listAll", method = { RequestMethod.POST })
public BocloudResult listAll() {
try {
return originService.listAll();
} catch (Exception e) {
logger.error("${class.className} listAll fail:", e);
return BocloudResult.FAILED("查询失败");
}
}
/**
* 创建
*
* @param params 属性信息的json字符串,数据转换发生异常时,对外抛出400异常【Bad Request】
* @param user 操作者
*/
@Log(module = BoCloudService.Cmdb, target = "${class.className}", operation = "创建")
@RequestMapping(value = "/create", method = {RequestMethod.POST})
public BocloudResult create(@RequestParam(value = Common.PARAMS) String params,
@Value(Common.REQ_USER) RequestUser user) {
try {
JSONObject object = JSONTools.isJSONObj(params);
if (null == object) {
return ResultTools.formatErrResult();
}
${class.className} entity = JSONObject.parseObject(object.toJSONString(), ${class.className}.class);
return originService.create(entity, user.getId());
} catch (Exception e) {
logger.error("${class.className} create fail:", e);
return BocloudResult.FAILED("创建失败");
}
}
/**
* 修改
*
* @param params 属性信息的json字符串,数据转换发生异常时,对外抛出400异常【Bad Request】
* @param id id
* @param user 操作者
*/
@Log(module = BoCloudService.Cmdb, target = "${class.className}", operation = "修改")
@RequestMapping(value = "/{id}/modify", method = {RequestMethod.POST})
public BocloudResult modify(@RequestParam(value = Common.PARAMS) String params,
@PathVariable(value = Common.ID) Long id, @Value(Common.REQ_USER) RequestUser user) {
try {
JSONObject object = JSONTools.isJSONObj(params);
if (null == object) {
return ResultTools.formatErrResult();
}
${class.className} entity = JSONObject.parseObject(object.toJSONString(), ${class.className}.class);
return originService.modify(id, entity, user.getId());
} catch (Exception e) {
logger.error("${class.className} modify fail:", e);
return BocloudResult.FAILED("修改失败");
}
}
/**
* 移除
*
* @param id id
* @param user 操作者
*/
@Log(module = BoCloudService.Cmdb, target = "${class.className}", operation = "移除")
@RequestMapping(value = "/{id}/remove", method = {RequestMethod.DELETE})
public BocloudResult remove(@PathVariable(Common.ID) Long id, @Value(Common.REQ_USER) RequestUser user) {
try {
return originService.remove(id, user.getId());
} catch (Exception e) {
logger.error("${class.className} remove fail:", e);
return BocloudResult.FAILED("移除失败");
}
}
/**
* 详情
*
* @param id id
*/
@Log(module = BoCloudService.Cmdb, target = "${class.className}", operation = "详情")
@RequestMapping(value = "/{id}/detail", method = {RequestMethod.GET})
public BocloudResult detail(@PathVariable(Common.ID) Long id) {
try {
return originService.detail(id);
} catch (Exception e) {
logger.error("${class.className} detail fail:", e);
return BocloudResult.FAILED("查询失败");
}
}
}
package com.bocloud.cmc.controller.cmdb.bsm;
import com.bocloud.cmc.controller.BasicController;
import com.bocloud.cmc.serviceLoader.BocloudServiceLoader;
import com.bocloud.common.annotations.Log;
import com.bocloud.common.enums.BoCloudService;
import com.bocloud.common.model.BocloudResult;
import com.bocloud.common.model.Pager;
import com.bocloud.common.utils.Common;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
/**
* ${class.className}控制层
*
* @author jiaohongtao
* @version 1.0
* @since $TIME
*/
@RestController
@RequestMapping("/${class.className}")
@Api(tags = "BSM-CMDB ${class.className}")
public class ${class.className}Controller {
private final String BASE_SERVICE = "${class.className}";
private static final BoCloudService SERVICE = BocloudServiceLoader.bsmCmdbService;
@Autowired
private BasicController basicController;
@ApiOperation(value = "新增", response = BocloudResult.class, httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
@Log(module = BoCloudService.Cmc, target = "${class.className}", operation = "新增")
@RequestMapping(value = "/create", method = {RequestMethod.POST})
@ApiImplicitParams(value = {
@ApiImplicitParam(name = "params", value = "{\"name\": \"\",\"code\": \"\"}")
})
public BocloudResult create(@RequestParam(value = Common.PARAMS, required = false) String params,
HttpServletRequest request) {
return basicController.create(SERVICE, BASE_SERVICE, params, request, ${class.className}Controller.class);
}
@ApiOperation(value = "列表查询", response = BocloudResult.class, httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
@Log(module = BoCloudService.Cmc, target = "${class.className}", operation = "列表查询")
@RequestMapping(value = "/list", method = {RequestMethod.POST})
@ApiImplicitParams(value = {
@ApiImplicitParam(name = "params", value = "[{\"param\":{\"name\":\"里斯辣个\"},\"sign\":\"EQ\"}]"),
@ApiImplicitParam(name = "sorter", value = "{\"gmtCreate\":\"1\"}")
})
public BocloudResult list(
@RequestParam(value = Common.PAGE, required = false, defaultValue = Common.ONE) Integer page,
@RequestParam(value = Common.ROWS, required = false, defaultValue = Common.TEN) Integer rows,
@RequestParam(value = Common.PARAMS, required = false) String params,
@RequestParam(value = Common.SORTER, required = false) String sorter,
@RequestParam(value = Common.SIMPLE, required = false) boolean simple, HttpServletRequest request) {
return basicController.list(SERVICE, BASE_SERVICE, new Pager(page, rows, params, sorter), simple, request,
${class.className}Controller.class);
}
@ApiOperation(value = "查询所有", response = BocloudResult.class, httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
@Log(module = BoCloudService.Cmc, target = "${class.className}", operation = "查询所有")
@RequestMapping(value = "/listAll", method = {RequestMethod.POST})
public BocloudResult tree(HttpServletRequest request) {
return basicController.operate(SERVICE, BASE_SERVICE + "/listAll", "{}", request, ${class.className}Controller.class);
}
@ApiOperation(value = "修改", response = BocloudResult.class, httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
@Log(module = BoCloudService.Cmc, target = "${class.className}", operation = "修改")
@RequestMapping(value = "/modify", method = {RequestMethod.POST})
@ApiImplicitParams(value = {
@ApiImplicitParam(name = "params", value = "{\"id\": 1,\"name\": \"\",\"code\": \"\"}")
})
public BocloudResult modify(@RequestParam(value = Common.PARAMS, required = false) String params,
HttpServletRequest request) {
return basicController.modify(SERVICE, BASE_SERVICE, params, request, ${class.className}Controller.class);
}
@ApiOperation(value = "删除", response = BocloudResult.class, httpMethod = "DELETE", produces = MediaType.APPLICATION_JSON_VALUE)
@Log(module = BoCloudService.Cmc, target = "${class.className}", operation = "删除")
@RequestMapping(value = "/remove", method = {RequestMethod.DELETE})
@ApiImplicitParams(value = {
@ApiImplicitParam(name = "params", value = "{\"id\": 1}")
})
public BocloudResult remove(@RequestParam(value = Common.PARAMS, required = false) String params,
HttpServletRequest request) {
return basicController.remove(SERVICE, BASE_SERVICE, params, request, ${class.className}Controller.class);
}
@ApiOperation(value = "详情", response = BocloudResult.class, httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
@Log(module = BoCloudService.Cmc, target = "${class.className}", operation = "详情")
@RequestMapping(value = "/detail", method = {RequestMethod.GET})
@ApiImplicitParams(value = {
@ApiImplicitParam(name = "params", value = "{\"id\": 1}")
})
public BocloudResult detail(@RequestParam(value = Common.PARAMS, required = false) String params,
HttpServletRequest request) {
return basicController.detail(SERVICE, BASE_SERVICE, params, request, ${class.className}Controller.class);
}
}
在需要构建的类名上右键(或者快捷键alt+insert),点击Generate:
点击自定义中需要生成的操作类:
选择生成的目录位置(可随意选择,最后都需要调整类的包名和路径),点击OK:
生成成功