這篇文章主要介紹“怎么理解Mybatis和Solon”,在日常操作中,相信很多人在怎么理解Mybatis和Solon問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”怎么理解Mybatis和Solon”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

黃驊ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)建站的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18982081108(備注:SSL證書合作)期待與您的合作!
| 環(huán)境 | 版本 |
|---|---|
| IDEA | 2020.2 |
| Maven | 4.0 |
| Solon | 1.0.10 |
| mybatis-solon-plugin | 1.0.10 (本例用到的關(guān)鍵框架) |
| mybatis-sqlhelper-solon-plugin | 1.0.10 |
| Mybatis | 5.3.3 |
| JDK | 1.8 |
新建個空白的Maven項目:solon_mybatis,下面開始操作:
(一)在 pom.xml 文件里添加依賴
<parent> <groupId>org.noear</groupId> <artifactId>solon-parent</artifactId> <version>1.0.10</version> <relativePath /> </parent> <dependencies> <dependency> <groupId>org.noear</groupId> <artifactId>solon-web</artifactId> <type>pom</type> </dependency> <dependency> <groupId>org.noear</groupId> <artifactId>mybatis-solon-plugin</artifactId> </dependency> <dependency> <groupId>org.noear</groupId> <artifactId>mybatis-sqlhelper-solon-plugin</artifactId> </dependency> <!-- 其它依賴參考源碼,不然占板面太多了 --> </dependencies>
(二)修改屬性文件 application.yml (添加多數(shù)據(jù)源和分布組件的配置)
Solon 沒有特定的數(shù)據(jù)源配置,所以隨便自己起個頭就可以;配置項與使用的數(shù)據(jù)源匹配即可。本例用的是HikariCP:
#數(shù)據(jù)庫1的配置 test.db1: schema: rock jdbcUrl: jdbc:MySQL://localdb:3306/rock?useUnicode=true&characterEncoding=utf8&autoReconnect=true&rewriteBatchedStatements=true driverClassName: com.mysql.cj.jdbc.Driver username: demo password: UL0hHlg0Ybq60xyb #數(shù)據(jù)庫2的配置(其實我用的是同一個庫) test.db2: schema: rock jdbcUrl: jdbc:mysql://localdb:3306/rock?useUnicode=true&characterEncoding=utf8&autoReconnect=true&rewriteBatchedStatements=true driverClassName: com.mysql.cj.jdbc.Driver username: demo password: UL0hHlg0Ybq60xyb #默認(rèn) mybatis: typeAliases: #支持包名 或 類名(.class 結(jié)尾) - "webapp.model" mappers: #支持包名 或 類名(.class 結(jié)尾)或 xml(.xml結(jié)尾) - "webapp.dso.mapper.AppxMapper.class" #再定義個新配置(為了體現(xiàn)多數(shù)據(jù)源性 - 應(yīng)該簡單吧?) mybatis.db2f: typeAliases: - "webapp.model" mappers: - "webapp.dso.mapper.Appx2Mapper.class" #分頁組件的配置 sqlhelper: mybatis: instrumentor: dialect: "mysql" cache-instrumented-sql: true subquery-paging-start-flag: "[PAGING_StART]" subquery-paging-end-flag: "[PAGING_END]" pagination: count: true default-page-size: 10 use-last-page-if-page-no-out: true count-suffix: _COUNT
(三)添加配置器(完成會話工廠的構(gòu)建 及 Mapper 的描述與關(guān)聯(lián);看上去,挺簡潔的)
基于 Spring 的@MapperScan實現(xiàn),需要多個配置器才可以完成;mybatis-solon-plugin把它調(diào)整為一個函數(shù),故多個數(shù)據(jù)源可以整到一個配置器里:
@XConfiguration
public class Config {
@XBean("db1f")
public SqlSessionFactory db1f(@XInject("${test.db1}") HikariDataSource dataSource) {
//
//可以用默認(rèn)的配置
//
return new MybatisAdapter(dataSource)
.mapperScan() //完成Spring 的 @MapperScan注解的功能(相對來說,改成函數(shù)可以把多個 mapperScan 安排在一個 Config里)
.getFactory();
}
@XBean("db2f")
public SqlSessionFactory db2f(
@XInject("${test.db2}") HikariDataSource dataSource,
@XInject("${mybatis.db2f}") Properties props) {
//
//可以指定配置 ${mybatis.db2f}
//
return new MybatisAdapter(dataSource, props)
.mapperScan()
.getFactory();
}
}(四)添加控制器
關(guān)于多數(shù)據(jù)源的分包模式示例:
/**
* 分包模式,一開始就被會話工廠mapperScan()并關(guān)聯(lián)好了
* */
@XMapping("/demo/")
@XController
public class DemoController {
@XInject
AppxMapper appxMapper; //已被db1f mapperScan 了,可直接注入
@XInject
Appx2Mapper appxMapper2; //已被db2f mapperScan 了,可直接注入
@XMapping("test")
public AppxModel test(){
return appxMapper.appx_get();
}
@XMapping("test2")
public AppxModel test2(){
return appxMapper2.appx_get2(48);
}
}關(guān)于多數(shù)據(jù)源的注解模式示例:
/**
* 注解模式,通過@Db注入,并指定具體的會話工廠
*
* @Df 可注入 Mapper, SqlSession, SqlSessionFactory, MybatisProxy
* */
@XMapping("/demo2/")
@XController
public class Demo2Controller {
@Df("db1f")
AppxMapper appxMapper; //使用@Db 指定會話工廠并注入
@Df("db2f")
Appx2Mapper appxMapper2;
@XMapping("test")
public AppxModel test(){
return appxMapper.appx_get();
}
@XMapping("test2")
public AppxModel test2(){
return appxMapper2.appx_get2(48);
}
}關(guān)于事務(wù)的示例:(分布式環(huán)境下,盡量用消息代理JDBC事務(wù))
/**
* 事務(wù)演示
*
* @Df 可注入 Mapper, SqlSession, SqlSessionFactory, MybatisProxy
* */
@XMapping("/tran/")
@XController
public class TranController {
@XInject
AppxMapper appxMapper;
/**
* mybatis-solon-plugin 的事務(wù),需要通過 MybatisProxy 發(fā)起
*
* solon 不目前支持注解事務(wù),說是出于性能和細(xì)顆粒度的考慮;以及現(xiàn)在都流行引入消息處理事務(wù)了。
* */
@Df("db1f")
MybatisProxy proxy;
@XMapping("test")
public Object test() throws Throwable{
return proxy.tran((s)->{
s.result = appxMapper.appx_get();
});
}
}關(guān)于分頁的示例:(本案用的是sqlhelper)
@XMapping("/page/")
@XController
public class PageController {
@XInject
AppxMapper appxMapper;
@XMapping("test")
public Object test() throws Throwable{
SqlPaginations.preparePagination(2,2);
return appxMapper.appx_get_page();
}
}(五)略過的代碼文件(看開頭的相關(guān)源碼)
//這幾個文件不是重點,可以直接看源碼 //Appx2Mapper.java //AppxMapper.java //Appx2Mapper.xml //AppxMapper.xml
到此,關(guān)于“怎么理解Mybatis和Solon”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
本文名稱:怎么理解Mybatis和Solon
新聞來源:http://www.chinadenli.net/article22/pechcc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、靜態(tài)網(wǎng)站、企業(yè)網(wǎng)站制作、搜索引擎優(yōu)化、小程序開發(fā)、品牌網(wǎng)站設(shè)計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)