今天搞畢設的時候發現有大量的增刪改查着實讓人頭疼,
想起在深圳實習的時候項目中可以自動生成sql語句,這次又去找了下。
發現了兩種解決方案:
1. mybatis-generator插件
2. MyBatisCodeHelper-Pro 插件
兩者都可以根據數據庫表自動生成pojo對象,sql語句和dao層
區別是前者免費功能簡單點,後者付費功能更全。
所以我選擇前者...
不過這裏還是貼一下MyBatisCodeHelper-Pro教程地址,等上班後支持一波
https://gejun123456.github.io/MyBatisCodeHelper-Pro/#/README
- maven配置mybatis-generator 插件
- 配置mybatis-generator 配置文件設置逆向工程細節
- 運行插件即可。
一步一步來:maven配置generator插件
<!-- mybatis逆向工程jar包 -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.4</version>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
<configurationFile>src/main/resources/generator/generatorConfig.xml</configurationFile>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
configurationFile標籤填寫的是generator配置文件地址
還得在插件裏添加mysql-connector-java依賴
注意這裏還會有版本問題,我就遇到了:
Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2:generate (default-cli) on project restful-demo: Execution default-cli of goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2:generate failed: Exception getting JDBC Driver
原因:
mybatis-generator-maven-plugin版本爲1.3.2
mysql-connector-java版本爲5.1.41
兩個版本不兼容,將mysql-connector-java版本修改爲8.0.13等合適版本即可
- 配置generatorConfig.xml文件
<?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>
<!-- 數據庫驅動:選擇你的本地硬盤上面的數據庫驅動包-->
<!-- <classPathEntry location="/usr/local/apache-maven-3.6.2/repository/mysql/mysql-connector-java/5.1.41/mysql-connector-java-5.1.41.jar"/>-->
<context id="DB2Tables" targetRuntime="MyBatis3">
<!-- JavaBean 實現 序列化 接口 -->
<plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
<!-- 生成toString -->
<plugin type="org.mybatis.generator.plugins.ToStringPlugin" />
<!-- optional,旨在創建class時,對註釋進行控制 -->
<commentGenerator>
<property name="suppressDate" value="true"/>
<!-- <property name="suppressAllComments" value="true"/>-->
</commentGenerator>
<!--數據庫鏈接URL,用戶名、密碼 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/scm"
userId="root"
password="123456">
</jdbcConnection>
<!-- 類型轉換 -->
<javaTypeResolver >
<!-- 是否使用bigDecimal,
false: 把JDBC DECIMAL 和 NUMERIC 類型解析爲 Integer(默認)
true: 把JDBC DECIMAL 和 NUMERIC 類型解析爲java.math.BigDecimal
-->
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 生成模型的包名和位置-->
<javaModelGenerator targetPackage="com.example.restfuldemo.entities" targetProject="src/main/java">
<!-- 默認false 是否允許子包 -->
<property name="enableSubPackages" value="true" />
<!-- 默認false 是否對model添加 構造函數 -->
<property name="constructorBased" value="false"/>
<!-- 默認false 建立的Model對象是否 不可改變 即生成的Model對象不會有 setter方法,只有構造方法 -->
<property name="immutable" value="false"/>
<!-- 默認false 是否對類CHAR類型的列的數據進行trim操作 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 生成映射文件的包名和位置-->
<sqlMapGenerator targetPackage="mybatis.mapper" targetProject="src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 生成DAO的包名和位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.example.restfuldemo.dao" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<table schema="general" tableName="order" domainObjectName="order"
enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" enableUpdateByExample="false">
</table>
<table schema="general" tableName="product" domainObjectName="product"
enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" enableUpdateByExample="false">
</table>
<table schema="general" tableName="supplier" domainObjectName="supplier"
enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" enableUpdateByExample="false">
</table>
</context>
</generatorConfiguration>