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

Spring整合Mycat2的具體過程詳解

 更新時(shí)間:2023年05月24日 11:48:56   作者:Lonely Faith  
這篇文章主要給大家介紹Springboot整合Mycat2的具體過程,文中有詳細(xì)的圖解過程,感興趣的小伙伴可以跟著小編一起來學(xué)習(xí)

Springboot連接Mycat2

環(huán)境準(zhǔn)備:

http://t.csdn.cn/zRO5v

創(chuàng)建Springboot項(xiàng)目

在這里插入圖片描述

1.新建項(xiàng)目:

配置好對(duì)應(yīng)的選項(xiàng):

在這里插入圖片描述

選擇好對(duì)應(yīng)的springboot版本,和項(xiàng)目自帶的默認(rèn)依賴:

如果你的環(huán)境搭建的時(shí)候是按照上面的教程搭建,那么請(qǐng)選擇MySQL Drive驅(qū)動(dòng)

在這里插入圖片描述

配置Maven:

[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將圖片保存下來直接上傳(img-GxCupYk1-1684724379377)(C:\Users\ubunt\AppData\Roaming\Typora\typora-user-images\1684720903155.png)]

如果下載速度慢,請(qǐng)?jiān)贛aven的/conf/settings.xml文件中配置國(guó)內(nèi)鏡像源

在這里插入圖片描述

在這里插入圖片描述

等待Mave依賴同步完成。。。。。

在這里插入圖片描述

將配置文件改為yml后綴

在這里插入圖片描述

  • pom.xml
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.42</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.0</version>
        </dependency>
  • 修改配置文件.yml
server:
  port: 8080
spring:
  main:
    allow-bean-definition-overriding: true
  application:
    name: mycat
  # Mycat配置信息
  datasource:
    url: jdbc:mysql://192.168.174.138:8066/TESTDB?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8
    username: root
    password: 123456
    # mybatis是依賴5.*的版本,所以相對(duì)于8.*的少個(gè)cj;
    driver-class-name: com.mysql.jdbc.Driver
# MyBatis
mybatis:
  type-aliases-package: com.example.bootmycat.dao
  mapper-locations: classpath:/mapper/*.xml
  configuration:
    map-underscore-to-camel-case: true
  • 新建一個(gè)實(shí)體類
/**
 * @author jigua
 * @version 1.0
 * @className Enterprise
 * @description
 * @create 2022/9/27 16:14
 */
public class Enterprise {
    private Long id;
    private String phone;
    private String name;
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}
  • 新建一個(gè)dao
import com.example.bootmycat.pojo.Enterprise;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
/**
 * @author jigua
 * @version 1.0
 * @className TestDao
 * @description
 * @create 2022/9/27 16:13
 */
@Mapper
public interface TestDao {
    @Select("select * from enterprise")
    List<Enterprise> selectAll();
    @Insert("insert into enterprise(phone,name) values(#{phone},#{name})")
    int add(Enterprise enterprise);
}
  • 新建一個(gè)controller
    • 示例提供一個(gè)查詢和一個(gè)插入方法
import com.example.bootmycat.dao.TestDao;
import com.example.bootmycat.pojo.Enterprise;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.UUID;
/**
 * @author jigua
 * @version 1.0
 * @className TestController
 * @description
 * @create 2022/9/27 16:15
 */
@RestController
@RequestMapping("/test")
public class TestController {
    @Autowired
    private TestDao testDao;
    @GetMapping("/selectAll")
    @ResponseBody
    public List<Enterprise> selectAll() {
        List<Enterprise> enterprises = testDao.selectAll();
        return enterprises;
    }
    @GetMapping("/insert")
    @ResponseBody
    public String insert() {
        Enterprise enterprise = new Enterprise();
        String name = UUID.randomUUID().toString().replaceAll("-", "");
        enterprise.setName(name);
        enterprise.setPhone(name.substring(0, 3));
        int i = testDao.add(enterprise);
        if (i > 0) {
            return "success";
        }
        return "fail";
    }
}

最終文件目錄結(jié)構(gòu)圖:

在這里插入圖片描述

測(cè)試插入

  • http://localhost:8080/test/insert

[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將圖片保存下來直接上傳(img-TCqkzBZJ-1684724379379)(C:\Users\ubunt\AppData\Roaming\Typora\typora-user-images\1684723833644.png)]

