更强大的codegenerator自定义工具

更强大的 code generator 自定义工具
我们以前用 mybatis 的 generator 感觉很爽,通用mapper(tk.mapper) 也很爽,但是。。。

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:域名注册、网页空间、营销软件、网站建设、茌平网站维护、网站推广。


还有更爽的 就是这两种结合 并且 还可以直接生成简单的 service controller 层 还可以包含 swagger
直接上代码:
引入依赖 pom.xml



        4.0.0
        
                com.jcbjcb.tools
                jcbjcb-tools
                1.0-SNAPSHOT
        
        jcbjcb-generator
        jcbjcb-generator
        Demo project for Spring Boot
        jar

        
                1.8
        

        





                
                        org.springframework.boot
                        spring-boot-starter-web
                

                
                        org.projectlombok
                        lombok
                        true
                

                
                
                        tk.mybatis
                        mapper-spring-boot-starter
                        2.1.5
                

                
                        org.springframework.boot
                        spring-boot-starter-data-jpa
                
                
                        org.springframework.boot
                        spring-boot-starter-test
                        test
                
                
                        org.mybatis.generator
                        mybatis-generator-core
                        1.3.7
                
                
                        tk.mybatis
                        mapper-generator
                        1.1.5
                
                
                        io.springfox
                        springfox-swagger2
                        2.9.2
                
                
                        io.springfox
                        springfox-swagger-ui
                        2.9.2
                

                
                        MySQL
                        mysql-connector-java
                        8.0.15
                
                
                        tk.mybatis
                        mapper
                        4.1.5
                
                
                        org.freemarker
                        freemarker
                        2.3.28
                

        

        
                
                        
                                maven-compiler-plugin
                                
                                        ${java.version}
                                        ${java.version}
                                
                        
                        
                                org.mybatis.generator
                                mybatis-generator-maven-plugin
                                1.3.7
                                
                                        
                                                ${basedir}/src/main/resources/generator/generatorConfig.xml
                                        
                                        true
                                        true
                                
                                
                                        
                                                mysql
                                                mysql-connector-java
                                                8.0.15
                                        
                                        
                                                tk.mybatis
                                                mapper
                                                4.1.5
                                        
                                        
                                                org.freemarker
                                                freemarker
                                                2.3.28
                                        
                                
                        
                
        

generatorConfig.xml




        

        
                
                

                
                        
                        

                        

                        


                        

                        

                        

                        
                        
                
                
                
                        
                        
                        
                        
                        
                        
                        
                        

                

                
                
                        
                        
                        
                        
                        
                        
                        
                        
                

                
                
                        
                        
                        
                        
                        
                        
                        
                        
                

                
                

                

                





                
                

config.properties

    jdbc.driverClass = com.mysql.cj.jdbc.Driver
    jdbc.url = jdbc:mysql://127.0.0.1:3306/school_score?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
    jdbc.user = root
    jdbc.password = root

代码生成模板
biz.ftl

package ${package};

import com.jcbjcb.tools.common.biz.BaseBiz;
import ${package?substring(0,package?last_index_of('.'))}.mapper.${tableClass.shortClassName}Mapper;
import ${tableClass.fullClassName};
import org.springframework.stereotype.Service;

<#assign dateTime = .now>
/**
* @description ${tableClass.shortClassName}业务层
* @author  jcb
* @since  ${dateTime?string["yyyy-MM-dd HH:mm:ss"]}
*/
@Service
public class ${tableClass.shortClassName}Biz extends BaseBiz<${tableClass.shortClassName}Mapper, ${tableClass.shortClassName}> {

}

controller.ftl

package ${package};

import com.jcbjcb.tools.common.rest.BaseController;
import ${package?substring(0,package?last_index_of('.'))}.biz.${tableClass.shortClassName}Biz;
import ${tableClass.fullClassName};
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

<#assign dateTime = .now>
/**
* @description ${tableClass.shortClassName}控制层
* @author  jcb
* @since  ${dateTime?string["yyyy-MM-dd HH:mm:ss"]}
*/
@RestController
@RequestMapping("/${tableClass.variableName}")
public class ${tableClass.shortClassName}Controller extends BaseController<${tableClass.shortClassName}Biz, ${tableClass.shortClassName}> {

}

mapper.ftl

package ${package};

import com.jcbjcb.tools.common.mapper.BaseMapper;
import ${tableClass.fullClassName};
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

<#assign dateTime = .now>
/**
 * @description ${tableClass.shortClassName}数据层
 * @author  jcb
 * @since  ${dateTime?string["yyyy-MM-dd HH:mm:ss"]}
*/
@Mapper
@Repository
public interface ${tableClass.shortClassName}Mapper extends BaseMapper<${tableClass.shortClassName}> {

}

