在开发项目中,适当的引用插件确实能提高工作效率,尤其是单人开发时候;
MybatisGenerator插件能节省单表操作的时间,能自动生成增删改查sql以及对应的dao(有的项目中叫mapper层);
SpringBoot开发时候引用MybatisGenerator插件步骤如下:
1.在pom.xml标签<build>中插入标签<plugin>,如下:
<!--mg-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.6</version>
<dependencies>
<dependency>
<groupId> mysql</groupId>
<artifactId> mysql-connector-java</artifactId>
<version> 5.1.46</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.6</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<phase>package</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<!--允许移动生成的文件 -->
<verbose>true</verbose>
<!-- 是否覆盖 -->
<overwrite>true</overwrite>
<!-- 自动生成的配置 -->
<configurationFile>
src/main/resources/mybatis-generator.xml</configurationFile>
</configuration>
</plugin>
保存后,maven会自动引入jar包;并且在Maven Project视图中,plugins目录中出现mybatis-generator目录,如下图:
2.导入配置文件,如pom.xml中标签<configurationFile>中配置路径,文件内容最好从官网下载,在模块XML Configuration ReferenceXML中把右侧的拷贝下来修改就行,如下图:
本人配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!--数据库链接URL,用户名、密码 -->
<context id="MySQL" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1/linda"
userId="root"
password="admin">
</jdbcConnection>
<!--是否启用java.math.BigDecimal-->
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<javaModelGenerator targetPackage="com.linda.modules.model" targetProject="D:\idea-projects\linda\src\main\java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<sqlMapGenerator targetPackage="sqlmappings" targetProject="D:\idea-projects\linda\src\main\resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER" targetPackage="com.linda.modules.mapper" targetProject="D:\idea-projects\linda\src\main\java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<table tableName="brand" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
</table>
<!-- <table tableName="persons" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
</table>
<table tableName="orders" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
</table>-->
</context>
</generatorConfiguration>
3.运行maven命令,生成对应的文件;在maven试图中,双击mybatis-generator:generate,会自动生成,如果报错,根据控制台报错信息修改文件;如下图: