current position:Home>Easy code plug-in (with spring data JPA generation template)

Easy code plug-in (with spring data JPA generation template)

2022-01-26 23:41:39 Cherry blowing snow_ Snowy

One 、 summary

EasyCode It can be generated automatically according to the table :entity,dao,service,serviceImpl,controller wait

Official documents :gitee.com/makejava/Ea…

Two 、 install

1. Installing a plug-in

stay File --> setting --> Plugins --> Marketplace in , Search for easy code, Click on Install Installation , After installation, you need to restart IDEA  Insert picture description here

2. Connect to database

(a) open Database, If IDEA The right sidebar can be clicked directly if there is any , If not, click on... In the following way :  Insert picture description here (b) Click on the plus sign , Select data source , Choose here MySql, Other database configurations are similar  Insert picture description here (c) add to JDBC drive , Select drive jar The location of the package  Insert picture description here (d) Configuration database  Insert picture description here

3、 ... and 、 The generated code

After successfully connecting to the database , Select the table to generate , Right mouse click , choice EasyCode --> Generate Code Used to generate code ,Config Table Used to configure tables  Insert picture description here Click on Generate Code after , The following window will pop up :  Insert picture description here

Four 、 To configure EasyCode

stay File --> Settings --> Other Settings --> Easy Code Chinese vs EasyCode To configure

  • Type Mapper: Type mapping , Configure the field types in the database ( regular expression ) And java Mapping of attribute types of objects
  • Template Setting: Configure template information , Installed by default EasyCode after , Automatic with two sets of templates , One is the default , One is MybatisPuls( It will be slightly modified later ), The next chapter provides Spring Data JPA Templates
  • Global Config: Global configuration , You can reference global variables in the template

5、 ... and 、Spring Data JPA Templates

For generating entity classes, please refer to :Spring Data JPA Detailed introduction

1. controller class

