怎么理解Mybatis和Solon

发布时间:2021-11-17 13:33:35 作者:iii
来源:亿速云 阅读:161

这篇文章主要介绍“怎么理解Mybatis和Solon”,在日常操作中,相信很多人在怎么理解Mybatis和Solon问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么理解Mybatis和Solon”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

一、环境说明
环境版本
IDEA2020.2
Maven4.0
Solon1.0.10
mybatis-solon-plugin1.0.10 (本例用到的关键框架)
mybatis-sqlhelper-solon-plugin1.0.10
Mybatis5.3.3
JDK1.8
二、代码

新建个空白的Maven项目:solon_mybatis,下面开始操作:

<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>

Solon 没有特定的数据源配置,所以随便自己起个头就可以;配置项与使用的数据源匹配即可。本例用的是HikariCP

#数据库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

#数据库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


#默认
mybatis:
    typeAliases:    #支持包名 或 类名(.class 结尾)
        - "webapp.model"
    mappers:        #支持包名 或 类名(.class 结尾)或 xml(.xml结尾)
        - "webapp.dso.mapper.AppxMapper.class"

#再定义个新配置(为了体现多数据源性 - 应该简单吧?)
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

基于 Spring 的@MapperScan实现,需要多个配置器才可以完成;mybatis-solon-plugin把它调整为一个函数,故多个数据源可以整到一个配置器里:

@XConfiguration
public class Config {
    @XBean("db1f")
    public SqlSessionFactory db1f(@XInject("${test.db1}") HikariDataSource dataSource) {
        //
        //可以用默认的配置
        //
        return new MybatisAdapter(dataSource)
                .mapperScan()   //完成Spring 的 @MapperScan注解的功能(相对来说,改成函数可以把多个 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();
    }
}

关于多数据源的分包模式示例:

/**
 * 分包模式,一开始就被会话工厂mapperScan()并关联好了
 * */
@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);
    }

}

关于多数据源的注解模式示例:

/**
 * 注解模式,通过@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);
    }

}

关于事务的示例:(分布式环境下,尽量用消息代理JDBC事务)

/**
 * 事务演示
 *
 * @Df 可注入 Mapper, SqlSession, SqlSessionFactory, MybatisProxy
 * */
@XMapping("/tran/")
@XController
public class TranController {
    @XInject
    AppxMapper appxMapper;

    /**
     * mybatis-solon-plugin 的事务,需要通过 MybatisProxy 发起
     *
     * solon 不目前支持注解事务,说是出于性能和细颗粒度的考虑;以及现在都流行引入消息处理事务了。
     * */
    @Df("db1f")
    MybatisProxy proxy;

    @XMapping("test")
    public Object test() throws Throwable{
        return proxy.tran((s)->{
            s.result = appxMapper.appx_get();
        });
    }
}

关于分页的示例:(本案用的是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();
    }
}
//这几个文件不是重点,可以直接看源码

//Appx2Mapper.java
//AppxMapper.java

//Appx2Mapper.xml
//AppxMapper.xml

到此,关于“怎么理解Mybatis和Solon”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. mybatis 基础理解
  2. mybatis-4 mybatis与spring结合使用及原理解析

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

solon mysql

上一篇:Spring与JSR330中Solon Ioc注解对比分析

下一篇:jquery如何获取tr里面有几个td

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》