Commit 9214bf2c by flying-cattle

整合mybatis-plus组件,使用了lombok,带swagger文档api,实现增删改查、分页查询接口

parent fb2e190c
......@@ -6,35 +6,40 @@
*/
package ${controllerUrl};
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.item.util.JsonResult;
import ${entityUrl}.${entityName};
import ${serviceUrl}.${entityName}Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.github.pagehelper.PageInfo;
import com.item.util.AppPage;
import com.item.util.JsonResult;
import ${entityUrl}.${entityName};
import ${serviceUrl}.${entityName}Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
/**
* Copyright: Copyright (c) 2019
*
* @Description: ${entityComment}接口层
* @Author: ${author}
* @CreateDate: ${createTime}
* @Version: ${version}
*
* <p>说明: ${entityComment}API接口层</P>
* @version: ${version}
* @author: ${author}
*
* Modification History:
* Date Author Version Description
*---------------------------------------------------------------*
* ${createTime} ${author} ${version} initialize
*/
@Api(description = "${entityComment}",value="${entityComment}" )
@RestController
......@@ -59,26 +64,25 @@ public class ${entityName}Controller {
public JsonResult<${entityName}> get${entityName}ById(@PathVariable("id")${idType} id){
JsonResult<${entityName}> result=new JsonResult<${entityName}>();
try {
${entityName} ${objectName}=${objectName}ServiceImpl.selectByPrimaryKey(id);
${entityName} ${objectName}=${objectName}ServiceImpl.getById(id);
if (${objectName}!=null) {
result.setCode(1);
result.setType("success");
result.setMessage("成功");
result.setData(${objectName});
} else {
logger.error("获取${entityComment}失败ID:"+id);
result.setCode(-1);
result.setType("fail");
result.setMessage("你获取的${entityComment}不存在");
}
} catch (Exception e) {
logger.error("获取${entityComment}执行异常:"+e.getMessage());
result.setCode(-1);
result.setMessage("执行异常,请稍后重试");
result=new JsonResult<${entityName}>(e);
}
return result;
}
/**
* @explain 添加${entityComment}对象
* @explain 添加或者更新${entityComment}对象
* @param 对象参数:${objectName}
* @return int
* @author ${author}
......@@ -89,20 +93,19 @@ public class ${entityName}Controller {
public JsonResult<${entityName}> insertSelective(${entityName} ${objectName}){
JsonResult<${entityName}> result=new JsonResult<${entityName}>();
try {
int rg=${objectName}ServiceImpl.insertSelective(${objectName});
if (rg>0) {
result.setCode(1);
boolean rg=${objectName}ServiceImpl.saveOrUpdate(${objectName});
if (rg) {
result.setType("success");
result.setMessage("成功");
result.setData(${objectName});
} else {
logger.error("添加${entityComment}执行失败:"+${objectName}.toString());
result.setCode(-1);
result.setType("fail");
result.setMessage("执行失败,请稍后重试");
}
} catch (Exception e) {
logger.error("添加${entityComment}执行异常:"+e.getMessage());
result.setCode(-1);
result.setMessage("执行异常,请稍后重试");
result=new JsonResult<${entityName}>(e);
}
return result;
}
......@@ -120,74 +123,19 @@ public class ${entityName}Controller {
public JsonResult<Object> deleteByPrimaryKey(${idType} id){
JsonResult<Object> result=new JsonResult<Object>();
try {
int reg=${objectName}ServiceImpl.deleteByPrimaryKey(id);
if (reg>0) {
result.setCode(1);
boolean reg=${objectName}ServiceImpl.removeById(id);
if (reg) {
result.setType("success");
result.setMessage("成功");
result.setData(id);
} else {
logger.error("删除${entityComment}失败ID:"+id);
result.setCode(-1);
result.setType("fail");
result.setMessage("执行错误,请稍后重试");
}
} catch (Exception e) {
logger.error("删除${entityComment}执行异常:"+e.getMessage());
result.setCode(-1);
result.setMessage("执行异常,请稍后重试");
}
return result;
}
/**
* @explain 修改${entityComment}对象
* @param 对象参数:${objectName}
* @return ${objectName}
* @author ${author}
* @time ${createTime}
*/
@ApiOperation(value = "修改${entityComment}", notes = "修改${entityComment}[${objectName}],作者:${author}")
@PostMapping("/updateByPrimaryKeySelective")
public JsonResult<${entityName}> updateByPrimaryKeySelective(${entityName} ${objectName}){
JsonResult<${entityName}> result=new JsonResult<${entityName}>();
try {
int reg = ${objectName}ServiceImpl.updateByPrimaryKeySelective(${objectName});
if (reg>0) {
result.setCode(1);
result.setMessage("成功");
result.setData(${objectName});
} else {
logger.error("修改${entityComment}失败ID:"+${objectName}.toString());
result.setCode(-1);
result.setMessage("执行错误,请稍后重试");
}
} catch (Exception e) {
logger.error("修改${entityComment}执行异常:"+e.getMessage());
result.setCode(-1);
result.setMessage("执行异常,请稍后重试");
}
return result;
}
/**
* @explain 获取匹配${entityComment}
* @param 对象参数:${objectName}
* @return List<${entityName}>
* @author ${author}
* @time ${createTime}
*/
@ApiOperation(value = "条件查询${entityComment}", notes = "条件查询[${objectName}],作者:${author}")
@PostMapping("/query${entityName}List")
public JsonResult<List<${entityName}>> query${entityName}List(${entityName} ${objectName}){
JsonResult<List<${entityName}>> result=new JsonResult<List<${entityName}>>();
try {
List<${entityName}> list = ${objectName}ServiceImpl.query${entityName}List(${objectName});
result.setCode(1);
result.setMessage("成功");
result.setData(list);
} catch (Exception e) {
logger.error("获取${entityComment}执行异常:"+e.getMessage());
result.setCode(-1);
result.setMessage("执行异常,请稍后重试");
result=new JsonResult<Object>(e);
}
return result;
}
......@@ -199,30 +147,26 @@ public class ${entityName}Controller {
* @author ${author}
* @time ${createTime}
*/
@GetMapping("/getPage${entityName}")
@ApiOperation(value = "分页查询", notes = "分页查询返回对象[PageInfo<${entityName}>],作者:边鹏")
@GetMapping("/get${entityName}Pages")
@ApiOperation(value = "分页查询", notes = "分页查询返回对象[IPage<${entityName}>],作者:边鹏")
@ApiImplicitParams({
@ApiImplicitParam(paramType="query", name = "pageNum", value = "当前页", required = true, dataType = "int"),
@ApiImplicitParam(paramType="query", name = "pageSize", value = "页行数", required = true, dataType = "int")
})
public JsonResult<PageInfo<${entityName}>> get${entityName}BySearch(Integer pageNum,Integer pageSize){
JsonResult<PageInfo<${entityName}>> result=new JsonResult<PageInfo<${entityName}>>();
AppPage<${entityName}> page =new AppPage<${entityName}>();
page.setPageNum(pageNum);
page.setPageSize(pageSize);
//其他参数
${entityName} ${objectName}=new ${entityName}();
page.setParam(${objectName});
public JsonResult<IPage<${entityName}>> get${entityName}Pages(Integer pageNum,Integer pageSize){
JsonResult<IPage<${entityName}>> result=new JsonResult<IPage<${entityName}>>();
Page<${entityName}> page=new Page<${entityName}>(pageNum,pageSize);
QueryWrapper<${entityName}> queryWrapper =new QueryWrapper<${entityName}>();
//分页数据
try {
PageInfo<${entityName}> pageInfo = ${objectName}ServiceImpl.get${entityName}BySearch(page);
result.setCode(1);
IPage<${entityName}> pageInfo=${objectName}ServiceImpl.page(page, queryWrapper);
result.setType("success");
result.setMessage("成功");
result.setData(pageInfo);
} catch (Exception e) {
logger.error("分页查询${entityComment}执行异常:"+e.getMessage());
result.setCode(-1);
result.setMessage("执行异常,请稍后重试");
result=new JsonResult<IPage<${entityName}>>(e);
}
return result;
}
......
......@@ -7,28 +7,24 @@
package ${daoUrl};
import java.util.List;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import ${entityUrl}.${entityName};
/**
*
* @Description: ${entityComment}——DAO
* @Author: ${author}
* @CreateDate: ${createTime}
* @Version: ${version}
*
* Copyright: Copyright (c) 2019
*
* <p>说明: ${entityComment}数据访问层</P>
* @version: ${version}
* @author: ${author}
*
* Modification History:
* Date Author Version Description
*---------------------------------------------------------------*
* ${createTime} ${author} ${version} initialize
*/
@Mapper
public interface ${entityName}Dao {
public interface ${entityName}Dao extends BaseMapper<${entityName}> {
public ${entityName} selectByPrimaryKey(${idType} id);
public int deleteByPrimaryKey(${idType} id);
public int insertSelective(${entityName} ${objectName});
public int updateByPrimaryKeySelective(${entityName} ${objectName});
public List<${entityName}> query${entityName}List(${entityName} ${objectName});
}
\ No newline at end of file
......@@ -6,46 +6,59 @@
*/
package ${entityUrl};
import java.io.Serializable;
import java.util.Date;
import org.springframework.format.annotation.DateTimeFormat;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import java.util.Date;
import org.springframework.format.annotation.DateTimeFormat;
import java.io.Serializable;
/**
*
* @Description: ${entityComment}
* @Author: ${author}
* @CreateDate: ${createTime}
* @Version: ${version}
*
* Copyright: Copyright (c) 2019
*
* <p>说明: ${entityComment}实体类</P>
* @version: ${version}
* @author: ${author}
*
* Modification History:
* Date Author Version Description
*---------------------------------------------------------------*
* ${createTime} ${author} ${version} initialize
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ${entityName} implements Serializable {
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class ${entityName} extends Model<${entityName}> {
private static final long serialVersionUID = ${agile}L;
<#list cis as ci>
<#if ci.javaType=="Date">
<#if ci.jdbcType=="date">
<#if ci.javaType=="Date">
<#if ci.jdbcType=="date">
@DateTimeFormat(pattern = "yyyy-MM-dd")
@JsonFormat(pattern="yyyy-MM-dd",timezone = "GMT+8")
<#elseif ci.jdbcType=="time">
<#elseif ci.jdbcType=="time">
@DateTimeFormat(pattern = "HH:mm:ss")
@JsonFormat(pattern="HH:mm:ss",timezone = "GMT+8")
<#else>
<#else>
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
</#if>
</#if>
</#if>
</#if>
<#if ci.property=="id">
@TableId(value = "id", type = IdType.AUTO)
</#if>
@ApiModelProperty(name = "${ci.property}" , value = "${ci.comment}")
private ${ci.javaType} ${ci.property};
</#list>
@Override
protected Serializable pkVal() {
return this.id;
}
}
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${daoUrl}.${entityName}Dao">
<resultMap id="BaseResultMap" type="${entityUrl}.${entityName}">
<#list cis as ci>
<id column="${ci.column}" jdbcType="${ci.jdbcType?upper_case}" property="${ci.property}" />
<id column="${ci.column}" property="${ci.property}" />
</#list>
</resultMap>
<sql id="Base_Column_List">
${agile}
${agile}
</sql>
<!-- 查询 -->
<select id="selectByPrimaryKey" parameterType="java.lang.${idType}"
resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from ${table}
where id = ${r'#{'}id,jdbcType=${idJdbcType?upper_case}}
</select>
<!-- 删除 -->
<delete id="deleteByPrimaryKey" parameterType="java.lang.${idType}">
delete from ${table}
where id = ${r'#{'}id,jdbcType=${idJdbcType?upper_case}}
</delete>
<!-- 选择添加 -->
<insert id="insertSelective" parameterType="${entityUrl}.${entityName}">
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.${idType}">
SELECT
LAST_INSERT_ID()
</selectKey>
insert into ${table}
<trim prefix="(" suffix=")" suffixOverrides=",">
<#list cis as ci>
<if test="${ci.property} != null">
${ci.column},
</if>
</#list>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<#list cis as ci>
<if test="${ci.property} != null">
${r'#{'}${ci.property},jdbcType=${ci.jdbcType?upper_case}},
</if>
</#list>
</trim>
</insert>
<!-- 选择修改 -->
<update id="updateByPrimaryKeySelective" parameterType="${entityUrl}.${entityName}">
update ${table}
<set>
<#list cis as ci>
<if test="${ci.property} != null">
${ci.column} = ${r'#{'}${ci.property},jdbcType=${ci.jdbcType?upper_case}},
</if>
</#list>
</set>
where id = ${r'#{'}id,jdbcType=${idJdbcType}}
</update>
<!-- 组合条件查询 -->
<select id="query${entityName}List" parameterType="${entityUrl}.${entityName}"
resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from ${table}
<where>
<if test="id != null">
id = ${r'#{'}id,jdbcType=${idJdbcType?upper_case}}
</if>
<#list cis as ci>
<#if ci.column!="id">
<if test="${ci.property} != null">
AND ${ci.column} = ${r'#{'}${ci.property} ,jdbcType=${ci.jdbcType?upper_case}}
</if>
</#if>
</#list>
</where>
</select>
</mapper>
\ No newline at end of file
......@@ -6,66 +6,20 @@
*/
package ${serviceUrl};
import java.util.List;
import com.github.pagehelper.PageInfo;
import com.item.util.AppPage;
import ${entityUrl}.${entityName};
import com.baomidou.mybatisplus.extension.service.IService;
/**
*
* @Description: ${entityComment}——SERVICE
* @Author: ${author}
* @CreateDate: ${createTime}
* @Version: ${version}
*
* Copyright: Copyright (c) 2019
*
* <p>说明: ${entityComment}服务层</P>
* @version: ${version}
* @author: ${author}
*
* Modification History:
* Date Author Version Description
*---------------------------------------------------------------*
* ${createTime} ${author} ${version} initialize
*/
public interface ${entityName}Service {
/**
* @explain 查询${entityComment}对象
* @param 对象参数:id
* @return ${entityName}
* @author ${author}
*/
public ${entityName} selectByPrimaryKey(${idType} id);
/**
* @explain 删除${entityComment}对象
* @param 对象参数:id
* @return int
* @author ${author}
*/
public int deleteByPrimaryKey(${idType} id);
/**
* @explain 添加${entityComment}对象
* @param 对象参数:${entityName}
* @return int
* @author ${author}
*/
public int insertSelective(${entityName} ${objectName});
/**
* @explain 修改${entityComment}对象
* @param 对象参数:${entityName}
* @return int
* @author ${author}
*/
public int updateByPrimaryKeySelective(${entityName} ${objectName});
/**
* @explain 查询${entityComment}集合
* @param 对象参数:${entityName}
* @return List<${entityName}>
* @author ${author}
*/
public List<${entityName}> query${entityName}List(${entityName} ${objectName});
public interface ${entityName}Service extends IService<${entityName}> {
/**
* @explain 分页查询${entityComment}
* @param 对象参数:${entityName}
* @return PageInfo<${entityName}>
* @author ${author}
*/
public PageInfo<${entityName}> get${entityName}BySearch(AppPage<${entityName}> page);
}
\ No newline at end of file
......@@ -6,69 +6,25 @@
*/
package ${serviceImplUrl};
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.item.util.AppPage;
import ${entityUrl}.${entityName};
import ${daoUrl}.${entityName}Dao;
import ${serviceUrl}.${entityName}Service;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
/**
*
* @Description: ${entityComment}——SERVICEIMPL
* @Author: ${author}
* @CreateDate: ${createTime}
* @Version: ${version}
*
* Copyright: Copyright (c) 2019
*
* <p>说明: ${entityComment}服务实现层</P>
* @version: ${version}
* @author: ${author}
*
* Modification History:
* Date Author Version Description
*---------------------------------------------------------------*
* ${createTime} ${author} ${version} initialize
*/
@Service
public class ${entityName}ServiceImpl implements ${entityName}Service {
@Autowired
public ${entityName}Dao ${objectName}Dao;
//查询对象
@Override
public ${entityName} selectByPrimaryKey(${idType} id) {
return ${objectName}Dao.selectByPrimaryKey(id);
}
//删除对象
@Override
public int deleteByPrimaryKey(${idType} id) {
return ${objectName}Dao.deleteByPrimaryKey(id);
}
//添加对象
@Override
public int insertSelective(${entityName} ${objectName}) {
return ${objectName}Dao.insertSelective(${objectName});
}
//修改对象
@Override
public int updateByPrimaryKeySelective(${entityName} ${objectName}) {
return ${objectName}Dao.updateByPrimaryKeySelective(${objectName});
}
//查询集合
@Override
public List<${entityName}> query${entityName}List(${entityName} ${objectName}) {
return ${objectName}Dao.query${entityName}List(${objectName});
}
public class ${entityName}ServiceImpl extends ServiceImpl<${entityName}Dao, ${entityName}> implements ${entityName}Service {
//分页查询
@Override
public PageInfo<${entityName}> get${entityName}BySearch(AppPage<${entityName}> page) {
// TODO Auto-generated method stub
PageHelper.startPage(page.getPageNum(),page.getPageSize());
List<${entityName}> list=${objectName}Dao.query${entityName}List(page.getParam());
PageInfo<${entityName}> pageInfo = new PageInfo<${entityName}>(list);
return pageInfo;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment