springboot整合mybatis(分頁及generator自動生成程式碼)
阿新 • • 發佈:2019-02-19
generator自動生成程式碼
1:匯入外掛
<!-- mybatis generator 自動生成程式碼外掛 --> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile> <overwrite>true</overwrite> <verbose>true</verbose> </configuration> </plugin>
2:建立generatorConfig.xml檔案
在resources目錄下新建一個generator目錄,再建立一個generatorConfig.xml檔案
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="D:\MvnRepo\mysql\mysql-connector-java\5.1.34\mysql-connector-java-5.1.34.jar"/> <context id="MysqlTables" targetRuntime="MyBatis3"> <!-- 注意這裡面的順序確定的,不能隨變更改 --> <!-- 自定義的分頁外掛 <plugin type="com.deppon.foss.module.helloworld.shared.PaginationPlugin"/> --> <!-- 可選的(0 or 1) --> <!-- 註釋生成器 --> <commentGenerator> <!-- 是否去除自動生成的註釋 true:是 : false:否 --> <property name="suppressAllComments" value="true" /> </commentGenerator> <!-- 必須的(1 required) --> <!--資料庫連線的資訊:驅動類、連線地址、使用者名稱、密碼 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test" userId="root" password="123"> </jdbcConnection> <!-- 可選的(0 or 1) --> <!-- 型別轉換器或者加型別解析器 --> <!-- 預設false,把JDBC DECIMAL 和 NUMERIC 型別解析為 Integer true,把JDBC DECIMAL 和 NUMERIC 型別解析為java.math.BigDecimal --> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- 必須的(1 required) --> <!-- java模型生成器 --> <!-- targetProject:自動生成程式碼的位置 --> <javaModelGenerator targetPackage="com.example.demo.entity" targetProject="E:\IDEA-project\day04\src\main\java"> <!-- TODO enableSubPackages:是否讓schema作為包的字尾 --> <property name="enableSubPackages" value="true" /> <!-- 從資料庫返回的值被清理前後的空格 --> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- 必須的(1 required) --> <!-- map xml 生成器 --> <sqlMapGenerator targetPackage="mapping" targetProject="E:\IDEA-project\day04\src\main\resources"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- 可選的(0 or 1) --> <!-- mapper 或者就是dao介面生成器 --> <javaClientGenerator targetPackage="com.example.demo.dao" targetProject="E:\IDEA-project\day04\src\main\java" type="XMLMAPPER"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- 必須的(1...N) --> <!-- pojo 實體生成器 --> <!-- tableName:用於自動生成程式碼的資料庫表;domainObjectName:對應於資料庫表的javaBean類名 --> <!-- schema即為資料庫名 可不寫 --> <table tableName="account" domainObjectName="Account" enableInsert="true" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> <!-- 忽略欄位 可選的(0 or 1) --> <!-- <ignoreColumn column="is_use" /> --> <!--//無論欄位是什麼型別,生成的類屬性都是varchar。 可選的(0 or 1) 測試無效 --> <!-- <columnOverride column="city_code" jdbcType="VARCHAR" /> --> </table> </context> </generatorConfiguration>
3:建立添置
4:執行
注意!!!同一張表一定不要執行多次,因為mapper的對映檔案中會生成多次的程式碼,導致報錯,切記
整合Mybatis
1:基於註解
主要是在dao層引入@mapper註解
在增刪改查的方法上依次加上註解為@insert,@delete,@update,@select註解,並在註解中寫上sql語句
測試新增可行,後面的略了...
2:基於XML方式
主要在application.yml中指定mapper的所在目錄,在啟動類上加上@MapperScan("dao所在的包")開啟包掃描
專案路徑
分頁外掛
1:匯入依賴
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.5</version> </dependency>
2:在application.yml中配置
pagehelper: helperDialect: mysql reasonable: true supportMethodsArguments: true params: count=countSql returnPageInfo: check