欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Mybatis與微服務注冊的詳細過程

 更新時間:2023年01月04日 09:42:13   作者:天蝎座的程序媛  
這篇文章主要介紹了Mybatis與微服務注冊,主要包括SpringBoot整合MybatisPlus,SpringBoot整合Freeamarker以及SpringBoot整合微服務&gateway&nginx的案例代碼,需要的朋友可以參考下

一、SpringBoot整合MybatisPlus

創(chuàng)建自動生成代碼子模塊

1.基于maven方式創(chuàng)建子模塊zmall-generator,用于結合mybatis-plus生成代碼。

 1.在公共模塊zmall-common中注釋掉mybatis的依賴引入,改換成mybatis-plus依賴引入

<!-- mybatis plus依賴 -->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.0</version>
</dependency>

2.在zmall-generator中引入mybatis-plus-generator依賴。該模塊專用于mybatis-plus的代碼生成,所以單獨在此引入該依賴即可。 

<!-- mybatis-plus-generator依賴 -->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-generator</artifactId>
    <version>3.4.0</version>
</dependency>

3. 在src/main/resources下創(chuàng)建templates目錄,并導入mybatis-generator生成代碼模板頁

4.在src/main/java下創(chuàng)建包com.zking.zmall,并導入generator下的CodeGenerator類用于代碼生成

5.修改CodeGenerator類基本生成參數(shù),并生成代碼

//數(shù)據(jù)庫連接參數(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="****";
//父級別包名稱
public static String parentPackage = "com.zking.zmall";
//項目名設置(如果是SpringCloud項目則需要設置,其他為""即可)
public static String projectName="/zmall-generator";
//代碼生成的目標路徑
public static String generateTo = "/src/main/java";
//mapper.xml的生成路徑
public static String mapperXmlPath = "/src/main/resources/mapper";
//控制器的公共基類,用于抽象控制器的公共方法,null值表示沒有父類
public static String baseControllerClassName ;
//業(yè)務層的公共基類,用于抽象公共方法
public static String baseServiceClassName ;
//作者名
public static String author = "zking";
//模塊名稱,用于組成包名
public static String modelName = "model";

注意:

  • 修改數(shù)據(jù)庫連接URL中的數(shù)據(jù)庫名、數(shù)據(jù)庫賬號和密碼;
  • 修改父級別包名稱
  • 修改項目名,如果是SpringCloud項目則修改,不是則默認“”

創(chuàng)建商品服務子模塊

1.基于Spring Initializr方式創(chuàng)建商品服務模塊zmall-product

2.在主模塊pom.xml中加入商品服務子模塊zmall-product 

<modules>
    <module>zmall-common</module>
    <module>zmall-user</module>
    <module>zmall-generator</module>
    <module>zmall-product</module>
</modules>

3.配置商品服務子模塊zmall-product的application.yml配置文件 

server:
  port: 8020
spring:
  application:
    name: zmall-product
  datasource:
    #type連接池類型 DBCP,C3P0,Hikari,Druid,默認為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:
  #所對應的 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.在商品服務子模塊中啟動類上添加

@SpringBootApplication
@MapperScan({"com.zking.zmall.mapper"})
public class ZmallProductApplication {
    public static void main(String[] args) {
        SpringApplication.run(ZmallProductApplication.class, args);
    }
}

5.將公共子模塊中生成的service層代碼復制到商品服務子模塊zmall-product中,并刪除掉非商品相關的service接口及實現(xiàn)類

 6.創(chuàng)建junit實現(xiàn)接口測試

@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依賴

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

2.在商品子模塊zmall-product中添加首頁和商品詳情頁面及公共資源(js/css/images)

將資料目錄中的《易買網網頁素材.rar》解壓后,將其中Index.html、Product.html和js/css/images等等添加到項目的templates和static目錄下,最好請將Index.html、Product.html頁面首字母改成小寫

導入資料目錄中的common目錄到項目的templates目錄下

將頁面中的頭部申明<!DOCTYPE html ....>修改成<!DOCTYPE html>(支持H5風格)

在頁面中通過<#include>指令引入common目錄中的head.html

3.創(chuàng)建ProductController定義請求方法

@Controller
public class ProductController {
 
    @Autowired
    private IProductService productService;
 
    @RequestMapping("/index.html")
    public String index(Model model){
        //按照商品的銷量降序排序獲取銷量排名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查詢商品詳情信息
        Product product = productService.getById(id);
        model.addAttribute("product",product);
        return "product";
    }
}

4.在index.html中綁定熱門數(shù)據(jù)和product.html中綁定商品詳情數(shù)據(jù)

三、SpringBoot整合微服務&gateway&nginx

請求鏈路要求:客戶端發(fā)送請求先經過nginx,再用nginx轉至內部訪問網關gateway,最后由網關服務的路由規(guī)則轉發(fā)到微服務的內部服務。

整合微服務之商品服務zmall-product

在公共模塊zmall-common中導入微服務相關依賴

<!--nacos客戶端-->
<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>

配置商品服務模塊zmall-product的application.yml文件

spring:
  application:
    name: zmall-product
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848

修改啟動類,向nacos進行注冊

