-
再resources文件下创建fileTemplates/internal文件夹(必须是这个)
-
添加模板文件 mapper.tsx.ft
<?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="${NAMESPACE}" > </mapper>
-
创建右键菜单mapper模板创建
//读取模板 FileTemplate template = FileTemplateManager.getInstance(project) .getInternalTemplate("mapper"); Properties properties = new Properties(); //替换模板里的参数 properties.setProperty("NAMESPACE", ""); String renderedText = ""; try { renderedText += template.getText(properties); } catch (IOException ex) { throw new RuntimeException(ex); } PsiFile psiFile = PsiFileFactory.getInstance(project).createFileFromText("mapper.xml", XMLLanguage.INSTANCE, renderedText); System.out.println(psiFile.isPhysical()); try { //执行写入文件 Runnable runnable = () -> psiElement.add(psiFile); //调度任务 WriteCommandAction.runWriteCommandAction(project, runnable); } catch (Exception exception) { //文件存在,会抛错;idea发送通知 NotificationGroup notificationGroup = new NotificationGroup("testid", NotificationDisplayType.BALLOON, false); Notification notification = notificationGroup.createNotification("File already exists", MessageType.INFO); Notifications.Bus.notify(notification); }