189 8069 5689

更强大的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自定义工具
新闻来源:http://cdxtjz.cn/article/jeheoi.html

联系我们

您好HELLO!
感谢您来到成都网站建设公司,若您有合作意向,请您为我们留言或使用以下方式联系我们, 我们将尽快给你回复,并为您提供真诚的设计服务,谢谢。
  • 电话:028- 86922220 18980695689
  • 商务合作邮箱:631063699@qq.com
  • 合作QQ: 532337155
  • 成都网站设计地址:成都市青羊区锣锅巷31号五金站写字楼6楼

小谭建站工作室

成都小谭网站建设公司拥有多年以上互联网从业经验的团队,始终保持务实的风格,以"帮助客户成功"为已任,专注于提供对客户有价值的服务。 我们已为众企业及上市公司提供专业的网站建设服务。我们不只是一家网站建设的网络公司;我们对营销、技术、管理都有自己独特见解,小谭建站采取“创意+综合+营销”一体化的方式为您提供更专业的服务!

小谭观点

相对传统的成都网站建设公司而言,小谭是互联网中的网站品牌策划,我们精于企业品牌与互联网相结合的整体战略服务。
我们始终认为,网站必须注入企业基因,真正使网站成为企业vi的一部分,让整个网站品牌策划体系变的深入而持久。