Mybatis 数据源

本文介绍了Mybatis中的三种数据源配置:POOLED、UNPOOLED和JNDI,并给出了详细的配置示例及测试代码。重点讨论了每种数据源的工作原理和应用场景,同时提供了配置数据源的步骤和注意事项,适合Mybatis使用者参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

迅捷的软件产品制作专家

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

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

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

打赏作者

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

抵扣说明:

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

余额充值