首页 > 代码库 > iBATIS小例子(初学)

iBATIS小例子(初学)

SqlMapConfig.xml文件:

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN" "http://www.ibatis.com/dtd/sql-map-config-2.dtd" >

<sqlMapConfig>

<properties resource="SqlMap.properties"/>

<transactionManager type="JDBC">

<dataSource type="SIMPLE">

<property name="JDBC.Driver" value="http://www.mamicode.com/${driver}"/>

<property name="JDBC.ConnectionURL" value="http://www.mamicode.com/${url}"/>

<property name="JDBC.Username" value="http://www.mamicode.com/${username}"/>

<property name="JDBC.Password" value="http://www.mamicode.com/${password}"/>

<property name ="Pool.MaximumActiveConnections" value ="http://www.mamicode.com/10" />

<property name ="Pool.MaximumIdleConnections" value ="http://www.mamicode.com/5" />

<property name ="Pool.MaximumCheckoutTime" value ="http://www.mamicode.com/120000" />

<property name ="TimeToWait" value ="http://www.mamicode.com/500" />

</dataSource>

</transactionManager>

<sqlMap resource="Student.xml"/>

</sqlMapConfig>

Department.xml文件:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd">

<sqlMap>

<typeAlias alias="Empdepartment" type="entity.Empdepartment"/>

<select id="selectAllDepartment" resultClass="Empdepartment">

select * from empdepartment </select>

</sqlMap>

SqlMap.properties文件:

driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:orcl
username=scott
password=tiger 

测试类Select.java:

package entity;

import java.io.IOException;

import java.io.Reader;

import java.sql.SQLException;

import java.util.List;

import com.ibatis.common.resources.Resources;

import com.ibatis.sqlmap.client.SqlMapClient;

import com.ibatis.sqlmap.client.SqlMapClientBuilder;

import entity.Empdepartment;

public class Select {  

private static SqlMapClient sqlMapClient = null;  

 static {   try {   Reader reader = Resources   .getResourceAsReader("SqlMapConfig.xml");

  sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);

  reader.close();   }

catch (IOException e)

{   e.printStackTrace();   }   }

public static  void main(String args[]){

  List<Empdepartment> students = null;

  try {   students = sqlMapClient.queryForList("selectAllDepartment");   }

catch (SQLException e)

{   e.printStackTrace();   }      }

}

iBATIS小例子(初学)