Spring整合Mycat2的具體過程詳解
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:
如果下載速度慢,請(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
多次測(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)文章
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-01mybatis中關(guān)于type-aliases-package的使用
這篇文章主要介紹了mybatis中關(guān)于type-aliases-package的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08在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-09springboot使用filter獲取自定義請(qǐng)求頭的實(shí)現(xiàn)代碼
這篇文章主要介紹了springboot使用filter獲取自定義請(qǐng)求頭的實(shí)例代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05Java數(shù)據(jù)類型轉(zhuǎn)換的示例詳解
Java程序中要求參與的計(jì)算的數(shù)據(jù),必須要保證數(shù)據(jù)類型的一致性,如果數(shù)據(jù)類型不一致將發(fā)生類型的轉(zhuǎn)換。本文將通過示例詳細(xì)說說Java中數(shù)據(jù)類型的轉(zhuǎn)換,感興趣的可以了解一下2022-10-10