Mybatis與微服務(wù)注冊(cè)的詳細(xì)過(guò)程
一、SpringBoot整合MybatisPlus
創(chuàng)建自動(dòng)生成代碼子模塊
1.基于maven方式創(chuàng)建子模塊zmall-generator,用于結(jié)合mybatis-plus生成代碼。
1.在公共模塊zmall-common中注釋掉mybatis的依賴(lài)引入,改換成mybatis-plus依賴(lài)引入
<!-- mybatis plus依賴(lài) --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.0</version> </dependency>
2.在zmall-generator中引入mybatis-plus-generator依賴(lài)。該模塊專(zhuān)用于mybatis-plus的代碼生成,所以單獨(dú)在此引入該依賴(lài)即可。
<!-- mybatis-plus-generator依賴(lài) --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.4.0</version> </dependency>
3. 在src/main/resources下創(chuàng)建templates目錄,并導(dǎo)入mybatis-generator生成代碼模板頁(yè)
4.在src/main/java下創(chuàng)建包c(diǎn)om.zking.zmall,并導(dǎo)入generator下的CodeGenerator類(lèi)用于代碼生成
5.修改CodeGenerator類(lèi)基本生成參數(shù),并生成代碼
//數(shù)據(jù)庫(kù)連接參數(shù) public static String driver = "com.mysql.jdbc.Driver"; public static String url = "jdbc:mysql://localhost:3306/zmall?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true"; public static String username="****"; public static String password="****"; //父級(jí)別包名稱(chēng) public static String parentPackage = "com.zking.zmall"; //項(xiàng)目名設(shè)置(如果是SpringCloud項(xiàng)目則需要設(shè)置,其他為""即可) public static String projectName="/zmall-generator"; //代碼生成的目標(biāo)路徑 public static String generateTo = "/src/main/java"; //mapper.xml的生成路徑 public static String mapperXmlPath = "/src/main/resources/mapper"; //控制器的公共基類(lèi),用于抽象控制器的公共方法,null值表示沒(méi)有父類(lèi) public static String baseControllerClassName ; //業(yè)務(wù)層的公共基類(lèi),用于抽象公共方法 public static String baseServiceClassName ; //作者名 public static String author = "zking"; //模塊名稱(chēng),用于組成包名 public static String modelName = "model";
注意:
- 修改數(shù)據(jù)庫(kù)連接URL中的數(shù)據(jù)庫(kù)名、數(shù)據(jù)庫(kù)賬號(hào)和密碼;
- 修改父級(jí)別包名稱(chēng)
- 修改項(xiàng)目名,如果是SpringCloud項(xiàng)目則修改,不是則默認(rèn)“”
創(chuàng)建商品服務(wù)子模塊
1.基于Spring Initializr方式創(chuàng)建商品服務(wù)模塊zmall-product
2.在主模塊pom.xml中加入商品服務(wù)子模塊zmall-product
<modules> <module>zmall-common</module> <module>zmall-user</module> <module>zmall-generator</module> <module>zmall-product</module> </modules>
3.配置商品服務(wù)子模塊zmall-product的application.yml配置文件
server: port: 8020 spring: application: name: zmall-product datasource: #type連接池類(lèi)型 DBCP,C3P0,Hikari,Druid,默認(rèn)為Hikari type: com.zaxxer.hikari.HikariDataSource driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/zmall?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true username: **** password: **** freemarker: suffix: .html template-loader-path: classpath:/templates/ #mybatis-plus配置 mybatis-plus: #所對(duì)應(yīng)的 XML 文件位置 mapper-locations: classpath*:/mapper/*Mapper.xml #別名包掃描路徑 type-aliases-package: com.zking.zmall.model configuration: #駝峰命名規(guī)則 map-underscore-to-camel-case: true #日志配置 logging: level: com.zking.zmall.mapper: debug
4.在商品服務(wù)子模塊中啟動(dòng)類(lèi)上添加
@SpringBootApplication @MapperScan({"com.zking.zmall.mapper"}) public class ZmallProductApplication { public static void main(String[] args) { SpringApplication.run(ZmallProductApplication.class, args); } }
5.將公共子模塊中生成的service層代碼復(fù)制到商品服務(wù)子模塊zmall-product中,并刪除掉非商品相關(guān)的service接口及實(shí)現(xiàn)類(lèi)
6.創(chuàng)建junit實(shí)現(xiàn)接口測(cè)試
@RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest public class ProductServiceImplTest { ? @Autowired private IProductService productService; ? @Before public void setUp() throws Exception { } ? @After public void tearDown() throws Exception { } ? @Test public void queryProduct() { List<Product> list = productService.list(); list.forEach(System.out::println); } }
二、SpringBoot整合Freeamarker
1.在公共模塊zmall-common中引入freemarker依賴(lài)
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
2.在商品子模塊zmall-product中添加首頁(yè)和商品詳情頁(yè)面及公共資源(js/css/images)
將資料目錄中的《易買(mǎi)網(wǎng)網(wǎng)頁(yè)素材.rar》解壓后,將其中Index.html、Product.html和js/css/images等等添加到項(xiàng)目的templates和static目錄下,最好請(qǐng)將Index.html、Product.html頁(yè)面首字母改成小寫(xiě)
導(dǎo)入資料目錄中的common目錄到項(xiàng)目的templates目錄下
將頁(yè)面中的頭部申明<!DOCTYPE html ....>修改成<!DOCTYPE html>(支持H5風(fēng)格)
在頁(yè)面中通過(guò)<#include>指令引入common目錄中的head.html
3.創(chuàng)建ProductController定義請(qǐng)求方法
@Controller public class ProductController { @Autowired private IProductService productService; @RequestMapping("/index.html") public String index(Model model){ //按照商品的銷(xiāo)量降序排序獲取銷(xiāo)量排名Top5的商品 List<Product> products = productService.list(new QueryWrapper<Product>() .orderByDesc("hot") .last("limit 5")); model.addAttribute("top5",products); return "index"; } @RequestMapping("/product.html") public String detail(Model model,Integer id){ //根據(jù)商品ID查詢(xún)商品詳情信息 Product product = productService.getById(id); model.addAttribute("product",product); return "product"; } }
4.在index.html中綁定熱門(mén)數(shù)據(jù)和product.html中綁定商品詳情數(shù)據(jù)
三、SpringBoot整合微服務(wù)&gateway&nginx
請(qǐng)求鏈路要求:客戶(hù)端發(fā)送請(qǐng)求先經(jīng)過(guò)nginx,再用nginx轉(zhuǎn)至內(nèi)部訪(fǎng)問(wèn)網(wǎng)關(guān)gateway,最后由網(wǎng)關(guān)服務(wù)的路由規(guī)則轉(zhuǎn)發(fā)到微服務(wù)的內(nèi)部服務(wù)。
整合微服務(wù)之商品服務(wù)zmall-product
在公共模塊zmall-common中導(dǎo)入微服務(wù)相關(guān)依賴(lài)
<!--nacos客戶(hù)端--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!--fegin組件--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <!--nacos配置中心--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency>
配置商品服務(wù)模塊zmall-product的application.yml文件
spring: application: name: zmall-product cloud: nacos: discovery: server-addr: localhost:8848
修改啟動(dòng)類(lèi),向nacos進(jìn)行注冊(cè)
@EnableDiscoveryClient @SpringBootApplication @MapperScan({"com.zking.zmall.mapper"}) public class ZmallProductApplication { public static void main(String[] args) { SpringApplication.run(ZmallProductApplication.class, args); } }
創(chuàng)建并配置網(wǎng)關(guān)gateway服務(wù)
1.基于Spring initializr方式創(chuàng)建網(wǎng)關(guān)模塊gateway
2.配置pom.xml添加nacos和gateway的依賴(lài)
<modelVersion>4.0.0</modelVersion> <parent> <groupId>com.zking</groupId> <artifactId>zmall</artifactId> <version>1.0-SNAPSHOT</version> </parent> <artifactId>zmall-gateway</artifactId> <dependencies> <!--gateway 注意 此模式不能引入starter-web --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <!--nacos客戶(hù)端--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency> </dependencies>
3.修改啟動(dòng)類(lèi),向nacos進(jìn)行注冊(cè)
@EnableDiscoveryClient @SpringBootApplication public class ZmallGatewayApplication { public static void main(String[] args) { SpringApplication.run(ZmallGatewayApplication.class, args); } }
4.配置application.yml設(shè)置gateway路由轉(zhuǎn)發(fā)規(guī)則
server: port: 8000 spring: application: name: zmall-gateway cloud: nacos: discovery: server-addr: localhost:8848 gateway: routes: - id: product_route uri: lb://zmall-product # lb指的是從nacos中按照名稱(chēng)獲取微服務(wù),并遵循負(fù)載均衡策略 predicates: - Path=/product-serv/** filters: - StripPrefix=1
5.將易買(mǎi)網(wǎng)網(wǎng)頁(yè)素材中的公共靜態(tài)資源js/css/images復(fù)制到gateway網(wǎng)關(guān)服務(wù)中
這里請(qǐng)注意了,之前在商品服務(wù)模塊zmall-product中已經(jīng)配置了易買(mǎi)網(wǎng)的靜態(tài)資源,為什么還要在gateway網(wǎng)關(guān)服務(wù)中再配置一次呢?這是因?yàn)楫?dāng)請(qǐng)求經(jīng)過(guò)gateway網(wǎng)關(guān)服務(wù)后會(huì)進(jìn)行斷言條件匹配和條件路徑截取等操作,從而導(dǎo)致gateway網(wǎng)關(guān)路由轉(zhuǎn)發(fā)后靜態(tài)資源失效404的問(wèn)題,所以特此在gateway網(wǎng)關(guān)服務(wù)中也配置一次易買(mǎi)網(wǎng)網(wǎng)頁(yè)素材中的公共靜態(tài)資源js/css/images,確保能正常訪(fǎng)問(wèn)。
解決方案:(此處將在第三次課解決,使用nginx動(dòng)靜分離方式實(shí)現(xiàn)) 配置靜態(tài)資源訪(fǎng)問(wèn)服務(wù)器,將各個(gè)微服務(wù)模塊中的靜態(tài)訪(fǎng)問(wèn)資源遷移到靜態(tài)資源訪(fǎng)問(wèn)服務(wù)器中,然后通過(guò)http方式訪(fǎng)問(wèn)即可。
安裝配置SwitchHosts
1.直接雙擊exe文件即可安裝SwitchHosts
2.進(jìn)入C:\Windows\System32\drivers\etc目錄,設(shè)置hosts文件訪(fǎng)問(wèn)權(quán)限并取消只讀模式
3.打開(kāi)SwitchHosts設(shè)置一級(jí)域名
安裝配置Windows版nginx
1.解壓nginx-1.18.0.zip至任意目錄
2.進(jìn)入conf目錄,并修改nginx.conf配置文件
server { listen 80; server_name zmall.com; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; location / { proxy_pass http://127.0.0.1:8000/; } }
3.最后運(yùn)行nginx根目錄下的nginx.exe啟動(dòng)nginx
請(qǐng)求鏈路測(cè)試
單獨(dú)訪(fǎng)問(wèn)商品服務(wù):http://localhost:8020/index.html
通過(guò)gateway訪(fǎng)問(wèn):http://localhost:8000/product-serv/index.html
通過(guò)nginx訪(fǎng)問(wèn):http://zmall.com/product-serv/index.html
到此這篇關(guān)于Mybatis與微服務(wù)注冊(cè)的文章就介紹到這了,更多相關(guān)Mybatis微服務(wù)注冊(cè)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 基于jib-maven-plugin插件快速構(gòu)建微服務(wù)docker鏡像的方法
- 微服務(wù)鏈路追蹤Spring Cloud Sleuth整合Zipkin解析
- Java微服務(wù)Filter過(guò)濾器集成Sentinel實(shí)現(xiàn)網(wǎng)關(guān)限流過(guò)程詳解
- Java微服務(wù)分布式調(diào)度Elastic-job環(huán)境搭建及配置
- Java微服務(wù)Nacos Config配置中心超詳細(xì)講解
- SpringCloud微服務(wù)中跨域配置的方法詳解
- Java Feign微服務(wù)接口調(diào)用方法詳細(xì)講解
- go微服務(wù)PolarisMesh源碼解析服務(wù)端啟動(dòng)流程
- 微服務(wù)Spring Boot 整合 Redis 實(shí)現(xiàn)UV 數(shù)據(jù)統(tǒng)計(jì)的詳細(xì)過(guò)程
- go-micro微服務(wù)JWT跨域認(rèn)證問(wèn)題
- 詳解go-micro微服務(wù)consul配置及注冊(cè)中心
- go-micro微服務(wù)domain層開(kāi)發(fā)示例詳解
- 微服務(wù)?Spring?Boot?整合?Redis?BitMap?實(shí)現(xiàn)?簽到與統(tǒng)計(jì)功能
- 一文帶你了解微服務(wù)架構(gòu)中的"發(fā)件箱模式"
- go?micro微服務(wù)框架項(xiàng)目搭建方法
- go?micro微服務(wù)proto開(kāi)發(fā)安裝及使用規(guī)則
- spring?Cloud微服務(wù)阿里開(kāi)源TTL身份信息的線(xiàn)程間復(fù)用
- 簡(jiǎn)單介紹一下什么是microservice微服務(wù)
相關(guān)文章
Java 關(guān)于eclipse導(dǎo)入項(xiàng)目發(fā)生的問(wèn)題及解決方法(推薦)
下面小編就為大家分享一篇Java 關(guān)于eclipse導(dǎo)入項(xiàng)目發(fā)生的問(wèn)題及解決方法(推薦),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12線(xiàn)上Java程序占用CPU過(guò)高解決方案
這篇文章主要介紹了線(xiàn)上Java程序占用CPU過(guò)高解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11java開(kāi)發(fā)之內(nèi)部類(lèi)的用法
本篇文章介紹了,java開(kāi)發(fā)之內(nèi)部類(lèi)的用法。需要的朋友參考下2013-05-05關(guān)于springboot中對(duì)sqlSessionFactoryBean的自定義
這篇文章主要介紹了springboot中對(duì)sqlSessionFactoryBean的自定義方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12