SpringBoot整合Mybatis Generator自動(dòng)生成代碼
Mybatis是目前主流的ORM框架,相比于hibernate的全自動(dòng),它是半自動(dòng)化需要手寫(xiě)sql語(yǔ)句、接口、實(shí)體對(duì)象,后來(lái)推出的Generator自動(dòng)生成代碼,可以幫我們提高開(kāi)發(fā)效率。
本文目的:SpringBoot 整合 Mybatis Generator自動(dòng)生成dao、entity、mapper.xml實(shí)現(xiàn)單表增刪改查。
1.創(chuàng)建SpringBoot項(xiàng)目
File→New→Project… 選擇Spring Initializr,選擇JDK版本,默認(rèn)初始化URL
填寫(xiě)項(xiàng)目名稱(chēng),java版本,其他描述信息
選擇項(xiàng)目存放路徑
選擇web、mybatis、mysql依賴
Next–>Finish完成項(xiàng)目創(chuàng)建
2. mybatis-generator-maven插件的配置
打開(kāi)項(xiàng)目的pom.xml文件添加
<plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <configuration> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> </plugin>
完整pom.xml
<?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"> <modelVersion>4.0.0</modelVersion> <groupId>com.xyz</groupId> <artifactId>mybatis</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>mybatis</name> <description>Spring Boot 整合 Mybatis</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.5</version> </dependency> </dependencies> <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> <configuration> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> </plugin> </plugins> </build> </project>
3. 項(xiàng)目結(jié)構(gòu)構(gòu)建
在項(xiàng)目目錄下(這里是mybatis)添加controller、service、dao、entity包,在resources下添加mapper包存放映射文件。
4. application.yml配置
#端口號(hào)配置 server: port: 8088 spring: #模板引擎配置 thymeleaf: prefix: classpath:/templates/ suffix: .html mode: HTML encoding: UTF-8 cache: false servlet: content-type: text/html #靜態(tài)文件配置 resources: static-locations: classpath:/static,classpath:/META-INF/resources,classpath:/templates/ #jdbc配置 datasource: url: jdbc:mysql://localhost:3306/video?useUnicode=true&characterEncoding=utf8 username: xyz password: xyz driver-class-name: com.mysql.jdbc.Driver #mybatis配置 mybatis: #映射文件路徑 mapper-locations: classpath:mapper/*.xml #模型所在的保命 type-aliases-package: com.xyz.mybatis.entity
5. generatorConfig.xml配置
在resources文件下創(chuàng)建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:數(shù)據(jù)庫(kù)的JDBC驅(qū)動(dòng),換成你自己的驅(qū)動(dòng)位置 可選 --> <classPathEntry location="E:\IdeaProjects\mysql-connector-java-5.1.47.jar"/> <!-- 一個(gè)數(shù)據(jù)庫(kù)一個(gè)context,defaultModelType="flat" 大數(shù)據(jù)字段,不分表 --> <context id="MysqlTables" targetRuntime="MyBatis3Simple" defaultModelType="flat"> <!-- 自動(dòng)識(shí)別數(shù)據(jù)庫(kù)關(guān)鍵字,默認(rèn)false,如果設(shè)置為true,根據(jù)SqlReservedWords中定義的關(guān)鍵字列表;一般保留默認(rèn)值,遇到數(shù)據(jù)庫(kù)關(guān)鍵字(Java關(guān)鍵字),使用columnOverride覆蓋 --> <property name="autoDelimitKeywords" value="true"/> <!-- 生成的Java文件的編碼 --> <property name="javaFileEncoding" value="utf-8"/> <!-- beginningDelimiter和endingDelimiter:指明數(shù)據(jù)庫(kù)的用于標(biāo)記數(shù)據(jù)庫(kù)對(duì)象名的符號(hào),比如ORACLE就是雙引號(hào),MYSQL默認(rèn)是`反引號(hào); --> <property name="beginningDelimiter" value="`"/> <property name="endingDelimiter" value="`"/> <!-- 格式化java代碼 --> <property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/> <!-- 格式化XML代碼 --> <property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/> <plugin type="org.mybatis.generator.plugins.SerializablePlugin"/> <plugin type="org.mybatis.generator.plugins.ToStringPlugin"/> <!-- 注釋 --> <commentGenerator> <property name="suppressAllComments" value="true"/><!-- 是否取消注釋 --> <property name="suppressDate" value="false"/> <!-- 是否生成注釋代時(shí)間戳--> </commentGenerator> <!-- jdbc連接--> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/video?serverTimezone=UTC" userId="xyz" password="xyz"/> <!-- 類(lèi)型轉(zhuǎn)換 --> <javaTypeResolver> <!-- 是否使用bigDecimal, false可自動(dòng)轉(zhuǎn)化以下類(lèi)型(Long, Integer, Short, etc.) --> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- 生成實(shí)體類(lèi)地址 --> <javaModelGenerator targetPackage="com.xyz.mybatis.entity" targetProject="src/main/java"> <!-- 是否讓schema作為包的后綴 --> <property name="enableSubPackages" value="false"/> <!-- 從數(shù)據(jù)庫(kù)返回的值去掉前后空格 --> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 生成map.xml文件存放地址 --> <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"> <property name="enableSubPackages" value="false"/> </sqlMapGenerator> <!-- 生成接口dao --> <javaClientGenerator targetPackage="com.xyz.mybatis.dao" targetProject="src/main/java" type="XMLMAPPER"> <property name="enableSubPackages" value="false"/> </javaClientGenerator> <!-- table可以有多個(gè),每個(gè)數(shù)據(jù)庫(kù)中的表都可以寫(xiě)一個(gè)table,tableName表示要匹配的數(shù)據(jù)庫(kù)表,也可以在tableName屬性中通過(guò)使用%通配符來(lái)匹配所有數(shù)據(jù)庫(kù)表,只有匹配的表才會(huì)自動(dòng)生成文件 enableSelectByPrimaryKey相應(yīng)的配置表示是否生成相應(yīng)的接口 --> <table tableName="t_manager" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" enableSelectByPrimaryKey="true" enableUpdateByPrimaryKey="true" enableDeleteByPrimaryKey="true"> <property name="useActualColumnNames" value="true"/> </table> </context> </generatorConfiguration>
注意:
classPathEntry location=“E:\IdeaProjects\mysql-connector-java-5.1.47.jar”,建議用5.X系列的,否則可能生成的接口會(huì)缺少
6. 在idea中添加一個(gè)mybatis generator maven插件啟動(dòng)選項(xiàng),點(diǎn)擊Run,選擇Edit Configuration… 點(diǎn)擊加號(hào)"+"添加,選擇maven,填寫(xiě)名稱(chēng)(這里用mybatis generator),命令行:mybatis-generator:generate -e
7. 選擇 Mybatis Generator 啟動(dòng),自動(dòng)在dao、entity、mapper包下生成代碼
注意:
利用Mybatis Generator自動(dòng)生成代碼,對(duì)于已經(jīng)存在的文件會(huì)存在覆蓋和在原有文件上追加的可能性,不宜多次生成。如需重新生成,需要?jiǎng)h除已生成的源文件。
到此這篇關(guān)于SpringBoot整合Mybatis Generator自動(dòng)生成代碼的文章就介紹到這了,更多相關(guān)SpringBoot Mybatis Generator自動(dòng)生成代碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot如何使用MyBatisPlus逆向工程自動(dòng)生成代碼
- SpringBoot集成Mybatis-plus并實(shí)現(xiàn)自動(dòng)生成相關(guān)文件的示例代碼
- Springboot Mybatis Plus自動(dòng)生成工具類(lèi)詳解代碼
- SpringBoot項(xiàng)目使用mybatis-plus逆向自動(dòng)生成全套代碼
- SpringBoot根據(jù)目錄結(jié)構(gòu)自動(dòng)生成路由前綴的實(shí)現(xiàn)代碼
- springboot整合freemarker代碼自動(dòng)生成器
- SpringBoot整合screw實(shí)現(xiàn)數(shù)據(jù)庫(kù)文檔自動(dòng)生成的示例代碼
- springboot 通過(guò)代碼自動(dòng)生成pid的方法
- SpringBoot+MyBatis-Plus+Velocity實(shí)現(xiàn)代碼自動(dòng)生成
相關(guān)文章
java?MongoDB實(shí)現(xiàn)列表分頁(yè)查詢的示例代碼
本文主要介紹了java?MongoDB實(shí)現(xiàn)列表分頁(yè)查詢的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07代理模式之Java動(dòng)態(tài)代理實(shí)現(xiàn)方法
今天一個(gè)偶然的機(jī)會(huì)我突然想看看JDK的動(dòng)態(tài)代理,因?yàn)橐郧耙仓酪稽c(diǎn),而且只是簡(jiǎn)單的想測(cè)試一下使用,使用很快里就寫(xiě)好了這么幾個(gè)接口和類(lèi),需要的朋友可以參考下2012-11-11SpringMVC源碼解讀之 HandlerMapping - AbstractDetectingUrlHandlerM
這篇文章主要介紹了SpringMVC源碼解讀之 HandlerMapping - AbstractDetectingUrlHandlerMapping系列初始化的相關(guān)資料,需要的朋友可以參考下2016-02-02SpringBoot可以同時(shí)處理多少請(qǐng)求流程分析
SpringBoot默認(rèn)的內(nèi)嵌容器是Tomcat,也就是我們的程序?qū)嶋H上是運(yùn)行在Tomcat里的,所以與其說(shuō)SpringBoot可以處理多少請(qǐng)求,到不如說(shuō)Tomcat可以處理多少請(qǐng)求,這篇文章主要介紹了SpringBoot可以同時(shí)處理多少請(qǐng)求,需要的朋友可以參考下2023-02-02使用Spring的StopWatch實(shí)現(xiàn)代碼性能監(jiān)控的方法詳解
在開(kāi)發(fā)過(guò)程中,偶爾還是需要分析代碼的執(zhí)行時(shí)間,Spring 框架提供了一個(gè)方便的工具類(lèi) StopWatch,本文將介紹 StopWatch 的基本用法,并通過(guò)示例演示如何在項(xiàng)目中使用 StopWatch 進(jìn)行代碼性能監(jiān)控2023-12-12淺談SpringMVC+Spring3+Hibernate4開(kāi)發(fā)環(huán)境搭建
MVC已經(jīng)是現(xiàn)代Web開(kāi)發(fā)中的一個(gè)很重要的部分,本文介紹一下SpringMVC+Spring3+Hibernate4的開(kāi)發(fā)環(huán)境搭建,有興趣的可以了解一下。2017-01-01Mybatis結(jié)果集自動(dòng)映射的實(shí)例代碼
在使用Mybatis時(shí),有的時(shí)候我們可以不用定義resultMap,而是直接在<select>語(yǔ)句上指定resultType。這個(gè)時(shí)候其實(shí)就用到了Mybatis的結(jié)果集自動(dòng)映射,下面通過(guò)本文給大家分享Mybatis結(jié)果集自動(dòng)映射的實(shí)例代碼,一起看看吧2017-02-02