码迷,mamicode.com
首页 > 其他好文 > 详细

代码生成器

时间:2019-01-09 00:27:31      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:hibernate   code   factory   变量   column   getc   wired   png   生成   

代码生成器

hibernate mybatis 都是在做一件简化工作量的事情,重复简单的事情,认真做一次就可以,何必将宝贵时间花在不必要的事情上呢???

单表的增删改查,分页查询等,都是数据库表格建立以后需要做的操作,可是为重复的操作;

技术分享图片

根据项目分析,可以抽取很多相同的部分,根据项目常用的功能书写demo;

pojo、vo生成的准备:

  • 读取数据库的表名称,注释
  • 数据库字段的名称、类型和说明等,转化成java对象中的属性的类型、名称(驼峰的转化)、注释等
  • 特定字段的判断,比如id上是不同的注解,有些字段不在前台展示等

 

client,controller,service生成的准备:

  • 定义常用的方法,方法属性名称分成对应
  • 引入正确的架包,减少后期添加
  • 在模板生成时候,可变和不可变量设置好

其他的准备:

  • 定义对相对应的ftl文件模板
  • 定义一个map,将数据和值放入其中,代码填充的时候,通过查找占位符对应的数值进行赋值,完成模板的填充

数据的准备过程:

    /**
     * 将对象信息存放找map的集合中
     * @param basePackage
     * @param db
     * @param tableName
     * @param className
     * @param service
     * @return
     */
    private Map<String, Object> getRoot(String basePackage, String db, String tableName, String className, String service){
        String lowerClassName =beanGenUtil.getLowerClassName(className);
        Map<String, Object> root = new HashMap<>();
        Map<String, Object> map = new HashMap<>();
        map.put("table_name", tableName);
        map.put("table_schema", db);
        Map<String,Object> tableInfo=codeMapper.selectTableInfo(map);
        List<Map<String, Object>> listResult = codeMapper.selectColumnsByTablename(map);
        beanGenUtil.mapPutJavaType(listResult);
        root.put("cols", listResult);
        root.put("table", tableName);
        root.put("className", className);
        root.put("basepackage", basePackage);
        root.put("tableInfo",tableInfo);
        root.put("lowerClassName",lowerClassName);
        root.put("service",service);
        root.put("nowDate", DateTime.now().toString("yyyy-MM-dd"));
        return root;
    }

  

根据项目的分层代码生成主要有基础的pojo,读写的dao和对应的xml文件,以及单表增删改查的部分

下面展示的单表增删改查的ftl文件模板:

client层(微服务对外提供接口)

package com.${service}.client;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;

@FeignClient("${service}")
@Api(tags = "${tableInfo.TABLE_COMMENT} API文档")
public interface ${className}Client {

    @ApiOperation(value = "新建 ${tableInfo.TABLE_COMMENT}")
	@PostMapping(value = "/v1-0/${lowerClassName}/save")
	Result<Long> save(@RequestBody @Valid ${className}SaveIVO ${lowerClassName}SaveIVO);

    @ApiOperation(value = "更新 ${tableInfo.TABLE_COMMENT}")
	@PostMapping(value = "/v1-0/${lowerClassName}/update")
	Result<Integer> update(@RequestBody @Valid ${className}UpdateIVO ${lowerClassName}UpdateIVO);

    @ApiOperation(value = "删除 ${tableInfo.TABLE_COMMENT}")
	@PostMapping(value = "/v1-0/${lowerClassName}/delete/{${lowerClassName}Id}")
	Result<Integer> delete(@PathVariable("${lowerClassName}Id") Long ${lowerClassName}Id);

    @ApiOperation(value = "分页查询 ${tableInfo.TABLE_COMMENT}")
	@GetMapping(value = "/v1-0/${lowerClassName}/query")
	Result<PageData<${className}OVO>> query(@ModelAttribute("${lowerClassName}PageIVO") ${className}PageIVO ${lowerClassName}PageIVO);

}
controller层
package com.${service}.server.controller;

import com.${service}.client.${className}Client;
import com.${service}.common.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;

@RestController
public class ${className}Controller extends BaseController implements ${className}Client {

	@Autowired
	private ${className}Service ${lowerClassName}Service;

	@Override
	public Result<Long> save(@RequestBody @Valid ${className}SaveIVO ${lowerClassName}SaveIVO){
		Long data = ${lowerClassName}Service.save(${lowerClassName}SaveIVO);
		return Result.ok(data);
	}

	@Override
	public Result<Integer> update(@RequestBody @Valid ${className}UpdateIVO ${lowerClassName}UpdateIVO){
        Integer data = ${lowerClassName}Service.update(${lowerClassName}UpdateIVO);
		return Result.ok(data);
	}

	@Override
	public Result<Integer> delete(@PathVariable("${lowerClassName}Id") Long ${lowerClassName}Id){
        Integer data = ${lowerClassName}Service.delete(${lowerClassName}Id);
		return Result.ok(data);
	}


	@Override
	public Result<PageData<${className}OVO>> query(@ModelAttribute("${lowerClassName}PageIVO") ${className}PageIVO ${lowerClassName}PageIVO){
    	PageData<${className}OVO> data = ${lowerClassName}Service.query(${lowerClassName}PageIVO);
		return Result.ok(data);
	}

}

  service层的部分

package com.${service}.server.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.BeanUtils;
import tk.mybatis.mapper.entity.Example;
import com.github.pagehelper.PageHelper;
import java.util.Date;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class ${className}Service extends BaseService {

	@Autowired
	private ${className}WriteMapper ${lowerClassName}WriteMapper;
	@Autowired
	private ${className}ReadMapper ${lowerClassName}ReadMapper;


	public Long save(${className}SaveIVO ${lowerClassName}SaveIVO){
		${className} saveBean = new ${className}();
		BeanUtils.copyProperties(${lowerClassName}SaveIVO,saveBean);
                //FIXME 对应的
		int saveNum = ${lowerClassName}WriteMapper.insert(saveBean);
		log.debug("新建,其影响条数是:{}",saveNum);
		return saveBean.getId();
	}



	public PageData<${className}OVO> query(${className}PageIVO ${lowerClassName}PageIVO){
		Example example = Example.builder(${className}.class).orderByAsc("createTime").build();
		PageHelper.startPage(${lowerClassName}PageIVO.getPageNum(),${lowerClassName}PageIVO.getPageSize());
		Example.Criteria criteria = example.createCriteria();
        // criteria.andEqualTo("",);
        //FIXME 添加查询条件
		List<${className}> queryList = ${lowerClassName}ReadMapper.selectByExample(example);
    	return PageData.getCopyPageData(queryList,${className}.class,${className}OVO.class);
    }

}    

  

vo等相关的生成

package com.${service}.common.vo;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.Date;
import java.math.BigDecimal;

/**
* ${tableInfo.TABLE_COMMENT}
* @Date ${nowDate}
*/
@Data
@ApiModel(value = "${tableInfo.TABLE_COMMENT}OVO")
public class ${className}OVO {
<#list cols as col>
<#if col.field != "isDelete" >
    <#if col.COLUMN_COMMENT != "">
    /**
    * ${col.COLUMN_COMMENT}
    */
    @ApiModelProperty(value = "${col.COLUMN_COMMENT}")
    </#if>
    private ${col.javaType} ${col.field};
</#if>
</#list>
}

  基本上就是依样画葫芦。

github代码的地址(待项目整理以后放上去)

代码生成器

标签:hibernate   code   factory   变量   column   getc   wired   png   生成   

原文地址:https://www.cnblogs.com/xiebq/p/10193153.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!