## Define the initial variable 
#set($tableName = $tool.append($tableInfo.name, "Controller"))
## Set callback 
$!callback.setFileName($tool.append($tableName, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/controller"))
## Get the primary key 
#if(!$tableInfo.pkColumn.isEmpty())
    #set($pk = $tableInfo.pkColumn.get(0))
#end

#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}controller;

import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/** * $!{tableInfo.comment}($!{tableInfo.name}) Table control layer  * * @author $!author * @since $!time.currTime() */
@RestController
@RequestMapping("$!tool.firstLowerCase($tableInfo.name)")
public class $!{tableName} {
    /** *  service object  */
    @Resource
    private $!{tableInfo.name}Service $!tool.firstLowerCase($tableInfo.name)Service;

    /** *  Query single data through primary key  * * @param id  Primary key  * @return  A single data  */
    @GetMapping("selectOne")
    public $!{tableInfo.name} selectOne($!pk.shortType id) {
        return this.$!{tool.firstLowerCase($tableInfo.name)}Service.queryById(id);
    }

}

 Copy code 

2. service Interface

## Define the initial variable 
#set($tableName = $tool.append($tableInfo.name, "Service"))
## Set callback 
$!callback.setFileName($tool.append($tableName, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/service"))

## Get the primary key 
#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 org.springframework.data.domain.Page;
/** * $!{tableInfo.comment}($!{tableInfo.name}) Table service interface  * * @author zry * @since $!time.currTime() */
public interface $!{tableName} {
    $!{tableInfo.name} queryById($!pk.shortType $!pk.name);
    Page<$!{tableInfo.name}> queryAllByLimit(int offset, int limit);
    $!{tableInfo.name} insert($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
    $!{tableInfo.name} update($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
    boolean deleteById($!pk.shortType $!pk.name);
     List<$!{tableInfo.name}> getall();
}

 Copy code 

3. serviceImpl Implementation class

## Define the initial variable 
#set($tableName = $tool.append($tableInfo.name, "ServiceImpl"))
## Set callback 
$!callback.setFileName($tool.append($tableName, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/service/impl"))

## Get the primary key 
#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.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import javax.annotation.Resource;
import java.util.List;
import org.springframework.data.domain.Page;
/** * $!{tableInfo.comment}($!{tableInfo.name}) Table service implementation class  * * @author $!author * @since $!time.currTime() */
@Service("$!tool.firstLowerCase($!{tableInfo.name})Service")
public class $!{tableName} implements $!{tableInfo.name}Service {
    @Resource
    private $!{tableInfo.name}Dao $!tool.firstLowerCase($!{tableInfo.name})Dao;

    @Override
    public $!{tableInfo.name} queryById($!pk.shortType $!pk.name) {
        return this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.getOne($!pk.name);
    }

    @Override
    public List<$!{tableInfo.name}> getall() {
        return this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.findAll();

    }
    
     @Override
    public Page<$!{tableInfo.name}> queryAllByLimit(int offset, int limit) {
        return this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.findAll(PageRequest.of((offset-1)
*limit,limit));
    }

    @Override
    public $!{tableInfo.name} insert($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) {
       
        return this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.save($!tool.firstLowerCase($!{tableInfo.name}));
    }


    @Override
    public $!{tableInfo.name} update($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) {
       
        return this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.save($!tool.firstLowerCase($!{tableInfo.name}));
    }

  
    @Override
    public boolean deleteById($!pk.shortType $!pk.name) {
    
     try{
             this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.deleteById($!pk.name) ;
        }catch (Exception ex){
            return false;
        }
        return true;
      
    }
}

 Copy code 

4. dao Interface

## Define the initial variable 
#set($tableName = $tool.append($tableInfo.name, "Dao"))
## Set callback 
$!callback.setFileName($tool.append($tableName, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/dao"))

## Get the primary key 
#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 java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
/** * $!{tableInfo.comment}($!{tableInfo.name}) Table database access layer  * * @author zry * @since $!time.currTime() */
public interface $!{tableName} extends JpaRepository<$!{tableInfo.name} ,$!pk.shortType>{

  
}

 Copy code 

6、 ... and 、MyBatisPlus Template modification

When modifying , It is recommended to copy the original template for backup .

1. entity Entity class

The original entity It will produce a lot of getter and setter, Use here lombok To simplify the code

## Import macro definition 
$!define

## Save the file ( Macro definition )
#save("/entity", ".java")

## Package path ( Macro definition )
#setPackageSuffix("entity")


## Automatically import packages ( Global variables )
$!autoImport
##import com.baomidou.mybatisplus.extension.activerecord.Model;
import java.io.Serializable;
import lombok.Data;
##import com.baomidou.mybatisplus.annotation.IdType;
##import com.baomidou.mybatisplus.annotation.TableId;

## Table annotation ( Macro definition )
#tableComment(" Table entity class ")
@Data
@SuppressWarnings("serial")
public class $!{tableInfo.name} implements Serializable {
  
#foreach($column in $tableInfo.fullColumn)
    #if(${column.comment})/**${column.comment}*/#end

    private $!{tool.getClsNameByFullName($column.type)} $!{column.name};
#end

###foreach($column in $tableInfo.fullColumn)
##    #getSetMethod($column)
###end

###foreach($column in $tableInfo.pkColumn)
##    /** ## *  Get the primary key value  ## * ## * @return  Primary key value  ## */
##    @Override
##    protected Serializable pkVal() {
##        return this.$!column.name;
##    }
##    #break
###end
}
 Copy code 

2. Table "t_" start

The original template : When the table "t_" At the beginning , All generated classes and interfaces , with T start . Such as table t_user The generated entity class is TUser modify : Find global configuration Easy Code --> Global Config --> define file , Add the following code to the beginning of the file ( The code is also easier to understand , If other prefixes , Just a little modification ):

## Remove the... From the table t_ Prefix 
#if($tableInfo.obj.name.startsWith("t_"))
    $!tableInfo.setName($tool.getClassName($tableInfo.obj.name.substring(2)))
#end
 Copy code 

copyright notice
author[Cherry blowing snow_ Snowy],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201262341367771.html

Random recommended