zoukankan      html  css  js  c++  java
  • idea配置easycode模板

      用到的框架:springboot+mybatis+lombok+mysql5.7

      使用mybatis开发的一大痛点就是搭建一些重复性的代码很多,相比起来的话用JPA就很容易了!所以我们需要简化mybatis的各种操作,其实就是自己生成一些代码;生成模板代码的方式有很多,如果是公司内部的话肯定也有自己的一套代码生成工具,但是我们平常写自己的项目的话,还是用自己喜欢的模板来写比较好!

      第一种:最原始的mybatis逆向工程的方式,这个应该很熟悉,就是可以自动生成表对应的po,mapper和xml

      第二种:就是用easycode插件直接生成表对应的controller,service,mapper和xml的常用的crud,减少代码量,用于我们平常自己写项目的时候比较实用;这也是下面会仔细说的

      第三种:有开源的项目可以直接一键生成vue和后端spingboot的所有代码,推荐人人开源的一个代码生成器,点击这里,有兴趣的可以用一下,贼好用,而且还有可视化页面!

      对于我们自己使用的话就没有必要那么华丽呼哨的了,这里就用第二种吧!,首先用spring Initializer随便创建一个springboot项目,务必使用阿里云的这个地址,spring的创建项目的东东贼鸡儿慢!!!

    ·  https://start.aliyun.com/

      使用idea连接mysql数据库,这个很容易,不在本篇教程之内,参考这个老哥的教程,连接成功之后是这个样子的(记得idea下载easycode插件然后重启):

      然后我们随便新建一张t_user表:

    create table t_user
    (
        id  int auto_increment primary key,
        username char(255)    null,
        password varchar(255) null
    )charset = utf8;

      然后根据:file->settings->Other Settings->Template Setting,新建分组,之后就是把我图中那些.java文件给创建出来(根据自己的需要进行修改生成规则,我也是参考别人的代码改了好多)

      

    entity.java

    ##引入宏定义
    $!define
    $!init
    
    ##使用宏定义设置回调(保存位置与文件后缀)
    #save("/entity", ".java")
    
    ##使用宏定义设置包后缀
    #setPackageSuffix("entity")
    
    ##使用全局变量实现默认包导入
    $!autoImport
    import java.io.Serializable;
    import lombok.Data;
    
    ##使用宏定义实现类注释信息
    #tableComment("实体类")
    @Data
    public class $!{tableInfo.name} implements Serializable {
        private static final long serialVersionUID = $!tool.serial();
    #foreach($column in $tableInfo.fullColumn)
        #if(${column.comment})/**
        * ${column.comment}
        */#end
    
        private $!{tool.getClsNameByFullName($column.type)} $!{column.name};
    #end
    }
    View Code

    controller.java

    ##定义初始变量
    #set($tableName = $tool.append($tableInfo.name, "Controller"))
    ##设置回调
    $!callback.setFileName($tool.append($tableName, ".java"))
    $!callback.setSavePath($tool.append($tableInfo.savePath, "/controller"))
    ##拿到主键
    #if(!$tableInfo.pkColumn.isEmpty())
        #set($pk = $tableInfo.pkColumn.get(0))
    #end
    
    #if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}controller;
    
    import lombok.extern.slf4j.Slf4j;
    import com.github.pagehelper.PageInfo;
    import $!{tableInfo.savePackageName}.response.PageResult;
    import $!{tableInfo.savePackageName}.response.Result;
    import $!{tableInfo.savePackageName}.response.StatusCode;
    import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
    import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
    import org.springframework.web.bind.annotation.*;
    import org.springframework.util.CollectionUtils;
    import javax.annotation.Resource;
    import java.util.List;
    import java.util.Objects;
    
    /**
     * $!{tableInfo.comment}($!{tableInfo.name})控制层
     *
     * @author protagonist
     * @since $!time.currTime()
     */
    @RestController
    @Slf4j
    @RequestMapping("/$!tool.firstLowerCase($tableInfo.name)")
    public class $!{tableName} {
        /**
         * 服务对象
         */
        @Resource
        private $!{tableInfo.name}Service $!tool.firstLowerCase($tableInfo.name)ServiceImpl;
    
        /**
         * 通过主键查询单条数据
         *
         * @param $!pk.name 主键
         * @return 单条数据
         */
        @GetMapping(value = "/get/{$!pk.name}")
        public Result selectOne(@PathVariable("$!pk.name") $!pk.shortType $!pk.name) {
            $tableInfo.name result = $!{tool.firstLowerCase($tableInfo.name)}ServiceImpl.selectById(id);
            if(Objects.nonNull(result)){
                return new Result<>(true,StatusCode.OK,"查询成功",result);
            }
            return new Result<>(true,StatusCode.ERROR,"查询失败");
        }
        
        /**
         * 新增一条数据
         *
         * @param $!tool.firstLowerCase($tableInfo.name) 实体类
         * @return Result对象
         */
        @PostMapping(value = "/insert")
        public Result insert(@RequestBody $tableInfo.name $!tool.firstLowerCase($tableInfo.name)) {
            int result = $!{tool.firstLowerCase($tableInfo.name)}ServiceImpl.insert($!tool.firstLowerCase($tableInfo.name));
            if (result > 0) {
              return new Result<>(true,StatusCode.OK,"新增成功",result);
            }
            return new Result<>(true,StatusCode.ERROR,"新增失败"); 
        }
    
        /**
         * 修改一条数据
         *
         * @param $!tool.firstLowerCase($tableInfo.name) 实体类
         * @return Result对象
         */
        @PutMapping(value = "/update")
        public Result update(@RequestBody $tableInfo.name $!tool.firstLowerCase($tableInfo.name)) {
            $tableInfo.name result = $!{tool.firstLowerCase($tableInfo.name)}ServiceImpl.update($!tool.firstLowerCase($tableInfo.name));
            if (Objects.nonNull(result)) {
              return new Result<>(true,StatusCode.OK,"修改成功",result);
            }
            return new Result<>(true,StatusCode.ERROR,"修改失败");
        }
    
        /**
         * 删除一条数据
         *
         * @param $!pk.name 主键
         * @return Result对象
         */
        @DeleteMapping(value = "/delete/{$!pk.name}")
        public Result delete(@PathVariable("$!pk.name") $!pk.shortType $!pk.name) {
            int result = $!{tool.firstLowerCase($tableInfo.name)}ServiceImpl.deleteById($!pk.name);
            if (result > 0) {
              return new Result<>(true,StatusCode.OK,"删除成功",result);
            }
            return new Result<>(true,StatusCode.ERROR,"删除失败");
        }
    
        /**
         * 查询全部
         *
         * @return Result对象
         */
        @GetMapping(value = "/selectAll")
        public Result<List<$tableInfo.name>> selectAll() {
            List<$tableInfo.name> $!tool.firstLowerCase($tableInfo.name)s = $!{tool.firstLowerCase($tableInfo.name)}ServiceImpl.selectAll();
            if (CollectionUtils.isEmpty($!tool.firstLowerCase($tableInfo.name)s)) {
                return new Result<>(true,StatusCode.ERROR,"查询全部数据失败");       
            }
            return new Result<>(true,StatusCode.OK,"查询全部数据成功",$!tool.firstLowerCase($tableInfo.name)s);
            
        }
    
        /**
         * 分页查询
         *
         * @param current 当前页  第零页和第一页的数据是一样
         * @param size 每一页的数据条数
         * @return Result对象
         */
        @GetMapping(value = "/selectPage/{current}/{size}")
        public Result selectPage(@PathVariable("current") Integer current,@PathVariable("size") Integer size) {
            PageInfo<$tableInfo.name> page = $!{tool.firstLowerCase($tableInfo.name)}ServiceImpl.selectPage(current, size);
            if (Objects.nonNull(page)) {
                return new Result<>(true,StatusCode.OK,"分页条件查询成功",new PageResult<>(page.getTotal(),page.getList()));
            }
            return new Result<>(true,StatusCode.ERROR,"分页查询数据失败");
        }
        
    }
    View Code

     service.java

    ##定义初始变量
    #set($tableName = $tool.append($tableInfo.name, "Service"))
    ##设置回调
    $!callback.setFileName($tool.append($tableName, ".java"))
    $!callback.setSavePath($tool.append($tableInfo.savePath, "/service"))
    
    ##拿到主键
    #if(!$tableInfo.pkColumn.isEmpty())
        #set($pk = $tableInfo.pkColumn.get(0))
    #end
    
    #if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}service;
    
    import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
    import java.util.List;
    import com.github.pagehelper.PageInfo;
    
    /**
     * $!{tableInfo.comment}($!{tableInfo.name})表服务接口
     *
     * @author protagonist
     * @since $!time.currTime()
     */
    public interface $!{tableName} {
    
        /**
         * 通过ID查询单条数据
         *
         * @param $!pk.name 主键
         * @return 实例对象
         */
        $!{tableInfo.name} selectById($!pk.shortType $!pk.name);
    
        /**
         * 分页查询
         *
         * @param current 当前页
         * @param size 每一页数据的条数
         * @return 对象列表
         */
        PageInfo<$!{tableInfo.name}> selectPage(int current, int size);
    
        /**
         * 查询全部
         *
         * @return 对象列表
         */
        List<$!{tableInfo.name}> selectAll();
        
        /**
         * 通过实体作为筛选条件查询
         *
         * @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象
         * @return 对象列表
         */
        List<$!{tableInfo.name}> selectList($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
    
        /**
         * 新增数据
         *
         * @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象
         * @return 影响行数
         */
        int insert($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
        
        /**
         * 批量新增
         *
         * @param $!tool.firstLowerCase($!{tableInfo.name})s 实例对象的集合
         * @return 影响行数
         */
        int batchInsert(List<$!{tableInfo.name}> $!tool.firstLowerCase($!{tableInfo.name})s);
        
        /**
         * 修改数据
         *
         * @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象
         * @return 修改
         */
        $!{tableInfo.name} update($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
    
        /**
         * 通过主键删除数据
         *
         * @param $!pk.name 主键
         * @return 影响行数
         */
        int deleteById($!pk.shortType $!pk.name);
        
        /**
         * 查询总数据数
         *
         * @return 数据总数
         */
        int count();
    }
    View Code

    serviceImpl.java

    ##定义初始变量
    #set($tableName = $tool.append($tableInfo.name, "ServiceImpl"))
    ##设置回调
    $!callback.setFileName($tool.append($tableName, ".java"))
    $!callback.setSavePath($tool.append($tableInfo.savePath, "/service/impl"))
    
    ##拿到主键
    #if(!$tableInfo.pkColumn.isEmpty())
        #set($pk = $tableInfo.pkColumn.get(0))
    #end
    
    #if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}service.impl;
    
    import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
    import $!{tableInfo.savePackageName}.dao.$!{tableInfo.name}Dao;
    import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
    import org.springframework.transaction.annotation.Transactional;
    import org.springframework.stereotype.Service;
    import com.github.pagehelper.PageHelper;
    import com.github.pagehelper.PageInfo;
    
    import javax.annotation.Resource;
    import java.util.List;
    
    
    /**
     * $!{tableInfo.comment}($!{tableInfo.name}表)服务实现类
     *
     * @author protagonist
     * @since $!time.currTime()
     */
    @Service("$!tool.firstLowerCase($!{tableInfo.name})ServiceImpl")
    public class $!{tableName} implements $!{tableInfo.name}Service {
        @Resource
        private $!{tableInfo.name}Dao $!tool.firstLowerCase($!{tableInfo.name})Dao;
    
        /**
         * 通过ID查询单条数据
         *
         * @param $!pk.name 主键
         * @return 实例对象
         */
        @Override
        public $!{tableInfo.name} selectById($!pk.shortType $!pk.name) {
            return this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.selectById($!pk.name);
        }
    
        /**
         * 分页查询
         *
         * @param current 当前页
         * @param size 每一页的条数
         * @return 对象列表
         */
        @Override
        public PageInfo<$!{tableInfo.name}> selectPage(int current, int size) {
            PageHelper.startPage(current,size);
            List<$!{tableInfo.name}> dataList = $!{tool.firstLowerCase($!{tableInfo.name})}Dao.selectAll();
            return new PageInfo<>(dataList);
        }
    
        /**
         * 查询所有
         *
         * @return 实例对象的集合
         */
         @Override
         public List<$!{tableInfo.name}> selectAll() {
            return this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.selectAll();
         }
         
        /**
         * 根据条件查询
         *
         * @return 实例对象的集合
         */
        @Override
        public List<$!{tableInfo.name}> selectList($!{tableInfo.name} $!{tool.firstLowerCase($!{tableInfo.name})}) {
            return this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.selectList($!{tool.firstLowerCase($!{tableInfo.name})});
        }
        
        /**
         * 新增数据
         *
         * @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象
         * @return 实例对象
         */
        @Override
        @Transactional
        public int insert($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) {
            return this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.insert($!tool.firstLowerCase($!{tableInfo.name}));
        }
    
        /**
         * 批量新增
         *
         * @param $!tool.firstLowerCase($!{tableInfo.name})s 实例对象的集合
         * @return 生效的条数
         */
        @Override
        @Transactional
        public int batchInsert(List<$!{tableInfo.name}> $!tool.firstLowerCase($!{tableInfo.name})s) {
            return this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.batchInsert($!tool.firstLowerCase($!{tableInfo.name})s);
        }
    
        /**
         * 修改数据
         *
         * @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象
         * @return 实例对象
         */
        @Override
        @Transactional
        public $!{tableInfo.name} update($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) {
            this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.update($!tool.firstLowerCase($!{tableInfo.name}));
            return this.selectById($!{tool.firstLowerCase($!{tableInfo.name})}.get$!tool.firstUpperCase($pk.name)());
        }
    
        /**
         * 通过主键删除数据
         *
         * @param $!pk.name 主键
         * @return 是否成功
         */
        @Override
        @Transactional
        public int deleteById($!pk.shortType $!pk.name) {
            return this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.deleteById($!pk.name);
        }
        
        /**
         * 查询总数据数
         *
         * @return 数据总数
         */
         @Override
         public int count(){
            return this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.count();
         }
    }
    View Code

     dao.java

    ##定义初始变量
    #set($tableName = $tool.append($tableInfo.name, "Dao"))
    ##设置回调
    $!callback.setFileName($tool.append($tableName, ".java"))
    $!callback.setSavePath($tool.append($tableInfo.savePath, "/dao"))
    
    ##拿到主键
    #if(!$tableInfo.pkColumn.isEmpty())
        #set($pk = $tableInfo.pkColumn.get(0))
    #end
    
    #if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}dao;
    
    import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
    import org.apache.ibatis.annotations.Mapper;
    import org.apache.ibatis.annotations.Param;
    import java.util.List;
    
    /**
     * $!{tableInfo.comment}($!{tableInfo.name})表数据库访问层
     *
     * @author protagonist
     * @since $!time.currTime()
     */
     @Mapper
    public interface $!{tableName} {
    
        /**
         * 通过ID查询单条数据
         *
         * @param $!pk.name 主键
         * @return 实例对象
         */
        $!{tableInfo.name} selectById($!pk.shortType $!pk.name);
        
        /**
         * 查询全部
         *
         * @return 对象列表
         */
        List<$!{tableInfo.name}> selectAll();
        
        /**
         * 通过实体作为筛选条件查询
         *
         * @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象
         * @return 对象列表
         */
        List<$!{tableInfo.name}> selectList($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
    
        /**
         * 新增数据
         *
         * @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象
         * @return 影响行数
         */
        int insert($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
        
        /**
         * 批量新增
         *
         * @param $!tool.firstLowerCase($!{tableInfo.name})s 实例对象的集合
         * @return 影响行数
         */
        int batchInsert(List<$!{tableInfo.name}> $!tool.firstLowerCase($!{tableInfo.name})s);
        
        /**
         * 修改数据
         *
         * @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象
         * @return 影响行数
         */
        int update($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
    
        /**
         * 通过主键删除数据
         *
         * @param $!pk.name 主键
         * @return 影响行数
         */
        int deleteById($!pk.shortType $!pk.name);
    
        /**
         * 查询总数据数
         *
         * @return 数据总数
         */
        int count();
    }
    View Code

     mapper.xml

    ##引入mybatis支持
    $!mybatisSupport
    
    ##设置保存名称与保存位置
    $!callback.setFileName($tool.append($!{tableInfo.name}, "Dao.xml"))
    $!callback.setSavePath($tool.append($modulePath, "/src/main/resources/mapper"))
    
    ##拿到主键
    #if(!$tableInfo.pkColumn.isEmpty())
        #set($pk = $tableInfo.pkColumn.get(0))
    #end
    
    <?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="$!{tableInfo.savePackageName}.dao.$!{tableInfo.name}Dao">
        <!-- 结果集 -->
        <resultMap type="$!{tableInfo.savePackageName}.entity.$!{tableInfo.name}" id="$!{tableInfo.name}Map">
    #foreach($column in $tableInfo.fullColumn)
            <result property="$!column.name" column="$!column.obj.name" jdbcType="$!column.ext.jdbcType"/>
    #end
        </resultMap>
        
        <!-- 基本字段 -->
        <sql id="Base_Column_List">
            #allSqlColumn()
        </sql>
        
        <!-- 查询单个 -->
        <select id="selectById" resultMap="$!{tableInfo.name}Map">
            select
              <include refid="Base_Column_List" />
            from $!tableInfo.obj.name
            where $!pk.obj.name = #{$!pk.name}
        </select>
    
        <!-- 查询全部 -->
        <select id="selectAll" resultMap="$!{tableInfo.name}Map">
            select
            <include refid="Base_Column_List" />
            from $!tableInfo.obj.name
        </select>
    
        <!--通过实体作为筛选条件查询-->
        <select id="selectList" resultMap="$!{tableInfo.name}Map">
            select
            <include refid="Base_Column_List" />
            from $!tableInfo.obj.name
            <where>
            #foreach($column in $tableInfo.fullColumn)
                <if test="$!column.name != null#if($column.type.equals("java.lang.String")) and $!column.name != ''#end">
                    and $!column.obj.name = #{$!column.name}
                </if>
            #end
            </where>
        </select>
    
        <!-- 新增所有列 -->
        <insert id="insert" keyProperty="$!pk.name" useGeneratedKeys="true">
            insert into $!{tableInfo.obj.name}(#foreach($column in $tableInfo.fullColumn)$!column.obj.name#if($velocityHasNext), #end#end)
            values ( #foreach($column in $tableInfo.fullColumn)#{$!{column.name}}#if($velocityHasNext), #end#end)
        </insert>
        
        <!-- 批量新增 -->
        <insert id="batchInsert">
            insert into $!{tableInfo.obj.name}(#foreach($column in $tableInfo.fullColumn)$!column.obj.name#if($velocityHasNext), #end#end)
            values 
            <foreach collection="$!tool.firstLowerCase($!{tableInfo.name})s" item="item" index="index" separator=",">
            (
                #foreach($column in $tableInfo.fullColumn)
                #{item.$!{column.name}}#if($velocityHasNext), #end
    #end
             )
             </foreach>
        </insert>
    
        <!-- 通过主键修改数据 -->
        <update id="update">
            update $!{tableInfo.obj.parent.name}.$!{tableInfo.obj.name}
            <set>
            #foreach($column in $tableInfo.otherColumn)
                <if test="$!column.name != null#if($column.type.equals("java.lang.String")) and $!column.name != ''#end">
                    $!column.obj.name = #{$!column.name},
                </if>
            #end
            </set>
            where $!pk.obj.name = #{$!pk.name}
        </update>
    
        <!--通过主键删除-->
        <delete id="deleteById">
            delete from $!{tableInfo.obj.name} where $!pk.obj.name = #{$!pk.name}
        </delete>
        
        <!-- 总数 -->
        <select id="count" resultType="int">
            select count(*) from $!{tableInfo.obj.name}
        </select>
    </mapper>
    View Code

     Result.java

    ##设置回调
    $!callback.setFileName($tool.append("Result", ".java"))
    $!callback.setSavePath($tool.append($tableInfo.savePath, "/response"))
    #if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}response;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    /**用于封装返回给前端的结果
      * @author protagonist
      * @title: StatusCode
      * @description: TODO
      * @date $!time.currTime()
     */
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class Result<T> {
        private boolean flag;
        private Integer code;
        private String message;
        private T data;
    
        public Result(boolean flag, int code, String message) {
            this.flag = flag;
            this.code = code;
            this.message = message;
        }
    }
    View Code

     PageResult.java

    ##设置回调
    $!callback.setFileName($tool.append("PageResult", ".java"))
    $!callback.setSavePath($tool.append($tableInfo.savePath, "/response"))
    
    #if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}response;
    ##使用全局变量实现默认包导入
    $!autoImport
    import lombok.Data;
    import java.util.List;
    
    /**分页结果对象
     * @author protagonist
     * @title: PageResult
     * @description: TODO
     * @date $!time.currTime()
     */
    @Data
    public class PageResult<T> {
        private Long total;
        private List<T> rows;
        public PageResult(Long total, List<T> rows) {
            this.total = total;
            this.rows = rows;
        }
    
    }
    View Code

     StateCode.java

    ##设置回调
    $!callback.setFileName($tool.append("StatusCode", ".java"))
    $!callback.setSavePath($tool.append($tableInfo.savePath, "/response"))
    #if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}response;
    
    /**错误码常量类
     * @author protagonist
     * @title: StatusCode
     * @description: TODO
     * @date $!time.currTime()
     */
    public class StatusCode {
        public static final int OK=20000;//成功
        public static final int ERROR =20001;//失败
        public static final int LOGINERROR =20002;//用户名或密码错误
        public static final int ACCESSERROR =20003;//权限不足
        public static final int REMOTEERROR =20004;//远程调用失败
        public static final int REPERROR =20005;//重复操作
    }
    View Code

      还有个地方可能需要配置一下,因为数据库中有的表有前缀,比如t_user这种,我们可能想把前缀干掉,如下所示:

    ## 去掉前缀
    #if($tableInfo.obj.name.startsWith("t_"))
        $!tableInfo.setName($tool.getClassName($tableInfo.obj.name.substring(2)))
    #end

        到这里其实就已经结束了,之后填写基本的信息,然后就是狂点yes,代码就生成了;

     

      生成代码之后的目录是这样的,我们再把mybatis需要的分页插件pageheper依赖导入,然后连接数据库的配置文件弄一下,就ok了;

     

    properties配置文件:

    # 应用名称
    spring.application.name=template
    # 应用服务 WEB 访问端口
    server.port=8090
    # spring 静态资源扫描路径
    spring.resources.static_locations=classpath:/static/
    
    #分页插件
    pagehelper.helper-dialect=mysql
    pagehelper.reasonable=true
    pagehelper.support-methods-arguments=true
    pagehelper.params=count=countSql
    
    #配置Mysql连接
    spring.datasource.url=jdbc:mysql://localhost:3306/white_jotter?characterEncoding=UTF-8
    spring.datasource.username=root
    spring.datasource.password=123456
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    #配置mybatis
    #配置实体类别名
    mybatis.type-aliases-package=com/protagonist/template/entity
    #配置xml映射路径
    mybatis.mapper-locations=classpath*:/mapper/**.xml
    #开启驼峰命名法
    mybatis.configuration.map-underscore-to-camel-case=true

      pom.xml把下面几个关键的导入就行了

     <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.21</version>
            </dependency>
            <!--  分页插件 -->
           <dependency>
             <groupId>com.github.pagehelper</groupId>
             <artifactId>pagehelper-spring-boot-starter</artifactId>
             <version>1.2.5</version>
           </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.16.10</version>
            </dependency>

     启动项目,访问controller,来个分页查询,可以看到返回的数据格式这就是通用的,前端只需要判断状态码然后拿数据就行了;

       别看这个博客很多,其实很少,有兴趣的可以把模板代码根据自己的需要进行魔改,还可以多加几个自己觉得常用的方法,而且这里还是有点问题,应该再来一个VO包,前后端的交互的VO应该放在这里,和entity中的类区分开,这里就不弄这么麻烦了,自己魔改吧!

      再说一点,其实这种方式生成的代码还是比较多,如果你会使用mybatisplus的话,那自己修改一下模板,生成springboot+mybatisplus+mybatis+lombok+mysql的代码,生成代码就更少了,有兴趣的可以自己弄一下!!!

      一定要有耐心弄一个适合自己的模板,这样以后直接用很方便,不然看别人的代码每个人的习惯都不一样,很难受!!!

  • 相关阅读:
    oc中 中文到拼音的转换
    ios 添加全屏返回手势
    自我总结- CGAffineTransform
    解决pod search出来的库不是最新
    四舍五入的函数,保留小数点后几位数不四舍五入
    iOS 键盘变中文
    LanguageImage尺寸
    打包上传64位支持的解决办法
    第1年11月2日 ssh分发秘钥时出现错误“Permission denied (publickey,gssapi-keyex,gssapi-with-mic)” yarn
    第1年11月1日 uniapp原生
  • 原文地址:https://www.cnblogs.com/wyq1995/p/13742075.html
Copyright © 2011-2022 走看看