Mybatis 数据源

LD is tigger forever,CG are not brothers forever, throw the pot and shine forever.
Modesty is not false, solid is not naive, treacherous but not deceitful, stay with good people, and stay away from poor people.
talk is cheap, show others the code and KPI, Keep progress,make a better result.
Survive during the day and develop at night。

目录

概 述

1.database 数据源:
通常有3种方法:
1、mybatis中的数据源
mybatis连接池为我们提供了3种⽅式的配置:

  1. POOLED:采⽤传统的javax.sql.DataSource规范中的连接池,mybatis中有针对规范的实现
  2. UNPOOLED:采⽤传统的获取连接的⽅式,虽然也实现Javax.sql.DataSource接⼝,但是并没有使⽤池的思想。
  3. JNDI:采⽤服务器提供的JNDI技术实现,来获取DataSource对象,不同的服务器所能拿到DataSource是不⼀样。
    注意:如果不是web或者maven的war⼯程,JNDI是不能使⽤的。
    2、在mybatis中配置数据源
    2.1、POOLED数据源的配置⽅式





测试代码:

 @Test
  void testNetworkTimeout_PooledDataSource() throws Exception {
    UnpooledDataSource unpooledDataSource = (UnpooledDataSource) PgContainer.getUnpooledDataSource();
    PooledDataSource dataSource = new PooledDataSource(unpooledDataSource);
    dataSource.setDefaultNetworkTimeout(5000);
    try (Connection connection = dataSource.getConnection()) {
      assertEquals(5000, connection.getNetworkTimeout());
    }
  }

2.2、UNPOOLED数据源的配置⽅式





测试代码:

  @Test
  void shouldNotRegisterTheSameDriverMultipleTimes() throws Exception {
    // https://code.google.com/p/mybatis/issues/detail?id=430
    UnpooledDataSource dataSource = null;
    dataSource = new UnpooledDataSource("org.hsqldb.jdbcDriver", "jdbc:hsqldb:mem:multipledrivers", "sa", "");
    dataSource.getConnection().close();
    int before = countRegisteredDrivers();
    dataSource = new UnpooledDataSource("org.hsqldb.jdbcDriver", "jdbc:hsqldb:mem:multipledrivers", "sa", "");
    dataSource.getConnection().close();
    assertEquals(before, countRegisteredDrivers());
  }

2.3、JNDI数据源的配置⽅式
将数据源的配置⽂件 context.xml 放到⼯程的webapp/META-INF/下
context.xml
<Resource
name="jdbc/mybatis"数据源的名称
type="javax.sql.DataSource"数据源类型
auth="Container"数据源提供者
maxActive="20"最⼤活动数
maxWait="10000"最⼤等待时间
maxIdle="5"最⼤空闲数
username="root"⽤户名
password="1234"密码
driverClassName="com.mysql.jdbc.Driver"驱动类
url="jdbc:mysql://localhost:3306/mybatis"连接url字符串
/>
然后在mybatis的主配置⽂件中配置数据源,其中前缀"java:comp/env/ "是固定的,后缀 “jdbc/mybatis” 是在context.xml中取的JNDI数据源名



</dataSourc

测试代码:

  @Test
  void shouldRetrieveDataSourceFromJNDI() {
    createJndiDataSource();
    JndiDataSourceFactory factory = new JndiDataSourceFactory();
    factory.setProperties(new Properties() {
      {
        setProperty(JndiDataSourceFactory.ENV_PREFIX + Context.INITIAL_CONTEXT_FACTORY, TEST_INITIAL_CONTEXT_FACTORY);
        setProperty(JndiDataSourceFactory.INITIAL_CONTEXT, TEST_INITIAL_CONTEXT);
        setProperty(JndiDataSourceFactory.DATA_SOURCE, TEST_DATA_SOURCE);
      }
    });
    DataSource actualDataSource = factory.getDataSource();
    assertEquals(expectedDataSource, actualDataSource);
  }

使用字符串建立一个factory ,通过JNDI找到对应的数据源。

小结

参考资料和推荐阅读

1.链接: link.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要排除MyBatis数据源,可以通过在Spring Cloud的配置文件中进行相应的配置。具体的配置方法有多种,下面提供两种常见的方式: 方式一:使用dynamic-datasource-spring-boot-starter 在Spring Cloud的配置文件中,可以添加以下配置来排除MyBatis数据源: ```yaml spring: datasource: dynamic: datasource: master: url: jdbc:mysql://localhost:3306/master username: root password: root slave: url: jdbc:mysql://localhost:3306/slave username: root password: root enabled: false # 设置为false,禁用slave数据源 ``` 这样就可以排除MyBatis数据源,并且只使用主数据源。 方式二:使用Feign的header参数 在Feign的接口定义中,可以增加一个header参数来指定使用的数据源,在header中添加一个名为"userDb"的参数,然后在具体的方法中根据该参数来选择数据源: ```java @FeignClient(name = "dynamic", url = "http://localhost:8010", fallback = SyncFeignDynamicServiceFallBack.class) public interface SyncFeignDynamicService { @RequestMapping("/user/getUserById") Object getUserById(@RequestParam("userId") Integer userId, @RequestHeader("userDb") String userDb); } ``` 通过在请求头中添加"userDb"参数,来指定使用的数据源。这样就可以在不同的请求中选择不同的数据源。 以上是两种常见的方法,可以根据具体的需求选择适合的方式来排除MyBatis数据源。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

执于代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值