SpringBoot+myBatis逆向工程的搭建
第一步 首先建立你的資料庫的表
第二步 在pom.xml新增:
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
第三步 在resource目錄下的application.yml(你也可以使用.properties)檔案中配置資料來源
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/newtest
username: root
password: admin
server:
port: 8081
第四步 在resource目錄下建立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>
<properties resource="application.yml" /> <!-- mysql驅動的位置 這個是MySQL連線的jar包,你需要指定你自己計算機上的jar包的位置--> <classPathEntry location="E:mysql-connector-java-5.1.38.jar" />
<context id="Tables" targetRuntime="MyBatis3">
<!-- 註釋 --> <commentGenerator> <!-- 是否生成註釋代時間戳 --> <property name="suppressDate" value="true"/> <!-- 是否去除自動生成的註釋 true:是 : false:否 --> <property name="suppressAllComments" value="true"/> </commentGenerator>
<!-- JDBC連線 其中connectionURL後面的newtest改為你建立的資料庫,緊跟在後面是資料庫連線的賬戶和密碼--> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/newtest" userId="root" password="123456"> </jdbcConnection>
<!-- 非必需,型別處理器,在資料庫型別和java型別之間的轉換控制--> <!-- 預設false,把JDBC DECIMAL 和 NUMERIC 型別解析為 Integer,為 true時把JDBC DECIMAL 和 NUMERIC 型別解析為java.math.BigDecimal --> <javaTypeResolver> <!-- 是否使用bigDecimal, false可自動轉化以下型別(Long, Integer, Short, etc.) --> <property name="forceBigDecimals" value="false" /> </javaTypeResolver>
<!-- 生成實體類地址 這裡需要你改動,其中targetPackage需要根據你自己建立的目錄進行改動 --> <javaModelGenerator targetPackage="com.example.demo.pojo" targetProject="src/main/java"> <!-- 從資料庫返回的值被清理前後的空格 --> <property name="trimStrings" value="true" /> <!-- enableSubPackages:是否讓schema作為包的字尾 --> <property name="enableSubPackages" value="false" /> </javaModelGenerator>
<!-- 生成mapper xml檔案 這裡不需要改動 --> <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"> <!-- enableSubPackages:是否讓schema作為包的字尾 --> <property name="enableSubPackages" value="false" /> </sqlMapGenerator>
<!-- 生成mapper xml對應Client 這裡需要改動targetPackage,依據你自己的工程--> <javaClientGenerator targetPackage="com.example.demo.mapper" targetProject="src/main/java" type="XMLMAPPER"> <!-- enableSubPackages:是否讓schema作為包的字尾 --> <property name="enableSubPackages" value="false" /> </javaClientGenerator>
<!-- 配置表資訊 --> <!-- schema即為資料庫名 tableName為對應的資料庫表 domainObjectName是要生成的實體類 enable*ByExample 是否生成 example類 -->
<table schema="newtest" tableName="category" domainObjectName="Category" enableCountByExample="true" enableDeleteByExample="true" enableSelectByExample="true" enableUpdateByExample="true"> </table>
<table schema="newtest" tableName="product" domainObjectName="Product" enableCountByExample="true" enableDeleteByExample="true" enableSelectByExample="true" enableUpdateByExample="true"> </table> </context> </generatorConfiguration>
第五步.使用maven配置生成程式碼
在IDEA中的操作步驟如下:
選擇Run->Edit Configurations
然後點選左上角的‘+’號,選擇Maven,最後在Working directory中填入你專案的根目錄,然後再下面的Command line中填入mybatis-generator:generate -e。點選OK即可。
然後執行即可
第六步.在application.yml檔案中新增mabatis的配置
mybatis: mapper-locations: classpath:mapper/*.xml type-aliases-package: com.example.springbootmybatisgeneratortest.pojo
第七步.在應用分啟動類新增@Mapper("mapper包的位置")
完成了,可能有的小夥伴要重新整理一下才可以弄出來