亲宝软件园·资讯

展开

mybatisx 插件使用

o_瓜田李下_o 人气:0

MybatisX 是一款基于 IDEA 的快速开发插件,为效率而生。今天给大家介绍下mybatisx 插件使用。

mybatisx 插件使用

官网:https://baomidou.com/pages/ba5b24

插件安装

file ==> preferences ==> plugins:搜索mybatisx,安装插件

mybatisx 功能:

文件跳转:点击图标,可实现mapper接口、对应xml之间的互相跳转,serverImpl层跳转到注入的mapper接口

代码自动生成:根据表自动生成实体类、mapper接口、mapper xml、service、serverImpl类,模板可自定义

mapper方法自动填充:mapper接口中输入部分方法名,可自动补全代码,并在对应的mapper xml中生成sql语句

自动生成代码

连接数据源

mybatisx-generator 自动生成代码

说明:实体类的包名、类名,moduler path、base path、package name均可手动编辑

查看自动生成的代码

文件跳转

mapper接口:点击图标,跳转mapper xml文件

mapper xml:点击图标,跳转到mapper接口

PersonServiceImpl:点击图标,跳转到mapper接口

代码自动补全

mapper 接口写出方法名 ==> 右击 ==> show context actions

generate mybatis sql

mapper xml生成的sql

<?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="com.example.demo.mapper.PersonMapper">
    <resultMap id="BaseResultMap" type="com.example.demo.pojo.Person">
            <id property="id" column="id" jdbcType="INTEGER"/>
            <result property="name" column="name" jdbcType="VARCHAR"/>
            <result property="age" column="age" jdbcType="INTEGER"/>
    </resultMap>
    <sql id="Base_Column_List">
        id,name,age
    </sql>
    <!-- mapper xml自动生成的sql语句 -->
    <select id="selectAllByName" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from person
        where
        name = #{name,jdbcType=VARCHAR}
    </select>
</mapper>

自动补全操作示例

自定义模板

mybatisx 模板:可对模板(ftl)进行修改、并恢复默认设置

.meta.xml

<?xml version="1.0" encoding="utf-8" ?>
<templates>
    <template>
        <property name="configName" value="serviceInterface"/>
        <property name="configFile" value="serviceInterface.ftl"/>
        <property name="fileName" value="${domain.fileName}Service"/>
        <property name="suffix" value=".java"/>
        <property name="packageName" value="${domain.basePackage}.service"/>
        <property name="encoding" value="${domain.encoding}"/>
        <property name="basePath" value="${domain.basePath}"/>
    </template>
    <template>
        <property name="configName" value="serviceImpl"/>
        <property name="configFile" value="serviceImpl.ftl"/>
        <property name="fileName" value="${domain.fileName}ServiceImpl"/>
        <property name="suffix" value=".java"/>
        <property name="packageName" value="${domain.basePackage}.service.impl"/>
        <property name="encoding" value="${domain.encoding}"/>
        <property name="basePath" value="${domain.basePath}"/>
    </template>
    <template>
        <property name="configName" value="mapperInterface"/>
        <property name="configFile" value="mapperInterface.ftl"/>
        <property name="fileName" value="${domain.fileName}Mapper"/>
        <property name="suffix" value=".java"/>
        <property name="packageName" value="${domain.basePackage}.mapper"/>
        <property name="encoding" value="${domain.encoding}"/>
        <property name="basePath" value="${domain.basePath}"/>
    </template>
    <template>
        <property name="configName" value="mapperXml"/>
        <property name="configFile" value="mapperXml.ftl"/>
        <property name="fileName" value="${domain.fileName}Mapper"/>
        <property name="suffix" value=".xml"/>
        <property name="packageName" value="mapper"/>
        <property name="encoding" value="${domain.encoding}"/>
        <property name="basePath" value="src/main/resources"/>
    </template>
</templates>

mapperInterface.ftl

package ${mapperInterface.packageName};
import ${tableClass.fullClassName};
<#if tableClass.pkFields??>
    <#list tableClass.pkFields as field><#assign pkName>${field.shortTypeName}</#assign></#list>
</#if>
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
* @author ${author!}
* @createDate ${.now?string('yyyy-MM-dd HH:mm:ss')}
*/
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}</#assign>
        </#if>
    </#list>
</#if>
import com.baomidou.mybatisplus.extension.service.IService;
/**
* @author ${author!}
* @description 针对表【${tableClass.tableName}<#if tableClass.remark?has_content>(${tableClass.remark!})</#if>】的数据库操作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}</#assign>
        </#if>
    </#list>
</#if>
import org.springframework.stereotype.Service;
/**
* @author ${author!}
* @description 针对表【${tableClass.tableName}<#if tableClass.remark?has_content>(${tableClass.remark!})</#if>】的数据库操作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

<?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="${mapperInterface.packageName}.${baseInfo.fileName}">
    <resultMap id="BaseResultMap" type="${tableClass.fullClassName}">
        <#list tableClass.pkFields as field>
            <id property="${field.fieldName}" column="${field.columnName}" jdbcType="${field.jdbcType}"/>
        </#list>
        <#list tableClass.baseFields as field>
            <result property="${field.fieldName}" column="${field.columnName}" jdbcType="${field.jdbcType}"/>
        </#list>
    </resultMap>
    <sql id="Base_Column_List">
        <#list tableClass.allFields as field>${field.columnName}<#sep>,<#if field_index%3==2>${"\n        "}</#if></#list>
    </sql>
</mapper>

模版文件恢复为默认设置

加载全部内容

相关教程
猜你喜欢
用户评论