SpringBoot整合mybatis-generator-maven-plugin的方法
前言:
SpringBoot版本 : 2.2.6
mybatis-generator-maven-plugin版本: 1.4.0
plugin 使用 mysql版本: 5.1.47
本次主要記錄日和使用該插件自動(dòng)生成pojo mapper xml文件(單表)
1 創(chuàng)建SpringBoot工程
這里不做具體記錄,創(chuàng)建工程往常教程很多,
下面貼出重要文件和注意事項(xiàng)
1.1 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>show.mrkay</groupId>
<artifactId>boot-mybatis-generator</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
</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>
<!--springboot-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.9</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<!--日志-->
<!--因?yàn)槭腔贏OP實(shí)現(xiàn)的所以這里必須要調(diào)入aop-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!--門(mén)面-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</dependency>
<!--log4j12-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
</dependencies>
<build>
<!--插件-->
<plugins>
<!--springboot的maven插件-->
<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.4.0</version>
<!--插件設(shè)置-->
<configuration>
<!--允許移動(dòng)生成的文件-->
<verbose>true</verbose>
<!--啟用覆蓋-->
<overwrite>true</overwrite>
<!--自動(dòng)生成配置 如果名字是generatorConfig.xml可以省略配置-->
<!--<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>-->
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
1.2 application.yml
server: port: 8080 spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/generaltor?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT username: root password: lk0313 druid: db-type: com.alibaba.druid.pool.DruidDataSource # 下面不配置就是采用默認(rèn)配置 # filters: stat # maxActive: 20 # initialSize: 1 # maxWait: 60000 # minIdle: 1 # timeBetweenEvictionRunsMillis: 60000 # minEvictableIdleTimeMillis: 300000 # validationQuery: select 'x' # testWhileIdle: true # testOnBorrow: false # testOnReturn: false # poolPreparedStatements: true # maxOpenPreparedStatements: 20 mybatis: # 該配置項(xiàng)配置了MyBatis配置文件保存路徑 mapper-locations: classpath*:show/makey/dao/*.xml # 配置掃描對(duì)應(yīng)實(shí)體所在包,將mapper的實(shí)現(xiàn)類(lèi)注入容器我們?cè)趩?dòng)器類(lèi)上加了@MapperScan注解這里就不需要了 logging: file: path: /logs # type-aliases-package: show.mrkay.dao
1.3 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>
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<!-- 是否生成注釋代時(shí)間戳-->
<property name="suppressDate" value="true"/>
<!-- 是否去除自動(dòng)生成的注釋 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--數(shù)據(jù)庫(kù)鏈接地址賬號(hào)密碼-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/generaltor?serverTimezone=Asia/Shanghai" userId="root" password="lk0313">
</jdbcConnection>
<javaTypeResolver>
<!--該屬性可以控制是否強(qiáng)制DECIMAL和NUMERIC類(lèi)型的字段轉(zhuǎn)換為Java類(lèi)型的java.math.BigDecimal,默認(rèn)值為false,一般不需要配置。-->
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!--生成Model類(lèi)存放位置-->
<javaModelGenerator targetPackage="show.mrkay.pojo" targetProject="src/main/java">
<!--enableSubPackages:如果true,MBG會(huì)根據(jù)catalog和schema來(lái)生成子包。如果false就會(huì)直接用targetPackage屬性。默認(rèn)為false。-->
<property name="enableSubPackages" value="true"/>
<!--trimStrings:是否對(duì)數(shù)據(jù)庫(kù)查詢(xún)結(jié)果進(jìn)行trim操作,如果設(shè)置為true就會(huì)生成類(lèi)似這樣public void setUsername(String username) {this.username = username == null ? null : username.trim();}的setter方法。默認(rèn)值為false。-->
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--生成映射xml文件存放位置-->
<sqlMapGenerator targetPackage="show.mrkay.dao" targetProject="src/main/resources/">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!--生成Dao類(lèi)存放位置(*Mapper.java)-->
<!-- 客戶(hù)端代碼,生成易于使用的針對(duì)Model對(duì)象和XML配置文件 的代碼
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper對(duì)象
type="MIXEDMAPPER",生成基于注解的Java Model 和相應(yīng)的Mapper對(duì)象
type="XMLMAPPER",生成SQLMap XML文件和獨(dú)立的Mapper接口
-->
<javaClientGenerator type="XMLMAPPER" targetPackage="show.mrkay.dao" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--生成對(duì)應(yīng)表及類(lèi)名-->
<table tableName="student" domainObjectName="Student" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
<!--useActualColumnNames:如果設(shè)置為true,那么MBG會(huì)使用從數(shù)據(jù)庫(kù)元數(shù)據(jù)獲取的列名作為生成的實(shí)體對(duì)象的屬性。 如果為false(默認(rèn)值),MGB將會(huì)嘗試將返回的名稱(chēng)轉(zhuǎn)換為駝峰形式。 在這兩種情況下,可以通過(guò) 元素顯示指定,在這種情況下將會(huì)忽略這個(gè)(useActualColumnNames)屬性。-->
<property name="useActualColumnNames" value="true"/>
<!-- 數(shù)據(jù)庫(kù)表主鍵 -->
<generatedKey column="id" sqlStatement="Mysql" identity="true" />
</table>
</context>
</generatorConfiguration>
步驟:
1 導(dǎo)入依賴(lài)
2 創(chuàng)建*generaltorConfig.xml配置文件
3 配置yml配置文件
注意:
pom中g(shù)eneralto-maven-plugs中必須指定mysql驅(qū)動(dòng),并且明確版本
generaltorConfig.xml中的
標(biāo)簽中需要指定tableName和生成的實(shí)體名字
2 創(chuàng)建數(shù)據(jù)庫(kù)表
-- 建表語(yǔ)句: /* SQLyog Enterprise v12.09 (64 bit) MySQL - 5.5.40 : Database - generaltor ********************************************************************* */ /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; CREATE DATABASE /*!32312 IF NOT EXISTS*/`generaltor` /*!40100 DEFAULT CHARACTER SET utf8 */; /*Table structure for table `student` */ DROP TABLE IF EXISTS `student`; CREATE TABLE `student` ( `id` int(32) NOT NULL AUTO_INCREMENT, `name` varchar(15) DEFAULT NULL, `sex` varchar(2) DEFAULT NULL, `phone` int(15) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; /*Data for the table `student` */ insert into `student`(`id`,`name`,`sex`,`phone`) values (1,'lk','男',12345578); /*!40101 SET SQL_MODE=@OLD_SQL_MODE */; /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
3 運(yùn)行
如果使用IDEA 我們只需要找到右邊側(cè)欄中的maven(沒(méi)有記得調(diào)出了)
找到plugins–>mybatis-generaltor–>mybatis-generaltor:generaltor 之后雙擊即可,此時(shí)刷新一下項(xiàng)目就自動(dòng)生成我們想要的,mapper和xml以及pojo
4 測(cè)試
創(chuàng)建穿點(diǎn)controller和service 來(lái)完成一次測(cè)試,
package show.mrkay.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.support.JstlUtils;
import show.mrkay.dao.StudentMapper;
import show.mrkay.pojo.Student;
import show.mrkay.service.UserService;
import java.util.List;
/**
* @ClassName: UserController
* @description: 用戶(hù)Controller
* @Author: MrKay
* @Date: 2020/11/8
*/
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/alllist")
public List<Student> selectAll(){
return userService.selectAll();
}
}
package show.mrkay.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import show.mrkay.dao.StudentMapper;
import show.mrkay.pojo.Student;
import java.util.List;
/**
* @ClassName: UserService
* @description: UserService
* @Author: MrKay
* @Date: 2020/11/8
*/
@Service
public class UserService {
@Autowired
private StudentMapper studentMapper;
public List<Student> selectAll(){
List<Student> students = studentMapper.selectAll();
return students;
}
}
package show.mrkay;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.annotation.MapperScans;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("show.mrkay.dao")
public class GeneratorApplication {
public static void main(String[] args) {
SpringApplication.run(GeneratorApplication.class, args);
}
}
注意:
啟動(dòng)器類(lèi)中需要加入@MapperScan(“mapper接口所在包”)注解,這樣我們就不需要手動(dòng)創(chuàng)建實(shí)現(xiàn)類(lèi)了,mybatis會(huì)自動(dòng)幫我們創(chuàng)建
啟動(dòng)之后:
地址欄輸入 localhost:8080/user/alllist
得到查詢(xún)結(jié)果:
[{"id":1,"name":"lk","sex":"男","phone":12345578}]
5 完整代碼
https://gitee.com/mrkay0313/boot-mybatis-generaltor.git
6 總結(jié)
便于自己以后使用,做的一個(gè)demo
到此這篇關(guān)于SpringBoot整合mybatis-generator-maven-plugin的文章就介紹到這了,更多相關(guān)SpringBoot整合mybatis-generator-maven-plugin內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
ConstraintValidator類(lèi)如何實(shí)現(xiàn)自定義注解校驗(yàn)前端傳參
這篇文章主要介紹了ConstraintValidator類(lèi)實(shí)現(xiàn)自定義注解校驗(yàn)前端傳參的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
關(guān)于SpringBoot在有Ajax時(shí)候不跳轉(zhuǎn)的問(wèn)題解決
最近在使用Ajax來(lái)發(fā)送一些數(shù)據(jù)給后臺(tái)一個(gè)Controller,但是遇到些問(wèn)題,所以下面這篇文章主要給大家介紹了關(guān)于SpringBoot在有Ajax時(shí)候不跳轉(zhuǎn)問(wèn)題的解決辦法,需要的朋友可以參考下2022-05-05
Java中Array List與Linked List的實(shí)現(xiàn)分析
這篇文章主要給大家介紹了關(guān)于Array List與Linked List實(shí)現(xiàn)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
Java中線(xiàn)程Thread的三種方式和對(duì)比
這篇文章主要介紹了Java中線(xiàn)程Thread的三種方式和對(duì)比,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
SpringBoot使用Druid數(shù)據(jù)源的配置方法
這篇文章主要介紹了SpringBoot使用Druid數(shù)據(jù)源的配置方法,文中代碼實(shí)例相結(jié)合的形式給大家介紹的非常詳細(xì),需要的朋友參考下吧2018-04-04
java實(shí)現(xiàn)合并單元格的同時(shí)并導(dǎo)出excel示例
這篇文章主要給大家介紹了關(guān)于java實(shí)現(xiàn)合并單元格的同時(shí)并導(dǎo)出excel的相關(guān)資料,文中先進(jìn)行了簡(jiǎn)單的介紹,之后給出了詳細(xì)的示例代碼,相信對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-03-03
Spring Cloud gateway 網(wǎng)關(guān)如何攔截Post請(qǐng)求日志
這篇文章主要介紹了Spring Cloud gateway 網(wǎng)關(guān)如何攔截Post請(qǐng)求日志的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
三行Java代碼實(shí)現(xiàn)計(jì)算多邊形的幾何中心點(diǎn)
因?yàn)楣ぷ餍枰?jì)算采煤機(jī)工作面的中心點(diǎn),如果套用數(shù)學(xué)的計(jì)算公式,用java去實(shí)現(xiàn),太麻煩了。本文將利用java幾何計(jì)算的工具包,幾行代碼就能求出多變形的中心,簡(jiǎn)直yyds!還不快跟隨小編一起學(xué)起來(lái)2022-10-10

