Spring boot + mybatis plus 快速構建項目,生成基本業務操作代碼。

---進行業務建表,這邊根據個人業務分析,不具體操作

?

--加入mybatis plus? pom依賴
<!-- mybatis-plus 3.0.5-->
<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.0.5</version>
</dependency>  --模板引擎
<!-- freemarker模板引擎用于自定義模板,生成代碼-->
<dependency><groupId>org.freemarker</groupId><artifactId>freemarker</artifactId><version>2.3.28</version>
</dependency><!-- apache模板引擎 -->
<dependency><groupId>org.apache.velocity</groupId><artifactId>velocity-engine-core</artifactId><version>2.0</version>
</dependency>

?

--在項目配置yml寫入數據源配置以及mapper對應實體entity的映射
# DataSource Config
spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driver
#devurl: jdbc:mysql://your database ip and database name?autoReconnect=true&autoReconnectForPools=true&useUnicode=true&characterEncoding=utf8&useSSL=false&failOverReadOnly=false&tinyInt1isBit=false&serverTimezone=Asia/Shanghaiusername:  your database userbamepassword: your database password#打印mybatis plus sql 日志輸出
mybatis-plus:configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImpldefault-statement-timeout: 120mapper-locations: classpath*:/mapper/**Mapper.xmltype-aliases-package: your project entity package location
--代碼生成工具類
/*** @Author: xxxx* @Description: ${description}* @Date: 2020/9/15 11:19*/import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class CodeGenerator {/*** <p>* 讀取控制臺內容* </p>*/public static String scanner(String tip) {Scanner scanner = new Scanner(System.in);StringBuilder help = new StringBuilder();help.append("請輸入" + tip + ":");System.out.println(help.toString());if (scanner.hasNext()) {String ipt = scanner.next();if (StringUtils.isNotBlank(ipt)) {return ipt;}}throw new MybatisPlusException("請輸入正確的" + tip + "!");}public static void main(String[] args) {// 代碼生成器AutoGenerator mpg = new AutoGenerator();// 全局配置GlobalConfig gc = new GlobalConfig();String projectPath = System.getProperty("user.dir");gc.setOutputDir(projectPath + "/src/main/java");gc.setAuthor("xxxx");//是否打開輸出目錄gc.setOpen(false);//service命名方式gc.setServiceName("%sService");//service impl命名方式gc.setServiceImplName("%sServiceImpl");//自定義文件命名,注意 %s 會自動填充表實體屬性!gc.setMapperName("%sMapper");gc.setXmlName("%sMapper");gc.setFileOverride(true);gc.setActiveRecord(true);// XML 二級緩存gc.setEnableCache(false);// XML ResultMapgc.setBaseResultMap(true);// XML columnListgc.setBaseColumnList(false);//gc.setSwagger2(true); //實體屬性 Swagger2 注解mpg.setGlobalConfig(gc);// 數據源配置DataSourceConfig dsc = new DataSourceConfig();dsc.setUrl("jdbc:mysql://your database ip /database  name?autoReconnect=true&autoReconnectForPools=true&useUnicode=true&characterEncoding=utf8&useSSL=false&failOverReadOnly=false&tinyInt1isBit=false&serverTimezone=Asia/Shanghai");dsc.setDriverName("com.mysql.cj.jdbc.Driver");dsc.setUsername("/your database username");dsc.setPassword("/your database password");mpg.setDataSource(dsc);// 包配置PackageConfig pc = new PackageConfig();pc.setParent("com.stdl.chargingpile");mpg.setPackageInfo(pc);// 自定義配置InjectionConfig cfg = new InjectionConfig() {@Overridepublic void initMap() {// to do nothing}};// 如果模板引擎是 freemarkerString templatePath = "/templates/mapper.xml.ftl";// 如果模板引擎是 velocity// String templatePath = "/templates/mapper.xml.vm";// 自定義輸出配置List<FileOutConfig> focList = new ArrayList<>();// 自定義配置會被優先輸出focList.add(new FileOutConfig(templatePath) {@Overridepublic String outputFile(TableInfo tableInfo) {// 自定義輸出文件名 , 如果你 Entity 設置了前后綴、此處注意 xml 的名稱會跟著發生變化!!return projectPath + "/src/main/resources/mapper/"+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;}});cfg.setFileOutConfigList(focList);mpg.setCfg(cfg);// 配置模板TemplateConfig templateConfig = new TemplateConfig();// 配置自定義輸出模板templateConfig.setXml(null);mpg.setTemplate(templateConfig);// 策略配置StrategyConfig strategy = new StrategyConfig();strategy.setNaming(NamingStrategy.underline_to_camel);strategy.setColumnNaming(NamingStrategy.underline_to_camel);strategy.setEntityLombokModel(true);strategy.setRestControllerStyle(true);// 公共父類//strategy.setSuperEntityColumns("id");strategy.setInclude(scanner("表名,多個英文逗號分割").split(","));strategy.setControllerMappingHyphenStyle(true);strategy.setTablePrefix(pc.getModuleName() + "_");mpg.setStrategy(strategy);mpg.setTemplateEngine(new FreemarkerTemplateEngine());mpg.execute();}}

?

?

--啟動類上加上mapper掃描的位置,否則回拋出找不到mapper的異常
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.retry.annotation.EnableRetry;import java.util.Arrays;/*** @author xxx*/
@SpringBootApplication
@MapperScan("your mapper interface package location")
@EnableRetry
public class ChargingPileApplication {public static void main(String[] args) {SpringApplication.run(ChargingPileApplication.class, args);//查看當前運行的線程名稱}@Beanpublic CommandLineRunner commandLineRunner(ApplicationContext ctx) {return args -> {String[] beanNames = ctx.getBeanDefinitionNames();Arrays.sort(beanNames);for (String beanName : args) {System.out.println(beanName);}};}
}

?

?

?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/450274.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/450274.shtml
英文地址,請注明出處:http://en.pswp.cn/news/450274.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

給手機瀏覽器減負 輕裝上陣才能速度制勝

隨著手機瀏覽器的發展&#xff0c;瀏覽器已經變得臃腫不堪&#xff0c;各種“功能”系于一身&#xff0c;有廣告、社區、樂園等等&#xff0c;我們真的需要它們嗎&#xff1f;如何才能讓瀏覽器做到輕裝上陣&#xff0c;又能高效滿足我們需求呢&#xff1f; 過多“功能”的瀏覽器…

653. Two Sum IV - Input is a BST

題目來源&#xff1a; 自我感覺難度/真實難度&#xff1a; 題意&#xff1a; 分析&#xff1a; 自己的代碼&#xff1a; class Solution(object):def findTarget(self, root, k):""":type root: TreeNode:type k: int:rtype: bool"""Allself.InO…

解決 dubbo問題:Forbid consumer 192.xx.xx.1 access service com.xx.xx.xx.rpc.api.xx from registry 116.xx1

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 我的情況是&#xff1a; 原本我把服務放在A工程中&#xff0c;后來改到B工程中了&#xff0c;所以原來的服務不存在了&#xff0c;查不…

vue學習:7、路由跳轉

2019獨角獸企業重金招聘Python工程師標準>>> <body><div id"app"></div></body><script type"text/javascript">var Login {template: <div>我是登陸界面</div>};var Register {template: <div…

Spring Retry 重試機制實現及原理

概要 Spring實現了一套重試機制&#xff0c;功能簡單實用。Spring Retry是從Spring Batch獨立出來的一個功能&#xff0c;已經廣泛應用于Spring Batch,Spring Integration, Spring for Apache Hadoop等Spring項目。本文將講述如何使用Spring Retry及其實現原理。 背景 重試&…

inline 內聯函數詳解 內聯函數與宏定義的區別

一、在C&C中   一、inline 關鍵字用來定義一個類的內聯函數&#xff0c;引入它的主要原因是用它替代C中表達式形式的宏定義。表達式形式的宏定義一例&#xff1a;#define ExpressionName(Var1,Var2) ((Var1)(Var2))*((Var1)-(Var2))為什么要取代這種形式呢&#xff0c;且…

Oracle序列更新為主鍵最大值

我們在使用 Oracle 數據庫的時候&#xff0c;有時候會選擇使用自增序列作為主鍵。但是在開發過程中往往會遇到一些不規范的操作&#xff0c;導致表的主鍵值不是使用序列插入的。這樣在數據移植的時候就會出現各種各樣的問題。當然數據庫主鍵不使用序列是一種很好的方式&#xf…

dubbo forbid service的解決辦法

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 017-05-31 10:36:54.523 [http-nio-8080-exec-5] ERROR c.h.pdl.web.APIExceptionHandler - Unknown Exception, URI /payday-loan-co…

用SSH登錄遠程的機器,在遠程機器上執行本地機器上的腳本

假設本地的機器IP為10.245.111.90&#xff0c;我們想要在10.245.111.93上執行一個保存在10.245.111.90上的腳本。經過測試通過的命令如下&#xff1a;ssh root10.245.111.93 bash -s < /root/testlocal.sh如果要帶參數的話&#xff0c;那就需要參考這篇文章中描述的代碼了。…

golang學習之旅(1)

這段時間我開始了golang語言學習&#xff0c;其實也是為了個人的職業發展的拓展和衍生&#xff0c;語言只是工具&#xff0c;但是每個語言由于各自的特點和優勢&#xff0c;golang對于當前編程語言的環境&#xff0c;是相對比較新的語言&#xff0c;對于區塊鏈&#xff0c;大數…

為什么要在Linux平臺上學C語言?用Windows學C語言不好嗎?

用Windows還真的是學不好C語言。C語言是一種面向底層的編程語言&#xff0c;要寫好C程序&#xff0c;必須對操作系統的工作原理非常清楚&#xff0c;因為操作系統也是用C寫的&#xff0c;我們用C寫應用程序直接使用操作系統提供的接口&#xff0c;Linux是一種開源的操作系統&am…

數據庫中Schema(模式)概念的理解

在學習SQL的過程中&#xff0c;會遇到一個讓你迷糊的Schema的概念。實際上&#xff0c;schema就是數據庫對象的集合&#xff0c;這個集合包含了各種對象如&#xff1a;表、視圖、存儲過程、索引等。為了區分不同的集合&#xff0c;就需要給不同的集合起不同的名字&#xff0c;默…

linux系統中打rz命令后出現waiting to receive.**B0100000023be50

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 linux系統中打rz命令后出現 waiting to receive.**B0100000023be50 而沒有出現選擇文件彈出框是什么問題&#xff1a; 我本來用的是 gi…

golang學習之旅(2)- go的數據基本數據類型及變量定義方式

叮鈴鈴&#xff0c;這不有人在評論問下一篇何時更新&#xff0c;這不就來了嘛&#xff0c;&#x1f604; 今天我們說說golang 的基本數據類型 基本類型如下&#xff1a; //基本類型 布爾類型&#xff1a;bool 即true 、flase 類似于java中的boolean 字符類型&#xff1a;s…

StackExchange.Redis 官方文檔(六) PipelinesMultiplexers

流水線和復用 糟糕的時間浪費。現代的計算機以驚人的速度產生大量的數據&#xff0c;而且高速網絡通道(通常在重要的服務器之間同時存在多個鏈路)提供了很高的帶寬&#xff0c;但是計算機花費了大量的時間在 等待數據 上面&#xff0c;這也是造成使用持久性鏈接的編程方式越來越…

開發優秀產品的六大秘訣

摘要&#xff1a;本文是Totango的聯合創始人兼公司CEO Guy Nirpaz發表在Mashable.com上的文章。無論是在哪個行業&#xff0c;用戶永遠是一款產品的中心&#xff0c;本文作者就以用戶為中心&#xff0c;為大家講述了六個如何為企業產品添加功能的秘訣。 隨著云計算的發展&#…

Spring Boot下無法加載主類 org.apache.maven.wrapper.MavenWrapperMain問題解決

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 引言&#xff1a; 在SpringBoot中需要使用mvnw來做相關操作&#xff0c;但是卻有時候會報出達不到MavenWrapperMain的錯誤信息&#xff…

【前端面試】字節跳動2019校招面經 - 前端開發崗(二)

【前端面試】字節跳動2019校招面經 - 前端開發崗&#xff08;二&#xff09; 因為之前的一篇篇幅有限&#xff0c;太長了看著也不舒服&#xff0c;所以還是另起一篇吧?一、 jQuery和Vue的區別 jQuery 輕量級Javascript庫Vue 漸進式Javascript-MVVM框架jQuery和Vue的對比 jQuer…

SpringBoot與SpringCloud的版本說明及對應關系

轉載原文地址&#xff1a;https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E

leetcode 8: 字符串轉整數(atoi)

實現 atoi&#xff0c;將字符串轉為整數。 該函數首先根據需要丟棄任意多的空格字符&#xff0c;直到找到第一個非空格字符為止。如果第一個非空字符是正號或負號&#xff0c;選取該符號&#xff0c;并將其與后面盡可能多的連續的數字組合起來&#xff0c;這部分字符即為整數的…