怎么理解Mybatis和Solon

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

黄骅ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联建站的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18982081108(备注:SSL证书合作)期待与您的合作!

一、环境说明
环境版本
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,下面开始操作:

  • (一)在 pom.xml 文件里添加依赖


    org.noear
    solon-parent
    1.0.10
    



    
        org.noear
        solon-web
        pom
    

    
        org.noear
        mybatis-solon-plugin
    
    
    
        org.noear
        mybatis-sqlhelper-solon-plugin
    
    
    
  • (二)修改属性文件 application.yml (添加多数据源和分布组件的配置)

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
  • (三)添加配置器(完成会话工厂的构建 及 Mapper 的描述与关联;看上去,挺简洁的)

基于 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”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!


当前题目:怎么理解Mybatis和Solon
标题URL:http://scyanting.com/article/pechcc.html