基础类

BaseMapper.class

package com.jcbjcb.tools.common.mapper;

import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;

public interface BaseMapper extends Mapper, MySqlMapper {

}

BaseBiz.class

package com.jcbjcb.tools.common.biz;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jcbjcb.tools.common.util.Query;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Example;

import java.lang.reflect.ParameterizedType;
import java.util.List;
import java.util.Map;

public abstract class BaseBiz,T> {
        @Autowired
        protected M mapper;

        public void setMapper(M mapper) {
                this.mapper = mapper;
        }

        public T selectOne(T entity) {
                return mapper.selectOne(entity);
        }

        public T selectById(Object id) {
                return mapper.selectByPrimaryKey(id);
        }

        public List selectList(T entity) {
                return mapper.select(entity);
        }

        public List selectListAll() {
                return mapper.selectAll();
        }

        public Long selectCount(T entity) {
                return new Long(mapper.selectCount(entity));
        }

        public void insert(T entity) {
                mapper.insert(entity);
        }

        public void insertSelective(T entity) {
                mapper.insertSelective(entity);
        }

        public void delete(T entity) {
                mapper.delete(entity);
        }

        public void deleteById(Object id) {
                mapper.deleteByPrimaryKey(id);
        }

        public void updateById(T entity) {
                mapper.updateByPrimaryKey(entity);
        }

        public void updateSelectiveById(T entity) {
                mapper.updateByPrimaryKeySelective(entity);

        }

        public List selectByExample(Object example) {
                return mapper.selectByExample(example);
        }

        public int selectCountByExample(Object example) {
                return mapper.selectCountByExample(example);
        }

        public PageInfo selectByQuery(Query query) {
                Class clazz = (Class) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[1];
                Example example = new Example(clazz);
                if(query.entrySet().size()>0) {
                        Example.Criteria criteria = example.createCriteria();
                        for (Map.Entry entry : query.entrySet()) {
                                criteria.andLike(entry.getKey(), "%" + entry.getValue().toString() + "%");
                        }
                }
                Page result = PageHelper.startPage(query.getPage(), query.getLimit());
                List list = mapper.selectByExample(example);
                return new PageInfo(list);
        }
}

BaseController.class

package com.jcbjcb.tools.common.rest;

import com.github.pagehelper.PageInfo;
import com.jcbjcb.tools.common.biz.BaseBiz;
import com.jcbjcb.tools.common.util.ObjectRestResponse;
import com.jcbjcb.tools.common.util.Query;
import com.jcbjcb.tools.common.util.TableRestResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

public class BaseController {
        @Autowired
        protected HttpServletRequest request;
        @Autowired
        protected Biz baseBiz;

        @RequestMapping(value = "",method = RequestMethod.POST)
        @ResponseBody
        public ObjectRestResponse add(@RequestBody Entity entity){
                baseBiz.insertSelective(entity);
                return new ObjectRestResponse<>(entity);
        }

        @RequestMapping(value = "/{id}",method = RequestMethod.GET)
        @ResponseBody
        public ObjectRestResponse get(@PathVariable int id){
                ObjectRestResponse entityObjectRestResponse = new ObjectRestResponse<>();
                Object o = baseBiz.selectById(id);
                entityObjectRestResponse.setData((Entity)o);
                return entityObjectRestResponse;
        }

        @RequestMapping(value = "/{id}",method = RequestMethod.PUT)
        @ResponseBody
        public ObjectRestResponse update(@RequestBody Entity entity){
                baseBiz.updateSelectiveById(entity);
                return new ObjectRestResponse<>(entity);
        }
        @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
        @ResponseBody
        public ObjectRestResponse remove(@PathVariable int id){
                baseBiz.deleteById(id);
                return new ObjectRestResponse<>();
        }

        @RequestMapping(value = "/all",method = RequestMethod.GET)
        @ResponseBody
        public List all(){
                return baseBiz.selectListAll();
        }

        @RequestMapping(value = "/page",method = RequestMethod.GET)
        @ResponseBody
        public TableRestResponse list(@RequestParam Map params){
                //查询列表数据
                Query query = new Query(params);
                PageInfo pageInfo = baseBiz.selectByQuery(query);
                return new TableRestResponse<>(pageInfo.getList());
        }
}

最后执行

mvn mybatis-generator:generate 生成代码

或者在idea上mvn插件里面生成


具体代码在 github 上
https://github.com/jcbjcb/jcbjcb-tools


网页标题:更强大的codegenerator自定义工具
网站URL:http://azwzsj.com/article/jeheoi.html

其他资讯