您好,登錄后才能下訂單哦!
本篇文章為大家展示了springboot開(kāi)發(fā)單體web shop中Mybatis Generator如何生成common mapper,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。
在我們開(kāi)啟一個(gè)新項(xiàng)目的研發(fā)后,通常要編寫(xiě)很多的entity/pojo/dto/mapper/dao...
, 大多研發(fā)兄弟們都會(huì)抱怨,為什么我要重復(fù)寫(xiě)CRUD
? 我們?yōu)榱吮苊饩帉?xiě)一些不必要的重復(fù)代碼。給大家介紹使用一個(gè)開(kāi)源工具,來(lái)幫助我們從這種簡(jiǎn)單枯燥的編碼中解救出來(lái)。 隆重有請(qǐng): MyBatis通用Mapper4 > 通用Mapper都可以極大的方便開(kāi)發(fā)人員??梢噪S意的按照自己的需要選擇通用方法,還可以很方便的開(kāi)發(fā)自己的通用方法。 極其方便的使用MyBatis單表的增刪改查。 支持單表操作,不支持通用的多表聯(lián)合查詢。 通用 Mapper 支持 Mybatis-3.2.4 及以上版本。 Tips: 各位技術(shù)同仁一定要有版本意識(shí)哦~
添加依賴(lài)
<!--?xml version="1.0" encoding="UTF-8"?--> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactid>expensive-shop</artifactid> <groupid>com.life-runner</groupid> <version>1.0-SNAPSHOT</version> </parent> <modelversion>4.0.0</modelversion> <artifactid>mybatis-generator-tool</artifactid> <properties> <project.build.sourceencoding>UTF-8</project.build.sourceencoding> </properties> <build> <plugins> <!--springboot 構(gòu)建可執(zhí)行fat jars必須的插件,如不添加,在生產(chǎn)環(huán)境會(huì)有問(wèn)題--> <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.6</version> <configuration> <!-- 設(shè)置配置文件路徑 --> <configurationfile> ${basedir}/src/main/resources/generator/generatorConfig.xml </configurationfile> <!--允許覆蓋--> <overwrite>true</overwrite> <verbose>true</verbose> </configuration> <dependencies> <!-- mysql8 驅(qū)動(dòng)--> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <version>8.0.16</version> </dependency> <!--通用 Mapper--> <dependency> <groupid>tk.mybatis</groupid> <artifactid>mapper</artifactid> <version>4.1.5</version> </dependency> </dependencies> </plugin> </plugins> </build> </project>
編寫(xiě)配置文件 根據(jù)我們?cè)趐om文件中指定的路徑:${basedir}/src/main/resources/generator/generatorConfig.xml
, 我們需要在項(xiàng)目src=>main=>resource
目錄下創(chuàng)建generator
文件夾,在文件夾下創(chuàng)建文件generatorConfig.xml
,內(nèi)容如下:
<!--?xml version="1.0" encoding="UTF-8"?--> <generatorconfiguration> <!--引入數(shù)據(jù)庫(kù)配置內(nèi)容--> <properties resource="generator/config.properties" /> <context id="MysqlContext" targetruntime="MyBatis3Simple" defaultmodeltype="flat"> <!--配置是否使用通用 Mapper 自帶的注釋擴(kuò)展,默認(rèn) true--> <!--<property name="useMapperCommentGenerator" value="false"/>--> <plugin type="tk.mybatis.mapper.generator.MapperPlugin"> <!--設(shè)置Mapper生成的basic,可自定義--> <property name="mappers" value="tk.mybatis.mapper.common.Mapper" /> <!--大小寫(xiě)轉(zhuǎn)換敏感--> <property name="caseSensitive" value="true" /> <!--引入lombok注解--> <property name="lombok" value="Getter,Setter,ToString" /> <!--分隔符定義--> <property name="beginningDelimiter" value="`" /> <property name="endingDelimiter" value="`" /> </plugin> <!-- 設(shè)置數(shù)據(jù)庫(kù)配置 --> <jdbcconnection driverclass="${jdbc.driverClass}" connectionurl="${jdbc.url}" userid="${jdbc.user}" password="${jdbc.password}"> </jdbcconnection> <!-- 對(duì)應(yīng)生成的pojo所在包 --> <javamodelgenerator targetPackage="com.liferunner.pojo" targetProject="src/main/java" /> <!-- 對(duì)應(yīng)生成的mapper所在目錄 --> <sqlmapgenerator targetPackage="mapper" targetProject="src/main/resources" /> <!-- 配置mapper對(duì)應(yīng)的java映射 --> <javaclientgenerator targetPackage="com.liferunner.mapper" targetProject="src/main/java" type="XMLMAPPER" /> <!-- 數(shù)據(jù)庫(kù)表 --> <table tablename="carousel"></table> <table tablename="category"></table> <table tablename="items"></table> <table tablename="items_comments"></table> <table tablename="items_img"></table> <table tablename="items_param"></table> <table tablename="items_spec"></table> <table tablename="order_items"></table> <table tablename="order_status"></table> <table tablename="orders"></table> <table tablename="shop_users"></table> <table tablename="user_address"></table> <table tablename="users"></table> </context> </generatorconfiguration>
我們可以看到一行配置內(nèi)容:<properties resource="generator/config.properties" />
,這里是為了將我們的數(shù)據(jù)庫(kù)連接、賬號(hào)等信息外置,配置內(nèi)容如下:
jdbc.driverClass = com.mysql.cj.jdbc.Driver jdbc.url = jdbc:mysql://localhost:3306/expensiveshop?characterEncoding=UTF-8&useSSL\ =false&useUnicode=true&serverTimezone=UTC jdbc.user = root jdbc.password = 12345678
可以看到這里設(shè)置的內(nèi)容就是下屬代碼中用到的。
... <jdbcconnection driverclass="${jdbc.driverClass}" connectionurl="${jdbc.url}" userid="${jdbc.user}" password="${jdbc.password}"> </jdbcconnection> ...
配置信息大家可以參考:傳送門(mén)
使用maven測(cè)試生成 執(zhí)行以下命令:
mybatis-generator-tool>mvn mybatis-generator:generate [INFO] Scanning for projects... [INFO] [INFO] ---------------< com.life-runner:mybatis-generator-tool >--------------- [INFO] Building mybatis-generator-tool 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- mybatis-generator-maven-plugin:1.3.6:generate (default-cli) @ mybatis-generator-tool --- [INFO] Connecting to the Database [INFO] Introspecting table carousel [INFO] Introspecting table category ... [INFO] Generating Record class for table carousel [INFO] Generating Mapper Interface for table carousel [INFO] Generating SQL Map for table carousel ... [INFO] Saving file CarouselMapper.xml ... [INFO] Saving file Carousel.java [INFO] Saving file Users.java ... [WARNING] Table configuration with catalog null, schema null, and table shop_users did not resolve to any tables [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.374 s [INFO] Finished at: 2019-11-05T15:40:07+08:00 [INFO] ------------------------------------------------------------------------
可以看到執(zhí)行成功,雖然這里執(zhí)行成功,但是當(dāng)我們打開(kāi)文件的時(shí)候會(huì)發(fā)現(xiàn):
package com.liferunner.pojo; import java.util.Date; import javax.persistence.*; import lombok.Getter; import lombok.Setter; import lombok.ToString; @Getter @Setter @ToString @Table(name = "Carousel") public class Carousel { /** * ????id ???id */ @Id private String id; /** * ????? ????? */ private String imageUrl; ... }
這里出現(xiàn)了亂碼問(wèn)題,這又是怎么回事呢? 沒(méi)關(guān)系,let's bing... 傳送門(mén),可以看到有265000條結(jié)果,那就說(shuō)明我們的問(wèn)題已經(jīng)有太多的人遇到了,隨便點(diǎn)開(kāi)一個(gè): 可以看到紅框里面的內(nèi)容我們?nèi)笔Я?,?code>\expensive-shop\mybatis-generator-tool\src\main\resources\generator\generatorConfig.xml中添加上 <property name="javaFileEncoding" value="UTF-8" />
,重新執(zhí)行生成命令,可以看到我們的亂碼就沒(méi)有了。
@Getter @Setter @ToString @Table(name = "`carousel`") public class Carousel { /** * 主鍵 */ @Id @Column(name = "`id`") private String id; /** * 圖片 圖片地址 */ @Column(name = "`image_url`") private String imageUrl; ...
> Tips: > 在這一環(huán)節(jié)先劇透一個(gè)bug,否則我擔(dān)心在后續(xù)大家遇到的時(shí)候,因?yàn)樗_實(shí)是和Common Mapper生成相關(guān)的。
我們點(diǎn)開(kāi)生成的Users.java
,可以看到如下所示:
@Getter @Setter @ToString @Table(name = "users") public class Users { @Column(name = "USER") private String user; @Column(name = "CURRENT_CONNECTIONS") private Long currentConnections; @Column(name = "TOTAL_CONNECTIONS") private Long totalConnections; }
可是我們的Users
表不是這樣的呀,這是怎么回事??? 讓我們分析分析: 1.既然沒(méi)有用到我們自己的Users表,但是又確實(shí)通過(guò)生成器生成了,那么很明顯肯定是Mysql數(shù)據(jù)庫(kù)中表,這是肯定的。 2.那么問(wèn)題就來(lái)了,它從哪里冒出來(lái)的?找它,盤(pán)它。 3.到底是哪個(gè)數(shù)據(jù)庫(kù)中的呢?sys?information_schema?performance_schema? 4.挨個(gè)查詢,果然: 可以看到,在performance_schema
數(shù)據(jù)庫(kù)中有一個(gè)users
表,那么到底是不是我們生成出來(lái)的呢?執(zhí)行SHOW CREATE TABLE users
, 結(jié)果如上圖,字段和生成出來(lái)的是一致的! 5.抓住它了,怎么盤(pán)它??? > 很簡(jiǎn)單,修改jdbc:mysql://localhost:3306/expensiveshop?nullCatalogMeansCurrent=true&characterEncoding=UTF-8&useSSL
=false&useUnicode=true&serverTimezone=UTC,新增上加粗部分就可以了。
nullCatalogMeansCurrent
字面意思很簡(jiǎn)單,就是說(shuō)如果是null catalog,我就選擇current.因?yàn)?code>mysql不支持catalog,我們需要告知mybatis
這個(gè)特性,設(shè)置為true
就行了。 > 按照SQL標(biāo)準(zhǔn)的解釋?zhuān)赟QL環(huán)境下Catalog和Schema都屬于抽象概念,主要用來(lái)解決命名沖突問(wèn)題。 從概念上說(shuō),一個(gè)數(shù)據(jù)庫(kù)系統(tǒng)包含多個(gè)Catalog,每個(gè)Catalog又包含多個(gè)Schema,而每個(gè)Schema又包含多個(gè)數(shù)據(jù)庫(kù)對(duì)象(表、視圖、序列等),反過(guò)來(lái)講一個(gè)數(shù)據(jù)庫(kù)對(duì)象必然屬于一個(gè)Schema,而該Schema又必然屬于一個(gè)Catalog,這樣我們就可以得到該數(shù)據(jù)庫(kù)對(duì)象的完全限定名稱(chēng)從而解決命名沖突的問(wèn)題了 從實(shí)現(xiàn)的角度來(lái)看,各種數(shù)據(jù)庫(kù)系統(tǒng)對(duì)Catalog和Schema的支持和實(shí)現(xiàn)方式千差萬(wàn)別,針對(duì)具體問(wèn)題需要參考具體的產(chǎn)品說(shuō)明書(shū),比較簡(jiǎn)單而常用的實(shí)現(xiàn)方式是使用數(shù)據(jù)庫(kù)名作為Catalog名,Oracle使用用戶名作為Schema名.
上述內(nèi)容就是springboot開(kāi)發(fā)單體web shop中Mybatis Generator如何生成common mapper,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。