多次測(cè)試查詢

  • http://localhost:8080/test/selectAll
  • 獲取一個(gè)zhang3,一個(gè)li4不同的結(jié)果,證明mycat讀寫分離成功

在這里插入圖片描述

在這里插入圖片描述

以上就是Spring整合Mycat2的具體過程的詳細(xì)內(nèi)容,更多關(guān)于Spring整合Mycat2的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • SpringMVC 攔截器的使用示例

    SpringMVC 攔截器的使用示例

    這篇文章主要介紹了SpringMVC 攔截器的使用示例,幫助大家更好的理解和學(xué)習(xí)使用SpringMVC,感興趣的朋友可以了解下
    2021-04-04
  • Java?Bluetooth?藍(lán)牙通訊?BlueCove?掃描附近的藍(lán)牙設(shè)備(測(cè)試代碼)

    Java?Bluetooth?藍(lán)牙通訊?BlueCove?掃描附近的藍(lán)牙設(shè)備(測(cè)試代碼)

    BlueCove是一個(gè)開源的藍(lán)牙協(xié)議棧實(shí)現(xiàn),旨在為Java開發(fā)者提供一個(gè)全面的、易于使用的API,從而在應(yīng)用程序中實(shí)現(xiàn)藍(lán)牙功能,該項(xiàng)目支持多種操作系統(tǒng),這篇文章主要介紹了Java?Bluetooth?藍(lán)牙通訊?BlueCove?掃描附近的藍(lán)牙設(shè)備,需要的朋友可以參考下
    2025-01-01
  • Netty內(nèi)存池泄漏問題以解決方案

    Netty內(nèi)存池泄漏問題以解決方案

    這篇文章主要介紹了Netty內(nèi)存池泄漏問題以解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • mybatis中關(guān)于type-aliases-package的使用

    mybatis中關(guān)于type-aliases-package的使用

    這篇文章主要介紹了mybatis中關(guān)于type-aliases-package的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • 在SpringBoot項(xiàng)目中整合攔截器的詳細(xì)步驟

    在SpringBoot項(xiàng)目中整合攔截器的詳細(xì)步驟

    在系統(tǒng)中經(jīng)常需要在處理用戶請(qǐng)求之前和之后執(zhí)行一些行為,例如檢測(cè)用戶的權(quán)限,或者將請(qǐng)求的信息記錄到日志中,即平時(shí)所說的"權(quán)限檢測(cè)"及"日志記錄",下面這篇文章主要給大家介紹了關(guān)于在SpringBoot項(xiàng)目中整合攔截器的相關(guān)資料,需要的朋友可以參考下
    2022-09-09
  • springboot使用filter獲取自定義請(qǐng)求頭的實(shí)現(xiàn)代碼

    springboot使用filter獲取自定義請(qǐng)求頭的實(shí)現(xiàn)代碼

    這篇文章主要介紹了springboot使用filter獲取自定義請(qǐng)求頭的實(shí)例代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-05-05
  • Java數(shù)據(jù)類型轉(zhuǎn)換的示例詳解

    Java數(shù)據(jù)類型轉(zhuǎn)換的示例詳解

    Java程序中要求參與的計(jì)算的數(shù)據(jù),必須要保證數(shù)據(jù)類型的一致性,如果數(shù)據(jù)類型不一致將發(fā)生類型的轉(zhuǎn)換。本文將通過示例詳細(xì)說說Java中數(shù)據(jù)類型的轉(zhuǎn)換,感興趣的可以了解一下
    2022-10-10
  • JAVA導(dǎo)出CSV文件實(shí)例教程

    JAVA導(dǎo)出CSV文件實(shí)例教程

    這篇文章主要介紹了如何用JAVA導(dǎo)出CSV文件,文中案例代碼十分詳細(xì),對(duì)大家的學(xué)習(xí)有所幫助,感興趣的朋友可以了解下
    2020-06-06
  • Java設(shè)計(jì)模式之策略模式詳解

    Java設(shè)計(jì)模式之策略模式詳解

    這篇文章主要為大家詳細(xì)介紹了Java設(shè)計(jì)模式之策略模式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • Spring如何將bean添加到容器中

    Spring如何將bean添加到容器中

    這篇文章主要介紹了Spring如何將bean添加到容器中,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-05-05

最新評(píng)論