Intellij idea plug-in development-Quickly locate SQL IN THE mybatis mapper file and intellijmybatis

Source: Internet
Author: User

Intellij idea plug-in development-Quickly locate SQL IN THE mybatis mapper file and intellijmybatis

Intellij idea provides openApi, through which we can develop our own plug-ins to improve work efficiency. You can directly paste a link here to get started with the demo. http://www.jianshu.com/p/2427e4cfd3e9, you can also find it online .. I use intellij idea 2017 and jdk 1.8 or above.

Step 1: New project, select IntelliJ Plaltform Plugin,

Click Next, enter the project name, and click finish. The project structure is as follows:

Plugin. xml is the configuration file of the plug-in;

Step 2: Click the src folder, Alt + Insert shortcut, and select action,

Then fill in the relevant information and group it to JavaGenerateGroup1. The shortcut key is Ctrl + Shift + X (that is, the code for executing this action)

Click OK to create a java class named GoToMapperAction and the corresponding configuration information appears in plugin. xml, such:

At this point, execute the shortcut key Ctrl + Shift + X to execute the actionreceivmed method, and then write the specific logic code;

Step 3: The AnActionEvent parameter in actionreceivmed is very useful and carries the current interactive context information. You need to retrieve the current status information of the IDE, such as the active project, the selected file, and the selected status in the editor, you can use AnActionEvent. getData () method. The DataKeys class defines different data key values that can be passed to this method;

1. Obtain Method Name:

PsiElement psiElement = e. getData (PlatformDataKeys. PSI_ELEMENT); // The mouse element. Here is the method if (psiElement = null) {return;} String methodName = psiElement. toString (). replace ("PsiMethod:", ""); // obtain the method name.

2. Get the Class Name of the method:

PsiElement psiElementParent = psiElement. getParent (); // obtain the method's parent element if (psiElementParent = null) {return;} PsiFile containingFile = psiElementParent. getContainingFile (); // get the file. Here is the java class String className = containingFile. getName (); // get the class name

3. at this point, we have obtained the class name and method name, so that we can determine the SQL location of the corresponding mybatis mapper File id = "method name; my project names are relatively regular, so the corresponding mapper file name can be determined as follows:

String mapperName ;if (className.endsWith("Service.java")) {    mapperName = className.replace("Service.java", "Mapper.xml");}else if (className.endsWith("Dao.java")) {    mapperName = className.replace("Dao.java", "Mapper.xml");}else {    return;}

Of course, it can also be found through the er namespace;

4. open xml

Project project = e. getProject (); // find the PsiFile named mapperName [] files = PsiShortNamesCache. getInstance (project ). getFilesByName (mapperName); if (files. length = 1) {XmlFile xmlFile = (XmlFile) files [0]; String xml = xmlFile. getDocument (). getText (); // obtain the mapper xml string // determine whether mapper has an id = "methodName" SQL statement. If there is a mapper, open the corresponding mapper xml // The judgment here is relatively simple, not rigorous. You can use XmlFile to traverse nodes to determine whether the if (StringUtil. isNotEmpty (xml) & xml. contains ("id = \" "+ methodName +" \ "") {toMapper (project, methodName, files [0]. getVirtualFile (), xml );}}
/*** Enter mapper * @ param project * @ param methodName * @ param mapperFile * @ param xml */private void toMapper (Project project, String methodName, VirtualFile mapperFile, String xml) {// open the xml file OpenFileDescriptor openFileDescriptor = new OpenFileDescriptor (project, mapperFile); Editor editor = FileEditorManager. getInstance (project ). openTextEditor (openFileDescriptor, true); // gets the number of rows where the SQL statement is located. This method is stupid. The api has been searching for a long time and does not find any way to obtain the row number. I hope you can give some advice on String [] split = xml. split ("\ n"); int lineNumber = 0; for (int I = 0; I <split. length; I ++) {String line = split [I]; if (StringUtil. isNotEmpty (line) & line. contains (methodName) {lineNumber = I; break ;}// locate the corresponding SQL CaretModel caretModel = editor. getCaretModel (); LogicalPosition logicalPosition = caretModel. getLogicalPosition (); logicalPosition. leanForward (true); LogicalPosition logical = new LogicalPosition (lineNumber, logicalPosition. column); caretModel. moveToLogicalPosition (logical); SelectionModel selectionModel = editor. getSelectionModel (); selectionModel. selectLineAtCaret ();}

5. Now the code is complete. Now you can test it. Click Run in the upper right corner to open a new idea for debugging.

Then open the project in the new idea, hover your mouse over the method you are looking for, and press Ctrl + Shift + X to jump to xml.

6. Publish, generate the jar file, and then install it to idea.

 

Last: The general logic is described here. You can also determine whether the SQL statement exists in xml and create a tag automatically if no tag exists. openapi has many concepts, you can go to Baidu or the official website to view the document.

It is a plug-in function recently written by myself. It only applies to my own projects and no code is available. More interesting functions can be developed by myself.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.