mybatis pooled怎么获取property
时间: 2023-11-17 14:04:24 浏览: 33
MyBatis中使用连接池技术,需要在配置文件中指定连接池的相关属性。对于pooled连接池,可以使用如下方式获取属性:
1.在配置文件中定义属性值:
```
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
<property name="poolMaximumActiveConnections" value="50"/>
<property name="poolMaximumIdleConnections" value="10"/>
<property name="poolMaximumCheckoutTime" value="20000"/>
</dataSource>
```
2.在Java代码中获取属性值:
```java
DataSource dataSource = sqlSessionFactory.getConfiguration().getEnvironment().getDataSource();
PooledDataSource pooledDataSource = (PooledDataSource) dataSource;
int poolMaximumActiveConnections = pooledDataSource.getPoolMaximumActiveConnections();
int poolMaximumIdleConnections = pooledDataSource.getPoolMaximumIdleConnections();
int poolMaximumCheckoutTime = pooledDataSource.getPoolMaximumCheckoutTime();
```
其中,sqlSessionFactory是通过MyBatis初始化后获取到的,可以通过配置文件构建:
```java
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)