Recently, I found that the MyBatis plugin I used before has not been updated for a long time, so I want to use another plugin. Accidentally found that the MyBatis-Plus team has also developed a plug-in MyBatisX
, and it is really easy to use after experiencing it.
SpringBoot actual e-commerce project mall (50k+star) address: https://github.com/macrozheng/mall
Introduction to MybatisX
MybatisX is a rapid development plug-in based on IDEA, developed and maintained by the MyBatis-Plus team for efficiency.
Its main functions are as follows:
- Support mutual navigation and jump of methods between mapper.xml and Mapper interface;
- Built-in code generator, through the use of GUI, can generate Domain, mapper.xml, Mapper, Service and Service implementation class codes according to the database;
- Code generator templates can be customized;
- The query implementation can be generated in mapper.xml directly based on the method name in a way similar to JPA, while supporting hints.
use
Next, I will introduce the use of MybatisX. Here is my scaffolding project mall-tiny as an example.
Install
Before using it, we need to search and install the MyBatisX plugin in the plugin market.
After the installation is complete, we will find that all Mapper interfaces and mapper.xml files have become the bird icon of MyBatis.
XML and interface jump
We can directly jump to the SQL implementation corresponding to mapper.xml by clicking the icon on the left side of the Mapper interface method. Clicking the icon on the left side of mapper.xml can also directly jump to the corresponding method in the Mapper interface.
Automatically generate code
Remember the code generator we wrote by hand in the mall-tiny project before, MyBatisX directly has a graphical interface, let's experience it below.
- After selecting the table, right-click can directly generate the CRUD code of the corresponding table, of course, you can also select multiple, support to generate multiple tables at one time;
- When generating, you can modify options through the GUI, such as modifying the basic package path, entity class package path, etc.;
- When generating, select the annotation and template type as Mybatis-Plus 3. If necessary, you can check the Lombok option and modify the file path of mapper.xml;
- After clicking OK, the following files will be generated. Remember the code generator written in code in the mall-tiny project before. With GUI, you don't need to write it!
custom build template
If you feel that the default code generator templates do not meet your requirements, you can also try custom templates.
- We generally add Swagger annotations to entity classes to facilitate the generation of API documents. The entity classes generated by MyBatisX by default do not have Swagger annotations;
- MyBatisX also provides an entity class template with Swagger annotations, but it is a bit complicated and does not meet my requirements. We can modify the generated template. The generated templates are all in the
extensions->MyBaitsX
directory;
- Here we modify the
domain.ftl
file, and the final file content is as follows;
package ${domain.packageName};
import java.io.Serializable;
<#list tableClass.importList as fieldType>${"\n"}import ${fieldType};</#list>
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import com.baomidou.mybatisplus.annotation.TableName;
/**
* ${tableClass.remark!}
*/
@Data
@EqualsAndHashCode(callSuper = false)
@TableName("${tableClass.tableName}")
@ApiModel(value="${tableClass.shortClassName}对象", description="${tableClass.remark!}")
public class ${tableClass.shortClassName} implements Serializable {
private static final long serialVersionUID=1L;
<#list tableClass.allFields as field>
@ApiModelProperty("${field.remark!}")
private ${field.shortTypeName} ${field.fieldName};
</#list>
}
- Then run the code generator and choose to only generate entity classes with Swagger annotations;
- After the generation is completed, the entity class will be annotated with Swagger, isn't it very convenient!
JPA Tips
MyBatisX also has a powerful function that can directly generate SQL implementation based on JPA-style method names, without handwriting SQL.
- For example, if we want to write a method of batch inserting data, we can do it like this;
- For example, we want to write a method for querying brands by name, MyBatisX will automatically prompt fields like JPA, and can automatically generate SQL implementation;
- For example, we want to write a method to modify the brand name according to the ID;
- For example, we want to write a method to delete a brand according to the name, and the JPA prompt of MyBatisX is still very comprehensive!
Icon settings
If you don't want both the Mapper interface and the mapper.xml file to become a bird icon, you can make changes in the settings of MyBatisX.
Summarize
MyBatisX is indeed a very useful IDEA plug-in. It not only has comprehensive prompts, but also comes with a graphical code generator, which can greatly improve our development efficiency. The JPA prompt function in MyBatisX is also very good. As long as your method name conforms to the JPA specification, the SQL implementation can be automatically generated, which is indeed a good function.
References
Official documentation: https://baomidou.com/pages/ba5b24/
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。