SpringBoot整合Mybatis多資料來源(Atomikos)
一、 Spring介紹
1.1、SpringBoot簡介
在您第1次接觸和學習Spring框架的時候,是否因為其繁雜的配置而退卻了?在你第n次使用Spring框架的時候,是否覺得一堆反覆黏貼的配置有一些厭煩?那麼您就不妨來試試使用Spring Boot來讓你更易上手,更簡單快捷地構建Spring應用!
Spring Boot讓我們的Spring應用變的更輕量化。比如:你可以僅僅依靠一個Java類來執行一個Spring引用。你也可以打包你的應用為jar並通過使用java -jar來執行你的Spring Web應用。
Spring Boot的主要優點:
為所有Spring開發者更快的入門
開箱即用,提供各種預設配置來簡化專案配置
內嵌式容器簡化Web專案
沒有冗餘程式碼生成和XML配置的要求
本章主要目標完成Spring Boot基礎專案的構建,並且實現一個簡單的Http請求處理,通過這個例子對Spring Boot有一個初步的瞭解,並體驗其結構簡單、開發快速的特性。
1.2、系統要求:
Java1.8及以上
Spring Framework 4.1.5及以上
本文采用Java 1.8.0_73、Spring Boot 2.0版本除錯通過。
1.3、SpringBoot和SpringMVC區別
SpringBoot 是一個快速開發的框架,能夠快速的整合第三方框架,簡化XML配置,全部採用註解形式,內建Tomcat容器,幫助開發者能夠實現快速開發,SpringBoot的Web元件 預設整合的是SpringMVC框架。
SpringMVC是控制層。
1.4、SpringBoot和SpringCloud區別
SpringBoot 是一個快速開發的框架,能夠快速的整合第三方框架,簡化XML配置,全部採用註解形式,內建Tomcat容器,幫助開發者能夠實現快速開發,SpringBoot的Web元件 預設整合的是SpringMVC框架。
SpringMVC是控制層。
SpringCloud依賴與SpringBoot元件,使用SpringMVC編寫Http協議介面,同時SpringCloud是一套完整的微服務解決框架。
1.5常見錯誤
Eclipse 下載SpringBoot2.0以上版本,pom檔案報錯解決辦法
org.apache.maven.archiver.MavenArchiver.getManifest(org.apache.maven.project.MavenProject, org.apache.maven.archiver.MavenArchiveConfiguration)
相關網址: http://bbs.itmayiedu.com/article/1527749194015
SpringBoot 是一個快速開發的框架,能夠快速的整合第三方框架,簡化XML配置,全部採用註解形式,內建Tomcat容器,幫助開發者能夠實現快速開發,SpringBoot的Web元件 預設整合的是SpringMVC框架。
SpringMVC是控制層。
二、快速入門
2.1、建立一個Maven工程
名為”SpringBoot” 型別為Jar工程專案
2.2、pom檔案引入依賴
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> |
spring-boot-starter-parent作用 在pom.xml中引入spring-boot-start-parent,spring官方的解釋叫什麼stater poms,它可以提供dependency management,也就是說依賴管理,引入以後在申明其它dependency的時候就不需要version了,後面可以看到。 spring-boot-starter-web作用 springweb 核心元件 spring-boot-maven-plugin作用 如果我們要直接Main啟動spring,那麼以下plugin必須要新增,否則是無法啟動的。如果使用maven的spring-boot:run的話是不需要此配置的。(我在測試的時候,如果不配置下面的plugin也是直接在Main中執行的。) |
2.3、編寫SpringBoot服務
建立package命名為com.gdlsky.controller(根據實際情況修改)
建立HelloController類,內容如下
@RestController @EnableAutoConfiguration publicclass HelloController { @RequestMapping("/hello") public String index() { return"Hello World"; } publicstaticvoid main(String[] args) { SpringApplication.run(HelloController.class, args); } } |
2.4、@RestController
在上加上RestController 表示修飾該Controller所有的方法返回JSON格式,直接可以編寫
Restful介面
2.5、@EnableAutoConfiguration
註解:作用在於讓 Spring Boot 根據應用所宣告的依賴來對 Spring 框架進行自動配置
這個註解告訴Spring Boot根據新增的jar依賴猜測你想如何配置Spring。由於spring-boot-starter-web添加了Tomcat和Spring MVC,所以auto-configuration將假定你正在開發一個web應用並相應地對Spring進行設定。
2.6 SpringApplication.run(HelloController.class, args);
標識為啟動類
2.7、SpringBoot啟動方式1
Springboot預設埠號為8080
@RestController @EnableAutoConfiguration publicclass HelloController { @RequestMapping("/hello") public String index() { return"Hello World"; } publicstaticvoid main(String[] args) { SpringApplication.run(HelloController.class, args); } } |
啟動主程式,開啟瀏覽器訪問http://localhost:8080/index,可以看到頁面輸出Hello World
2.8、SpringBoot啟動方式2
@ComponentScan(basePackages = "com.gdlsky.controller")---控制器掃包範圍
@ComponentScan(basePackages = "com.gdlsky.controller") @EnableAutoConfiguration public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } } |
2.9、SpringBoot啟動方式3
@SpringBootApplication
@SpringBootApplication 被 @Configuration、@EnableAutoConfiguration、@ComponentScan 註解所修飾,換言之 Springboot 提供了統一的註解來替代以上三個註解
掃包範圍:在啟動類上加上@SpringBootApplication註解,當前包下或者子包下所有的類都可以掃到。
三、 Web開發
3.1、靜態資源訪問
在我們開發Web應用的時候,需要引用大量的js、css、圖片等靜態資源。
預設配置
Spring Boot預設提供靜態資源目錄位置需置於classpath下,目錄名需符合如下規則:
/static
/public
/resources
/META-INF/resources
舉例:我們可以在src/main/resources/目錄下建立static,在該位置放置一個圖片檔案。啟動程式後,嘗試訪問http://localhost:8080/D.jpg。如能顯示圖片,配置成功。
3.2、渲染Web頁面
渲染Web頁面
在之前的示例中,我們都是通過@RestController來處理請求,所以返回的內容為json物件。那麼如果需要渲染html頁面的時候,要如何實現呢?
模板引擎
在動態HTML實現上Spring Boot依然可以完美勝任,並且提供了多種模板引擎的預設配置支援,所以在推薦的模板引擎下,我們可以很快的上手開發動態網站。
Spring Boot提供了預設配置的模板引擎主要有以下幾種:
· Thymeleaf
· FreeMarker
· Velocity
· Groovy
· Mustache
Spring Boot建議使用這些模板引擎,避免使用JSP,若一定要使用JSP將無法實現Spring Boot的多種特性,具體可見後文:支援JSP的配置
當你使用上述模板引擎中的任何一個,它們預設的模板配置路徑為:src/main/resources/templates。當然也可以修改這個路徑,具體如何修改,可在後續各模板引擎的配置屬性中查詢並修改。
3.3.1、pom檔案引入:
<!-- 引入freeMarker的依賴包. --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> |
3.3.2、後臺程式碼
在src/main/resources/建立一個templates資料夾,字尾為*.ftl
@RequestMapping("/index") public String index(Map<String, Object> map) { map.put("name","美麗的天使..."); return"index"; } |
3.3.3、前臺程式碼
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8" /> <title></title> </head> <body> ${name} </body> </html> |
3.3.4、Freemarker其他用法
@RequestMapping("/freemarkerIndex") public String index(Map<String, Object> result) { result.put("name", "yushengjun"); result.put("sex", "0"); List<String> listResult = new ArrayList<String>(); listResult.add("zhangsan"); listResult.add("lisi"); listResult.add("itmayiedu"); result.put("listResult", listResult); return "index"; } <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8" /> <title>首頁</title> </head> <body> ${name} <#if sex=="1"> 男 <#elseif sex=="2"> 女 <#else> 其他 </#if> <#list userlist as user> ${user} </#list> </body> </html> |
3.3.5、Freemarker配置
新建application.properties檔案
######################################################## ###FREEMARKER (FreeMarkerAutoConfiguration) ######################################################## spring.freemarker.allow-request-override=false spring.freemarker.cache=true spring.freemarker.check-template-location=true spring.freemarker.charset=UTF-8 spring.freemarker.content-type=text/html spring.freemarker.expose-request-attributes=false spring.freemarker.expose-session-attributes=false spring.freemarker.expose-spring-macro-helpers=false #spring.freemarker.prefix= #spring.freemarker.request-context-attribute= #spring.freemarker.settings.*= spring.freemarker.suffix=.ftl spring.freemarker.template-loader-path=classpath:/templates/ #comma-separated list #spring.freemarker.view-names= # whitelist of view names that can be resolved |
3.4、使用JSP渲染Web檢視
3.4.1、pom檔案引入以下依賴
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> </parent> <dependencies> <!-- SpringBoot web 核心元件 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> <!-- SpringBoot 外部tomcat支援 --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> </dependencies> |
3.4.2、在application.properties建立以下配置
spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.view.suffix=.jsp |
3.4.3、後臺程式碼
@Controller publicclass IndexController { @RequestMapping("/index") public String index() { return"index"; } } |
注意:建立SpringBoot整合JSP,一定要為war型別,否則會找不到頁面.
不要把JSP頁面存放在resources// jsp 不能被訪問到
3.5、全域性捕獲異常
@ExceptionHandler 表示攔截異常
· @ControllerAdvice 是 controller 的一個輔助類,最常用的就是作為全域性異常處理的切面類
· @ControllerAdvice 可以指定掃描範圍
· @ControllerAdvice 約定了幾種可行的返回值,如果是直接返回 model 類的話,需要使用 @ResponseBody 進行 json 轉換
o 返回 String,表示跳到某個 view
o 返回 modelAndView
o 返回 model + @ResponseBody
@ControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(RuntimeException.class) @ResponseBody public Map<String, Object> exceptionHandler() { Map<String, Object> map = new HashMap<String, Object>(); map.put("errorCode", "101"); map.put("errorMsg", "系統錯誤!"); return map; } } |
四、 資料訪問
4.1.1 pom檔案引入
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> </parent> <dependencies> <!-- jdbcTemplate 依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!-- mysql 依賴 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- 測試 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- springboot-web元件 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> |
4.1.2 application.properties新增配置
spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver |
4.1.3 UserService類
@Service publicclass UserServiceImpl implements UserService { @Autowired private JdbcTemplate jdbcTemplate; publicvoid createUser(String name, Integer age) { jdbcTemplate.update("insert into users values(null,?,?);", name, age); } } |
4.1.4 App類
//@ComponentScan(basePackages = { "com.itmayiedu.controller", "com.itmayiedu.service" }) //@EnableAutoConfiguration @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } } |
注意: spring-boot-starter-parent要在1.5以上版本
4.2、springboot整合使用mybatis
4.2.1、pom檔案引入
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- 測試 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> <!-- mysql 依賴 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- springboot-web元件 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> |
4.2.2、配置檔案引入
spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver |
Controller程式碼
/** * @ClassDes:描述資訊
一、 Spring介紹
1.1、SpringBoot簡介
在您第1次接觸和學習Spring框架的時候,是否因為其繁雜的配置而退卻了?在你第n次使用Spring框架的時候,是否覺得一堆反覆黏貼的配置有一些厭煩?那麼您就不妨來試試使用Spring Boot來讓你更易上手,更簡
application.properties
#mysql
[email protected]@
spring.datasource.primary.username= @[email protected]
spring.datasource.prim 建表 相同字段 將他 必備 關聯 per con 表示 status 1.各關聯表盡量不要使用相同的字段。因為在多表聯查時,如果出現相同的字段,數據庫自動使這些相同字段的值相等。
比如說,訂單表有一個表示訂單狀態的status字段,而它的外鍵關聯的表car有
新建com.millery.utils包在其下新建DataSourceContextHolder類
package com.millery.utils;
public class DataSourceContextHolder {
private
package com.he.config;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.my package com.ehaoyao.paycenter.job.common.config;/**
* ERP資料來源配置類
*
* @author PF
* Created by dell on 2018-05-04.
*/
import org.springframework.beans. 最近在專案開發中,需要為一個使用 MySQL 資料庫的 SpringBoot 專案,新新增一個 PLSQL 資料庫資料來源,那麼就需要進行 SpringBoot 的多資料來源開發。程式碼很簡單,下面是實現的過程。
## 環境準備
實驗環境:
- JDK 1.8
- SpringBoot 2.4.1
- springboot配置多資料來源有好幾種方式
1.application.properties配置
## 埠
server.port=8080
# 資料庫訪問配置
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spri
1:SpringBoot整合mybatis實現多資料來源有兩種方法
1:靜態方式
將每個資料來源都實現一個mybatis的sqlSessionFactory中,但是這種方法,缺點在於:你有幾個資料來源都會有幾個mybatis的配置類;對於資料來源的事務也不是很
閒暇之餘,寫點最近的收穫,寫一點心得,便於以後參考方便,另外可以幫助有這樣需求的人少走彎路。 整合多個數據源,很多部落格會提到springboot整合jdbcTemplete為例子,網上有很多,今天主要推薦的是整合 pagehelp
SpringBoot整合Mybatis
第一步我們需要在pom.xml裡面引入mybatis相關的jar包
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artif 最近專案有一個非解決不可的問題,我們的專案中的使用者表是用的自己庫的資料,但是這些資料都是從一個已有庫中遷過來的,所以使用者資訊都是在那個專案裡面維護,自然而然我們專案不提供使用者註冊功能,這就有個問題,如何解決資料遷移的問題,總不能我每次都手動導資料吧,所以我決心寫一個介面把那個庫中的使用者資訊同步我們
springboot整合Mybatis、事務、多資料來源
文章目錄
springboot整合Mybatis、事務、多資料來源
一. 整合Mybatis
二. 事務
2.1 回顧事務
相信大家已經看了不少的教程了,所以在此我不在贅述。。。。。。
遇到的坑,在專案中readme.md 中有描述。具體下載下來配置比較詳細,初始化sql ,單元測試。。。檢視流程即可。
demo非常簡單,下載下來參考 readme.md 修改必要內容即可完成配 背景
之前做資料抽取的時候,搭了一個mybatis動態資料來源切換的架子。方便他們寫抽取的程式碼。今天同事問我,架子裡面的mybatisplus的IPage失效了是什麼問題。想了一下,應該是寫動態資料來源的時候,我自定義的mybatis的配置覆蓋了已有的配置。於是我讓他先把我寫的配置進行刪除,看是否正常。得到
轉載至:https://blog.csdn.net/xiaosheng_papa/article/details/80218006
親測有效。
注:有些系統中已經配置了單資料來源,現在要轉成多資料來源,可能需要額外的配置。拿我自己當前專案來說:
專案在啟動類中配置了單資料來源:
Github 地址:github.com/Snailclimb/…(SpringBoot和其他常用技術的整合,可能是你遇到的講解最詳細的學習案例,力爭新手也能看懂並且能夠在看完之後獨立實踐。基於最新的 SpringBoot2.0+,是你學習SpringBoot 的最佳指南。) ,歡迎各位 Star。
Github 地址:https://github.com/Snailclimb/springboot-integration-examples(SpringBoot和其他常用技術的整合,可能是你遇到的講解最詳細的學習案例,力爭新手也能看懂並且能夠在看完之後獨立實踐。基於最新的 S
關於整合
網上關於springboot2.0和shiro+myabtis整合的案例很少,大神的教程也是用jpa編寫,jpa很方便,但是還有很多人用mybatis,加之剛學習完mybatis多資料來源整合和druid連線池監控配置,所以算是階段性記錄。
專案目
本篇實際上和mybatisext專案並沒有太大關係了,但在實際專案中脫離不開多個數據源,尤其是主從分離,同樣網上一些資料大同小異而且大部分並不能真正解決問題,所以單獨提出來說一下
假設我們就是要解決一個主從分離,資料來源定義在了application.properties中 |