我所有的代码都在ibatis包下,包括类和配置文件
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<settings cacheModelsEnabled="false" enhancementEnabled="false"
lazyLoadingEnabled="false" maxRequests="3000" maxSessions="3000"
maxTransactions="3000" useStatementNamespaces="false" />
<transactionManager type="JDBC">
<dataSource type="simple">
<property name="JDBC.Driver"
value="com.mysql.jdbc.Driver" />
<property name="JDBC.ConnectionURL"
value="jdbc:mysql://localhost:3306/test" />
<property name="JDBC.Username" value="root" />
<property name="JDBC.Password" value="******" />
</dataSource>
</transactionManager>
<sqlMap resource="ibatis/user.xml" />
</sqlMapConfig>
接下去是user.xml
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap
PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="User">
<typeAlias alias="user" type="ibatis.User" />
<select id="getUser" resultClass="user" parameterClass="java.lang.String">
select * from user where name like '%$name$%';
</select>
<select id="getAllUser" resultClass="user">
select * from user;
</select>
</sqlMap>
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
package ibatis;
import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.List;
import org.apache.log4j.BasicConfigurator;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
public class IbatisDemo {
public static void main(String[] args) throws IOException, SQLException {
BasicConfigurator.configure();
String resource = "ibatis/sqlmap.xml";
Reader reader = Resources.getResourceAsReader(resource);
SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
List list = sqlMap.queryForList("getUser", "sa");
System.out.println(list.size());
list = sqlMap.queryForList("getAllUser");
System.out.println(list.size());
}
}
User.java类我就不贴了,一个只有两个private属性的bean,很简单的run就可以得出结果,这也是最简单的ibatis应用了