IDEA使用MybatisX插件初始化代码

数据库建模后可以通过idea的MybatisX插件生成数据模型初始化代码,代码在这个路径下

IDEA使用MybatisX插件初始化代码_第1张图片

 

.meta.xml用于编辑需要生成哪些代码文件,xxx.ftl为生成代码文件的模板,配置好模板后在idea配置Database连接,连上数据库,选择要生成代码的表(可多选),点击MybatisX-Generator

IDEA使用MybatisX插件初始化代码_第2张图片

 

module path选择需要生成代码的项目,base package为代码的所在包,ignore table prefix可以选择忽略表前缀,relative package为默认实体所在包

IDEA使用MybatisX插件初始化代码_第3张图片

 

可以从config name中删除不需要生成的代码文件

IDEA使用MybatisX插件初始化代码_第4张图片

 

点击 finish 按各个路径生产好初始化代码文件

ftl语法可以参考:ftl常用语法整理 - 简书

                            FTL语法使用_WWW_LJJ的博客-CSDN博客_ftl 字符串

注意:如果路径上有同名文件会被覆盖

.meta.xml



    
    
    
    
    

domain.ftl 

package ${domain.packageName};

import java.io.Serializable;
import java.util.*;
import lombok.Data;
import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.extension.activerecord.Model;

/**
* @author ${author!}
* @description 表【${tableClass.tableName}<#if tableClass.remark?has_content>(${tableClass.remark!})】的实体类
* @createDate ${.now?string('yyyy-MM-dd HH:mm:ss')}
* @Entity ${tableClass.fullClassName}
*/
@Data
public class ${domain.fileName} extends Model<${domain.fileName}> implements Serializable {

<#list tableClass.allFields as field>
    /**
    * ${field.remark!}
    */<#if field.fieldName=="id" || field.fieldName=="revision">${"\n    "}<#if field.fieldName=="id">@TableId(type= IdType.AUTO)<#if field.fieldName=="revision">@Version
    private ${field.shortTypeName} ${field.fieldName};


}

mapperInterface.ftl

package ${mapperInterface.packageName};

import ${tableClass.fullClassName};
<#if tableClass.pkFields??>
    <#list tableClass.pkFields as field><#assign pkName>${field.shortTypeName}

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.springframework.stereotype.Repository;

/**
* @author ${author!}
* @description 针对表【${tableClass.tableName}<#if tableClass.remark?has_content>(${tableClass.remark!})】的数据库操作Mapper
* @createDate ${.now?string('yyyy-MM-dd HH:mm:ss')}
* @Entity ${tableClass.fullClassName}
*/
@Repository
public interface ${mapperInterface.fileName} extends BaseMapper<${tableClass.shortClassName}> {

}




serviceInterface.ftl

package ${baseInfo.packageName};

import ${tableClass.fullClassName};
<#if baseService??&&baseService!="">
import ${baseService};
    <#list baseService?split(".") as simpleName>
        <#if !simpleName_has_next>
            <#assign serviceSimpleName>${simpleName}
        
    

import com.baomidou.mybatisplus.extension.service.IService;

/**
* @author ${author!}
* @description 针对表【${tableClass.tableName}<#if tableClass.remark?has_content>(${tableClass.remark!})】的数据库操作Service
* @createDate ${.now?string('yyyy-MM-dd HH:mm:ss')}
*/
public interface ${baseInfo.fileName} extends IService<${tableClass.shortClassName}> {

}

 serviceImpl.ftl

package ${baseInfo.packageName};

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import ${tableClass.fullClassName};
import ${serviceInterface.packageName}.${serviceInterface.fileName};
import ${mapperInterface.packageName}.${mapperInterface.fileName};
<#if baseService??&&baseService!="">
import ${baseService};
    <#list baseService?split(".") as simpleName>
        <#if !simpleName_has_next>
            <#assign serviceSimpleName>${simpleName}
        
    

import org.springframework.stereotype.Service;

/**
* @author ${author!}
* @description 针对表【${tableClass.tableName}<#if tableClass.remark?has_content>(${tableClass.remark!})】的数据库操作Service实现
* @createDate ${.now?string('yyyy-MM-dd HH:mm:ss')}
*/
@Service
public class ${baseInfo.fileName} extends ServiceImpl<${mapperInterface.fileName}, ${tableClass.shortClassName}>
    implements ${serviceInterface.fileName}{

}




mapperXml.ftl





    
        <#list tableClass.pkFields as field>
            
        
        <#list tableClass.baseFields as field>
            
        
    

    
        <#list tableClass.allFields as field>${field.columnName}<#sep>,<#if field_index%3==2>${"\n        "}
    

 

 

你可能感兴趣的:(Java,intellij-idea,java,ide)