@EnableDiscoveryClient
@SpringBootApplication
@MapperScan({"com.zking.zmall.mapper"})
public class ZmallProductApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(ZmallProductApplication.class, args);
    }
}

創(chuàng)建并配置網關gateway服務

1.基于Spring initializr方式創(chuàng)建網關模塊gateway

2.配置pom.xml添加nacos和gateway的依賴

<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客戶端-->
    <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.修改啟動類,向nacos進行注冊

@EnableDiscoveryClient
@SpringBootApplication
public class ZmallGatewayApplication {
    public static void main(String[] args) {
        SpringApplication.run(ZmallGatewayApplication.class, args);
    }
}

4.配置application.yml設置gateway路由轉發(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中按照名稱獲取微服務,并遵循負載均衡策略
          predicates:
            - Path=/product-serv/**
          filters:
            - StripPrefix=1

5.將易買網網頁素材中的公共靜態(tài)資源js/css/images復制到gateway網關服務中

這里請注意了,之前在商品服務模塊zmall-product中已經配置了易買網的靜態(tài)資源,為什么還要在gateway網關服務中再配置一次呢?這是因為當請求經過gateway網關服務后會進行斷言條件匹配和條件路徑截取等操作,從而導致gateway網關路由轉發(fā)后靜態(tài)資源失效404的問題,所以特此在gateway網關服務中也配置一次易買網網頁素材中的公共靜態(tài)資源js/css/images,確保能正常訪問。

解決方案:(此處將在第三次課解決,使用nginx動靜分離方式實現(xiàn)) 配置靜態(tài)資源訪問服務器,將各個微服務模塊中的靜態(tài)訪問資源遷移到靜態(tài)資源訪問服務器中,然后通過http方式訪問即可。

安裝配置SwitchHosts

1.直接雙擊exe文件即可安裝SwitchHosts

2.進入C:\Windows\System32\drivers\etc目錄,設置hosts文件訪問權限并取消只讀模式

3.打開SwitchHosts設置一級域名

安裝配置Windows版nginx

1.解壓nginx-1.18.0.zip至任意目錄

2.進入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.最后運行nginx根目錄下的nginx.exe啟動nginx

請求鏈路測試

單獨訪問商品服務:http://localhost:8020/index.html

通過gateway訪問:http://localhost:8000/product-serv/index.html

通過nginx訪問:http://zmall.com/product-serv/index.html

到此這篇關于Mybatis與微服務注冊的文章就介紹到這了,更多相關Mybatis微服務注冊內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Java縮小文件內存占用的方法技巧分享

    Java縮小文件內存占用的方法技巧分享

    在Java應用程序中,處理大文件時經常會遇到內存占用過高的問題,為了縮小文件的內存占用,我們可以采取一些有效的方法來優(yōu)化和管理內存的使用,本文將介紹一些在Java中縮小文件內存占用的技巧,需要的朋友可以參考下
    2024-10-10
  • SpringMVC使用RESTful接口案例詳解

    SpringMVC使用RESTful接口案例詳解

    RESTful是一種web軟件風格,它不是標準也不是協(xié)議,它不一定要采用,只是一種風格,它倡導的是一個資源定位(url)及資源操作的風格,這篇文章主要介紹了SpringBoot使用RESTful接口
    2022-11-11
  • java編程abstract類和方法詳解

    java編程abstract類和方法詳解

    這篇文章主要介紹了java編程abstract類和方法詳解,具有一定借鑒價值,需要的朋友可以參考下。
    2017-12-12
  • Java 關于eclipse導入項目發(fā)生的問題及解決方法(推薦)

    Java 關于eclipse導入項目發(fā)生的問題及解決方法(推薦)

    下面小編就為大家分享一篇Java 關于eclipse導入項目發(fā)生的問題及解決方法(推薦),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-12-12
  • 線上Java程序占用CPU過高解決方案

    線上Java程序占用CPU過高解決方案

    這篇文章主要介紹了線上Java程序占用CPU過高解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-11-11
  • java開發(fā)之內部類的用法

    java開發(fā)之內部類的用法

    本篇文章介紹了,java開發(fā)之內部類的用法。需要的朋友參考下
    2013-05-05
  • Java多線程優(yōu)化方法及使用方式

    Java多線程優(yōu)化方法及使用方式

    這篇文章主要介紹了Java多線程優(yōu)化方法及使用方式,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2018-02-02
  • Spring框架中IoC容器與DI依賴注入教程

    Spring框架中IoC容器與DI依賴注入教程

    IOC也是Spring的核心之一了,之前學的時候是采用xml配置文件的方式去實現(xiàn)的,后來其中也多少穿插了幾個注解,但是沒有說完全采用注解實現(xiàn)。那么這篇文章就和大家分享一下,全部采用注解來實現(xiàn)IOC + DI
    2023-01-01
  • 你可知HashMap為什么是線程不安全的

    你可知HashMap為什么是線程不安全的

    這篇文章主要介紹了你可知HashMap為什么是線程不安全的,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • 關于springboot中對sqlSessionFactoryBean的自定義

    關于springboot中對sqlSessionFactoryBean的自定義

    這篇文章主要介紹了springboot中對sqlSessionFactoryBean的自定義方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12

最新評論