溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么理解Mybatis和Solon

發(fā)布時(shí)間:2021-11-17 13:33:35 來(lái)源:億速云 閱讀:182 作者:iii 欄目:大數(shù)據(jù)

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

一、環(huán)境說(shuō)明
環(huán)境版本
IDEA2020.2
Maven4.0
Solon1.0.10
mybatis-solon-plugin1.0.10 (本例用到的關(guān)鍵框架)
mybatis-sqlhelper-solon-plugin1.0.10
Mybatis5.3.3
JDK1.8
二、代碼

新建個(gè)空白的Maven項(xiàng)目:solon_mybatis,下面開(kāi)始操作:

  • (一)在 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 沒(méi)有特定的數(shù)據(jù)源配置,所以隨便自己起個(gè)頭就可以;配置項(xiàng)與使用的數(shù)據(jù)源匹配即可。本例用的是HikariCP

#數(shù)據(jù)庫(kù)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ù)庫(kù)2的配置(其實(shí)我用的是同一個(gè)庫(kù))
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"

#再定義個(gè)新配置(為了體現(xiàn)多數(shù)據(jù)源性 - 應(yīng)該簡(jiǎn)單吧?)
mybatis.db2f:
    typeAliases:
        - "webapp.model"
    mappers:
        - "webapp.dso.mapper.Appx2Mapper.class"


#分頁(yè)組件的配置
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
  • (三)添加配置器(完成會(huì)話工廠的構(gòu)建 及 Mapper 的描述與關(guān)聯(lián);看上去,挺簡(jiǎn)潔的)

基于 Spring 的@MapperScan實(shí)現(xiàn),需要多個(gè)配置器才可以完成;mybatis-solon-plugin把它調(diào)整為一個(gè)函數(shù),故多個(gè)數(shù)據(jù)源可以整到一個(gè)配置器里:

@XConfiguration
public class Config {
    @XBean("db1f")
    public SqlSessionFactory db1f(@XInject("${test.db1}") HikariDataSource dataSource) {
        //
        //可以用默認(rèn)的配置
        //
        return new MybatisAdapter(dataSource)
                .mapperScan()   //完成Spring 的 @MapperScan注解的功能(相對(duì)來(lái)說(shuō),改成函數(shù)可以把多個(gè) mapperScan 安排在一個(gè) 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ù)源的分包模式示例:

/**
 * 分包模式,一開(kāi)始就被會(huì)話工廠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ù)源的注解模式示例:

/**
 * 注解模式,通過(guò)@Db注入,并指定具體的會(huì)話工廠
 *
 * @Df 可注入 Mapper, SqlSession, SqlSessionFactory, MybatisProxy
 * */
@XMapping("/demo2/")
@XController
public class Demo2Controller {
    @Df("db1f")
    AppxMapper appxMapper;     //使用@Db 指定會(huì)話工廠并注入

    @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ù),需要通過(guò) MybatisProxy 發(fā)起
     *
     * solon 不目前支持注解事務(wù),說(shuō)是出于性能和細(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)于分頁(yè)的示例:(本案用的是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ò)的代碼文件(看開(kāi)頭的相關(guān)源碼)

//這幾個(gè)文件不是重點(diǎn),可以直接看源碼

//Appx2Mapper.java
//AppxMapper.java

//Appx2Mapper.xml
//AppxMapper.xml

到此,關(guān)于“怎么理解Mybatis和Solon”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI