├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── build.gradle ├── doc ├── README.md ├── error_sample1.png ├── error_sample2.png ├── generate.gif ├── inspect.gif ├── inspect_sample2.gif ├── inspect_sample3.gif ├── navi.gif ├── sample1.gif └── sample_new.gif ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle ├── src └── main │ ├── java │ └── com │ │ └── github │ │ └── mustfun │ │ └── mybatis │ │ └── plugin │ │ ├── action │ │ ├── GenerateCodeMainActionGroup.java │ │ ├── GenerateCodeUIAction.java │ │ ├── InsertDBConfigAction.java │ │ ├── MybatisTypedHandler.java │ │ ├── ProjectRightPopupAction.java │ │ └── TemplateEditMenuAction.java │ │ ├── agent │ │ ├── MainAdapter.java │ │ └── SqlSessionTransformer.java │ │ ├── alias │ │ ├── AliasClassReference.java │ │ ├── AliasDesc.java │ │ ├── AliasFacade.java │ │ ├── AliasResolver.java │ │ ├── AliasResolverFactory.java │ │ ├── AnnotationAliasResolver.java │ │ ├── BeanAliasResolver.java │ │ ├── BootConfigAliasResolver.java │ │ ├── ConfigPackageAliasResolver.java │ │ ├── InnerAliasResolver.java │ │ ├── PackageAliasResolver.java │ │ └── SingleAliasResolver.java │ │ ├── annotation │ │ └── Annotation.java │ │ ├── contributor │ │ ├── SqlFieldCompletionContributor.java │ │ ├── SqlParamCompletionContributor.java │ │ └── XmlParamContributor.java │ │ ├── definitionsearch │ │ └── MapperDefinitionSearch.java │ │ ├── dom │ │ ├── MapperBacktrackingUtils.java │ │ ├── converter │ │ │ ├── AbstractConverterAdaptor.java │ │ │ ├── AliasConverter.java │ │ │ ├── DaoMethodConverter.java │ │ │ ├── IdBasedTagConverter.java │ │ │ ├── ParameterMapConverter.java │ │ │ ├── PropertyConverter.java │ │ │ ├── ResultMapConverter.java │ │ │ └── SqlConverter.java │ │ ├── description │ │ │ ├── ConfigurationDescription.java │ │ │ └── MapperDescription.java │ │ └── model │ │ │ ├── Arg.java │ │ │ ├── Association.java │ │ │ ├── Bean.java │ │ │ ├── BeanProperty.java │ │ │ ├── Beans.java │ │ │ ├── Bind.java │ │ │ ├── Cache.java │ │ │ ├── CacheRef.java │ │ │ ├── Case.java │ │ │ ├── Choose.java │ │ │ ├── Collection.java │ │ │ ├── Configuration.java │ │ │ ├── Constructor.java │ │ │ ├── Delete.java │ │ │ ├── Discriminator.java │ │ │ ├── Foreach.java │ │ │ ├── GroupFour.java │ │ │ ├── GroupOne.java │ │ │ ├── GroupThree.java │ │ │ ├── GroupTwo.java │ │ │ ├── Id.java │ │ │ ├── IdArg.java │ │ │ ├── IdDomElement.java │ │ │ ├── If.java │ │ │ ├── Include.java │ │ │ ├── Insert.java │ │ │ ├── Mapper.java │ │ │ ├── Otherwise.java │ │ │ ├── Package.java │ │ │ ├── Parameter.java │ │ │ ├── ParameterMap.java │ │ │ ├── Property.java │ │ │ ├── PropertyGroup.java │ │ │ ├── Result.java │ │ │ ├── ResultMap.java │ │ │ ├── ResultMapGroup.java │ │ │ ├── Select.java │ │ │ ├── SelectKey.java │ │ │ ├── Set.java │ │ │ ├── Sql.java │ │ │ ├── Trim.java │ │ │ ├── TypeAlias.java │ │ │ ├── TypeAliases.java │ │ │ ├── Update.java │ │ │ ├── When.java │ │ │ └── Where.java │ │ ├── generate │ │ ├── AbstractStatementGenerator.java │ │ ├── DeleteGenerator.java │ │ ├── GenerateModel.java │ │ ├── InsertGenerator.java │ │ ├── SelectGenerator.java │ │ └── UpdateGenerator.java │ │ ├── init │ │ └── InitMybatisLiteActivity.java │ │ ├── inspection │ │ ├── AbstractMapperInspection.java │ │ ├── GenericQuickFix.java │ │ ├── MapperInjectionPointsAutowiringInspection.java │ │ ├── MapperMethodInspection.java │ │ ├── MapperXmlInspection.java │ │ ├── ResultTypeQuickFix.java │ │ ├── SpringJavaAutowiredFieldsFixInspection.java │ │ └── StatementNotExistsQuickFix.java │ │ ├── intention │ │ ├── GenerateMapperChooser.java │ │ ├── GenerateMapperIntention.java │ │ ├── GenerateParamAnnotationIntention.java │ │ ├── GenerateParamChooser.java │ │ ├── GenerateStatementChooser.java │ │ ├── GenerateStatementIntention.java │ │ ├── GenericIntention.java │ │ ├── IntentionChooser.java │ │ ├── JavaFileIntentionChooser.java │ │ └── LombokIntentionAction.java │ │ ├── listener │ │ ├── CheckMouseListener.java │ │ └── SettingApplyListener.java │ │ ├── locator │ │ ├── LocateStrategy.java │ │ ├── MapperLocator.java │ │ ├── MapperXmlPackageProvider.java │ │ ├── PackageLocateStrategy.java │ │ └── PackageProvider.java │ │ ├── model │ │ ├── DbSourcePo.java │ │ ├── LocalColumn.java │ │ ├── LocalTable.java │ │ ├── ModuleConfig.java │ │ ├── PluginConfig.java │ │ ├── Template.java │ │ └── enums │ │ │ └── VmTypeEnums.java │ │ ├── provider │ │ ├── AbstractFileProvider.java │ │ ├── AbstractMarkerProviderAdaptor.java │ │ ├── AbstractSimpleLineAbstractMarkerProvider.java │ │ ├── DefaultProviderImpl.java │ │ ├── FileProviderFactory.java │ │ ├── InjectionLineMarkerProvider.java │ │ ├── MapperImplicitBeansProvider.java │ │ ├── MapperInfrastructureBean.java │ │ ├── MapperLineMarkerProvider.java │ │ └── StatementLineAbstractMarkerProvider.java │ │ ├── refactoring │ │ └── MapperRefactoringProvider.java │ │ ├── reference │ │ ├── ContextPsiFieldReference.java │ │ ├── ContextReferenceSetResolver.java │ │ ├── PsiFieldReferenceSetResolver.java │ │ ├── ReferenceSetResolverFactory.java │ │ └── ResultPropertyReferenceSet.java │ │ ├── service │ │ ├── AnnotationService.java │ │ ├── DbServiceFactory.java │ │ ├── EditorService.java │ │ ├── JavaService.java │ │ ├── MysqlService.java │ │ ├── SqlLiteService.java │ │ └── resolver │ │ │ ├── AbstractFileResolver.java │ │ │ ├── FileResolver.java │ │ │ ├── PropertiesFileResolver.java │ │ │ ├── ResolverFacade.java │ │ │ ├── XmlConfigFileResolver.java │ │ │ └── YamlFileResolver.java │ │ ├── setting │ │ ├── ConnectDbSetting.form │ │ ├── ConnectDbSetting.java │ │ ├── ModuleDBConfigUI.form │ │ ├── ModuleDBConfigUI.java │ │ ├── MybatisLiteConfigurable.java │ │ ├── MybatisLiteProjectConfigurable.java │ │ ├── MybatisLiteProjectSetting.java │ │ ├── MybatisLiteProjectSettingUI.form │ │ ├── MybatisLiteProjectSettingUI.java │ │ ├── MybatisLiteSetting.java │ │ ├── MybatisSettingForm.form │ │ ├── MybatisSettingForm.java │ │ ├── SingleDBConnectInfoUI.form │ │ ├── SingleDBConnectInfoUI.java │ │ ├── TemplateEdit.form │ │ ├── TemplateEdit.java │ │ ├── TemplateListForm.form │ │ ├── TemplateListForm.java │ │ └── endpoint │ │ │ ├── AbstractEndpointTabConfigurable.java │ │ │ ├── MybatisLiteProjectExtendConfigurableAbstract.java │ │ │ ├── MybatisLiteProjectExtendConfigurableUi.java │ │ │ └── MybatisLiteProjectExtendSetting.java │ │ ├── template │ │ ├── MybatisFileTemplateDescriptorFactory.java │ │ └── SqlLiveTemplateProvider.java │ │ ├── ui │ │ ├── ClickableListener.java │ │ ├── ExecutableListener.java │ │ ├── ListSelectionListener.java │ │ ├── UiComponentFacade.java │ │ ├── UiGenerateUtil.java │ │ └── custom │ │ │ ├── DialogWrapperPanel.java │ │ │ ├── ModuleDBConfigListPanel.java │ │ │ ├── TemplateCodeEditPanel.java │ │ │ ├── TemplateListPanel.java │ │ │ └── TemplateViewPanel.java │ │ └── util │ │ ├── ArrayUtils.java │ │ ├── CollectionUtils.java │ │ ├── ConnectionHolder.java │ │ ├── DatabaseType.java │ │ ├── DbUtil.java │ │ ├── ExternalClassLoader.java │ │ ├── Icons.java │ │ ├── JavaUtils.java │ │ ├── MapperUtils.java │ │ ├── MybatisConstants.java │ │ ├── MybatisDomUtils.java │ │ ├── OrderedProperties.java │ │ ├── SqlUtil.java │ │ └── crypto │ │ └── ConfigTools.java │ ├── resources │ ├── META-INF │ │ ├── plugin.xml │ │ └── pluginIcon.svg │ ├── db │ │ └── generate_web.db │ ├── fileTemplates │ │ └── j2ee │ │ │ └── Mybatis Mapper.xml.ft │ ├── images │ │ ├── 7.png │ │ ├── 7d.png │ │ ├── down.png │ │ ├── down@2x.png │ │ ├── injection.png │ │ ├── logo.png │ │ ├── logo_17.png │ │ ├── logo_32.png │ │ ├── mapper_method.png │ │ ├── s.png │ │ ├── showAutowiredDependencies.png │ │ ├── springBean.png │ │ ├── statement.png │ │ ├── up.png │ │ └── up@2x.png │ ├── inspectionDescriptions │ │ ├── MabatisInspection.html │ │ ├── MapperAutowiredInspection.html │ │ └── MybatisMapperXmlInspection.html │ ├── intentionDescriptions │ │ └── GenerateStatementIntention │ │ │ └── description.html │ ├── liveTemplates │ │ └── sql.xml │ └── logback.xml │ └── test │ └── com │ └── github │ └── mustfun │ └── mybatis │ └── plugin │ └── test │ ├── intention │ └── GenerateXMLCodeTest.java │ └── util │ └── MybatisPluginTestUtil.java └── testData ├── GenerateStatement-Input-File.md └── GenerateStatement-Output-File.md /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | */target/ 3 | */*.iml 4 | .settings 5 | .classpath 6 | .project 7 | log/ 8 | */log/ 9 | pom.xml.releaseBackup 10 | output 11 | *tmp 12 | *.iml 13 | *.DS_Store 14 | .idea 15 | .mvn 16 | git 17 | build/ 18 | .gradle 19 | out -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | before_install: 3 | - chmod +x gradlew 4 | before_cache: 5 | - rm -f $HOME/.gradle/caches/modules-2/modules-2.lock 6 | - rm -fr $HOME/.gradle/caches/*/plugin-resolution/ 7 | cache: 8 | directories: 9 | - $HOME/.gradle/caches/ 10 | - $HOME/.gradle/wrapper/ 11 | jdk: 12 | - openjdk8 -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 mustfun 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Mybatis Lite 2 | 3 | ![编译](https://travis-ci.org/mustfun/mybatis-lite.svg?branch=master) 4 | ![](https://camo.githubusercontent.com/1e5d6c593654e3673fe4323032b7af9656157b1e/68747470733a2f2f6261646765732e66726170736f66742e636f6d2f6f732f6d69742f6d69742e7376673f763d313033) 5 | 6 | 7 | ## 案例演示 8 | 9 | ### I. 生成代码 10 | > Mybatis Lite根据预先设置模板生成controller/Service/dao/sql等层次代码,一次性生成常用增删改查代码,模板可编辑,减少重复工作,也可单个生成部分代码 11 | - 智能记忆上次输入数据库 12 | - 系统自动定位类存储位置,无需手动选择 13 | - 可选保存当前位置,作用于当前项目,避免系统选择错误 14 | - 双击模板一列即可实时编辑模板,语法高亮显示 15 | - 可选PO大小写功能,默认小写Po 16 | > Tools->MyBatis-Lite->Generate-Code 17 | 18 | ![生成controller等案例](doc/sample_new.gif) 19 | ### II. 跳转配置 20 | > Mybatis Lite提供Dao层到Xml-sql层的跳转,根据官方建议优化跳转性能,修复编辑XML过程中标签消失bug,此功能默认打开(可能会与Mybatis Plugin / free mybatis plugin等插件冲突显示2个标签),需要关闭的同学,请在IDEA->File菜单->Setting菜单->Mybatis Lite菜单->导航开关(不同版本IDEA可能路径有点不同但是都在setting里面) 关闭 21 | - 优化lineMarker性能,资源损耗更小 22 | - 编辑过程中不会有标签突然出现或者消失bug 23 | 24 | ![跳转](doc/navi.gif) 25 | ### III. 生成xml-sql 26 | > 用户在dao层编写方法的时候,可按`alt+enter`快捷键,系统会自动根据方法前缀生成相应的增删改查sql,系统自动识别当前XML用的表名,并编写部分预设sql,如果要新增匹配前缀可到setting里面设置 27 | - 自动识别前缀选择增删改查语句无需选择 28 | - 生成的sql语句自动追加在XML末尾,便于查找 29 | - 自动编写部分sql语句,包括表名 30 | 31 | ![跳转](doc/generate.gif) 32 | 33 | ### IV. 智能提示 34 | > 在编写XML-Sql过程中,智能提示可辅助用户编写更快速 35 | 36 | 例2:表字段提示 37 | ![跳转](doc/inspect_sample3.gif) 38 | > 值得注意的是这个功能依赖于用户在项目中的数据库配置文件,系统自动查找并连接,如果系统获取不到建议在Setting->Mybatis-Lite设置中指定配置文件名,如果DB配置高度定制化,这样系统是无法获取到数据库信息的,建议到Tools->Mybatis Lite->DB config填写下数据库信息,这样智能提示即可生效,如果还是有问题,建议暂时关闭此功能(Setting中),并在issue中描述一下,以便作者兼容,谢谢 39 | 40 | 例2:标签内容智能提示 41 | ![跳转](doc/inspect.gif) 42 | 43 | 例3:参数智能提示 44 | ![跳转](doc/inspect_sample2.gif) 45 | 46 | ### V. 错误提示 47 | > 如果误删除XML等或者返回类型编写错误时候系统会有提示 48 | 49 | 例1: Dao中方法未定义 50 | ![跳转](doc/error_sample1.png) 51 | 52 | 例2: 返回值不匹配 53 | ![跳转](doc/error_sample2.png) 54 | 55 | ### VI. 打印SQL 56 | > File->Setting->Mybatis Lite -> 其它配置-> 启用SQL打印选择开即可,然后运行项目即可打印SQL,暂时只针对使用druid数据源的项目生效,如果有使用到其它数据源请在issue中提出,后期会陆续适配 57 | 58 | ## 下载安装 59 | - IDEA-> File -> Settings -> Plugins -> MarketPlace,输入mybatis lite,点install安装即可 60 | - 点击 [这里](https://plugins.jetbrains.com/plugin/10921-mybatis-lite) 下载最新版进行离线安装,IDEA-> File -> Settings -> Plugins ->setting(齿轮图标)-> Install plugin from disk 61 | -------------------------------------------------------------------------------- /doc/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mustfun/mybatis-lite/6b8e253ec09c8d75f3e43c27139ee26ff8fa62c4/doc/README.md -------------------------------------------------------------------------------- /doc/error_sample1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mustfun/mybatis-lite/6b8e253ec09c8d75f3e43c27139ee26ff8fa62c4/doc/error_sample1.png -------------------------------------------------------------------------------- /doc/error_sample2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mustfun/mybatis-lite/6b8e253ec09c8d75f3e43c27139ee26ff8fa62c4/doc/error_sample2.png -------------------------------------------------------------------------------- /doc/generate.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mustfun/mybatis-lite/6b8e253ec09c8d75f3e43c27139ee26ff8fa62c4/doc/generate.gif -------------------------------------------------------------------------------- /doc/inspect.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mustfun/mybatis-lite/6b8e253ec09c8d75f3e43c27139ee26ff8fa62c4/doc/inspect.gif -------------------------------------------------------------------------------- /doc/inspect_sample2.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mustfun/mybatis-lite/6b8e253ec09c8d75f3e43c27139ee26ff8fa62c4/doc/inspect_sample2.gif -------------------------------------------------------------------------------- /doc/inspect_sample3.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mustfun/mybatis-lite/6b8e253ec09c8d75f3e43c27139ee26ff8fa62c4/doc/inspect_sample3.gif -------------------------------------------------------------------------------- /doc/navi.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mustfun/mybatis-lite/6b8e253ec09c8d75f3e43c27139ee26ff8fa62c4/doc/navi.gif -------------------------------------------------------------------------------- /doc/sample1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mustfun/mybatis-lite/6b8e253ec09c8d75f3e43c27139ee26ff8fa62c4/doc/sample1.gif -------------------------------------------------------------------------------- /doc/sample_new.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mustfun/mybatis-lite/6b8e253ec09c8d75f3e43c27139ee26ff8fa62c4/doc/sample_new.gif -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mustfun/mybatis-lite/6b8e253ec09c8d75f3e43c27139ee26ff8fa62c4/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Thu Jun 07 18:19:48 CST 2018 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.1-all.zip 7 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS="-Xmx64m" 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'MyBatis-Lite' 2 | 3 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/action/GenerateCodeMainActionGroup.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.action; 2 | 3 | import com.github.mustfun.mybatis.plugin.util.Icons; 4 | import com.intellij.openapi.actionSystem.AnActionEvent; 5 | import com.intellij.openapi.actionSystem.CommonDataKeys; 6 | import com.intellij.openapi.actionSystem.DefaultActionGroup; 7 | import com.intellij.openapi.editor.Editor; 8 | 9 | /** 10 | * @author itar 11 | * @version 1.0 12 | * @date 2018/6/12 13 | * @since 1.0 14 | */ 15 | public class GenerateCodeMainActionGroup extends DefaultActionGroup { 16 | 17 | @Override 18 | public void update(AnActionEvent event) { 19 | Editor editor = event.getData(CommonDataKeys.EDITOR); 20 | event.getPresentation().setVisible(true); 21 | //大家要求,编辑器关闭的时候也允许打开 22 | //event.getPresentation().setEnabled(editor != null); 23 | event.getPresentation().setIcon(Icons.MYBATIS_LOGO_MINI); 24 | } 25 | 26 | @Override 27 | public boolean hideIfNoVisibleChildren() { 28 | return false; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/action/GenerateCodeUIAction.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.action; 2 | 3 | import com.github.mustfun.mybatis.plugin.ui.UiGenerateUtil; 4 | import com.github.mustfun.mybatis.plugin.ui.custom.DialogWrapperPanel; 5 | import com.intellij.openapi.actionSystem.AnAction; 6 | import com.intellij.openapi.actionSystem.AnActionEvent; 7 | import com.intellij.openapi.project.Project; 8 | 9 | import java.util.Objects; 10 | 11 | /** 12 | * @author itar 13 | * @version 1.0 14 | * @date 2018/6/12 15 | * @since 1.0 16 | */ 17 | public class GenerateCodeUIAction extends AnAction { 18 | 19 | @Override 20 | public void actionPerformed(AnActionEvent e) { 21 | Project project = e.getProject(); 22 | DialogWrapperPanel commonDialog = UiGenerateUtil.getInstance(Objects.requireNonNull(project)).getCommonDialog(null); 23 | commonDialog.show(); 24 | } 25 | 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/action/ProjectRightPopupAction.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.action; 2 | 3 | import com.github.mustfun.mybatis.plugin.ui.UiGenerateUtil; 4 | import com.github.mustfun.mybatis.plugin.ui.custom.DialogWrapperPanel; 5 | import com.intellij.openapi.actionSystem.AnAction; 6 | import com.intellij.openapi.actionSystem.AnActionEvent; 7 | import com.intellij.openapi.actionSystem.CommonDataKeys; 8 | import com.intellij.openapi.actionSystem.LangDataKeys; 9 | import com.intellij.openapi.module.Module; 10 | import com.intellij.openapi.project.Project; 11 | 12 | import java.util.Objects; 13 | 14 | /** 15 | * @author itar 16 | * @date 2020-03-17 17 | * 18 | */ 19 | public class ProjectRightPopupAction extends AnAction { 20 | 21 | @Override 22 | public void actionPerformed(AnActionEvent e) { 23 | Project project = e.getProject(); 24 | Module module = e.getDataContext().getData(LangDataKeys.MODULE); 25 | DialogWrapperPanel commonDialog = UiGenerateUtil.getInstance(Objects.requireNonNull(project)).getCommonDialog(module); 26 | commonDialog.show(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/agent/MainAdapter.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.agent; 2 | 3 | import java.lang.instrument.Instrumentation; 4 | 5 | /** 6 | * 格式 7 | *    "\033[*;*;*m"  //比如 "\033[1;2;3m" 8 | * 9 | *     前缀"\033[",后缀"m" 10 | * 11 | *     颜色、背景颜色、样式都是用数字表示 12 | * 13 | *     所以只需要把对应的字码用";"隔开就好了 14 | * 15 | * 范围 16 | *     转义符之后的字符都会变成转义符所表示的样式 17 | * 18 | * 样式 19 | * 20 | *     0  空样式 21 | * 22 | *     1  粗体 23 | * 24 | *     4  下划线 25 | * 26 | *     7  反色 27 | * 28 | *     颜色1: 29 | * 30 | *     30  白色 31 | * 32 | *     31  红色 33 | * 34 | *     32  绿色 35 | * 36 | *     33  黄色 37 | * 38 | *     34  蓝色 39 | * 40 | *     35  紫色 41 | * 42 | *     36  浅蓝 43 | * 44 | *     37  灰色 45 | * 46 | *     背景颜色: 47 | * 48 | *     40-47 和颜色顺序相同 49 | * 50 | *     颜色2: 51 | * 52 | *     90-97  比颜色1更鲜艳一些 53 | * @author itar 54 | * @date 2020-03-10 55 | * Main函数代理类 56 | */ 57 | public class MainAdapter { 58 | 59 | public static void premain(String agentOps, Instrumentation inst) { 60 | System.out.println("\033[33;4m" + "【Mybatis Lite】应用已开启增强模式" + "\033[0m"); 61 | System.out.println("\033[33;4m" + "【Mybatis Lite】Sql增强已开启" + "\033[0m"); 62 | // 添加Transformer 63 | inst.addTransformer(new SqlSessionTransformer()); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/alias/AliasClassReference.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.alias; 2 | 3 | import com.google.common.base.Function; 4 | import com.google.common.collect.Collections2; 5 | import com.intellij.codeInsight.lookup.LookupElement; 6 | import com.intellij.codeInsight.lookup.LookupElementBuilder; 7 | import com.intellij.psi.PsiElement; 8 | import com.intellij.psi.PsiReference; 9 | import com.intellij.psi.PsiReferenceBase; 10 | import com.intellij.psi.xml.XmlAttributeValue; 11 | import org.jetbrains.annotations.NotNull; 12 | import org.jetbrains.annotations.Nullable; 13 | 14 | import java.util.ArrayList; 15 | import java.util.Collection; 16 | import java.util.List; 17 | 18 | /** 19 | * @author yanglin 20 | * @updater itar 21 | * @funciton 别名类跳转,比如int直接可以跳转到Integer源码 22 | * @see PsiReference PsiReference: 对PSI元素的引用。例如,表达式中使用的变量名,可以使用“Go to Declaration”操作从引用转到它引用的元素 23 | */ 24 | public class AliasClassReference extends PsiReferenceBase { 25 | 26 | 27 | public AliasClassReference(@NotNull XmlAttributeValue element) { 28 | super(element, true); 29 | } 30 | 31 | @Nullable 32 | @Override 33 | public PsiElement resolve() { 34 | XmlAttributeValue attributeValue = getElement(); 35 | //把 long ,int 这种转化为psiElement 36 | return AliasFacade.getInstance(attributeValue.getProject()) 37 | .findPsiClass(attributeValue, attributeValue.getValue()).orElse(null); 38 | } 39 | 40 | /** 41 | * 返回字符串、PsiElement和或com.intellij.codeInsight.lookup.LookupElement的数组表示在引用位置可见的所有标识符的实例。 42 | * 内容用于构建用于基本代码完成的查找列表。(列表的可见标识符可能不会被完成前缀字符串过滤过滤稍后由IDE执行。) 43 | * @return 44 | * 这里返回了别名的所有数组, list , long, int 等候选 45 | */ 46 | @NotNull 47 | @Override 48 | public Object[] getVariants() { 49 | List result = new ArrayList<>(); 50 | AliasFacade aliasFacade = AliasFacade.getInstance(getElement().getProject()); 51 | Collection aliasDescs = aliasFacade.getAliasDescs(getElement()); 52 | for (AliasDesc aliasDesc : aliasDescs) { 53 | LookupElementBuilder lookupElementBuilder = LookupElementBuilder.createWithIcon(aliasDesc.getClazz()).withTypeText(aliasDesc.getClazz().getQualifiedName()); 54 | result.add(lookupElementBuilder); 55 | } 56 | return result.toArray(new LookupElement[0]); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/alias/AliasDesc.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.alias; 2 | 3 | import com.intellij.psi.PsiClass; 4 | import org.jetbrains.annotations.NotNull; 5 | 6 | /** 7 | * @author yanglin 8 | * @updater itar 9 | * @function 别名的一个描述类,左边是类,右边是别名 10 | */ 11 | public class AliasDesc { 12 | 13 | private PsiClass clazz; 14 | 15 | private String alias; 16 | 17 | public AliasDesc() { 18 | } 19 | 20 | public static AliasDesc create(@NotNull PsiClass psiClass, @NotNull String alias) { 21 | return new AliasDesc(psiClass, alias); 22 | } 23 | 24 | public AliasDesc(PsiClass clazz, String alias) { 25 | this.clazz = clazz; 26 | this.alias = alias; 27 | } 28 | 29 | public PsiClass getClazz() { 30 | return clazz; 31 | } 32 | 33 | public void setClazz(PsiClass clazz) { 34 | this.clazz = clazz; 35 | } 36 | 37 | public String getAlias() { 38 | return alias; 39 | } 40 | 41 | public void setAlias(String alias) { 42 | this.alias = alias; 43 | } 44 | 45 | @Override 46 | public boolean equals(Object o) { 47 | if (this == o) { 48 | return true; 49 | } 50 | if (o == null || getClass() != o.getClass()) { 51 | return false; 52 | } 53 | 54 | AliasDesc aliasDesc = (AliasDesc) o; 55 | 56 | if (alias != null ? !alias.equals(aliasDesc.alias) : aliasDesc.alias != null) { 57 | return false; 58 | } 59 | if (clazz != null ? !clazz.equals(aliasDesc.clazz) : aliasDesc.clazz != null) { 60 | return false; 61 | } 62 | 63 | return true; 64 | } 65 | 66 | @Override 67 | public int hashCode() { 68 | int result = clazz != null ? clazz.hashCode() : 0; 69 | result = 31 * result + (alias != null ? alias.hashCode() : 0); 70 | return result; 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/alias/AliasResolver.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.alias; 2 | 3 | import com.github.mustfun.mybatis.plugin.util.JavaUtils; 4 | import com.intellij.openapi.project.Project; 5 | import com.intellij.psi.PsiClass; 6 | import com.intellij.psi.PsiElement; 7 | import org.jetbrains.annotations.NotNull; 8 | import org.jetbrains.annotations.Nullable; 9 | 10 | import java.util.Optional; 11 | import java.util.Set; 12 | 13 | /** 14 | * @author yanglin 15 | * @updater itar 16 | * @function 别名解析抽象类 17 | */ 18 | public abstract class AliasResolver { 19 | 20 | protected Project project; 21 | 22 | public AliasResolver(Project project) { 23 | this.project = project; 24 | } 25 | 26 | /** 27 | * 新增别名描述 28 | * @param descs 29 | * @param clazz 30 | * @param alias 31 | * @return 32 | */ 33 | @NotNull 34 | protected Optional addAliasDesc(@NotNull Set descs, @Nullable PsiClass clazz, 35 | @Nullable String alias) { 36 | if (null == alias || !JavaUtils.isModelClazz(clazz)) { 37 | return Optional.empty(); 38 | } 39 | AliasDesc desc = new AliasDesc(); 40 | descs.add(desc); 41 | desc.setClazz(clazz); 42 | desc.setAlias(alias); 43 | return Optional.of(desc); 44 | } 45 | 46 | /** 47 | * 获取别名描述 48 | * @param element 49 | * @return 50 | */ 51 | @NotNull 52 | public abstract Set getClassAliasDescriptions(@Nullable PsiElement element); 53 | 54 | /** 55 | * 获取别名描述 - 根据名称 56 | * @param name 57 | * @return 58 | */ 59 | @Nullable 60 | public AliasDesc getClassAliasByName(String name){ 61 | return null; 62 | } 63 | 64 | public Project getProject() { 65 | return project; 66 | } 67 | 68 | public void setProject(Project project) { 69 | this.project = project; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/alias/AliasResolverFactory.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.alias; 2 | 3 | import com.intellij.openapi.project.Project; 4 | import org.jetbrains.annotations.NotNull; 5 | 6 | /** 7 | * @author yanglin 8 | * @updater itar 9 | * @function 解决依赖引用用的工厂类 10 | */ 11 | public class AliasResolverFactory { 12 | 13 | @NotNull 14 | public static AliasResolver createInnerAliasResolver(@NotNull Project project) { 15 | return new InnerAliasResolver(project); 16 | } 17 | 18 | @NotNull 19 | public static AliasResolver createAnnotationResolver(@NotNull Project project) { 20 | return new AnnotationAliasResolver(project); 21 | } 22 | 23 | @NotNull 24 | public static AliasResolver createBeanResolver(@NotNull Project project) { 25 | return new BeanAliasResolver(project); 26 | } 27 | 28 | @NotNull 29 | public static AliasResolver createConfigPackageResolver(@NotNull Project project) { 30 | return new ConfigPackageAliasResolver(project); 31 | } 32 | 33 | @NotNull 34 | public static AliasResolver createSingleAliasResolver(@NotNull Project project) { 35 | return new SingleAliasResolver(project); 36 | } 37 | @NotNull 38 | public static AliasResolver createBootConfigAliasResolver(@NotNull Project project) { 39 | return new BootConfigAliasResolver(project); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/alias/AnnotationAliasResolver.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.alias; 2 | 3 | import com.github.mustfun.mybatis.plugin.annotation.Annotation; 4 | import com.github.mustfun.mybatis.plugin.util.JavaUtils; 5 | import com.google.common.base.Function; 6 | import com.google.common.collect.Collections2; 7 | import com.google.common.collect.Sets; 8 | import com.intellij.openapi.project.Project; 9 | import com.intellij.psi.PsiClass; 10 | import com.intellij.psi.PsiElement; 11 | import com.intellij.psi.search.GlobalSearchScope; 12 | import com.intellij.psi.search.searches.AnnotatedElementsSearch; 13 | import org.jetbrains.annotations.NotNull; 14 | import org.jetbrains.annotations.Nullable; 15 | 16 | import java.util.Collection; 17 | import java.util.Collections; 18 | import java.util.Optional; 19 | import java.util.Set; 20 | 21 | /** 22 | * @author yanglin 23 | * @updater itar 24 | * @funcion 注解解析器 25 | 26 | */ 27 | public class AnnotationAliasResolver extends AliasResolver { 28 | 29 | private static final Function FUN = new Function() { 30 | @Override 31 | public AliasDesc apply(PsiClass psiClass) { 32 | Optional txt = JavaUtils.getAnnotationValueText(psiClass, Annotation.ALIAS); 33 | if (!txt.isPresent()) { 34 | return null; 35 | } 36 | AliasDesc ad = new AliasDesc(); 37 | ad.setAlias(txt.get()); 38 | ad.setClazz(psiClass); 39 | return ad; 40 | } 41 | }; 42 | 43 | public AnnotationAliasResolver(Project project) { 44 | super(project); 45 | } 46 | 47 | public static final AnnotationAliasResolver getInstance(@NotNull Project project) { 48 | return project.getComponent(AnnotationAliasResolver.class); 49 | } 50 | 51 | @SuppressWarnings("unchecked") 52 | @NotNull 53 | @Override 54 | public Set getClassAliasDescriptions(@Nullable PsiElement element) { 55 | Optional clazz = Annotation.ALIAS.toPsiClass(project); 56 | if (clazz.isPresent()) { 57 | Collection res = AnnotatedElementsSearch 58 | .searchPsiClasses(clazz.get(), GlobalSearchScope.allScope(project)).findAll(); 59 | return Sets.newHashSet(Collections2.transform(res, FUN)); 60 | } 61 | return Collections.emptySet(); 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/alias/BootConfigAliasResolver.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.alias; 2 | 3 | import com.github.mustfun.mybatis.plugin.model.ModuleConfig; 4 | import com.github.mustfun.mybatis.plugin.util.ConnectionHolder; 5 | import com.google.common.collect.Sets; 6 | import com.intellij.openapi.module.Module; 7 | import com.intellij.openapi.module.ModuleUtil; 8 | import com.intellij.openapi.project.Project; 9 | import com.intellij.openapi.util.Pair; 10 | import com.intellij.psi.PsiElement; 11 | import org.jetbrains.annotations.NotNull; 12 | import org.jetbrains.annotations.Nullable; 13 | 14 | import java.util.Collection; 15 | import java.util.Set; 16 | 17 | /** 18 | * @author itar 19 | * @date 2020-03-08 20 | * @funcion bean别名解析器 21 | */ 22 | public class BootConfigAliasResolver extends PackageAliasResolver { 23 | 24 | 25 | public BootConfigAliasResolver(Project project) { 26 | super(project); 27 | } 28 | 29 | @NotNull 30 | @Override 31 | public Collection getPackages(@Nullable PsiElement element) { 32 | Set res = Sets.newHashSet(); 33 | //如果没有config是boot文件的 34 | Module currentModule = ModuleUtil.findModuleForPsiElement(element); 35 | if(currentModule==null){ 36 | return res; 37 | } 38 | Pair config = ConnectionHolder.getInstance(project).getConfigOrOne(currentModule.getName()) ; 39 | if (config==null||config.second==null){ 40 | return res; 41 | } 42 | ModuleConfig moduleConfig = (ModuleConfig) config.second; 43 | String typeAliasPackage = moduleConfig.getTypeAliasPackage(); 44 | //找到package 45 | //PsiDirectory possiblePackageDirectoryInModule = PackageUtil.findPossiblePackageDirectoryInModule(currentModule, typeAliasPackage); 46 | res.add(typeAliasPackage); 47 | return res; 48 | } 49 | 50 | private Module getPoModule() { 51 | return null; 52 | } 53 | 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/alias/ConfigPackageAliasResolver.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.alias; 2 | 3 | import com.github.mustfun.mybatis.plugin.dom.model.Package; 4 | import com.github.mustfun.mybatis.plugin.util.MapperUtils; 5 | import com.google.common.collect.Lists; 6 | import com.intellij.openapi.project.Project; 7 | import com.intellij.psi.PsiElement; 8 | import com.intellij.util.Processor; 9 | import java.util.ArrayList; 10 | import java.util.Collection; 11 | import org.jetbrains.annotations.NotNull; 12 | import org.jetbrains.annotations.Nullable; 13 | 14 | /** 15 | * @author yanglin 16 | * @updater itar 17 | * @funcion 配置包路径解析器 - configuration package时候用 18 | */ 19 | public class ConfigPackageAliasResolver extends PackageAliasResolver { 20 | 21 | public ConfigPackageAliasResolver(Project project) { 22 | super(project); 23 | } 24 | 25 | @NotNull 26 | @Override 27 | public Collection getPackages(@Nullable PsiElement element) { 28 | final ArrayList result = Lists.newArrayList(); 29 | MapperUtils.processConfiguredPackage(project, new Processor() { 30 | @Override 31 | public boolean process(Package pkg) { 32 | result.add(pkg.getName().getStringValue()); 33 | return true; 34 | } 35 | }); 36 | return result; 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/alias/InnerAliasResolver.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.alias; 2 | 3 | import com.github.mustfun.mybatis.plugin.util.JavaUtils; 4 | import com.google.common.collect.ImmutableSet; 5 | import com.intellij.openapi.project.Project; 6 | import com.intellij.psi.PsiElement; 7 | import org.jetbrains.annotations.NotNull; 8 | import org.jetbrains.annotations.Nullable; 9 | 10 | import java.util.Set; 11 | 12 | /** 13 | * @author yanglin 14 | * @updater itar 15 | * @funcion 简单类型 解析器 16 | */ 17 | public class InnerAliasResolver extends AliasResolver { 18 | 19 | private final Set innerAliasDescs = ImmutableSet.of( 20 | AliasDesc.create(JavaUtils.findClazz(project, "java.lang.String").get(), "string"), 21 | AliasDesc.create(JavaUtils.findClazz(project, "java.lang.Byte").get(), "byte"), 22 | AliasDesc.create(JavaUtils.findClazz(project, "java.lang.Long").get(), "long"), 23 | AliasDesc.create(JavaUtils.findClazz(project, "java.lang.Short").get(), "short"), 24 | AliasDesc.create(JavaUtils.findClazz(project, "java.lang.Integer").get(), "int"), 25 | AliasDesc.create(JavaUtils.findClazz(project, "java.lang.Integer").get(), "integer"), 26 | AliasDesc.create(JavaUtils.findClazz(project, "java.lang.Double").get(), "double"), 27 | AliasDesc.create(JavaUtils.findClazz(project, "java.lang.Float").get(), "float"), 28 | AliasDesc.create(JavaUtils.findClazz(project, "java.lang.Boolean").get(), "boolean"), 29 | AliasDesc.create(JavaUtils.findClazz(project, "java.util.Date").get(), "date"), 30 | AliasDesc.create(JavaUtils.findClazz(project, "java.math.BigDecimal").get(), "decimal"), 31 | AliasDesc.create(JavaUtils.findClazz(project, "java.lang.Object").get(), "object"), 32 | AliasDesc.create(JavaUtils.findClazz(project, "java.util.Map").get(), "map"), 33 | AliasDesc.create(JavaUtils.findClazz(project, "java.util.HashMap").get(), "hashmap"), 34 | AliasDesc.create(JavaUtils.findClazz(project, "java.util.List").get(), "list"), 35 | AliasDesc.create(JavaUtils.findClazz(project, "java.util.ArrayList").get(), "arraylist"), 36 | AliasDesc.create(JavaUtils.findClazz(project, "java.util.Collection").get(), "collection"), 37 | AliasDesc.create(JavaUtils.findClazz(project, "java.util.Iterator").get(), "iterator") 38 | ); 39 | 40 | public InnerAliasResolver(Project project) { 41 | super(project); 42 | } 43 | 44 | @NotNull 45 | @Override 46 | public Set getClassAliasDescriptions(@Nullable PsiElement element) { 47 | return innerAliasDescs; 48 | } 49 | 50 | @Nullable 51 | @Override 52 | public AliasDesc getClassAliasByName(String name) { 53 | for (AliasDesc innerAliasDesc : innerAliasDescs) { 54 | if (innerAliasDesc.getAlias().equalsIgnoreCase(name)){ 55 | return innerAliasDesc; 56 | } 57 | } 58 | return null; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/alias/PackageAliasResolver.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.alias; 2 | 3 | import com.google.common.collect.Sets; 4 | import com.intellij.openapi.project.Project; 5 | import com.intellij.psi.JavaPsiFacade; 6 | import com.intellij.psi.PsiClass; 7 | import com.intellij.psi.PsiElement; 8 | import com.intellij.psi.PsiPackage; 9 | import org.jetbrains.annotations.NotNull; 10 | import org.jetbrains.annotations.Nullable; 11 | 12 | import java.util.Collection; 13 | import java.util.Set; 14 | 15 | /** 16 | * @author yanglin 17 | * @updater itar 18 | * @function 包名解析器 19 | */ 20 | public abstract class PackageAliasResolver extends AliasResolver { 21 | 22 | private JavaPsiFacade javaPsiFacade; 23 | 24 | public PackageAliasResolver(Project project) { 25 | super(project); 26 | this.javaPsiFacade = JavaPsiFacade.getInstance(project); 27 | } 28 | 29 | @NotNull 30 | @Override 31 | public Set getClassAliasDescriptions(@Nullable PsiElement element) { 32 | Set result = Sets.newHashSet(); 33 | for (String pkgName : getPackages(element)) { 34 | if (null == pkgName) { 35 | continue; 36 | } 37 | PsiPackage pkg = javaPsiFacade.findPackage(pkgName); 38 | if (null != pkg) { 39 | addAliasDesc(result, pkg); 40 | for (PsiPackage tmp : pkg.getSubPackages()) { 41 | addAliasDesc(result, tmp); 42 | } 43 | } 44 | } 45 | return result; 46 | } 47 | 48 | private void addAliasDesc(Set result, PsiPackage pkg) { 49 | for (PsiClass clazz : pkg.getClasses()) { 50 | addAliasDesc(result, clazz, clazz.getName()); 51 | } 52 | } 53 | 54 | @NotNull 55 | public abstract Collection getPackages(@Nullable PsiElement element); 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/alias/SingleAliasResolver.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.alias; 2 | 3 | import com.github.mustfun.mybatis.plugin.dom.model.TypeAlias; 4 | import com.github.mustfun.mybatis.plugin.model.ModuleConfig; 5 | import com.github.mustfun.mybatis.plugin.util.ConnectionHolder; 6 | import com.github.mustfun.mybatis.plugin.util.MapperUtils; 7 | import com.google.common.collect.Sets; 8 | import com.intellij.ide.util.PackageUtil; 9 | import com.intellij.openapi.module.Module; 10 | import com.intellij.openapi.module.ModuleManager; 11 | import com.intellij.openapi.module.ModuleUtil; 12 | import com.intellij.openapi.project.Project; 13 | import com.intellij.openapi.util.Pair; 14 | import com.intellij.psi.*; 15 | import com.intellij.psi.impl.source.PsiJavaFileImpl; 16 | import com.intellij.psi.util.PsiUtil; 17 | import com.intellij.util.Processor; 18 | import java.util.Set; 19 | import org.jetbrains.annotations.NotNull; 20 | import org.jetbrains.annotations.Nullable; 21 | 22 | /** 23 | * @author yanglin 24 | * @updater itar 25 | * @funcion 单个别名解析器 - mybatis配置文件用 26 | * 1. 27 | * 2. getVariants(ConvertContext context) { 46 | Mapper mapper = MapperUtils.getMapper(context.getInvocationElement()); 47 | return JavaUtils.findMethods(context.getProject(), MapperUtils.getNamespace(mapper)); 48 | } 49 | 50 | @Nullable 51 | @Override 52 | public LookupElement createLookupElement(PsiMethod psiMethod) { 53 | return LookupElementBuilder.createWithIcon(psiMethod).withTypeText(((PsiClassImpl) psiMethod.getParent()).getName()); 54 | } 55 | } -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/dom/converter/ParameterMapConverter.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.dom.converter; 2 | 3 | import com.github.mustfun.mybatis.plugin.dom.model.IdDomElement; 4 | import com.github.mustfun.mybatis.plugin.dom.model.Mapper; 5 | import com.intellij.util.xml.ConvertContext; 6 | import java.util.Collection; 7 | import org.jetbrains.annotations.NotNull; 8 | import org.jetbrains.annotations.Nullable; 9 | 10 | /** 11 | * @author yanglin 12 | * @updater itar 13 | * @function 参数列表 14 | */ 15 | public class ParameterMapConverter extends IdBasedTagConverter { 16 | 17 | @NotNull 18 | @Override 19 | public Collection getComparisons(@Nullable Mapper mapper, 20 | ConvertContext context) { 21 | return mapper.getParameterMaps(); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/dom/converter/PropertyConverter.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.dom.converter; 2 | 3 | import com.github.mustfun.mybatis.plugin.reference.ResultPropertyReferenceSet; 4 | import com.intellij.psi.ElementManipulators; 5 | import com.intellij.psi.PsiElement; 6 | import com.intellij.psi.PsiReference; 7 | import com.intellij.psi.xml.XmlAttributeValue; 8 | import com.intellij.util.xml.ConvertContext; 9 | import com.intellij.util.xml.CustomReferenceConverter; 10 | import com.intellij.util.xml.DomElement; 11 | import com.intellij.util.xml.GenericAttributeValue; 12 | import com.intellij.util.xml.GenericDomValue; 13 | import org.apache.commons.lang.StringUtils; 14 | import org.jetbrains.annotations.NonNls; 15 | import org.jetbrains.annotations.NotNull; 16 | import org.jetbrains.annotations.Nullable; 17 | 18 | /** 19 | * @author yanglin 20 | * @updater itar 21 | * @function 等使用的需要可以跳转到java的属性类 22 | * property转为XmlAttributeValue 23 | */ 24 | public class PropertyConverter extends AbstractConverterAdaptor implements 25 | CustomReferenceConverter { 26 | 27 | /** 28 | * 跟java实体类做一个连接,可以跳转 29 | * @param value 30 | * @param element 31 | * @param context 32 | * @return 33 | * @see com.intellij.psi.impl.source.resolve.reference.impl.providers.JavaClassReferenceProvider#getReferencesByElement(PsiElement) 34 | */ 35 | @NotNull 36 | @Override 37 | public PsiReference[] createReferences(GenericDomValue value, PsiElement element, 38 | ConvertContext context) { 39 | final String s = value.getStringValue(); 40 | if (s == null) { 41 | return PsiReference.EMPTY_ARRAY; 42 | } 43 | //这个类更加底层一点,确定元素的位置,进行跳转 44 | return new ResultPropertyReferenceSet(s, element, ElementManipulators.getOffsetInElement(element)) 45 | .getPsiReferences(); 46 | } 47 | 48 | @Nullable 49 | @Override 50 | public XmlAttributeValue fromString(@Nullable @NonNls String s, ConvertContext context) { 51 | DomElement ctxElement = context.getInvocationElement(); 52 | return ctxElement instanceof GenericAttributeValue ? ((GenericAttributeValue) ctxElement).getXmlAttributeValue() 53 | : null; 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/dom/converter/ResultMapConverter.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.dom.converter; 2 | 3 | import com.github.mustfun.mybatis.plugin.dom.model.IdDomElement; 4 | import com.github.mustfun.mybatis.plugin.dom.model.Mapper; 5 | import com.github.mustfun.mybatis.plugin.dom.model.ResultMap; 6 | import com.github.mustfun.mybatis.plugin.util.MapperUtils; 7 | import com.google.common.base.Predicate; 8 | import com.google.common.collect.Collections2; 9 | import com.intellij.util.xml.ConvertContext; 10 | import com.intellij.util.xml.DomElement; 11 | import java.util.Collection; 12 | import org.jetbrains.annotations.NotNull; 13 | import org.jetbrains.annotations.Nullable; 14 | 15 | /** 16 | * @author yanglin 17 | * @updater itar 18 | * @function resultMap解析器,如果发现了extends属性就会调用这个converterl 19 | */ 20 | public class ResultMapConverter extends IdBasedTagConverter { 21 | 22 | /** 23 | * 上层getValue调用 - string转为xmlValue 24 | * @param mapper mapper in the project, null if {@link #IdBasedTagConverter} is false 25 | * @param context the dom convert context 26 | * @return 27 | * @function 如果唤醒的上下文是引起的转换 28 | * 那么toString的时候就将整个 resultMap给它 29 | * 如果唤醒的是,那么就获取所有的resultMap然后filter 30 | */ 31 | @NotNull 32 | @Override 33 | public Collection getComparisons(@Nullable Mapper mapper, ConvertContext context) { 34 | DomElement invocationElement = context.getInvocationElement(); 35 | if (isContextElementOfResultMap(mapper, invocationElement)) { 36 | return doFilterResultMapItself(mapper, (ResultMap) invocationElement.getParent()); 37 | } else { 38 | return mapper.getResultMaps(); 39 | } 40 | } 41 | 42 | private boolean isContextElementOfResultMap(Mapper mapper, DomElement invocationElement) { 43 | return MapperUtils.isMapperWithSameNamespace(MapperUtils.getMapper(invocationElement), mapper) 44 | && invocationElement.getParent() instanceof ResultMap; 45 | } 46 | 47 | private Collection doFilterResultMapItself(Mapper mapper, final ResultMap resultMap) { 48 | return Collections2.filter(mapper.getResultMaps(), new Predicate() { 49 | @Override 50 | public boolean apply(ResultMap input) { 51 | return !MapperUtils.getId(input).equals(MapperUtils.getId(resultMap)); 52 | } 53 | }); 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/dom/converter/SqlConverter.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.dom.converter; 2 | 3 | import com.github.mustfun.mybatis.plugin.dom.model.IdDomElement; 4 | import com.github.mustfun.mybatis.plugin.dom.model.Mapper; 5 | import com.intellij.util.xml.ConvertContext; 6 | import java.util.Collection; 7 | import org.jetbrains.annotations.NotNull; 8 | import org.jetbrains.annotations.Nullable; 9 | 10 | /** 11 | * @author yanglin 12 | * @updater itar 13 | * @function mapper的sql标签使用,指向id 14 | */ 15 | public class SqlConverter extends IdBasedTagConverter { 16 | 17 | @NotNull 18 | @Override 19 | public Collection getComparisons(@Nullable Mapper mapper, ConvertContext context) { 20 | return mapper.getSqls(); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/dom/description/ConfigurationDescription.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.dom.description; 2 | 3 | import com.github.mustfun.mybatis.plugin.dom.model.Configuration; 4 | import com.github.mustfun.mybatis.plugin.util.MybatisDomUtils; 5 | import com.intellij.openapi.module.Module; 6 | import com.intellij.psi.xml.XmlFile; 7 | import com.intellij.util.xml.DomFileDescription; 8 | import org.jetbrains.annotations.NotNull; 9 | import org.jetbrains.annotations.Nullable; 10 | 11 | /** 12 | * @author yanglin 13 | * @updater itar 14 | * @function mybatis配置文件的描述文件 15 | */ 16 | public class ConfigurationDescription extends DomFileDescription { 17 | 18 | public ConfigurationDescription() { 19 | super(Configuration.class, "configuration"); 20 | } 21 | 22 | @Override 23 | public boolean isMyFile(@NotNull XmlFile file, @Nullable Module module) { 24 | return MybatisDomUtils.isMybatisConfigurationFile(file); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/dom/description/MapperDescription.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.dom.description; 2 | 3 | import com.github.mustfun.mybatis.plugin.dom.model.Mapper; 4 | import com.github.mustfun.mybatis.plugin.util.MybatisDomUtils; 5 | import com.intellij.openapi.module.Module; 6 | import com.intellij.psi.xml.XmlFile; 7 | import com.intellij.util.xml.DomFileDescription; 8 | import org.jetbrains.annotations.NotNull; 9 | import org.jetbrains.annotations.Nullable; 10 | 11 | /** 12 | * @author yanglin 13 | * @updater mapper的描述文件 14 | */ 15 | public class MapperDescription extends DomFileDescription { 16 | 17 | public MapperDescription() { 18 | super(Mapper.class, "mapper"); 19 | } 20 | 21 | @Override 22 | public boolean isMyFile(@NotNull XmlFile file, @Nullable Module module) { 23 | return MybatisDomUtils.isMybatisFile(file); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/dom/model/Arg.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.dom.model; 2 | 3 | import com.intellij.util.xml.DomElement; 4 | 5 | /** 6 | * @author yanglin 7 | */ 8 | public interface Arg extends DomElement { 9 | 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/dom/model/Association.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.dom.model; 2 | 3 | import com.github.mustfun.mybatis.plugin.dom.converter.AliasConverter; 4 | import com.intellij.psi.PsiClass; 5 | import com.intellij.util.xml.Attribute; 6 | import com.intellij.util.xml.Convert; 7 | import com.intellij.util.xml.GenericAttributeValue; 8 | import org.jetbrains.annotations.NotNull; 9 | 10 | /** 11 | * @author yanglin 12 | */ 13 | public interface Association extends GroupFour, ResultMapGroup, PropertyGroup { 14 | 15 | @NotNull 16 | @Attribute("javaType") 17 | @Convert(AliasConverter.class) 18 | public GenericAttributeValue getJavaType(); 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/dom/model/Bean.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.dom.model; 2 | 3 | import com.intellij.util.xml.DomElement; 4 | import com.intellij.util.xml.SubTagList; 5 | import java.util.List; 6 | import org.jetbrains.annotations.NotNull; 7 | 8 | /** 9 | * @author yanglin 10 | */ 11 | public interface Bean extends DomElement { 12 | 13 | @NotNull 14 | @SubTagList("property") 15 | public List getBeanProperties(); 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/dom/model/BeanProperty.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.dom.model; 2 | 3 | import com.intellij.util.xml.Attribute; 4 | import com.intellij.util.xml.DomElement; 5 | import com.intellij.util.xml.GenericAttributeValue; 6 | import org.jetbrains.annotations.NotNull; 7 | 8 | /** 9 | * @author yanglin 10 | */ 11 | public interface BeanProperty extends DomElement { 12 | 13 | @NotNull 14 | @Attribute("name") 15 | public GenericAttributeValue getName(); 16 | 17 | @NotNull 18 | @Attribute("value") 19 | public GenericAttributeValue getValue(); 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/dom/model/Beans.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.dom.model; 2 | 3 | import com.intellij.util.xml.DomElement; 4 | import com.intellij.util.xml.SubTagList; 5 | import java.util.List; 6 | import org.jetbrains.annotations.NotNull; 7 | 8 | /** 9 | * @author yanglin 10 | */ 11 | public interface Beans extends DomElement { 12 | 13 | @NotNull 14 | @SubTagList("bean") 15 | public List getBeans(); 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/dom/model/Bind.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.dom.model; 2 | 3 | import com.intellij.util.xml.DomElement; 4 | 5 | /** 6 | * @author yanglin 7 | */ 8 | public interface Bind extends DomElement { 9 | 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/dom/model/Cache.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.dom.model; 2 | 3 | import com.intellij.util.xml.DomElement; 4 | import com.intellij.util.xml.SubTagList; 5 | import java.util.List; 6 | 7 | /** 8 | * @author yanglin 9 | */ 10 | public interface Cache extends DomElement { 11 | 12 | @SubTagList("property") 13 | public List getProperties(); 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/dom/model/CacheRef.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.dom.model; 2 | 3 | import com.intellij.util.xml.DomElement; 4 | 5 | /** 6 | * @author yanglin 7 | */ 8 | public interface CacheRef extends DomElement { 9 | 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/dom/model/Case.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.dom.model; 2 | 3 | /** 4 | * @author yanglin 5 | */ 6 | public interface Case extends GroupFour { 7 | 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/dom/model/Choose.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.dom.model; 2 | 3 | import com.intellij.util.xml.DomElement; 4 | import com.intellij.util.xml.Required; 5 | import com.intellij.util.xml.SubTag; 6 | import com.intellij.util.xml.SubTagList; 7 | import java.util.List; 8 | import org.jetbrains.annotations.NotNull; 9 | 10 | /** 11 | * @author yanglin 12 | */ 13 | public interface Choose extends DomElement { 14 | 15 | @NotNull 16 | @Required 17 | @SubTagList("when") 18 | public List getWhens(); 19 | 20 | @SubTag("otherwise") 21 | public Otherwise getOtherwise(); 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/dom/model/Collection.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.dom.model; 2 | 3 | import com.github.mustfun.mybatis.plugin.dom.converter.AliasConverter; 4 | import com.intellij.psi.PsiClass; 5 | import com.intellij.util.xml.Attribute; 6 | import com.intellij.util.xml.Convert; 7 | import com.intellij.util.xml.GenericAttributeValue; 8 | import org.jetbrains.annotations.NotNull; 9 | 10 | /** 11 | * @author yanglin 12 | */ 13 | public interface Collection extends GroupFour, ResultMapGroup, PropertyGroup { 14 | 15 | @NotNull 16 | @Attribute("ofType") 17 | @Convert(AliasConverter.class) 18 | public GenericAttributeValue getOfType(); 19 | 20 | } -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/dom/model/Configuration.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.dom.model; 2 | 3 | import com.intellij.util.xml.DomElement; 4 | import com.intellij.util.xml.SubTagList; 5 | import java.util.List; 6 | import org.jetbrains.annotations.NotNull; 7 | 8 | /** 9 | * @author yanglin 10 | */ 11 | public interface Configuration extends DomElement { 12 | 13 | @NotNull 14 | @SubTagList("typeAliases") 15 | public List getTypeAliases(); 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/dom/model/Constructor.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.dom.model; 2 | 3 | import com.intellij.util.xml.DomElement; 4 | import com.intellij.util.xml.SubTagList; 5 | import java.util.List; 6 | 7 | /** 8 | * @author yanglin 9 | */ 10 | public interface Constructor extends DomElement { 11 | 12 | @SubTagList("arg") 13 | public List getArgs(); 14 | 15 | @SubTagList("idArg") 16 | public List getIdArgs(); 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/dom/model/Delete.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.dom.model; 2 | 3 | /** 4 | * delete语句 5 | * @author yanglin 6 | * @updater itar 7 | */ 8 | public interface Delete extends GroupTwo { 9 | 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/dom/model/Discriminator.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.dom.model; 2 | 3 | import com.intellij.util.xml.DomElement; 4 | import com.intellij.util.xml.Required; 5 | import com.intellij.util.xml.SubTagList; 6 | import java.util.List; 7 | 8 | /** 9 | * @author yanglin 10 | */ 11 | public interface Discriminator extends DomElement { 12 | 13 | @Required 14 | @SubTagList("case") 15 | public List getCases(); 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/dom/model/Foreach.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.dom.model; 2 | 3 | /** 4 | * @author yanglin 5 | */ 6 | public interface Foreach extends GroupOne { 7 | 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/dom/model/GroupFour.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.dom.model; 2 | 3 | import com.intellij.util.xml.DomElement; 4 | import com.intellij.util.xml.SubTag; 5 | import com.intellij.util.xml.SubTagList; 6 | import java.util.List; 7 | 8 | /** 9 | * @author yanglin 10 | * @function dom的一些共有属性,归类 11 | */ 12 | public interface GroupFour extends DomElement { 13 | 14 | @SubTag("constructor") 15 | public Constructor getConstructor(); 16 | 17 | @SubTagList("id") 18 | public List getIds(); 19 | 20 | @SubTagList("result") 21 | public List getResults(); 22 | 23 | @SubTagList("association") 24 | public List getAssociations(); 25 | 26 | @SubTagList("collection") 27 | public List getCollections(); 28 | 29 | @SubTag("discriminator") 30 | public Discriminator getDiscriminator(); 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/dom/model/GroupOne.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.dom.model; 2 | 3 | import com.intellij.util.xml.DomElement; 4 | import com.intellij.util.xml.SubTagList; 5 | import java.util.List; 6 | import org.jetbrains.annotations.NotNull; 7 | 8 | /** 9 | * @author yanglin 10 | * @update: itar 11 | * @function xml中的一系列dom节点,包括include, trim , where 等标签 12 | */ 13 | public interface GroupOne extends DomElement { 14 | 15 | @NotNull 16 | @SubTagList("include") 17 | List getIncludes(); 18 | 19 | @NotNull 20 | @SubTagList("trim") 21 | List getTrims(); 22 | 23 | @NotNull 24 | @SubTagList("where") 25 | List getWheres(); 26 | 27 | @NotNull 28 | @SubTagList("set") 29 | List getSets(); 30 | 31 | @NotNull 32 | @SubTagList("foreach") 33 | List getForeachs(); 34 | 35 | @NotNull 36 | @SubTagList("choose") 37 | List getChooses(); 38 | 39 | @NotNull 40 | @SubTagList("if") 41 | List getIfs(); 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/dom/model/GroupThree.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.dom.model; 2 | 3 | import com.intellij.util.xml.SubTagList; 4 | import java.util.List; 5 | 6 | /** 7 | * @author yanglin 8 | * @updater itar 9 | */ 10 | public interface GroupThree extends GroupTwo { 11 | 12 | @SubTagList("selectKey") 13 | public List getSelectKey(); 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/dom/model/GroupTwo.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.dom.model; 2 | 3 | import com.github.mustfun.mybatis.plugin.dom.converter.AliasConverter; 4 | import com.github.mustfun.mybatis.plugin.dom.converter.DaoMethodConverter; 5 | import com.github.mustfun.mybatis.plugin.dom.converter.ParameterMapConverter; 6 | import com.intellij.psi.PsiClass; 7 | import com.intellij.psi.xml.XmlTag; 8 | import com.intellij.util.xml.*; 9 | 10 | import java.util.List; 11 | import org.jetbrains.annotations.NotNull; 12 | 13 | /** 14 | * xml中的一些节点,parameterMap , parameterType 15 | * @author yanglin 16 | * @updater itar 17 | */ 18 | public interface GroupTwo extends GroupOne, IdDomElement { 19 | 20 | @SubTagList("bind") 21 | List getBinds(); 22 | 23 | @NotNull 24 | @Attribute("parameterMap") 25 | @Convert(ParameterMapConverter.class) 26 | GenericAttributeValue getParameterMap(); 27 | 28 | @Override 29 | @Attribute("id") 30 | @Convert(DaoMethodConverter.class) 31 | GenericAttributeValue getId(); 32 | 33 | @NotNull 34 | @Attribute("parameterType") 35 | @Convert(AliasConverter.class) 36 | GenericAttributeValue getParameterType(); 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/dom/model/Id.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.dom.model; 2 | 3 | /** 4 | * @author yanglin 5 | */ 6 | public interface Id extends PropertyGroup { 7 | 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/dom/model/IdArg.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.dom.model; 2 | 3 | import com.intellij.util.xml.DomElement; 4 | 5 | /** 6 | * @author yanglin 7 | */ 8 | public interface IdArg extends DomElement { 9 | 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/dom/model/IdDomElement.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.dom.model; 2 | 3 | import com.intellij.util.xml.*; 4 | 5 | /** 6 | * @author yanglin 7 | * @update itar 8 | * @function 通用的dom元素 - id select/update等都有id属性 9 | */ 10 | public interface IdDomElement extends DomElement { 11 | 12 | /** 13 | * 通用的属性值 14 | * NameValue表示这里的id有可能是string,有可能是其他值 15 | */ 16 | @Required 17 | @NameValue 18 | @Attribute("id") 19 | GenericAttributeValue getId(); 20 | 21 | /** 22 | * 固定格式setValue方法,否则加@TagValue注解 23 | * @param content 24 | */ 25 | void setValue(String content); 26 | 27 | 28 | /** 29 | * 固定格式getValue,否则加@TagValue注解 30 | */ 31 | String getValue(); 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/dom/model/If.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.dom.model; 2 | 3 | /** 4 | * @author yanglin 5 | */ 6 | public interface If extends GroupOne { 7 | 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/dom/model/Include.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.dom.model; 2 | 3 | import com.github.mustfun.mybatis.plugin.dom.converter.SqlConverter; 4 | import com.intellij.psi.xml.XmlTag; 5 | import com.intellij.util.xml.Attribute; 6 | import com.intellij.util.xml.Convert; 7 | import com.intellij.util.xml.DomElement; 8 | import com.intellij.util.xml.GenericAttributeValue; 9 | 10 | /** 11 | * @author yanglin 12 | * @updater itar 13 | * @function 各个sql里面的include标签 14 | */ 15 | public interface Include extends DomElement { 16 | 17 | @Attribute("refid") 18 | @Convert(SqlConverter.class) 19 | public GenericAttributeValue getRefId(); 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/dom/model/Insert.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.dom.model; 2 | 3 | /** 4 | * insert语句 5 | * @author yanglin 6 | * @updater itar 7 | */ 8 | public interface Insert extends GroupThree { 9 | 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/dom/model/Mapper.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.dom.model; 2 | 3 | import com.intellij.util.xml.*; 4 | 5 | import java.util.List; 6 | 7 | import org.jetbrains.annotations.NotNull; 8 | 9 | /** 10 | * @author yanglin 11 | * @updater itar 12 | * @function mapper节点描述信息,为了找到mapper.xml起作用的 13 | * 就是根据这些描述信息找到mapper文件的 14 | */ 15 | public interface Mapper extends DomElement { 16 | 17 | /** 18 | * 共有节点 - 属性列表 19 | * 20 | * @return 21 | */ 22 | @NotNull 23 | @SubTagsList({"insert", "update", "delete", "select"}) 24 | public List getMergedDaoElements(); 25 | 26 | @Required 27 | /** 28 | * @NameValue表示返回string或者GenericValue两种属性 29 | */ 30 | @NameValue 31 | @NotNull 32 | @Attribute("namespace") 33 | public GenericAttributeValue getNamespace(); 34 | 35 | @NotNull 36 | @SubTagList("resultMap") 37 | public List getResultMaps(); 38 | 39 | /** 40 | * mapper下面的子标签,parameterMap 41 | * 42 | * @return 43 | */ 44 | @NotNull 45 | @SubTagList("parameterMap") 46 | public List getParameterMaps(); 47 | 48 | /** 49 | * mapper下面的sql标签 50 | * 51 | * @return 52 | */ 53 | @NotNull 54 | @SubTagList("sql") 55 | public List getSqls(); 56 | 57 | /** 58 | * insert标签 59 | * 60 | * @return 61 | */ 62 | @NotNull 63 | @SubTagList("insert") 64 | public List getInserts(); 65 | 66 | /** 67 | * update标签 68 | * 69 | * @return 70 | */ 71 | @NotNull 72 | @SubTagList("update") 73 | public List getUpdates(); 74 | 75 | /** 76 | * delete标签 77 | * 78 | * @return 79 | */ 80 | @NotNull 81 | @SubTagList("delete") 82 | public List getDeletes(); 83 | 84 | /** 85 | * select标签 86 | * 87 | * @return 88 | */ 89 | @NotNull 90 | @SubTagList("select") 91 | public List 就拿到的是一个PsiMethod,那么就不会报错了,高级 22 | * @param element 23 | * @param holder 24 | * @param helper 25 | * 还会对required注解进行校验 26 | */ 27 | @Override 28 | protected void checkDomElement(DomElement element, DomElementAnnotationHolder holder, 29 | DomHighlightingHelper helper) { 30 | super.checkDomElement(element, holder, helper); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/inspection/ResultTypeQuickFix.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.inspection; 2 | 3 | import com.github.mustfun.mybatis.plugin.dom.model.Select; 4 | import com.intellij.codeInspection.ProblemDescriptor; 5 | import com.intellij.openapi.project.Project; 6 | import com.intellij.psi.PsiClass; 7 | import com.intellij.psi.SmartPointerManager; 8 | import com.intellij.psi.SmartPsiElementPointer; 9 | import com.intellij.util.xml.GenericAttributeValue; 10 | import org.jetbrains.annotations.NotNull; 11 | 12 | import java.util.Objects; 13 | 14 | /** 15 | * @author yanglin 16 | * @updater itar 17 | * @function 快速修复返回值不匹配 18 | */ 19 | public class ResultTypeQuickFix extends GenericQuickFix { 20 | 21 | public static final String CORRECT_RESULTTYPE = "修正返回类型"; 22 | private Select select; 23 | private SmartPsiElementPointer targetPointer; 24 | 25 | public ResultTypeQuickFix(@NotNull Select select, @NotNull PsiClass target) { 26 | this.select = select; 27 | this.targetPointer = SmartPointerManager.getInstance(target.getProject()).createSmartPsiElementPointer(target, target.getContainingFile()); 28 | } 29 | 30 | @NotNull 31 | @Override 32 | public String getName() { 33 | return CORRECT_RESULTTYPE; 34 | } 35 | 36 | @Override 37 | public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { 38 | GenericAttributeValue resultType = select.getResultType(); 39 | resultType.setValue(targetPointer.getElement()); 40 | } 41 | 42 | @NotNull 43 | public PsiClass getTarget() { 44 | return Objects.requireNonNull(targetPointer.getElement()); 45 | } 46 | 47 | 48 | @NotNull 49 | public Select getSelect() { 50 | return select; 51 | } 52 | 53 | public void setSelect(@NotNull Select select) { 54 | this.select = select; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/inspection/StatementNotExistsQuickFix.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.inspection; 2 | 3 | import com.github.mustfun.mybatis.plugin.generate.AbstractStatementGenerator; 4 | import com.intellij.codeInspection.ProblemDescriptor; 5 | import com.intellij.openapi.project.Project; 6 | import com.intellij.psi.PsiMethod; 7 | import com.intellij.psi.SmartPointerManager; 8 | import com.intellij.psi.SmartPsiElementPointer; 9 | import org.jetbrains.annotations.NotNull; 10 | 11 | import java.util.Objects; 12 | 13 | /** 14 | * @author yanglin 15 | * @updater itar 16 | * @function xml不存在的一个快速修复 17 | */ 18 | public class StatementNotExistsQuickFix extends GenericQuickFix { 19 | 20 | public static final String GENERATE_STATEMENT = "生成SQL语句"; 21 | private SmartPsiElementPointer methodPointer; 22 | 23 | public StatementNotExistsQuickFix(@NotNull PsiMethod method) { 24 | methodPointer = SmartPointerManager.getInstance(method.getProject()).createSmartPsiElementPointer(method, method.getContainingFile()); 25 | } 26 | 27 | @NotNull 28 | @Override 29 | public String getName() { 30 | return GENERATE_STATEMENT; 31 | } 32 | 33 | @Override 34 | public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { 35 | AbstractStatementGenerator.applyGenerate(methodPointer.getElement()); 36 | } 37 | 38 | @NotNull 39 | public PsiMethod getMethod() { 40 | return Objects.requireNonNull(methodPointer.getElement()); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/intention/GenerateMapperChooser.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.intention; 2 | 3 | import com.intellij.psi.PsiClass; 4 | import com.intellij.psi.PsiElement; 5 | import com.intellij.psi.util.PsiTreeUtil; 6 | import org.jetbrains.annotations.NotNull; 7 | 8 | /** 9 | * @author yanglin 10 | * @updater itar 11 | * 自定义chooser , 主要是avaliable方法 12 | */ 13 | public class GenerateMapperChooser extends JavaFileIntentionChooser { 14 | 15 | static final JavaFileIntentionChooser INSTANCE = new GenerateMapperChooser(); 16 | 17 | @Override 18 | public boolean isAvailable(@NotNull PsiElement element) { 19 | if (isPositionOfInterfaceDeclaration(element)) { 20 | PsiClass clazz = PsiTreeUtil.getParentOfType(element, PsiClass.class); 21 | if (null != clazz) { 22 | return !isTargetPresentInXml(clazz); 23 | } 24 | } 25 | return false; 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/intention/GenerateParamAnnotationIntention.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.intention; 2 | 3 | import com.github.mustfun.mybatis.plugin.service.AnnotationService; 4 | import com.intellij.openapi.editor.Editor; 5 | import com.intellij.openapi.project.Project; 6 | import com.intellij.psi.PsiElement; 7 | import com.intellij.psi.PsiFile; 8 | import com.intellij.psi.PsiMethod; 9 | import com.intellij.psi.PsiParameter; 10 | import com.intellij.psi.util.PsiTreeUtil; 11 | import com.intellij.util.IncorrectOperationException; 12 | import org.jetbrains.annotations.NotNull; 13 | 14 | /** 15 | * @author yanglin 16 | * @update by itar alt+enter 响应 , 添加@Param注解 17 | */ 18 | public class GenerateParamAnnotationIntention extends GenericIntention { 19 | 20 | public static final String ADD_MYBATIS_PARAM_ANNOTATION = "[mybatis lite]添加@param注解"; 21 | 22 | public GenerateParamAnnotationIntention() { 23 | super(GenerateParamChooser.INSTANCE); 24 | } 25 | 26 | @NotNull 27 | @Override 28 | public String getText() { 29 | return ADD_MYBATIS_PARAM_ANNOTATION; 30 | } 31 | 32 | @Override 33 | public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException { 34 | PsiElement element = file.findElementAt(editor.getCaretModel().getOffset()); 35 | PsiParameter parameter = PsiTreeUtil.getParentOfType(element, PsiParameter.class); 36 | AnnotationService annotationService = AnnotationService.getInstance(project); 37 | if (null != parameter) { 38 | annotationService.addAnnotationWithParameterName(parameter); 39 | } else { 40 | PsiMethod method = PsiTreeUtil.getParentOfType(element, PsiMethod.class); 41 | if (null != method) { 42 | annotationService.addAnnotationWithParameterNameForMethodParameters(method); 43 | } 44 | } 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/intention/GenerateParamChooser.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.intention; 2 | 3 | import com.github.mustfun.mybatis.plugin.annotation.Annotation; 4 | import com.github.mustfun.mybatis.plugin.util.JavaUtils; 5 | import com.intellij.psi.PsiElement; 6 | import com.intellij.psi.PsiMethod; 7 | import com.intellij.psi.PsiParameter; 8 | import com.intellij.psi.util.PsiTreeUtil; 9 | import org.jetbrains.annotations.NotNull; 10 | 11 | /** 12 | * @author yanglin 13 | */ 14 | public class GenerateParamChooser extends JavaFileIntentionChooser { 15 | 16 | public static final JavaFileIntentionChooser INSTANCE = new GenerateParamChooser(); 17 | 18 | @Override 19 | public boolean isAvailable(@NotNull PsiElement element) { 20 | PsiParameter parameter = PsiTreeUtil.getParentOfType(element, PsiParameter.class); 21 | PsiMethod method = PsiTreeUtil.getParentOfType(element, PsiMethod.class); 22 | return (null != parameter && !JavaUtils.isAnnotationPresent(parameter, Annotation.PARAM)) || 23 | (null != method && !JavaUtils.isAllParameterWithAnnotation(method, Annotation.PARAM)); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/intention/GenerateStatementChooser.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.intention; 2 | 3 | import com.github.mustfun.mybatis.plugin.annotation.Annotation; 4 | import com.github.mustfun.mybatis.plugin.util.JavaUtils; 5 | import com.intellij.psi.PsiClass; 6 | import com.intellij.psi.PsiElement; 7 | import com.intellij.psi.PsiMethod; 8 | import com.intellij.psi.util.PsiTreeUtil; 9 | import org.jetbrains.annotations.NotNull; 10 | 11 | /** 12 | * @author yanglin 13 | */ 14 | public class GenerateStatementChooser extends JavaFileIntentionChooser { 15 | 16 | public static final JavaFileIntentionChooser INSTANCE = new GenerateStatementChooser(); 17 | 18 | @Override 19 | public boolean isAvailable(@NotNull PsiElement element) { 20 | if (!isPositionOfMethodDeclaration(element)) { 21 | return false; 22 | } 23 | PsiMethod method = PsiTreeUtil.getParentOfType(element, PsiMethod.class); 24 | PsiClass clazz = PsiTreeUtil.getParentOfType(element, PsiClass.class); 25 | return null != method && null != clazz && 26 | !JavaUtils.isAnyAnnotationPresent(method, Annotation.STATEMENT_SYMMETRIES) && 27 | !isTargetPresentInXml(method) && 28 | isTargetPresentInXml(clazz); 29 | } 30 | } -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/intention/GenerateStatementIntention.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.intention; 2 | 3 | import com.github.mustfun.mybatis.plugin.generate.AbstractStatementGenerator; 4 | import com.github.mustfun.mybatis.plugin.setting.MybatisLiteSetting; 5 | import com.github.mustfun.mybatis.plugin.util.MybatisConstants; 6 | import com.intellij.ide.util.PropertiesComponent; 7 | import com.intellij.openapi.editor.Editor; 8 | import com.intellij.openapi.project.Project; 9 | import com.intellij.psi.PsiElement; 10 | import com.intellij.psi.PsiFile; 11 | import com.intellij.psi.PsiMethod; 12 | import com.intellij.psi.util.PsiTreeUtil; 13 | import com.intellij.util.IncorrectOperationException; 14 | import org.jetbrains.annotations.NotNull; 15 | 16 | import java.util.Map; 17 | 18 | /** 19 | * @author yanglin 20 | * @update itar 21 | * @function 生成语句用 22 | */ 23 | public class GenerateStatementIntention extends GenericIntention { 24 | 25 | public GenerateStatementIntention() { 26 | super(GenerateStatementChooser.INSTANCE); 27 | } 28 | 29 | @NotNull 30 | @Override 31 | public String getText() { 32 | return "[Mybatis Lite] 生成xml-sql语句"; 33 | } 34 | 35 | @Override 36 | public void invoke(@NotNull final Project project, Editor editor, PsiFile file) throws IncorrectOperationException { 37 | Map valueMap = MybatisLiteSetting.getInstance().getValueMap(); 38 | if (!MybatisConstants.TRUE.equalsIgnoreCase(valueMap.get(MybatisConstants.NAVIGATION_OPEN_STATUS))) { 39 | return; 40 | } 41 | 42 | PsiElement element = file.findElementAt(editor.getCaretModel().getOffset()); 43 | PsiMethod method = PsiTreeUtil.getParentOfType(element, PsiMethod.class); 44 | //generator开始生成语句 45 | AbstractStatementGenerator.applyGenerate(method); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/intention/GenericIntention.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.intention; 2 | 3 | import com.intellij.codeInsight.intention.IntentionAction; 4 | import com.intellij.openapi.editor.Editor; 5 | import com.intellij.openapi.project.Project; 6 | import com.intellij.psi.PsiFile; 7 | import org.jetbrains.annotations.NotNull; 8 | 9 | /** 10 | * @author yanglin 11 | * @update itar 12 | * @function 通用alt+enter检测器,继承IntentionAction接口,alt+enter唤醒 13 | */ 14 | public abstract class GenericIntention implements IntentionAction { 15 | 16 | protected IntentionChooser chooser; 17 | 18 | public GenericIntention(@NotNull IntentionChooser chooser) { 19 | this.chooser = chooser; 20 | } 21 | 22 | /** 23 | * alt+enter操作所在的组 24 | * @return 25 | */ 26 | @NotNull 27 | @Override 28 | public String getFamilyName() { 29 | return getText(); 30 | } 31 | 32 | /** 33 | * 是否可用,可用时候高亮显示 34 | * @param project 35 | * @param editor 36 | * @param file 37 | * @return 38 | */ 39 | @Override 40 | public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) { 41 | return chooser.isAvailable(project, editor, file); 42 | } 43 | 44 | /** 45 | * 系统在做写入操作时候是否可用 46 | * @return 47 | */ 48 | @Override 49 | public boolean startInWriteAction() { 50 | return true; 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/intention/IntentionChooser.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.intention; 2 | 3 | import com.intellij.openapi.editor.Editor; 4 | import com.intellij.openapi.project.Project; 5 | import com.intellij.psi.PsiFile; 6 | import org.jetbrains.annotations.NotNull; 7 | 8 | /** 9 | * @author yanglin 最终实现操作的接口 10 | */ 11 | public interface IntentionChooser { 12 | 13 | /** 14 | * 15 | * @param project 16 | * @param editor 17 | * @param file 18 | * @return 19 | */ 20 | public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file); 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/intention/JavaFileIntentionChooser.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.intention; 2 | 3 | import com.github.mustfun.mybatis.plugin.service.JavaService; 4 | import com.github.mustfun.mybatis.plugin.util.JavaUtils; 5 | import com.intellij.openapi.editor.Editor; 6 | import com.intellij.openapi.project.Project; 7 | import com.intellij.psi.*; 8 | import org.jetbrains.annotations.NotNull; 9 | 10 | /** 11 | * @author yanglin 12 | * @updater itar 自定义alt+enter选择框 13 | */ 14 | public abstract class JavaFileIntentionChooser implements IntentionChooser { 15 | 16 | @Override 17 | public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) { 18 | if (!(file instanceof PsiJavaFile)) { 19 | return false; 20 | } 21 | PsiElement element = file.findElementAt(editor.getCaretModel().getOffset()); 22 | return null != element && JavaUtils.isElementWithinInterface(element) && isAvailable(element); 23 | } 24 | 25 | /** 26 | * 是否可用方法 27 | * @param element 28 | * @return 29 | */ 30 | public abstract boolean isAvailable(@NotNull PsiElement element); 31 | 32 | /** 33 | * 当前元素是不是一个参数 34 | * @param element 35 | * @return 36 | */ 37 | public boolean isPositionOfParameterDeclaration(@NotNull PsiElement element) { 38 | return element.getParent() instanceof PsiParameter; 39 | } 40 | 41 | /** 42 | * 当前元素是不是一个方法 43 | * @param element 44 | * @return 45 | */ 46 | public boolean isPositionOfMethodDeclaration(@NotNull PsiElement element) { 47 | return element.getParent() instanceof PsiMethod; 48 | } 49 | 50 | /** 51 | * 当前元素是不是一个类 52 | * @param element 53 | * @return 54 | */ 55 | public boolean isPositionOfInterfaceDeclaration(@NotNull PsiElement element) { 56 | return element.getParent() instanceof PsiClass; 57 | } 58 | 59 | /** 60 | * 当前元素是不是在xml里面 61 | * @param element 62 | * @return 63 | */ 64 | public boolean isTargetPresentInXml(@NotNull PsiElement element) { 65 | return JavaService.getInstance(element.getProject()).findWithFindFirstProcessor(element).isPresent(); 66 | } 67 | 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/listener/SettingApplyListener.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.listener; 2 | 3 | import com.github.mustfun.mybatis.plugin.init.InitMybatisLiteActivity; 4 | import com.github.mustfun.mybatis.plugin.util.MybatisConstants; 5 | import com.intellij.openapi.project.Project; 6 | import com.intellij.openapi.project.ProjectManager; 7 | 8 | import java.util.Map; 9 | 10 | import static com.github.mustfun.mybatis.plugin.util.MybatisConstants.*; 11 | 12 | /** 13 | * @author itar 14 | * @date 2020-03-09 15 | * 设置保存的时候唤醒 16 | */ 17 | public class SettingApplyListener { 18 | 19 | /** 20 | * 21 | * @param newMap 22 | * @param rawStateMap 23 | */ 24 | public void notify(Map newMap, Map rawStateMap) { 25 | boolean a = false,b=false; 26 | if (checkNeedRefresh(newMap.get(CONFIG_FILE_NAME), rawStateMap.get(CONFIG_FILE_NAME))){ 27 | a = true; 28 | } 29 | if (checkNeedRefresh(newMap.get(SQL_PRINT_STATUS), rawStateMap.get(SQL_PRINT_STATUS))){ 30 | b = true; 31 | } 32 | if (a||b) { 33 | refresh(); 34 | } 35 | } 36 | 37 | private void refresh() { 38 | Project[] defaultProject = ProjectManager.getInstance().getOpenProjects(); 39 | for (Project project : defaultProject) { 40 | new InitMybatisLiteActivity().runActivity(project); 41 | } 42 | } 43 | 44 | private boolean checkNeedRefresh(String configNameNew, String configNameRaw) { 45 | if (configNameNew==null){ 46 | return false; 47 | } 48 | if (configNameNew.equalsIgnoreCase(configNameRaw)){ 49 | return false; 50 | } 51 | return true; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/locator/LocateStrategy.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.locator; 2 | 3 | import com.intellij.psi.PsiClass; 4 | import org.jetbrains.annotations.NotNull; 5 | 6 | /** 7 | * @author yanglin 8 | * @author itar 9 | * @function 本地策略类 10 | */ 11 | public abstract class LocateStrategy { 12 | 13 | public abstract boolean apply(@NotNull PsiClass clazz); 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/locator/MapperLocator.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.locator; 2 | 3 | import com.github.mustfun.mybatis.plugin.util.JavaUtils; 4 | import com.intellij.openapi.components.ServiceManager; 5 | import com.intellij.openapi.project.Project; 6 | import com.intellij.psi.PsiClass; 7 | import com.intellij.psi.PsiMethod; 8 | import org.jetbrains.annotations.NotNull; 9 | import org.jetbrains.annotations.Nullable; 10 | 11 | /** 12 | * @author yanglin 13 | * @updater itar 14 | * @function mapper定位工具类 15 | */ 16 | public class MapperLocator { 17 | 18 | public static LocateStrategy dfltLocateStrategy = new PackageLocateStrategy(); 19 | 20 | public static MapperLocator getInstance(@NotNull Project project) { 21 | return ServiceManager.getService(project, MapperLocator.class); 22 | } 23 | 24 | public boolean process(@Nullable PsiMethod method) { 25 | return null != method && process(method.getContainingClass()); 26 | } 27 | 28 | public boolean process(@Nullable PsiClass clazz) { 29 | return null != clazz && JavaUtils.isElementWithinInterface(clazz) && dfltLocateStrategy.apply(clazz); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/locator/MapperXmlPackageProvider.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.locator; 2 | 3 | import com.github.mustfun.mybatis.plugin.dom.model.Mapper; 4 | import com.github.mustfun.mybatis.plugin.util.MapperUtils; 5 | import com.google.common.collect.Sets; 6 | import com.intellij.openapi.project.Project; 7 | import com.intellij.psi.JavaPsiFacade; 8 | import com.intellij.psi.PsiClass; 9 | import com.intellij.psi.PsiFile; 10 | import com.intellij.psi.PsiJavaFile; 11 | import com.intellij.psi.PsiPackage; 12 | import com.intellij.psi.search.GlobalSearchScope; 13 | import java.util.Collection; 14 | import java.util.HashSet; 15 | import java.util.Set; 16 | import org.jetbrains.annotations.NotNull; 17 | 18 | /** 19 | * @author yanglin 20 | * @updater itar 21 | * @function 找到所有mapper文件对应的包信息 22 | */ 23 | public class MapperXmlPackageProvider extends PackageProvider { 24 | 25 | @NotNull 26 | @Override 27 | public Set getPackages(@NotNull Project project) { 28 | HashSet res = Sets.newHashSet(); 29 | //找到所有的mapper文件 30 | Collection mappers = MapperUtils.findMappers(project); 31 | //findClass、package的一个Service 32 | JavaPsiFacade javaPsiFacade = JavaPsiFacade.getInstance(project); 33 | for (Mapper mapper : mappers) { 34 | String namespace = MapperUtils.getNamespace(mapper); 35 | PsiClass clazz = javaPsiFacade.findClass(namespace, GlobalSearchScope.allScope(project)); 36 | if (null != clazz) { 37 | PsiFile file = clazz.getContainingFile(); 38 | if (file instanceof PsiJavaFile) { 39 | String packageName = ((PsiJavaFile) file).getPackageName(); 40 | PsiPackage pkg = javaPsiFacade.findPackage(packageName); 41 | if (null != pkg) { 42 | res.add(pkg); 43 | } 44 | } 45 | } 46 | } 47 | return res; 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/locator/PackageLocateStrategy.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.locator; 2 | 3 | import com.intellij.psi.JavaPsiFacade; 4 | import com.intellij.psi.PsiClass; 5 | import com.intellij.psi.PsiJavaFile; 6 | import com.intellij.psi.PsiPackage; 7 | import org.jetbrains.annotations.NotNull; 8 | 9 | /** 10 | * @author yanglin 11 | * @updater itar 12 | * @function 包定位策略类 13 | */ 14 | public class PackageLocateStrategy extends LocateStrategy { 15 | 16 | private PackageProvider provider = new MapperXmlPackageProvider(); 17 | 18 | /** 19 | * 检查传递进来的class文件是不是对应的mapper文件 20 | * @param clazz 21 | * @return 22 | */ 23 | @Override 24 | public boolean apply(@NotNull PsiClass clazz) { 25 | String packageName = ((PsiJavaFile) clazz.getContainingFile()).getPackageName(); 26 | PsiPackage pkg = JavaPsiFacade.getInstance(clazz.getProject()).findPackage(packageName); 27 | for (PsiPackage tmp : provider.getPackages(clazz.getProject())) { 28 | if (tmp.equals(pkg)) { 29 | return true; 30 | } 31 | } 32 | return false; 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/locator/PackageProvider.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.locator; 2 | 3 | import com.intellij.openapi.project.Project; 4 | import com.intellij.psi.PsiPackage; 5 | import java.util.Set; 6 | import org.jetbrains.annotations.NotNull; 7 | 8 | /** 9 | * @author yanglin 10 | * @updater itar 11 | * @function 包的提供者 12 | */ 13 | public abstract class PackageProvider { 14 | 15 | @NotNull 16 | public abstract Set getPackages(@NotNull Project project); 17 | 18 | } -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/model/DbSourcePo.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.model; 2 | 3 | import java.util.Date; 4 | import lombok.Getter; 5 | import lombok.Setter; 6 | 7 | /** 8 | * @author itar 9 | * @version 1.0 10 | * @date 2018/5/16 11 | * @since 1.0 12 | */ 13 | @Setter 14 | @Getter 15 | public class DbSourcePo { 16 | 17 | private Integer id; 18 | private Integer port; 19 | private String userName; 20 | private String url; 21 | private String dbAddress; 22 | private String password; 23 | private String dbName; 24 | private Date createTime; 25 | private String moduleName; 26 | 27 | public DbSourcePo() { 28 | 29 | } 30 | 31 | public DbSourcePo(String dbAddress, Integer port, String dbName, String userName, String password, String moduleName) { 32 | this.port = port; 33 | this.userName = userName; 34 | this.dbAddress = dbAddress; 35 | this.password = password; 36 | this.dbName = dbName; 37 | this.moduleName = moduleName; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/model/LocalColumn.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.model; 2 | 3 | import lombok.Getter; 4 | import lombok.Setter; 5 | 6 | /** 7 | * @author itar 8 | * @version 1.0 9 | * @date 2018/4/16 10 | * @since 1.0 11 | */ 12 | @Setter 13 | @Getter 14 | public class LocalColumn { 15 | 16 | /** 17 | * 列名 18 | */ 19 | private String columnName; 20 | /** 21 | * 数据类型 22 | */ 23 | private String dataType; 24 | private String columnComment; 25 | private Integer size; 26 | /** 27 | * 是否可空 28 | */ 29 | private Boolean nullable; 30 | private Integer position; 31 | 32 | private String extra; 33 | private String jpaColumnDefinition; 34 | /** 35 | * 属性名称 36 | */ 37 | private String attrName; 38 | /** 39 | * 属性小写名称 40 | */ 41 | private String attrLittleName; 42 | 43 | /** 44 | * 属性类型 45 | */ 46 | private String attrType; 47 | /** 48 | * 属性类型全路径 49 | */ 50 | private String attrTypePath; 51 | /** 52 | * 主键 53 | */ 54 | private String pk; 55 | private String columnKey; 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/model/LocalTable.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.model; 2 | 3 | import java.util.List; 4 | import lombok.Getter; 5 | import lombok.Setter; 6 | 7 | /** 8 | * @author itar 9 | * @version 1.0 10 | * @date 2018/4/16 11 | * @since 1.0 12 | */ 13 | @Setter 14 | @Getter 15 | public class LocalTable { 16 | 17 | private Integer id; 18 | private String tableName; 19 | private String tableType; 20 | private String comment; 21 | private String className; 22 | private String classLittleName; 23 | private List columnList; 24 | 25 | private LocalColumn pk; 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/model/ModuleConfig.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.model; 2 | 3 | import lombok.Getter; 4 | import lombok.Setter; 5 | 6 | /** 7 | * module的配置文件 - yml对应的配置信息 8 | * @author itar 9 | * @version 1.0 10 | * @date 2018/5/16 11 | * @since 1.0 12 | */ 13 | @Setter 14 | @Getter 15 | public class ModuleConfig extends DbSourcePo{ 16 | /** 17 | * mybatis 18 | */ 19 | private String typeAliasPackage; 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/model/PluginConfig.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.model; 2 | 3 | import lombok.Getter; 4 | import lombok.Setter; 5 | 6 | /** 7 | * @author itar 8 | * @version 1.0 9 | * @date 2018/6/21 10 | * @since 1.0 11 | */ 12 | @Setter 13 | @Getter 14 | public class PluginConfig { 15 | 16 | private Integer id; 17 | private String key; 18 | private String value; 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/model/Template.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.model; 2 | 3 | import java.util.Date; 4 | import lombok.Getter; 5 | import lombok.Setter; 6 | 7 | /** 8 | * @author itar 9 | * @version 1.0 10 | * @date 2018/5/16 11 | * @since 1.0 12 | */ 13 | 14 | @Setter 15 | @Getter 16 | public class Template { 17 | 18 | private Integer id; 19 | private String tepName; 20 | private String tepDesc; 21 | private String tepContent; 22 | private String createBy; 23 | /** 24 | * 模板类型 25 | */ 26 | private Integer vmType; 27 | /** 28 | * db类型 29 | */ 30 | private Integer dbType; 31 | private Date createTime; 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/model/enums/VmTypeEnums.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.model.enums; 2 | 3 | /** 4 | * @author itar 5 | * @version 1.0 6 | * @date 2018/5/17 7 | * @since 1.0 8 | */ 9 | public enum VmTypeEnums { 10 | RESULT(4, "Result层"), 11 | MODEL_PO(5, "Po层"), 12 | MODEL_BO(6, "Bo层"), 13 | MODEL_REQ(7, "请求Model层"), 14 | MODEL_RESP(8, "返回Model层"), 15 | DAO(9, "DAO层"), 16 | MAPPER(10, "Mapper文件"), 17 | SERVICE(11, "Service层"), 18 | SERVICE_IMPL(12, "ServiceImpl实现层"), 19 | CONTROLLER(13, "Controller层"), 20 | CONTROLLER_IMPL(14, "ControllerImpl实现层"); 21 | 22 | private Integer code; 23 | private String mgs; 24 | 25 | VmTypeEnums(Integer code, String mgs) { 26 | this.code = code; 27 | this.mgs = mgs; 28 | } 29 | 30 | public static VmTypeEnums findVmNameByVmType(Integer code) { 31 | for (VmTypeEnums vmTypeEnums : VmTypeEnums.values()) { 32 | if (vmTypeEnums.getCode().equals(code)) { 33 | return vmTypeEnums; 34 | } 35 | } 36 | return VmTypeEnums.RESULT; 37 | } 38 | 39 | public Integer getCode() { 40 | return code; 41 | } 42 | 43 | public void setCode(Integer code) { 44 | this.code = code; 45 | } 46 | 47 | public String getMgs() { 48 | return mgs; 49 | } 50 | 51 | public void setMgs(String mgs) { 52 | this.mgs = mgs; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/provider/AbstractFileProvider.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.provider; 2 | 3 | 4 | import com.intellij.openapi.fileTypes.LanguageFileType; 5 | import com.intellij.openapi.project.Project; 6 | import com.intellij.psi.PsiDirectory; 7 | import com.intellij.psi.PsiFile; 8 | import com.intellij.psi.PsiFileFactory; 9 | import com.intellij.psi.PsiJavaFile; 10 | import com.intellij.psi.codeStyle.CodeStyleManager; 11 | import com.intellij.psi.codeStyle.JavaCodeStyleManager; 12 | import org.jetbrains.annotations.NotNull; 13 | 14 | /** 15 | * 文件提供者抽象类 16 | * 17 | * @author hehaiyangwork@gmail.com 18 | * @date 2017/04/07 19 | * @update itar 20 | */ 21 | public abstract class AbstractFileProvider { 22 | 23 | protected Project project; 24 | 25 | protected String outputPath; 26 | 27 | public AbstractFileProvider(Project project, String outputPath) { 28 | this.project = project; 29 | this.outputPath = outputPath; 30 | } 31 | 32 | public abstract PsiFile create(String fullFile, String fileName); 33 | 34 | protected PsiFile createFile(Project project, @NotNull PsiDirectory psiDirectory, String fileName, String context, 35 | LanguageFileType fileType) { 36 | 37 | //+ final String simpleContent = XmlSorterUtil.replaceAllByRegex(content, ">" + lineSeparator + "*\\s+?<", "><"); 38 | String replace = context.replaceAll("\r\n", "\n"); 39 | PsiFile file = psiDirectory.findFile(fileName); 40 | if (file != null) { 41 | file.delete(); 42 | } 43 | PsiFile psiFile = PsiFileFactory.getInstance(project).createFileFromText(fileName, fileType, replace); 44 | // reformat class 45 | CodeStyleManager.getInstance(project).reformat(psiFile); 46 | if (psiFile instanceof PsiJavaFile) { 47 | JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(project); 48 | styleManager.optimizeImports(psiFile); 49 | styleManager.shortenClassReferences(psiFile); 50 | } 51 | //加载到磁盘中 52 | psiDirectory.add(psiFile); 53 | return psiFile; 54 | } 55 | 56 | } -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/provider/AbstractMarkerProviderAdaptor.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.provider; 2 | 3 | import com.intellij.codeInsight.daemon.LineMarkerInfo; 4 | import com.intellij.codeInsight.daemon.LineMarkerProvider; 5 | import com.intellij.psi.PsiElement; 6 | import java.util.Collection; 7 | import java.util.List; 8 | import org.jetbrains.annotations.NotNull; 9 | import org.jetbrains.annotations.Nullable; 10 | 11 | /** 12 | * @author yanglin 13 | * @updater itar 14 | * @function 行标记的一个适配器 15 | */ 16 | public abstract class AbstractMarkerProviderAdaptor implements LineMarkerProvider { 17 | 18 | 19 | 20 | @Nullable 21 | @Override 22 | public LineMarkerInfo getLineMarkerInfo(@NotNull PsiElement element) { 23 | return null; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/provider/DefaultProviderImpl.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.provider; 2 | 3 | 4 | import com.intellij.openapi.command.WriteCommandAction; 5 | import com.intellij.openapi.diagnostic.Logger; 6 | import com.intellij.openapi.fileTypes.LanguageFileType; 7 | import com.intellij.openapi.project.Project; 8 | import com.intellij.openapi.vfs.VfsUtil; 9 | import com.intellij.openapi.vfs.VirtualFile; 10 | import com.intellij.psi.PsiDirectory; 11 | import com.intellij.psi.PsiFile; 12 | import com.intellij.psi.impl.file.PsiDirectoryFactory; 13 | 14 | /** 15 | * 默认文件提供者 16 | * 17 | * @author hehaiyangwork@gmail.com 18 | * @date 2017/04/07 19 | * @update itar 20 | */ 21 | public class DefaultProviderImpl extends AbstractFileProvider { 22 | 23 | private static final Logger LOGGER = Logger.getInstance(DefaultProviderImpl.class); 24 | 25 | protected LanguageFileType languageFileType; 26 | 27 | public DefaultProviderImpl(Project project, String outputPath, LanguageFileType languageFileType) { 28 | super(project, outputPath); 29 | this.languageFileType = languageFileType; 30 | } 31 | 32 | @Override 33 | public PsiFile create(String fullFile, String fileName) { 34 | 35 | final PsiFile[] psiFile = {null}; 36 | 37 | WriteCommandAction.runWriteCommandAction(this.project, () -> { 38 | try { 39 | VirtualFile vFile = VfsUtil.createDirectoryIfMissing(outputPath); 40 | PsiDirectory psiDirectory = PsiDirectoryFactory.getInstance(this.project).createDirectory(vFile); 41 | //PsiDirectory directory = subDirectory(psiDirectory, template.getSubPath(), template.getResources()); 42 | 43 | //PsiPackage psiPackage = JavaDirectoryService.getInstance().getPackage(directory); 44 | 45 | psiFile[0] = createFile(project, psiDirectory, fileName, fullFile, this.languageFileType); 46 | } catch (Exception e) { 47 | LOGGER.error(e); 48 | } 49 | }); 50 | return psiFile[0]; 51 | } 52 | 53 | } -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/provider/FileProviderFactory.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.provider; 2 | 3 | 4 | import com.intellij.ide.highlighter.JavaFileType; 5 | import com.intellij.ide.highlighter.XmlFileType; 6 | import com.intellij.openapi.project.Project; 7 | import com.intellij.sql.SqlFileType; 8 | 9 | /** 10 | * 文件提供者工厂 11 | * 12 | * @author hehaiyangwork@gmail.com 13 | * @date 2017/3/17 14 | * @update itar 15 | */ 16 | public class FileProviderFactory { 17 | 18 | final private Project project; 19 | 20 | final private String outputPath; 21 | 22 | public FileProviderFactory(Project project, String outputPath) { 23 | this.project = project; 24 | this.outputPath = outputPath; 25 | } 26 | 27 | public AbstractFileProvider getInstance(String type) { 28 | if ("java".equals(type)) { 29 | return new DefaultProviderImpl(project, outputPath, JavaFileType.INSTANCE); 30 | } else if ("sql".equals(type)) { 31 | return new DefaultProviderImpl(project, outputPath, SqlFileType.INSTANCE); 32 | } else if ("xml".equals(type)) { 33 | return new DefaultProviderImpl(project, outputPath, XmlFileType.INSTANCE); 34 | } else { 35 | return new DefaultProviderImpl(project, outputPath, JavaFileType.INSTANCE); 36 | } 37 | } 38 | } -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/provider/MapperImplicitBeansProvider.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.provider; 2 | 3 | import com.github.mustfun.mybatis.plugin.dom.model.Mapper; 4 | import com.github.mustfun.mybatis.plugin.util.MapperUtils; 5 | import com.intellij.openapi.module.Module; 6 | import com.intellij.spring.model.CommonSpringBean; 7 | import com.intellij.spring.model.SpringImplicitBeansProviderBase; 8 | import com.intellij.util.xml.GenericAttributeValue; 9 | import org.jetbrains.annotations.NotNull; 10 | 11 | import java.util.ArrayList; 12 | import java.util.Collection; 13 | import java.util.List; 14 | 15 | /** 16 | * 将类注册进Spring模型 17 | * @author itar 18 | * @date 2020-03-02 19 | */ 20 | public class MapperImplicitBeansProvider extends SpringImplicitBeansProviderBase { 21 | 22 | 23 | @Override 24 | protected Collection getImplicitBeans(@NotNull Module module) { 25 | List beans = new ArrayList<>(5); 26 | Collection mappers = MapperUtils.findMappers(module.getProject()); 27 | for (Mapper mapper : mappers) { 28 | String stringValue = MapperUtils.getNamespace(mapper); 29 | String[] split = stringValue.split("\\."); 30 | String name = split[split.length - 1]; 31 | this.addImplicitLibraryBean(beans, module, stringValue, name); 32 | } 33 | return beans; 34 | } 35 | 36 | 37 | @NotNull 38 | @Override 39 | public String getProviderName() { 40 | return "Mybatis Lite"; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/provider/MapperInfrastructureBean.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.provider; 2 | 3 | import com.intellij.spring.model.SpringInfrastructureBean; 4 | 5 | 6 | public class MapperInfrastructureBean implements SpringInfrastructureBean { 7 | 8 | 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/refactoring/MapperRefactoringProvider.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.refactoring; 2 | 3 | import com.github.mustfun.mybatis.plugin.dom.model.Mapper; 4 | import com.github.mustfun.mybatis.plugin.util.MapperUtils; 5 | import com.intellij.openapi.application.ApplicationManager; 6 | import com.intellij.openapi.vfs.VirtualFile; 7 | import com.intellij.psi.PsiClass; 8 | import com.intellij.psi.PsiElement; 9 | import com.intellij.refactoring.listeners.RefactoringElementListener; 10 | import com.intellij.refactoring.listeners.RefactoringElementListenerProvider; 11 | import java.io.IOException; 12 | import java.util.Collection; 13 | import java.util.Objects; 14 | 15 | import org.jetbrains.annotations.NotNull; 16 | import org.jetbrains.annotations.Nullable; 17 | 18 | /** 19 | * @author yanglin 20 | */ 21 | public class MapperRefactoringProvider implements RefactoringElementListenerProvider { 22 | 23 | @Nullable 24 | @Override 25 | public RefactoringElementListener getListener(final PsiElement element) { 26 | if (!(element instanceof PsiClass)) { 27 | return null; 28 | } 29 | return new RefactoringElementListener() { 30 | @Override 31 | public void elementMoved(@NotNull PsiElement newElement) { 32 | } 33 | 34 | @Override 35 | public void elementRenamed(@NotNull final PsiElement newElement) { 36 | if (newElement instanceof PsiClass) { 37 | ApplicationManager.getApplication().runWriteAction(new Runnable() { 38 | @Override 39 | public void run() { 40 | renameMapperXml((PsiClass) element, (PsiClass) newElement); 41 | } 42 | }); 43 | } 44 | } 45 | }; 46 | } 47 | 48 | private void renameMapperXml(@NotNull final PsiClass oldClazz, @NotNull final PsiClass newClazz) { 49 | Collection mappers = MapperUtils.findMappers(oldClazz.getProject(), oldClazz); 50 | try { 51 | for (Mapper mapper : mappers) { 52 | VirtualFile vf = Objects.requireNonNull(mapper.getXmlTag()).getOriginalElement().getContainingFile().getVirtualFile(); 53 | if (null != vf) { 54 | vf.rename(MapperRefactoringProvider.this, newClazz.getName() + "." + vf.getExtension()); 55 | } 56 | } 57 | } catch (IOException e) { 58 | } 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/reference/ContextPsiFieldReference.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.reference; 2 | 3 | import com.github.mustfun.mybatis.plugin.dom.MapperBacktrackingUtils; 4 | import com.github.mustfun.mybatis.plugin.service.JavaService; 5 | import com.github.mustfun.mybatis.plugin.util.JavaUtils; 6 | import com.github.mustfun.mybatis.plugin.util.MybatisConstants; 7 | import com.intellij.openapi.util.TextRange; 8 | import com.intellij.psi.PsiClass; 9 | import com.intellij.psi.PsiElement; 10 | import com.intellij.psi.PsiReference; 11 | import com.intellij.psi.PsiReferenceBase; 12 | import com.intellij.psi.xml.XmlAttributeValue; 13 | import org.jetbrains.annotations.NotNull; 14 | import org.jetbrains.annotations.Nullable; 15 | 16 | import java.util.Optional; 17 | 18 | /** 19 | * @author yanglin 20 | * @updater itar 21 | * @function java属性跳转用 - property属性跳java 22 | */ 23 | public class ContextPsiFieldReference extends PsiReferenceBase { 24 | 25 | protected ContextReferenceSetResolver resolver; 26 | 27 | protected int index; 28 | 29 | public ContextPsiFieldReference(XmlAttributeValue element, TextRange range, int index) { 30 | super(element, range, false); 31 | this.index = index; 32 | resolver = ReferenceSetResolverFactory.createPsiFieldResolver(element); 33 | } 34 | 35 | /** 36 | * 返回值是对应的真是类型 37 | * @return 38 | */ 39 | @Nullable 40 | @Override 41 | public PsiElement resolve() { 42 | Optional resolved = resolver.resolve(index); 43 | return resolved.isPresent()?(PsiElement) resolved.get():null; 44 | } 45 | 46 | /** 47 | * class的所有属性全部都 48 | * @return 49 | */ 50 | @NotNull 51 | @Override 52 | public Object[] getVariants() { 53 | Optional clazz = getTargetClazz(); 54 | return clazz.isPresent() ? JavaUtils.findSettablePsiFields(clazz.get()) : PsiReference.EMPTY_ARRAY; 55 | } 56 | 57 | @SuppressWarnings("unchecked") 58 | private Optional getTargetClazz() { 59 | if (getElement().getValue().contains(MybatisConstants.DOT_SEPARATOR)) { 60 | int ind = 0 == index ? 0 : index - 1; 61 | Optional resolved = resolver.resolve(ind); 62 | if (resolved.isPresent()) { 63 | return JavaService.getInstance(myElement.getProject()).getReferenceClazzOfPsiField((PsiElement) resolved.get()); 64 | } 65 | } else { 66 | return MapperBacktrackingUtils.getPropertyClazz(myElement); 67 | } 68 | return Optional.empty(); 69 | } 70 | 71 | public ContextReferenceSetResolver getResolver() { 72 | return resolver; 73 | } 74 | 75 | public void setResolver(ContextReferenceSetResolver resolver) { 76 | this.resolver = resolver; 77 | } 78 | 79 | public int getIndex() { 80 | return index; 81 | } 82 | 83 | public void setIndex(int index) { 84 | this.index = index; 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/reference/ContextReferenceSetResolver.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.reference; 2 | 3 | import com.github.mustfun.mybatis.plugin.util.MybatisConstants; 4 | import com.google.common.base.Splitter; 5 | import com.google.common.collect.Iterables; 6 | import com.google.common.collect.Lists; 7 | import com.intellij.openapi.project.Project; 8 | import com.intellij.psi.PsiElement; 9 | import java.util.List; 10 | import java.util.Optional; 11 | 12 | import org.jetbrains.annotations.NotNull; 13 | import org.jetbrains.annotations.Nullable; 14 | 15 | /** 16 | * @author yanglin 17 | * @updater itar 18 | * @function 最原始的上下文解析类 将string解析为psiElement -reference用 19 | */ 20 | public abstract class ContextReferenceSetResolver { 21 | 22 | private static final Splitter SPLITTER = Splitter.on(MybatisConstants.DOT_SEPARATOR); 23 | 24 | protected Project project; 25 | 26 | protected F element; 27 | 28 | protected List texts; 29 | 30 | protected ContextReferenceSetResolver(@NotNull F element) { 31 | this.element = element; 32 | this.project = element.getProject(); 33 | this.texts = Lists.newArrayList(SPLITTER.split(getText())); 34 | } 35 | 36 | @NotNull 37 | public final Optional resolve(int index) { 38 | Optional startElement = getStartElement(); 39 | return startElement.isPresent() ? (texts.size() > 1 ? parseNext(startElement, texts, index) : startElement) 40 | : Optional.empty(); 41 | } 42 | 43 | private Optional parseNext(Optional current, List texts, int index) { 44 | int ind = 1; 45 | while (current.isPresent() && ind <= index) { 46 | String text = texts.get(ind); 47 | if (text.contains(" ")) { 48 | return Optional.empty(); 49 | } 50 | current = resolve(current.get(), text); 51 | ind++; 52 | } 53 | return current; 54 | } 55 | 56 | public Optional getStartElement() { 57 | return getStartElement(Iterables.getFirst(texts, null)); 58 | } 59 | 60 | @NotNull 61 | public abstract Optional getStartElement(@Nullable String firstText); 62 | 63 | @NotNull 64 | public abstract String getText(); 65 | 66 | @NotNull 67 | public abstract Optional resolve(@NotNull K current, @NotNull String text); 68 | 69 | public F getElement() { 70 | return element; 71 | } 72 | 73 | public void setElement(F element) { 74 | this.element = element; 75 | } 76 | 77 | public Project getProject() { 78 | return project; 79 | } 80 | 81 | public void setProject(Project project) { 82 | this.project = project; 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/reference/PsiFieldReferenceSetResolver.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.reference; 2 | 3 | import com.github.mustfun.mybatis.plugin.dom.MapperBacktrackingUtils; 4 | import com.github.mustfun.mybatis.plugin.util.JavaUtils; 5 | import com.intellij.psi.PsiClass; 6 | import com.intellij.psi.PsiField; 7 | import com.intellij.psi.PsiType; 8 | import com.intellij.psi.impl.source.PsiClassReferenceType; 9 | import com.intellij.psi.xml.XmlAttributeValue; 10 | import org.jetbrains.annotations.NotNull; 11 | import org.jetbrains.annotations.Nullable; 12 | 13 | import java.util.Optional; 14 | 15 | /** 16 | * @author yanglin 17 | * @updater itar 18 | * @function PSI属性的一个跳转处理类 19 | */ 20 | public class PsiFieldReferenceSetResolver extends ContextReferenceSetResolver { 21 | 22 | protected PsiFieldReferenceSetResolver(XmlAttributeValue from) { 23 | super(from); 24 | } 25 | 26 | @NotNull 27 | @Override 28 | public String getText() { 29 | return getElement().getValue(); 30 | } 31 | 32 | @NotNull 33 | @Override 34 | public Optional resolve(@NotNull PsiField current, @NotNull String text) { 35 | PsiType type = current.getType(); 36 | if (type instanceof PsiClassReferenceType && !((PsiClassReferenceType) type).hasParameters()) { 37 | PsiClass clazz = ((PsiClassReferenceType) type).resolve(); 38 | if (null != clazz) { 39 | return JavaUtils.findSettablePsiField(clazz, text); 40 | } 41 | } 42 | return Optional.empty(); 43 | } 44 | 45 | @NotNull 46 | @Override 47 | public Optional getStartElement(@Nullable String firstText) { 48 | Optional clazz = MapperBacktrackingUtils.getPropertyClazz(getElement()); 49 | return clazz.flatMap(psiClass -> JavaUtils.findPsiField(psiClass, firstText)); 50 | } 51 | 52 | } -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/reference/ReferenceSetResolverFactory.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.reference; 2 | 3 | import com.intellij.psi.xml.XmlAttributeValue; 4 | import org.jetbrains.annotations.NotNull; 5 | 6 | /** 7 | * @author yanglin 8 | * @updater itar 9 | * @function referenceSet的一个工厂类 10 | */ 11 | public final class ReferenceSetResolverFactory { 12 | 13 | private ReferenceSetResolverFactory() { 14 | throw new UnsupportedOperationException(); 15 | } 16 | 17 | public static ContextReferenceSetResolver createPsiFieldResolver(@NotNull F target) { 18 | return new PsiFieldReferenceSetResolver(target); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/reference/ResultPropertyReferenceSet.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.reference; 2 | 3 | import com.intellij.openapi.util.TextRange; 4 | import com.intellij.psi.PsiElement; 5 | import com.intellij.psi.PsiReference; 6 | import com.intellij.psi.util.ReferenceSetBase; 7 | import com.intellij.psi.xml.XmlAttributeValue; 8 | import org.jetbrains.annotations.NonNls; 9 | import org.jetbrains.annotations.NotNull; 10 | import org.jetbrains.annotations.Nullable; 11 | 12 | /** 13 | * @author yanglin 14 | * @updater itar 15 | * @function 要想构造ReferenceSet必须继承ReferenceSetBase 16 | */ 17 | public class ResultPropertyReferenceSet extends ReferenceSetBase { 18 | 19 | public ResultPropertyReferenceSet(String text, @NotNull PsiElement element, int offset) { 20 | //最后一个是分隔符 21 | super(text, element, offset, DOT_SEPARATOR); 22 | } 23 | 24 | /** 25 | * property对应的reference 26 | * @param range 27 | * @param index 28 | * @return 29 | */ 30 | @Nullable 31 | @NonNls 32 | @Override 33 | protected PsiReference createReference(TextRange range, int index) { 34 | XmlAttributeValue element = (XmlAttributeValue) getElement(); 35 | return null == element ? null : new ContextPsiFieldReference(element, range, index); 36 | } 37 | 38 | } -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/service/AnnotationService.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.service; 2 | 3 | import com.github.mustfun.mybatis.plugin.annotation.Annotation; 4 | import com.github.mustfun.mybatis.plugin.util.JavaUtils; 5 | import com.intellij.openapi.components.ServiceManager; 6 | import com.intellij.openapi.project.Project; 7 | import com.intellij.psi.JavaPsiFacade; 8 | import com.intellij.psi.PsiAnnotation; 9 | import com.intellij.psi.PsiElementFactory; 10 | import com.intellij.psi.PsiJavaFile; 11 | import com.intellij.psi.PsiMethod; 12 | import com.intellij.psi.PsiModifierList; 13 | import com.intellij.psi.PsiModifierListOwner; 14 | import com.intellij.psi.PsiParameter; 15 | import com.intellij.psi.PsiParameterList; 16 | import com.intellij.psi.codeStyle.JavaCodeStyleManager; 17 | import org.jetbrains.annotations.NotNull; 18 | 19 | /** 20 | * @author yanglin 21 | * @updater itar 22 | * 23 | */ 24 | public class AnnotationService { 25 | 26 | private Project project; 27 | 28 | public AnnotationService(Project project) { 29 | this.project = project; 30 | } 31 | 32 | public static AnnotationService getInstance(@NotNull Project project) { 33 | return ServiceManager.getService(project, AnnotationService.class); 34 | } 35 | 36 | public void addAnnotation(@NotNull PsiModifierListOwner parameter, @NotNull Annotation annotation) { 37 | PsiModifierList modifierList = parameter.getModifierList(); 38 | if (JavaUtils.isAnnotationPresent(parameter, annotation) || null == modifierList) { 39 | return; 40 | } 41 | JavaService.getInstance(parameter.getProject()) 42 | .importClazz((PsiJavaFile) parameter.getContainingFile(), annotation.getQualifiedName()); 43 | 44 | PsiElementFactory elementFactory = JavaPsiFacade.getInstance(project).getElementFactory(); 45 | PsiAnnotation psiAnnotation = elementFactory.createAnnotationFromText(annotation.toString(), parameter); 46 | modifierList.add(psiAnnotation); 47 | JavaCodeStyleManager.getInstance(project).shortenClassReferences(psiAnnotation.getParent()); 48 | } 49 | 50 | public void addAnnotationWithParameterNameForMethodParameters(@NotNull PsiMethod method) { 51 | PsiParameterList parameterList = method.getParameterList(); 52 | if (null == parameterList) { 53 | return; 54 | } 55 | PsiParameter[] parameters = parameterList.getParameters(); 56 | for (PsiParameter param : parameters) { 57 | addAnnotationWithParameterName(param); 58 | } 59 | } 60 | 61 | public void addAnnotationWithParameterName(@NotNull PsiParameter parameter) { 62 | String name = parameter.getName(); 63 | if (null != name) { 64 | AnnotationService.getInstance(parameter.getProject()) 65 | .addAnnotation(parameter, Annotation.PARAM.withValue(new Annotation.StringValue(name))); 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/service/DbServiceFactory.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.service; 2 | 3 | import com.intellij.openapi.components.ServiceManager; 4 | import com.intellij.openapi.project.Project; 5 | import org.jetbrains.annotations.NotNull; 6 | 7 | /** 8 | * @author itar 9 | * @date 2020-02-24 10 | * 获取dbService服务的工厂类 11 | */ 12 | public class DbServiceFactory { 13 | 14 | private Project project; 15 | 16 | public DbServiceFactory(Project project) { 17 | this.project = project; 18 | } 19 | 20 | /** 21 | * 获取Service实例 22 | * @param project 23 | * @return 24 | */ 25 | public static DbServiceFactory getInstance(@NotNull Project project){ 26 | return ServiceManager.getService(project,DbServiceFactory.class); 27 | } 28 | 29 | /** 30 | * 获取mysql的service 31 | * @return 32 | */ 33 | public MysqlService createMysqlService(){ 34 | return new MysqlService(project); 35 | } 36 | 37 | /** 38 | * 获取SqlLite的service 39 | * @return 40 | */ 41 | public SqlLiteService createSqlLiteService(){ 42 | return new SqlLiteService(project); 43 | } 44 | 45 | /** 46 | * 获取SqlLite的service 47 | * @return 48 | */ 49 | public SqlLiteService createInnerSqlLiteService(){ 50 | return new SqlLiteService(); 51 | } 52 | } 53 | 54 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/service/EditorService.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.service; 2 | 3 | import com.intellij.application.options.CodeStyle; 4 | import com.intellij.codeInsight.navigation.NavigationUtil; 5 | import com.intellij.formatting.FormatTextRanges; 6 | import com.intellij.openapi.components.ServiceManager; 7 | import com.intellij.openapi.editor.Editor; 8 | import com.intellij.openapi.editor.ScrollType; 9 | import com.intellij.openapi.fileEditor.FileEditorManager; 10 | import com.intellij.openapi.project.Project; 11 | import com.intellij.psi.PsiElement; 12 | import com.intellij.psi.PsiFile; 13 | import com.intellij.psi.impl.source.codeStyle.CodeFormatterFacade; 14 | import org.jetbrains.annotations.NotNull; 15 | 16 | /** 17 | * @author yanglin 18 | */ 19 | public class EditorService { 20 | 21 | private Project project; 22 | 23 | private FileEditorManager fileEditorManager; 24 | 25 | private CodeFormatterFacade codeFormatterFacade; 26 | 27 | public EditorService(Project project) { 28 | this.project = project; 29 | this.fileEditorManager = FileEditorManager.getInstance(project); 30 | } 31 | 32 | public static EditorService getInstance(@NotNull Project project) { 33 | return ServiceManager.getService(project, EditorService.class); 34 | } 35 | 36 | public void format(@NotNull PsiFile file, @NotNull PsiElement element) { 37 | this.codeFormatterFacade = new CodeFormatterFacade(CodeStyle.getSettings(element.getProject()), 38 | element.getLanguage()); 39 | codeFormatterFacade.processText(file, new FormatTextRanges(element.getTextRange(), true), true); 40 | } 41 | 42 | public void scrollTo(@NotNull PsiElement element, int offset) { 43 | NavigationUtil.activateFileWithPsiElement(element, true); 44 | Editor editor = fileEditorManager.getSelectedTextEditor(); 45 | if (null != editor) { 46 | editor.getCaretModel().moveToOffset(offset); 47 | editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE); 48 | } 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/service/resolver/FileResolver.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.service.resolver; 2 | 3 | /** 4 | * @author itar 5 | * @date 2020-03-03 6 | * 文件解析接口 , T进F出 7 | */ 8 | public interface FileResolver { 9 | /** 10 | * 11 | * @param file 12 | * @return 13 | */ 14 | F resolve(T file); 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/service/resolver/PropertiesFileResolver.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.service.resolver; 2 | 3 | import com.github.mustfun.mybatis.plugin.util.CollectionUtils; 4 | import com.intellij.openapi.project.Project; 5 | import com.intellij.openapi.vfs.VirtualFile; 6 | 7 | import java.io.File; 8 | import java.io.FileInputStream; 9 | import java.io.IOException; 10 | import java.util.ArrayList; 11 | import java.util.Arrays; 12 | import java.util.List; 13 | import java.util.Properties; 14 | 15 | /** 16 | * @author itar 17 | * @date 2020-03-03 18 | * 属性解析接口 19 | */ 20 | public class PropertiesFileResolver extends AbstractFileResolver{ 21 | 22 | public PropertiesFileResolver(Project project) { 23 | super(project); 24 | } 25 | 26 | @Override 27 | String[] getPattern() { 28 | List strings =null; 29 | String[] pattern = super.getPattern(); 30 | List result = new ArrayList<>(); 31 | for (String s : pattern) { 32 | if (s.contains("properties")){ 33 | result.add(s); 34 | } 35 | } 36 | if (CollectionUtils.isEmpty(result)){ 37 | strings=Arrays.asList("application*.properties", "db*.properties"); 38 | }else{ 39 | strings = result; 40 | } 41 | return strings.toArray(new String[0]); 42 | } 43 | 44 | 45 | 46 | @Override 47 | protected Properties convert(VirtualFile map) { 48 | Properties properties = new Properties(); 49 | try { 50 | properties.load(new FileInputStream(new File(map.getPath()))); 51 | return properties; 52 | } catch (IOException e) { 53 | e.printStackTrace(); 54 | } 55 | return null; 56 | } 57 | 58 | 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/service/resolver/ResolverFacade.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.service.resolver; 2 | 3 | import com.intellij.openapi.components.ServiceManager; 4 | import com.intellij.openapi.project.Project; 5 | import com.intellij.openapi.vfs.VirtualFile; 6 | import org.jetbrains.annotations.NotNull; 7 | 8 | import java.util.ArrayList; 9 | import java.util.List; 10 | import java.util.Properties; 11 | 12 | /** 13 | * 解析类总类 14 | * @author itar 15 | * @date 2020-03-08 16 | */ 17 | public class ResolverFacade { 18 | 19 | private List> fileResolvers; 20 | private Project project; 21 | 22 | public ResolverFacade(Project project) { 23 | initFileResolvers(project); 24 | } 25 | 26 | public static ResolverFacade getInstance(@NotNull Project project) { 27 | return ServiceManager.getService(project, ResolverFacade.class); 28 | } 29 | 30 | private void initFileResolvers(Project project) { 31 | fileResolvers = new ArrayList<>(); 32 | fileResolvers.add(new YamlFileResolver(project)); 33 | fileResolvers.add(new PropertiesFileResolver(project)); 34 | fileResolvers.add(new XmlConfigFileResolver(project)); 35 | } 36 | 37 | public List> getFileResolvers() { 38 | return fileResolvers; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/service/resolver/XmlConfigFileResolver.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.service.resolver; 2 | 3 | import com.intellij.openapi.project.Project; 4 | import com.intellij.openapi.vfs.VirtualFile; 5 | 6 | import java.util.Collections; 7 | import java.util.List; 8 | import java.util.Map; 9 | import java.util.Properties; 10 | 11 | /** 12 | * @author itar 13 | * @date 2020-03-03 14 | * 属性解析接口 15 | */ 16 | public class XmlConfigFileResolver extends AbstractFileResolver{ 17 | 18 | public XmlConfigFileResolver(Project project) { 19 | super(project); 20 | } 21 | 22 | @Override 23 | String[] getPattern() { 24 | List strings = Collections.singletonList("application-dev.xml"); 25 | return strings.toArray(new String[0]); 26 | } 27 | 28 | 29 | @Override 30 | protected Properties convert(VirtualFile map) { 31 | return null; 32 | } 33 | 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/service/resolver/YamlFileResolver.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.service.resolver; 2 | 3 | import com.github.mustfun.mybatis.plugin.setting.MybatisLiteSetting; 4 | import com.github.mustfun.mybatis.plugin.util.CollectionUtils; 5 | import com.github.mustfun.mybatis.plugin.util.MybatisConstants; 6 | import com.intellij.openapi.project.Project; 7 | import com.intellij.openapi.vfs.VirtualFile; 8 | import org.apache.commons.lang.StringUtils; 9 | import org.yaml.snakeyaml.Yaml; 10 | 11 | import java.io.File; 12 | import java.io.FileInputStream; 13 | import java.io.FileNotFoundException; 14 | import java.util.*; 15 | 16 | /** 17 | * @author itar 18 | * @date 2020-03-03 19 | * 抽象解析接口 - 专业解析Ymal 30年 20 | */ 21 | public class YamlFileResolver extends AbstractFileResolver { 22 | 23 | public YamlFileResolver(Project project) { 24 | super(project); 25 | } 26 | 27 | @Override 28 | String[] getPattern() { 29 | List strings =null; 30 | String[] pattern = super.getPattern(); 31 | List result = new ArrayList<>(); 32 | for (String s : pattern) { 33 | if (s.contains("yml")||s.contains("ymal")){ 34 | result.add(s); 35 | } 36 | } 37 | if (CollectionUtils.isEmpty(result)){ 38 | strings= Arrays.asList("application*.yml","application*.ymal"); 39 | }else{ 40 | strings = result; 41 | } 42 | return strings.toArray(new String[0]); 43 | } 44 | 45 | @SuppressWarnings("unchecked") 46 | @Override 47 | protected Properties convert(VirtualFile map) { 48 | //典型普通yml文件 49 | Yaml yaml = new Yaml(); 50 | try { 51 | Map source = yaml.load(new FileInputStream(new File(map.getPath()))); 52 | if (source==null){ 53 | return null; 54 | } 55 | Map resultMap = new HashMap<>(20); 56 | tranHashMapToFlatMap(source, new StringBuilder(), resultMap); 57 | Properties properties = new Properties(); 58 | properties.putAll(resultMap); 59 | return properties; 60 | } catch (FileNotFoundException e) { 61 | e.printStackTrace(); 62 | } 63 | return null; 64 | } 65 | 66 | /** 67 | * 平铺hashMap - 筛选关键字用 68 | * @param map 69 | * @param key properties的键 70 | * @param resultMap 返回map 71 | */ 72 | @SuppressWarnings("unchecked") 73 | private void tranHashMapToFlatMap(Map map,StringBuilder key,Map resultMap) { 74 | for (String s : map.keySet()) { 75 | Object o = map.get(s); 76 | if(o instanceof Map){ 77 | tranHashMapToFlatMap((Map) o,new StringBuilder(key).append(s).append("."),resultMap); 78 | }else{ 79 | resultMap.put(new StringBuilder(key).append(s), o); 80 | } 81 | } 82 | } 83 | 84 | } 85 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/setting/ModuleDBConfigUI.form: -------------------------------------------------------------------------------- 1 | 2 |
3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/setting/ModuleDBConfigUI.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.setting; 2 | 3 | import com.intellij.ui.components.JBScrollPane; 4 | import lombok.Getter; 5 | import lombok.Setter; 6 | 7 | import javax.swing.*; 8 | 9 | 10 | /** 11 | * @author itar 12 | * @date 2020-03-18 13 | * @function DB配置信息 14 | * JList只能单独显示一下,不能实现像panel那么智能..... 15 | * 16 | */ 17 | @Setter 18 | @Getter 19 | public class ModuleDBConfigUI { 20 | private JPanel mainPanel; 21 | private JBScrollPane scrollPane; 22 | private JCheckBox multiEdit; 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/setting/MybatisLiteProjectSetting.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.setting; 2 | 3 | import com.intellij.openapi.components.*; 4 | import com.intellij.openapi.project.Project; 5 | import org.jetbrains.annotations.NotNull; 6 | import org.jetbrains.annotations.Nullable; 7 | 8 | import java.util.HashMap; 9 | import java.util.Map; 10 | 11 | import static com.github.mustfun.mybatis.plugin.util.MybatisConstants.CONFIG_FILE_NAME; 12 | 13 | 14 | /** 15 | * @author yanglin 16 | * @update itar 17 | * @function Mybatis配置项 - 全局级别- 持久化 18 | */ 19 | @State( 20 | name = "MybatisLiteProjectSettings", 21 | storages = @Storage(StoragePathMacros.WORKSPACE_FILE)) 22 | public class MybatisLiteProjectSetting implements PersistentStateComponent { 23 | 24 | public static MybatisLiteProjectSetting getInstance(Project project) { 25 | return ServiceManager.getService(project,MybatisLiteProjectSetting.class); 26 | } 27 | 28 | public MybatisLiteProjectSetting() { 29 | this.mybatisLiteState = new MybatisLiteProjectState(); 30 | } 31 | 32 | private MybatisLiteProjectState mybatisLiteState; 33 | 34 | /** 35 | * 每次保存设置都会调用这个方法,这个方法如果看到mybatisLiteState有变化,就会持久化 36 | * @return 37 | */ 38 | @Nullable 39 | @Override 40 | public MybatisLiteProjectState getState() { 41 | return mybatisLiteState; 42 | } 43 | 44 | /** 45 | * loadState()方法在创建组件之后 46 | * (仅当组件有一些非默认的持久化状态时)和在外部更改具有持久化状态的XML文件之后 47 | * (例如,如果项目文件是从版本控制系统更新的)调用 48 | * @param state 49 | */ 50 | @Override 51 | public void loadState(@NotNull MybatisLiteProjectState state) { 52 | this.mybatisLiteState = state; 53 | } 54 | 55 | public Map getValueMap(){ 56 | return mybatisLiteState.map; 57 | } 58 | 59 | /** 60 | * 存储字段的map 61 | * 构造函数决定最初初始值 62 | */ 63 | static class MybatisLiteProjectState { 64 | public Map map; 65 | 66 | public MybatisLiteProjectState() { 67 | this.map = new HashMap<>(); 68 | map.put(CONFIG_FILE_NAME, ""); 69 | } 70 | 71 | public MybatisLiteProjectState(Map map) { 72 | this.map = map; 73 | } 74 | 75 | public Map getMap() { 76 | return map; 77 | } 78 | 79 | public void setMap(Map map) { 80 | this.map = map; 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/setting/MybatisLiteProjectSettingUI.form: -------------------------------------------------------------------------------- 1 | 2 |
3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/setting/MybatisLiteProjectSettingUI.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.setting; 2 | 3 | import lombok.Getter; 4 | import lombok.Setter; 5 | 6 | import javax.swing.*; 7 | 8 | /** 9 | * @author itar 10 | * 项目设置相关UI 11 | * @date 2020-03-17 12 | */ 13 | @Setter 14 | @Getter 15 | public class MybatisLiteProjectSettingUI { 16 | private JPanel mainPanel; 17 | private JPanel configPanel; 18 | private JTextField bootConfigNameTextField; 19 | 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/setting/MybatisSettingForm.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.setting; 2 | 3 | import javax.swing.ButtonGroup; 4 | import javax.swing.JComboBox; 5 | import javax.swing.JLabel; 6 | import javax.swing.JPanel; 7 | import javax.swing.JRadioButton; 8 | import javax.swing.JTextField; 9 | import lombok.Getter; 10 | import lombok.Setter; 11 | 12 | /** 13 | * @author yanglin 14 | * @update itar 15 | * @function form 16 | * 心得: 只有当最后一个radioBox是want Grow的时候才会撑满后面的 17 | * 心得2: 两个panel互相嵌套时候 直接往后拉 18 | */ 19 | @Setter 20 | @Getter 21 | public class MybatisSettingForm { 22 | 23 | public JPanel mainPanel; 24 | 25 | public JComboBox modelComboBox; 26 | 27 | public JRadioButton openNaviButton; 28 | public JLabel openNaviLabel; 29 | public JRadioButton closeNaviRadioButton; 30 | private JPanel generateSettingPanel; 31 | private JPanel naviPanel; 32 | private JTextField insertPatternInput; 33 | private JTextField updatePatternInput; 34 | private JTextField selectPattenInput; 35 | private JTextField deletePattenInput; 36 | private JPanel otherSettingPanel1; 37 | private JRadioButton sqlFieldOpenButton; 38 | private JRadioButton sqlFieldCloseButton; 39 | private JRadioButton sqlPrintClose; 40 | private JRadioButton sqlPrintOpen; 41 | 42 | public MybatisSettingForm() { 43 | ButtonGroup buttonGroup = new ButtonGroup(); 44 | buttonGroup.add(closeNaviRadioButton); 45 | buttonGroup.add(openNaviButton); 46 | openNaviButton.setSelected(true); 47 | 48 | ButtonGroup buttonGroup2 = new ButtonGroup(); 49 | buttonGroup2.add(sqlFieldCloseButton); 50 | buttonGroup2.add(sqlFieldOpenButton); 51 | 52 | ButtonGroup buttonGroup3 = new ButtonGroup(); 53 | buttonGroup3.add(sqlPrintClose); 54 | buttonGroup3.add(sqlPrintOpen); 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/setting/SingleDBConnectInfoUI.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.setting; 2 | 3 | import lombok.Getter; 4 | import lombok.Setter; 5 | 6 | import javax.swing.*; 7 | 8 | /** 9 | * @author itar 10 | * @date 2020-03-18 11 | * @function 单个连接信息,便于布局 12 | */ 13 | @Setter 14 | @Getter 15 | public class SingleDBConnectInfoUI { 16 | private JLabel moduleName; 17 | private JComboBox dbTypeComboBox; 18 | private JTextField ipText; 19 | private JTextField portText; 20 | private JTextField userNameText; 21 | private JTextField passwordText; 22 | private JPanel mainPanel; 23 | private JTextField dbNameText; 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/setting/TemplateEdit.form: -------------------------------------------------------------------------------- 1 | 2 |
3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/setting/TemplateEdit.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.setting; 2 | 3 | import com.intellij.ui.EditorTextField; 4 | import com.intellij.ui.components.JBScrollPane; 5 | import javax.swing.JPanel; 6 | import javax.swing.JTextArea; 7 | import lombok.Getter; 8 | import lombok.Setter; 9 | 10 | /** 11 | * @author itar 12 | * @version 1.0 13 | * @date 2018/6/15 14 | * @since 1.0 15 | * 查看模板时候用,语法高亮功能呢 16 | */ 17 | @Setter 18 | @Getter 19 | public class TemplateEdit { 20 | 21 | private JPanel mainPanel; 22 | private EditorTextField editorTextField; 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/setting/TemplateListForm.form: -------------------------------------------------------------------------------- 1 | 2 |
3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/setting/endpoint/AbstractEndpointTabConfigurable.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.setting.endpoint; 2 | 3 | import com.intellij.openapi.extensions.ExtensionPointName; 4 | import com.intellij.openapi.options.ConfigurableBase; 5 | import com.intellij.openapi.options.ConfigurableUi; 6 | import org.jetbrains.annotations.NotNull; 7 | import org.jetbrains.annotations.Nullable; 8 | 9 | /** 10 | * 设置扩展点 - area=application 如果等于project会造成 使用时候构造函数需要一个project参数 11 | * 12 | * @param 13 | * @param 14 | * @author itar 15 | */ 16 | public abstract class AbstractEndpointTabConfigurable, S> extends ConfigurableBase { 17 | public static final ExtensionPointName EP_NAME = ExtensionPointName.create("com.github.mustfun.plugin.mybatis.setting.projectConfigurable"); 18 | 19 | protected AbstractEndpointTabConfigurable(@NotNull String id, @NotNull String displayName, @Nullable String helpTopic) { 20 | super(id, displayName, helpTopic); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/setting/endpoint/MybatisLiteProjectExtendConfigurableAbstract.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.setting.endpoint; 2 | 3 | import com.intellij.openapi.project.Project; 4 | import org.jetbrains.annotations.NotNull; 5 | 6 | 7 | /** 8 | * @author itar 9 | * @function 项目级配置 10 | * 11 | */ 12 | public class MybatisLiteProjectExtendConfigurableAbstract extends AbstractEndpointTabConfigurable { 13 | 14 | private Project project; 15 | 16 | 17 | MybatisLiteProjectExtendConfigurableAbstract(Project project) { 18 | super("MybatisLiteProjectSetting", "Mybatis Lite", null); 19 | this.project = project; 20 | } 21 | 22 | 23 | @NotNull 24 | @Override 25 | protected MybatisLiteProjectExtendSetting getSettings() { 26 | return MybatisLiteProjectExtendSetting.getInstance(project); 27 | } 28 | 29 | @Override 30 | protected MybatisLiteProjectExtendConfigurableUi createUi() { 31 | return new MybatisLiteProjectExtendConfigurableUi(project); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/setting/endpoint/MybatisLiteProjectExtendConfigurableUi.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.setting.endpoint; 2 | 3 | import com.github.mustfun.mybatis.plugin.listener.SettingApplyListener; 4 | import com.intellij.openapi.options.ConfigurableUi; 5 | import com.intellij.openapi.project.Project; 6 | import org.jetbrains.annotations.NotNull; 7 | 8 | import javax.swing.*; 9 | import java.util.HashMap; 10 | import java.util.Map; 11 | import java.util.Objects; 12 | 13 | /** 14 | * @author itar 15 | * @function 配置相关, 新增配置 transDataToMap 方法和reset方法小改一下就可以了,很方便 16 | * 主要存储project级别的配置 17 | */ 18 | public class MybatisLiteProjectExtendConfigurableUi implements ConfigurableUi { 19 | 20 | private JPanel myRootPanel; 21 | private Map rawStateMap; 22 | private Project project; 23 | 24 | public MybatisLiteProjectExtendConfigurableUi(Project project) { 25 | rawStateMap = Objects.requireNonNull(MybatisLiteProjectExtendSetting.getInstance(project).getState()).map; 26 | this.project = project; 27 | this.setupUI(); 28 | } 29 | 30 | private void setupUI() { 31 | myRootPanel = new JPanel(); 32 | myRootPanel.add(new JButton("test")); 33 | } 34 | 35 | 36 | @NotNull 37 | @Override 38 | public JComponent getComponent() { 39 | return this.myRootPanel; 40 | } 41 | 42 | 43 | @Override 44 | public void reset(@NotNull MybatisLiteProjectExtendSetting settings) { 45 | 46 | } 47 | 48 | @Override 49 | public boolean isModified(@NotNull MybatisLiteProjectExtendSetting settings) { 50 | Map afterMap = transDataToMap(); 51 | return needUpdate(rawStateMap, afterMap); 52 | } 53 | 54 | @Override 55 | public void apply(@NotNull MybatisLiteProjectExtendSetting settings) { 56 | Map newMap = transDataToMap(); 57 | MybatisLiteProjectExtendSetting.getInstance(project).loadState(new MybatisLiteProjectExtendSetting(newMap)); 58 | new SettingApplyListener().notify(newMap, rawStateMap); 59 | rawStateMap = newMap; 60 | } 61 | 62 | 63 | 64 | 65 | /** 66 | * 如果有新配置项这里要改 67 | * 68 | * @return 69 | */ 70 | public Map transDataToMap() { 71 | Map map = new HashMap<>(); 72 | return map; 73 | } 74 | 75 | public Boolean needUpdate(Map beforeMap, Map afterMap) { 76 | for (String s : beforeMap.keySet()) { 77 | if (!afterMap.get(s).equalsIgnoreCase(beforeMap.get(s))) { 78 | return true; 79 | } 80 | } 81 | return false; 82 | } 83 | 84 | } 85 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/setting/endpoint/MybatisLiteProjectExtendSetting.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.setting.endpoint; 2 | 3 | import com.github.mustfun.mybatis.plugin.util.MybatisConstants; 4 | import com.intellij.openapi.components.*; 5 | import com.intellij.openapi.project.Project; 6 | import com.intellij.util.xmlb.XmlSerializerUtil; 7 | import org.jetbrains.annotations.NotNull; 8 | import org.jetbrains.annotations.Nullable; 9 | 10 | import java.util.HashMap; 11 | import java.util.Map; 12 | 13 | 14 | /** 15 | * @author yanglin 16 | * @update itar 17 | * @function Mybatis配置项 - 项目级别的 - 持久化 18 | */ 19 | @State( 20 | name = "MybatisLiteProjectExtendSettings", 21 | storages = {@Storage(StoragePathMacros.WORKSPACE_FILE)} 22 | ) 23 | public class MybatisLiteProjectExtendSetting implements PersistentStateComponent { 24 | public Map map; 25 | 26 | public MybatisLiteProjectExtendSetting() { 27 | this.map = new HashMap<>(2); 28 | map.put(MybatisConstants.CONFIG_FILE_NAME, ""); 29 | map.put(MybatisConstants.SQL_PRINT_STATUS, "0"); 30 | } 31 | 32 | public MybatisLiteProjectExtendSetting(Map map) { 33 | this.map = map; 34 | } 35 | 36 | public static MybatisLiteProjectExtendSetting getInstance(@NotNull Project project) { 37 | return ServiceManager.getService(project, MybatisLiteProjectExtendSetting.class); 38 | } 39 | 40 | @Nullable 41 | @Override 42 | public MybatisLiteProjectExtendSetting getState() { 43 | return this; 44 | } 45 | 46 | @Override 47 | public void loadState(@NotNull MybatisLiteProjectExtendSetting state) { 48 | //从state copy 到 this 49 | XmlSerializerUtil.copyBean(state, this); 50 | } 51 | 52 | public Map getMap() { 53 | return map; 54 | } 55 | 56 | public void setMap(Map map) { 57 | this.map = map; 58 | } 59 | } -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/template/MybatisFileTemplateDescriptorFactory.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.template; 2 | 3 | import com.github.mustfun.mybatis.plugin.util.Icons; 4 | import com.intellij.ide.fileTemplates.FileTemplateDescriptor; 5 | import com.intellij.ide.fileTemplates.FileTemplateGroupDescriptor; 6 | import com.intellij.ide.fileTemplates.FileTemplateGroupDescriptorFactory; 7 | 8 | /** 9 | * @author yanglin 10 | */ 11 | public class MybatisFileTemplateDescriptorFactory implements FileTemplateGroupDescriptorFactory { 12 | 13 | public static final String MYBATIS_MAPPER_XML_TEMPLATE = "Mybatis Mapper.xml"; 14 | 15 | @Override 16 | public FileTemplateGroupDescriptor getFileTemplatesDescriptor() { 17 | FileTemplateGroupDescriptor group = new FileTemplateGroupDescriptor("Mybatis", Icons.MYBATIS_LOGO); 18 | group.addTemplate(new FileTemplateDescriptor(MYBATIS_MAPPER_XML_TEMPLATE, Icons.MYBATIS_LOGO)); 19 | return group; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/template/SqlLiveTemplateProvider.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.template; 2 | 3 | import com.intellij.codeInsight.template.impl.DefaultLiveTemplatesProvider; 4 | import org.jetbrains.annotations.Nullable; 5 | 6 | /** 7 | * @author yanglin 8 | */ 9 | //public class SqlLiveTemplateProvider implements DefaultLiveTemplatesProvider { 10 | // 11 | // public static final String[] TEMPLATE_FILES = {"liveTemplates/sql"}; 12 | // 13 | // @Override 14 | // public String[] getDefaultLiveTemplateFiles() { 15 | // return TEMPLATE_FILES; 16 | // } 17 | // 18 | // @Nullable 19 | // @Override 20 | // public String[] getHiddenLiveTemplateFiles() { 21 | // return null; 22 | // } 23 | //} 24 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/ui/ClickableListener.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.ui; 2 | 3 | /** 4 | * @author yanglin 5 | * @updater itar 6 | * @function 某一项被点击之后唤醒 7 | */ 8 | public interface ClickableListener extends ExecutableListener { 9 | 10 | /** 11 | * 被点击之后唤醒 12 | * 13 | */ 14 | public void clicked(); 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/ui/ExecutableListener.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.ui; 2 | 3 | /** 4 | * @author yanglin 5 | * @updater itar 6 | * @function 一个监听接口 7 | */ 8 | public interface ExecutableListener { 9 | 10 | /** 11 | * 当前是否在写入操作 12 | * @return 13 | */ 14 | public boolean isWriteAction(); 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/ui/ListSelectionListener.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.ui; 2 | 3 | /** 4 | * @author yanglin 5 | * @updater itar 6 | * @function 列表选择监听者 7 | */ 8 | public interface ListSelectionListener extends ExecutableListener { 9 | 10 | /** 11 | * 列表某一项被选中 12 | * @param index 13 | */ 14 | public void selected(int index); 15 | 16 | } -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/ui/custom/TemplateCodeEditPanel.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.ui.custom; 2 | 3 | import com.github.mustfun.mybatis.plugin.model.Template; 4 | import com.github.mustfun.mybatis.plugin.service.DbServiceFactory; 5 | import com.github.mustfun.mybatis.plugin.service.SqlLiteService; 6 | import com.github.mustfun.mybatis.plugin.setting.TemplateEdit; 7 | import com.intellij.openapi.project.Project; 8 | import com.intellij.openapi.ui.DialogWrapper; 9 | 10 | import javax.swing.*; 11 | 12 | import com.intellij.openapi.ui.Messages; 13 | import com.intellij.ui.EditorTextField; 14 | import org.apache.commons.lang.StringUtils; 15 | import org.jetbrains.annotations.Nullable; 16 | 17 | /** 18 | * @author itar 19 | * @version 1.0 20 | * @date 2018/6/13 21 | * @since 1.0 22 | */ 23 | public class TemplateCodeEditPanel extends DialogWrapper { 24 | 25 | private TemplateEdit templateEdit; 26 | private Template template; 27 | private Project project; 28 | 29 | public TemplateCodeEditPanel(@Nullable Project project, boolean canBeParent, TemplateEdit templateEdit,Template template) { 30 | super(project, canBeParent); 31 | this.templateEdit = templateEdit; 32 | this.template = template; 33 | this.project = project; 34 | super.init(); 35 | } 36 | 37 | @Nullable 38 | @Override 39 | protected JComponent createCenterPanel() { 40 | return templateEdit.getMainPanel(); 41 | } 42 | 43 | /** 44 | * 点击确认之后,持久化到磁盘里面吧 45 | */ 46 | @Override 47 | protected void doOKAction() { 48 | super.doOKAction(); 49 | EditorTextField editorTextField = templateEdit.getEditorTextField(); 50 | String text = editorTextField.getText(); 51 | if(StringUtils.isEmpty(text)){ 52 | Messages.showMessageDialog("模板修改成功", "模板修改提示", Messages.getErrorIcon()); 53 | return; 54 | } 55 | System.out.println("template = " + template.getTepName()); 56 | SqlLiteService sqlLiteService = DbServiceFactory.getInstance(project).createSqlLiteService(); 57 | if (template.getTepContent().equals(text)) { 58 | return ; 59 | } 60 | template.setTepContent(text); 61 | sqlLiteService.updateTemplate(template); 62 | Messages.showMessageDialog("模板修改成功", "模板修改提示", Messages.getInformationIcon()); 63 | 64 | } 65 | 66 | @Override 67 | public void doCancelAction() { 68 | super.doCancelAction(); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/ui/custom/TemplateListPanel.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.ui.custom; 2 | 3 | import com.github.mustfun.mybatis.plugin.setting.TemplateListForm; 4 | import com.intellij.openapi.project.Project; 5 | import com.intellij.openapi.ui.DialogWrapper; 6 | import javax.swing.JComponent; 7 | import org.jetbrains.annotations.Nullable; 8 | 9 | /** 10 | * @author itar 11 | * @version 1.0 12 | * @date 2018/6/13 13 | * @since 1.0 14 | */ 15 | public class TemplateListPanel extends DialogWrapper { 16 | 17 | private TemplateListForm templateEdit; 18 | 19 | public TemplateListPanel(@Nullable Project project, boolean canBeParent, TemplateListForm templateEdit) { 20 | super(project, canBeParent); 21 | this.templateEdit = templateEdit; 22 | super.init(); 23 | } 24 | 25 | @Nullable 26 | @Override 27 | protected JComponent createCenterPanel() { 28 | return templateEdit.getMainPanel(); 29 | } 30 | 31 | @Override 32 | public void doCancelAction() { 33 | super.doCancelAction(); 34 | } 35 | 36 | @Override 37 | protected void doOKAction() { 38 | super.doOKAction(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/ui/custom/TemplateViewPanel.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.ui.custom; 2 | 3 | import com.github.mustfun.mybatis.plugin.setting.TemplateEdit; 4 | import com.intellij.openapi.project.Project; 5 | import com.intellij.openapi.ui.DialogWrapper; 6 | import org.jetbrains.annotations.Nullable; 7 | 8 | import javax.swing.*; 9 | 10 | /** 11 | * @author itar 12 | * @version 1.0 13 | * @date 2020/02/26 14 | * @since 1.0 15 | * 16 | */ 17 | public class TemplateViewPanel extends DialogWrapper { 18 | 19 | private TemplateEdit templateEdit; 20 | 21 | public TemplateViewPanel(@Nullable Project project, boolean canBeParent, TemplateEdit templateEdit) { 22 | super(project, canBeParent); 23 | this.templateEdit = templateEdit; 24 | super.init(); 25 | } 26 | 27 | @Nullable 28 | @Override 29 | protected JComponent createCenterPanel() { 30 | return templateEdit.getMainPanel(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/util/ArrayUtils.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.util; 2 | 3 | import org.jetbrains.annotations.NotNull; 4 | import org.jetbrains.annotations.Nullable; 5 | 6 | import java.util.Optional; 7 | 8 | /** 9 | * @author yanglin 10 | * @function array的util 11 | */ 12 | public final class ArrayUtils { 13 | 14 | private ArrayUtils() { 15 | throw new UnsupportedOperationException(); 16 | } 17 | 18 | @NotNull 19 | public static Optional getOnlyElement(@Nullable T[] target, @NotNull T defValue) { 20 | return Optional.of(getOnlyElement(target).orElse(defValue)); 21 | } 22 | 23 | @NotNull 24 | public static Optional getOnlyElement(@Nullable T[] target) { 25 | return (null == target || 1 != target.length) ? Optional.empty() : Optional.ofNullable(target[0]); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/util/CollectionUtils.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.util; 2 | 3 | import java.util.Collection; 4 | 5 | /** 6 | * @author yanglin 7 | */ 8 | public final class CollectionUtils { 9 | 10 | private CollectionUtils() { 11 | throw new UnsupportedOperationException(); 12 | } 13 | 14 | public static boolean isEmpty(Collection collection) { 15 | return null == collection || 0 == collection.size(); 16 | } 17 | 18 | public static boolean isNotEmpty(Collection collection) { 19 | return !isEmpty(collection); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/util/DatabaseType.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.util; 2 | 3 | /** 4 | * 将来支持多数据库用 5 | * @author itar 6 | * 7 | */ 8 | public enum DatabaseType { 9 | 10 | /** 11 | * 12 | */ 13 | MySQL("com.mysql.jdbc.Driver", "jdbc:mysql://%s:%s/%s?useUnicode=true&useSSL=false&characterEncoding=%s", "mysql-connector-java-5.1.37.jar"), 14 | /** 15 | * 16 | */ 17 | MySQL_8("com.mysql.cj.jdbc.Driver", "jdbc:mysql://%s:%s/%s?serverTimezone=UTC&useUnicode=true&useSSL=false&characterEncoding=%s", "mysql-connector-java-8.0.12.jar"), 18 | /** 19 | * 20 | */ 21 | Oracle("oracle.jdbc.driver.OracleDriver", "jdbc:oracle:thin:@%s:%s:%s", "ojdbc14.jar"), 22 | /** 23 | * 24 | */ 25 | PostgreSQL("org.postgresql.Driver", "jdbc:postgresql://%s:%s/%s", "postgresql-9.4.1209.jar"), 26 | /** 27 | * 28 | */ 29 | SqlServer("com.microsoft.sqlserver.jdbc.SQLServerDriver", "jdbc:sqlserver://%s:%s;databaseName=%s", "sqljdbc4-4.0.jar"), 30 | /** 31 | * 32 | */ 33 | Sqlite("org.sqlite.JDBC", "jdbc:sqlite:%s", "sqlite-jdbc-3.19.3.jar"), 34 | /** 35 | * 36 | */ 37 | MariaDB("org.mariadb.jdbc.Driver", "", "mariadb-java-client-2.3.0.jar"); 38 | 39 | 40 | private String driverClassName; 41 | private String urlPattern; 42 | private String driverName; 43 | 44 | DatabaseType(String driverClassName, String urlPattern, String driverName) { 45 | this.driverClassName = driverClassName; 46 | this.urlPattern = urlPattern; 47 | this.driverName = driverName; 48 | } 49 | } -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/util/ExternalClassLoader.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.util; 2 | 3 | import java.net.URL; 4 | import java.net.URLClassLoader; 5 | 6 | /** 7 | * 加载外部类用 8 | * @author itar 9 | */ 10 | public class ExternalClassLoader extends URLClassLoader { 11 | public ExternalClassLoader(URL[] urls, ClassLoader parent) { 12 | super(urls, parent); 13 | } 14 | 15 | public ExternalClassLoader(URL[] urls) { 16 | super(urls); 17 | } 18 | public void addJar(URL url){ 19 | this.addURL(url); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/util/Icons.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.util; 2 | 3 | import com.intellij.openapi.util.IconLoader; 4 | import com.intellij.util.PlatformIcons; 5 | import icons.SpringApiIcons; 6 | 7 | import javax.swing.Icon; 8 | 9 | /** 10 | * @author yanglin 11 | */ 12 | public interface Icons { 13 | 14 | Icon MYBATIS_LOGO = IconLoader.getIcon("/javaee/persistenceId.png"); 15 | 16 | 17 | Icon PARAM_COMPLETION_ICON = PlatformIcons.PARAMETER_ICON; 18 | Icon FIELD_COMPLETION_ICON = PlatformIcons.FIELD_ICON; 19 | 20 | Icon MAPPER_LINE_MARKER_ICON = IconLoader.getIcon("/images/down.png"); 21 | 22 | Icon STATEMENT_LINE_MARKER_ICON = IconLoader.getIcon("/images/up.png"); 23 | 24 | Icon SPRING_BEAN_ICON = IconLoader.getIcon("/images/springBean.png"); 25 | Icon SPRING_INJECTION_ICON = IconLoader.getIcon("/images/showAutowiredDependencies.png"); 26 | Icon SPRING_INJECTION_ICON_NEW = SpringApiIcons.Gutter.ShowAutowiredDependencies; 27 | Icon MYBATIS_LOGO_TRUE = IconLoader.getIcon("/images/logo_raw.png"); 28 | 29 | Icon MYBATIS_FAVICON = IconLoader.getIcon("/images/favicon.ico"); 30 | Icon MYBATIS_LOGO_MINI = IconLoader.getIcon("/images/logo_17.png"); 31 | } -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/util/MybatisConstants.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.util; 2 | 3 | import com.intellij.openapi.application.PathManager; 4 | import com.intellij.psi.util.ReferenceSetBase; 5 | 6 | import java.util.Arrays; 7 | 8 | /** 9 | * @author itar 10 | * @function 常量配置 11 | */ 12 | public final class MybatisConstants { 13 | 14 | public static final String PLUGIN_NAME = "MyBatis-Lite"; 15 | public static final String PLUGIN_NO_CONNECT_NAME = "MyBatis Lite"; 16 | 17 | public static final String TEMP_DIR_PATH = PathManager.getPluginsPath() + "/" + PLUGIN_NAME + "/temp"; 18 | public static final String PLUGIN_LIB_PATH = PathManager.getPluginsPath() + "/" + PLUGIN_NAME + "/lib"; 19 | 20 | public static final String SQL_LITE_CONNECTION = "sqlLiteConnection"; 21 | public static final String MYSQL_DB_CONNECTION = "mysqlDbConnection"; 22 | 23 | private MybatisConstants() { 24 | throw new UnsupportedOperationException(); 25 | } 26 | 27 | public static final String DOT_SEPARATOR = String.valueOf(ReferenceSetBase.DOT_SEPARATOR); 28 | 29 | public static final double PRIORITY = 400.0; 30 | 31 | public static final String[] DEFAULT_SELECT_PATTEN = Arrays.asList("select", "get", "look", "find", 32 | "list", "search", "count", "query").toArray(new String[0]); 33 | public static final String[] DEFAULT_UPDATE_PATTEN = Arrays.asList("update", "modify", "set").toArray(new String[0]); 34 | public static final String[] DEFAULT_INSERT_PATTEN = Arrays.asList("insert", "add", "new", "batch", "batchInsert","insertBatch").toArray(new String[0]); 35 | public static final String[] DEFAULT_DELETE_PATTEN = Arrays.asList("del", "cancel", "delete", "drop").toArray(new String[0]); 36 | public static final String DEFAULT_SELECT_PATTEN_KEY = "DEFAULT_SELECT_PATTEN"; 37 | public static final String DEFAULT_UPDATE_PATTEN_KEY = "DEFAULT_UPDATE_PATTEN"; 38 | public static final String DEFAULT_INSERT_PATTEN_KEY = "DEFAULT_INSERT_PATTEN"; 39 | public static final String DEFAULT_DELETE_PATTEN_KEY = "DEFAULT_DELETE_PATTEN"; 40 | public static final String NAVIGATION_OPEN_STATUS = "NAVIGATION_OPEN_STATUS"; 41 | public static final String SQL_FIELD_STATUS = "SQL_FIELD_STATUS"; 42 | public static final String SQL_PRINT_STATUS = "SQL_PRINT_STATUS"; 43 | public static final String CONFIG_FILE_NAME = "CONFIG_FILE_NAME"; 44 | public static final String TRUE = "1"; 45 | public static final String MODULE_DB_CONFIG = "MODULE_ALL_DB_CONFIG"; 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/util/MybatisDomUtils.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.util; 2 | 3 | import com.google.common.base.Function; 4 | import com.google.common.collect.Collections2; 5 | import com.intellij.openapi.project.Project; 6 | import com.intellij.psi.PsiFile; 7 | import com.intellij.psi.search.GlobalSearchScope; 8 | import com.intellij.psi.xml.XmlFile; 9 | import com.intellij.psi.xml.XmlTag; 10 | import com.intellij.util.xml.DomElement; 11 | import com.intellij.util.xml.DomFileElement; 12 | import com.intellij.util.xml.DomService; 13 | import java.util.Collection; 14 | import java.util.List; 15 | import org.jetbrains.annotations.NonNls; 16 | import org.jetbrains.annotations.NotNull; 17 | import org.jetbrains.annotations.Nullable; 18 | 19 | /** 20 | * dom节点工具类 21 | * 操作mapper文件用, 跟mavenDomUtil类似 22 | */ 23 | public final class MybatisDomUtils { 24 | 25 | private MybatisDomUtils() { 26 | throw new UnsupportedOperationException(); 27 | } 28 | 29 | @NotNull 30 | @NonNls 31 | public static Collection findDomElements(@NotNull Project project, Class clazz) { 32 | GlobalSearchScope scope = GlobalSearchScope.allScope(project); 33 | List> elements = DomService.getInstance().getFileElements(clazz, project, scope); 34 | //讲domFileElement转化为domElement 35 | return Collections2.transform(elements, new Function, T>() { 36 | @Override 37 | public T apply(DomFileElement input) { 38 | return input.getRootElement(); 39 | } 40 | }); 41 | } 42 | 43 | /** 44 | * 是否是xml mapper file 45 | */ 46 | public static boolean isMybatisFile(@Nullable PsiFile file) { 47 | if (!isXmlFile(file)) { 48 | return false; 49 | } 50 | XmlTag rootTag = ((XmlFile) file).getRootTag(); 51 | return null != rootTag && rootTag.getName().equals("mapper"); 52 | } 53 | 54 | /** 55 | * 是否是mybatis 配置文件 56 | * @param file 57 | * @return 58 | */ 59 | public static boolean isMybatisConfigurationFile(@NotNull PsiFile file) { 60 | if (!isXmlFile(file)) { 61 | return false; 62 | } 63 | XmlTag rootTag = ((XmlFile) file).getRootTag(); 64 | return null != rootTag && rootTag.getName().equals("configuration"); 65 | } 66 | 67 | public static boolean isBeansFile(@NotNull PsiFile file) { 68 | if (!isXmlFile(file)) { 69 | return false; 70 | } 71 | XmlTag rootTag = ((XmlFile) file).getRootTag(); 72 | return null != rootTag && rootTag.getName().equals("beans"); 73 | } 74 | 75 | static boolean isXmlFile(@NotNull PsiFile file) { 76 | return file instanceof XmlFile; 77 | } 78 | 79 | } -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/util/OrderedProperties.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.util; 2 | 3 | import java.util.Enumeration; 4 | import java.util.LinkedHashMap; 5 | import java.util.Map; 6 | import java.util.Properties; 7 | import java.util.Set; 8 | 9 | /** 10 | * @author itar 11 | * @version 1.0 12 | * @date 2018/6/25 13 | * @since 1.0 14 | */ 15 | public class OrderedProperties extends Properties { 16 | 17 | private static final long serialVersionUID = 1L; 18 | 19 | private Map linkMap = new LinkedHashMap(); 20 | 21 | @Override 22 | public synchronized Object put(Object key, Object value) { 23 | return linkMap.put(key, value); 24 | } 25 | 26 | @Override 27 | public synchronized boolean contains(Object value) { 28 | return linkMap.containsValue(value); 29 | } 30 | 31 | @Override 32 | public boolean containsValue(Object value) { 33 | return linkMap.containsValue(value); 34 | } 35 | 36 | @Override 37 | public synchronized Enumeration elements() { 38 | throw new UnsupportedOperationException( 39 | "Enumerations are so old-school, don't use them, " 40 | + "use keySet() or entrySet() instead"); 41 | } 42 | 43 | @Override 44 | public Set> entrySet() { 45 | return linkMap.entrySet(); 46 | } 47 | 48 | @Override 49 | public synchronized void clear() { 50 | linkMap.clear(); 51 | } 52 | 53 | @Override 54 | public synchronized boolean containsKey(Object key) { 55 | return linkMap.containsKey(key); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/com/github/mustfun/mybatis/plugin/util/SqlUtil.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.util; 2 | 3 | import com.github.mustfun.mybatis.plugin.dom.model.*; 4 | import com.intellij.psi.PsiElement; 5 | 6 | /** 7 | * @author itar 8 | * @date 2020-03-02 9 | * 从sql中提炼表名 10 | */ 11 | public class SqlUtil { 12 | 13 | /** 14 | * 15 | * @param 16 | * @param position 17 | * @return 18 | */ 19 | public static String getTableNameFromSql(IdDomElement element, PsiElement position){ 20 | String value = element.getValue(); 21 | String sql = value.replace("<", "").replace(">", "").replace("\n","").replace(position.getText(), "").toLowerCase(); 22 | String tableName = null; 23 | 24 | try { 25 | if (element instanceof Select){ 26 | tableName = sql.substring(sql.indexOf("from")+4, !sql.contains("where") ?sql.length():sql.indexOf("where")).trim(); 27 | }else if (element instanceof Update){ 28 | tableName = sql.substring(sql.indexOf("update")+6, !sql.contains("set") ? sql.indexOf("include") : sql.indexOf("set")).trim(); 29 | }else if (element instanceof Insert){ 30 | sql = sql.substring(0, !sql.contains("(") ?0:sql.indexOf(")")) + sql.substring(sql.indexOf(")")+1); 31 | tableName = sql.substring(sql.indexOf("into")+4, !sql.contains("values") ? sql.length() : sql.indexOf("values")).trim(); 32 | }else if (element instanceof Delete){ 33 | tableName = sql.substring(sql.indexOf("from")+4, !sql.contains("where") ? sql.length() : sql.indexOf("where")).trim(); 34 | } 35 | } catch (Exception e) { 36 | return tableName; 37 | } 38 | return tableName; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/resources/db/generate_web.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mustfun/mybatis-lite/6b8e253ec09c8d75f3e43c27139ee26ff8fa62c4/src/main/resources/db/generate_web.db -------------------------------------------------------------------------------- /src/main/resources/fileTemplates/j2ee/Mybatis Mapper.xml.ft: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /src/main/resources/images/7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mustfun/mybatis-lite/6b8e253ec09c8d75f3e43c27139ee26ff8fa62c4/src/main/resources/images/7.png -------------------------------------------------------------------------------- /src/main/resources/images/7d.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mustfun/mybatis-lite/6b8e253ec09c8d75f3e43c27139ee26ff8fa62c4/src/main/resources/images/7d.png -------------------------------------------------------------------------------- /src/main/resources/images/down.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mustfun/mybatis-lite/6b8e253ec09c8d75f3e43c27139ee26ff8fa62c4/src/main/resources/images/down.png -------------------------------------------------------------------------------- /src/main/resources/images/down@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mustfun/mybatis-lite/6b8e253ec09c8d75f3e43c27139ee26ff8fa62c4/src/main/resources/images/down@2x.png -------------------------------------------------------------------------------- /src/main/resources/images/injection.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mustfun/mybatis-lite/6b8e253ec09c8d75f3e43c27139ee26ff8fa62c4/src/main/resources/images/injection.png -------------------------------------------------------------------------------- /src/main/resources/images/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mustfun/mybatis-lite/6b8e253ec09c8d75f3e43c27139ee26ff8fa62c4/src/main/resources/images/logo.png -------------------------------------------------------------------------------- /src/main/resources/images/logo_17.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mustfun/mybatis-lite/6b8e253ec09c8d75f3e43c27139ee26ff8fa62c4/src/main/resources/images/logo_17.png -------------------------------------------------------------------------------- /src/main/resources/images/logo_32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mustfun/mybatis-lite/6b8e253ec09c8d75f3e43c27139ee26ff8fa62c4/src/main/resources/images/logo_32.png -------------------------------------------------------------------------------- /src/main/resources/images/mapper_method.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mustfun/mybatis-lite/6b8e253ec09c8d75f3e43c27139ee26ff8fa62c4/src/main/resources/images/mapper_method.png -------------------------------------------------------------------------------- /src/main/resources/images/s.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mustfun/mybatis-lite/6b8e253ec09c8d75f3e43c27139ee26ff8fa62c4/src/main/resources/images/s.png -------------------------------------------------------------------------------- /src/main/resources/images/showAutowiredDependencies.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mustfun/mybatis-lite/6b8e253ec09c8d75f3e43c27139ee26ff8fa62c4/src/main/resources/images/showAutowiredDependencies.png -------------------------------------------------------------------------------- /src/main/resources/images/springBean.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mustfun/mybatis-lite/6b8e253ec09c8d75f3e43c27139ee26ff8fa62c4/src/main/resources/images/springBean.png -------------------------------------------------------------------------------- /src/main/resources/images/statement.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mustfun/mybatis-lite/6b8e253ec09c8d75f3e43c27139ee26ff8fa62c4/src/main/resources/images/statement.png -------------------------------------------------------------------------------- /src/main/resources/images/up.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mustfun/mybatis-lite/6b8e253ec09c8d75f3e43c27139ee26ff8fa62c4/src/main/resources/images/up.png -------------------------------------------------------------------------------- /src/main/resources/images/up@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mustfun/mybatis-lite/6b8e253ec09c8d75f3e43c27139ee26ff8fa62c4/src/main/resources/images/up@2x.png -------------------------------------------------------------------------------- /src/main/resources/inspectionDescriptions/MabatisInspection.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |

当Dao java文件里面找不到xml层的sql时候,就发出预警 4 |

5 | 6 |

当Dao java文件里面找不到xml层的sql时候,就发出预警

7 | 8 | -------------------------------------------------------------------------------- /src/main/resources/inspectionDescriptions/MapperAutowiredInspection.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |

Mapper Autowired时候作用. 4 |

5 | 6 |

Maper Autowired时候不会报错

7 | 8 | -------------------------------------------------------------------------------- /src/main/resources/inspectionDescriptions/MybatisMapperXmlInspection.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |

mybatis xml中的语法校验,发现语法不准确时候可以简单快速修复 4 |

5 | 6 |

mybatis xml中的语法校验。

7 | 8 | -------------------------------------------------------------------------------- /src/main/resources/intentionDescriptions/GenerateStatementIntention/description.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |

生成sql语句 4 |

5 | 6 |

设置

7 | 8 | -------------------------------------------------------------------------------- /src/main/resources/liveTemplates/sql.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 10 | -------------------------------------------------------------------------------- /src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | true 13 | 14 | ${pattern} 15 | 16 | 17 | 18 | 19 | 20 | 25 | 26 | ${LOG_HOME}/debug/%d{yyyy-MM-dd}/%i.log 27 | ${log.maxHistory} 28 | 30 | 2MB 31 | 32 | 33 | 34 | ${pattern} 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /src/main/test/com/github/mustfun/mybatis/plugin/test/intention/GenerateXMLCodeTest.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.test.intention; 2 | 3 | import com.github.mustfun.mybatis.plugin.intention.GenerateStatementIntention; 4 | import com.github.mustfun.mybatis.plugin.test.util.MybatisPluginTestUtil; 5 | import com.intellij.openapi.application.PathManager; 6 | import com.intellij.openapi.editor.Editor; 7 | import com.intellij.testFramework.fixtures.BasePlatformTestCase; 8 | import org.junit.Test; 9 | 10 | public class GenerateXMLCodeTest extends BasePlatformTestCase { 11 | 12 | /** 13 | * UsefulTestCase.getTestName(); 14 | * idea 2019.3路径 15 | * 16 | * @return 17 | */ 18 | @Override 19 | protected String getTestDataPath() { 20 | String testDataPath = super.getTestDataPath(); 21 | System.out.println("testDataPath = " + testDataPath); 22 | //其实就是相对路径的testData 23 | return MybatisPluginTestUtil.BASE_TEST_DATA_PATH; 24 | } 25 | 26 | @Test 27 | public void testGenerateStatement() throws Exception { 28 | //IdeaTestFixtureFactory fixtureFactory = IdeaTestFixtureFactory.getFixtureFactory(); 29 | //CodeInsightTestFixture codeInsightFixture = fixtureFactory.createCodeInsightFixture(myFixture); 30 | //codeInsightFixture.findSingleIntention("com.github.mustfun.mybatis.plugin.intention.GenerateStatementIntention"); 31 | final String homePath = PathManager.getHomePath(); 32 | 33 | System.out.println("homePath = " + homePath); 34 | myFixture.configureByFile("GenerateStatement-Input-File.md"); 35 | Editor editor = myFixture.getEditor(); 36 | 37 | GenerateStatementIntention generateStatementIntention = new GenerateStatementIntention(); 38 | generateStatementIntention.invoke(getProject(), editor, myFixture.getFile()); 39 | myFixture.checkResultByFile(getTestName(false) + "-Output-File.md"); 40 | 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/main/test/com/github/mustfun/mybatis/plugin/test/util/MybatisPluginTestUtil.java: -------------------------------------------------------------------------------- 1 | package com.github.mustfun.mybatis.plugin.test.util; 2 | 3 | import com.intellij.openapi.Disposable; 4 | import com.intellij.openapi.application.ApplicationManager; 5 | import com.intellij.openapi.application.PathManager; 6 | import com.intellij.openapi.module.Module; 7 | import com.intellij.openapi.projectRoots.ProjectJdkTable; 8 | import com.intellij.openapi.projectRoots.Sdk; 9 | import com.intellij.openapi.roots.ProjectRootManager; 10 | import com.intellij.openapi.util.Disposer; 11 | import com.intellij.openapi.util.io.FileUtil; 12 | import com.intellij.openapi.vfs.newvfs.impl.VfsRootAccess; 13 | import com.intellij.testFramework.LightProjectDescriptor; 14 | import com.intellij.testFramework.fixtures.CodeInsightTestFixture; 15 | import com.intellij.util.PathUtil; 16 | import org.jetbrains.annotations.NotNull; 17 | import org.jetbrains.annotations.TestOnly; 18 | import org.junit.Assert; 19 | 20 | import java.io.File; 21 | 22 | public class MybatisPluginTestUtil { 23 | public static final String BASE_TEST_DATA_PATH = findTestDataPath(); 24 | public static final String SDK_HOME_PATH = BASE_TEST_DATA_PATH + "/sdk"; 25 | 26 | private static String findTestDataPath() { 27 | if (new File(PathManager.getHomePath() + "/contrib").isDirectory()) { 28 | // started from IntelliJ IDEA Ultimate project 29 | return FileUtil.toSystemIndependentName(PathManager.getHomePath() + "/contrib/Dart/testData"); 30 | } 31 | 32 | final File f = new File("testData"); 33 | if (f.isDirectory()) { 34 | // started from 'mybatis-plugin' project 35 | return FileUtil.toSystemIndependentName(f.getAbsolutePath()); 36 | } 37 | 38 | final String parentPath = PathUtil.getParentPath(PathManager.getHomePath()); 39 | 40 | if (new File(parentPath + "/intellij-plugins").isDirectory()) { 41 | // started from IntelliJ IDEA Community Edition + mybatis Plugin project 42 | return FileUtil.toSystemIndependentName(parentPath + "/intellij-plugins/mybatis-lite/testData"); 43 | } 44 | 45 | if (new File(parentPath + "/contrib").isDirectory()) { 46 | // started from IntelliJ IDEA Community + Dart Plugin project 47 | return FileUtil.toSystemIndependentName(parentPath + "/contrib/mybatis-lite/testData"); 48 | } 49 | 50 | return ""; 51 | } 52 | 53 | /** 54 | * 设置sdk 55 | */ 56 | @TestOnly 57 | public void setUpProjectSdk(LightProjectDescriptor descriptor, CodeInsightTestFixture myFixture) { 58 | ApplicationManager.getApplication().runWriteAction(new Runnable() { 59 | @Override 60 | public void run() { 61 | Sdk sdk = descriptor.getSdk(); 62 | ProjectJdkTable.getInstance().addJdk(sdk); 63 | ProjectRootManager.getInstance(myFixture.getProject()).setProjectSdk(sdk); 64 | } 65 | }); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /testData/GenerateStatement-Input-File.md: -------------------------------------------------------------------------------- 1 | # something you see 2 | 3 | ## you could ignore 4 | 5 | > itar design -------------------------------------------------------------------------------- /testData/GenerateStatement-Output-File.md: -------------------------------------------------------------------------------- 1 | # intention 之后的结果 --------------------------------------------------------------------------------