---
title: "使用ibatis里的Connection"
author: "axhack"
date: "2008-08-21T23:16:59Z"
tags: []
slug: "ibatis-connection"
draft: false
lastmod: "2008-08-21T23:16:59Z"
id: 231372
url: "https://axhack.iteye.com/blog/231372"
---

最近在写个小程序，使用的是ibatis作的ORM, 但是程序中需要对数据表直接操作， 

在程序中再建立JDBC的连接不利于程序的改动。
    后来在ibatis的文档中发现可以获取ibatis的Connection，但是自己试了几次，怎
么也联不上去。看API文档时发现可以通过DataSource来获取Connection
写道

```java

String resource = "SqlMap.xml"; Reader reader = Resources.getResourceAsReader(resource); 
SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader); 
Connection db = sqlMap.getDataSource().getConnection(); 
Statement st = db.createStatement(); 
ResultSet rs = st.executeQuery("show tables");
```


这样以后换数据什么的就很方便了，不需要更改源程序，只改ibatis的配置文件就行了