小編給大家分享一下Spring boot整合Mybatis-plus過(guò)程的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
成都創(chuàng)新互聯(lián)公司,為您提供成都網(wǎng)站建設(shè)公司、成都網(wǎng)站制作、網(wǎng)站營(yíng)銷(xiāo)推廣、網(wǎng)站開(kāi)發(fā)設(shè)計(jì),對(duì)服務(wù)成都石涼亭等多個(gè)行業(yè)擁有豐富的網(wǎng)站建設(shè)及推廣經(jīng)驗(yàn)。成都創(chuàng)新互聯(lián)公司網(wǎng)站建設(shè)公司成立于2013年,提供專(zhuān)業(yè)網(wǎng)站制作報(bào)價(jià)服務(wù),我們深知市場(chǎng)的競(jìng)爭(zhēng)激烈,認(rèn)真對(duì)待每位客戶(hù),為客戶(hù)提供賞心悅目的作品。 與客戶(hù)共同發(fā)展進(jìn)步,是我們永遠(yuǎn)的責(zé)任!
Mybatis初期使用比較麻煩,需要很多配置文件、實(shí)體類(lèi)、dao層映射、還有很多其他的配置。初期開(kāi)發(fā)使用generator可以根據(jù)表結(jié)構(gòu)自動(dòng)生產(chǎn)實(shí)體類(lèi)、dao層代碼,這樣是可以減輕一部分開(kāi)發(fā)量;后期mybatis進(jìn)行大量的優(yōu)化,現(xiàn)在可以使用注解版本,自動(dòng)管理dao層和配置文件。
maven 依賴(lài) 注意:本文使用的是MySQL,數(shù)據(jù)庫(kù)依賴(lài)就不展示了
<!-- 引入mvbatie -plus starter--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>2.3</version> </dependency> <!-- 模板引擎 mybatis使用code生成代碼需要 --> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-engine-core</artifactId> <version>2.0</version> </dependency>
代碼模版引擎需要velocity或freemarker(mybatis-plus默認(rèn)使用velocity,兩者任選其一),這里使用velocity
代碼生成器
public static void main(String[] args) { CodeGeneration codeGeneration = new CodeGeneration(); codeGeneration.execute(); } /** * * @ClassName: CodeGeneration * @Description: 代碼生成器 */ public void execute() { AutoGenerator mpg = new AutoGenerator(); // 全局配置 GlobalConfig gc = new GlobalConfig(); //生成的代碼路徑(系統(tǒng)路徑) gc.setOutputDir("/Users/wangxiaowei/wxw/eclipseWorkSpace/study/src/main/java"); gc.setFileOverride(true); gc.setActiveRecord(false);// 不需要ActiveRecord特性的請(qǐng)改為false gc.setEnableCache(false);// XML 二級(jí)緩存 gc.setBaseResultMap(true);// XML ResultMap gc.setBaseColumnList(false);// XML columList gc.setAuthor("wxw");// 作者 // 自定義文件命名,注意 %s 會(huì)自動(dòng)填充表實(shí)體屬性! gc.setControllerName("%sController"); gc.setServiceName("%sService"); gc.setServiceImplName("%sServiceImpl"); gc.setMapperName("%sDao"); gc.setXmlName("%sMapper"); mpg.setGlobalConfig(gc); // 數(shù)據(jù)源配置 DataSourceConfig dsc = new DataSourceConfig(); dsc.setDbType(DbType.MYSQL); dsc.setDriverName("com.mysql.jdbc.Driver"); dsc.setUsername("xxx");//數(shù)據(jù)庫(kù)用戶(hù)名 dsc.setPassword("xxx");//密碼 //數(shù)據(jù)庫(kù)路徑 dsc.setUrl( "jdbc:mysql://localhost:30870/horus_dev?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true"); mpg.setDataSource(dsc); // 策略配置 StrategyConfig strategy = new StrategyConfig(); strategy.setTablePrefix(new String[] { "" });// 此處可以修改為您的表前綴 strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略 // 需要生成的表的名稱(chēng),這里app_user_info即為表名 strategy.setInclude(new String[] { "app_user_info", }); strategy.setSuperServiceClass(null); strategy.setSuperServiceImplClass(null); strategy.setSuperMapperClass(null); mpg.setStrategy(strategy); // 包配置 PackageConfig pc = new PackageConfig(); pc.setParent("com.wang.study");//父包,下面的子包均在這父包之下 pc.setController("controller");//上面生成的controller類(lèi) 放到controller子包 pc.setService("service");//上面生成的service 放到service子包,下面類(lèi)似 pc.setMapper("dao"); pc.setEntity("pojo"); pc.setXml("mapper"); mpg.setPackageInfo(pc); // 執(zhí)行生成 mpg.execute(); }
mybatis 基礎(chǔ)配置(這里使用的properties)
#數(shù)據(jù)源
spring.datasource.url=jdbc:mysql://localhost:30870/horus_dev?useUnicode=true&characterEncoding=utf-8
spring.datasource.username =xxx
spring.datasource.password =xxx
spring.datasource.type =com.alibaba.druid.pool.DruidDataSource
#mapper文件
mybatis-plus.mapper-locations=classpath:com/wang/study/mapper/*.xml
#數(shù)據(jù)庫(kù)表對(duì)應(yīng)的實(shí)體類(lèi)所在包
mybatis-plus.type-aliases-package=com/wang/study/pojo
#日志 打印sql
logging.level.com.wang.study.dao=debug
mybatis-plus 分頁(yè),在配置類(lèi)里添加以下配置
/** * mybatis-plus分頁(yè)插件<br> * 文檔:http://mp.baomidou.com<br> */ @Bean public PaginationInterceptor paginationInterceptor() { PaginationInterceptor paginationInterceptor = new PaginationInterceptor(); paginationInterceptor.setDialectType("mysql"); return paginationInterceptor; }
springboot一種全新的編程規(guī)范,其設(shè)計(jì)目的是用來(lái)簡(jiǎn)化新Spring應(yīng)用的初始搭建以及開(kāi)發(fā)過(guò)程,SpringBoot也是一個(gè)服務(wù)于框架的框架,服務(wù)范圍是簡(jiǎn)化配置文件。
以上是“Spring boot整合Mybatis-plus過(guò)程的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
標(biāo)題名稱(chēng):Springboot整合Mybatis-plus過(guò)程的示例分析
網(wǎng)址分享:http://www.muchs.cn/article46/jiojeg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)、云服務(wù)器、品牌網(wǎng)站設(shè)計(jì)、虛擬主機(jī)、網(wǎng)站制作、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)