参考了官方的 iBATIS-SqlMaps-2-Tutorial_cn.pdf
注意 6中一定要加上try catch 否则会报Unhandled exception type SQLException错误
1.Person.java
根据自己的数据库表制作Person.java,他也可能是User.java,Company.java等等
2.Person.java
Code
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> 1<?xml version="1.0" encoding="UTF-8"?>
2
3<!DOCTYPE sqlMap
4 PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
5 "http://www.ibatis.com/dtd/sql-map-2.dtd">
6
7<sqlMap namespace="Person">
8
9 <select id="getPerson" parameterClass="int" resultClass="com.xc.ibatis.Person">
10 <![CDATA[
11 select
12 *
13 from Person
14 WHERE id = #value#
15 ]]>
16 </select>
17
18</sqlMap>
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> 1<?xml version="1.0" encoding="UTF-8"?>
2
3<!DOCTYPE sqlMap
4 PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
5 "http://www.ibatis.com/dtd/sql-map-2.dtd">
6
7<sqlMap namespace="Person">
8
9 <select id="getPerson" parameterClass="int" resultClass="com.xc.ibatis.Person">
10 <![CDATA[
11 select
12 *
13 from Person
14 WHERE id = #value#
15 ]]>
16 </select>
17
18</sqlMap>
3.SqlMapConfig.xml
Code
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> 1<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2<!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN" "http://www.ibatis.com/dtd/sql-map-config-2.dtd">
3
4<sqlMapConfig>
5
6 <properties resource="SqlMapConfig.properties" />
7 <settings
8 cacheModelsEnabled="true"
9 lazyLoadingEnabled="true"
10 enhancementEnabled="true"
11 maxSessions="64"
12 maxTransactions="8"
13 maxRequests="128" />
14
15 <transactionManager type="JDBC">
16 <dataSource type="SIMPLE">
17 <property value="${driver}" name="JDBC.Driver" />
18 <property value="${url}" name="JDBC.ConnectionURL" />
19 <property value="${username}" name="JDBC.Username" />
20 <property value="${password}" name="JDBC.Password" />
21 <property value="15" name="Pool.MaximumActiveConnections" />
22 <property value="15" name="Pool.MaximumIdleConnections" />
23 <property value="1000" name="Pool.MaximumWait" />
24 </dataSource>
25 </transactionManager>
26
27 <sqlMap resource="com/xc/ibatis/Person.xml" />
28
29</sqlMapConfig>
30
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> 1<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2<!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN" "http://www.ibatis.com/dtd/sql-map-config-2.dtd">
3
4<sqlMapConfig>
5
6 <properties resource="SqlMapConfig.properties" />
7 <settings
8 cacheModelsEnabled="true"
9 lazyLoadingEnabled="true"
10 enhancementEnabled="true"
11 maxSessions="64"
12 maxTransactions="8"
13 maxRequests="128" />
14
15 <transactionManager type="JDBC">
16 <dataSource type="SIMPLE">
17 <property value="${driver}" name="JDBC.Driver" />
18 <property value="${url}" name="JDBC.ConnectionURL" />
19 <property value="${username}" name="JDBC.Username" />
20 <property value="${password}" name="JDBC.Password" />
21 <property value="15" name="Pool.MaximumActiveConnections" />
22 <property value="15" name="Pool.MaximumIdleConnections" />
23 <property value="1000" name="Pool.MaximumWait" />
24 </dataSource>
25 </transactionManager>
26
27 <sqlMap resource="com/xc/ibatis/Person.xml" />
28
29</sqlMapConfig>
30
4.SqlMapConfig.properties
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->username=user
password=pass
url=jdbc:oracle:thin:@192.168.0.78:1521:ora9i
driver=oracle.jdbc.driver.OracleDriver
5.MyAppSqlConfig.java
Code
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> 1package com.xc.ibatis;
2
3import java.io.Reader;
4
5import com.ibatis.common.resources.Resources;
6import com.ibatis.sqlmap.client.SqlMapClient;
7import com.ibatis.sqlmap.client.SqlMapClientBuilder;
8
9public class MyAppSqlConfig {
10 private static final SqlMapClient sqlMap;
11
12 static{
13 try{
14 String resource = "SqlMapConfig.xml";
15 Reader reader = Resources.getResourceAsReader(resource);
16 sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
17 reader.close();
18 }catch(Exception e){
19 e.printStackTrace();
20 throw new RuntimeException("Error initializing MyAppSqlConfig class. Cause:"+ e);
21 }
22 }
23
24 public static SqlMapClient getSqlMapInstance(){
25 return sqlMap;
26 }
27}
28
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> 1package com.xc.ibatis;
2
3import java.io.Reader;
4
5import com.ibatis.common.resources.Resources;
6import com.ibatis.sqlmap.client.SqlMapClient;
7import com.ibatis.sqlmap.client.SqlMapClientBuilder;
8
9public class MyAppSqlConfig {
10 private static final SqlMapClient sqlMap;
11
12 static{
13 try{
14 String resource = "SqlMapConfig.xml";
15 Reader reader = Resources.getResourceAsReader(resource);
16 sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
17 reader.close();
18 }catch(Exception e){
19 e.printStackTrace();
20 throw new RuntimeException("Error initializing MyAppSqlConfig class. Cause:"+ e);
21 }
22 }
23
24 public static SqlMapClient getSqlMapInstance(){
25 return sqlMap;
26 }
27}
28
6.testibatis.java
Code
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> 1package Test;
2
3import com.ibatis.sqlmap.client.SqlMapClient;
4import com.xc.ibatis.*;
5
6public class testibatis {
7
8 /** *//**
9 * @param args
10 */
11 public static void main(String[] args) {
12 // TODO Auto-generated method stub
13 SqlMapClient sqlMap = MyAppSqlConfig.getSqlMapInstance();
14 try{
15
16 Integer Orgid = new Integer(3);
17 Person person = (Person)sqlMap.queryForObject("getPerson", id);
18 System.out.println(person.getName()+ " " + person.getAge());
19 }catch(Exception e){
20 e.printStackTrace();
21 throw new RuntimeException("Error testibatis Cause:"+ e);
22 }
23 }
24
25}
26
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> 1package Test;
2
3import com.ibatis.sqlmap.client.SqlMapClient;
4import com.xc.ibatis.*;
5
6public class testibatis {
7
8 /** *//**
9 * @param args
10 */
11 public static void main(String[] args) {
12 // TODO Auto-generated method stub
13 SqlMapClient sqlMap = MyAppSqlConfig.getSqlMapInstance();
14 try{
15
16 Integer Orgid = new Integer(3);
17 Person person = (Person)sqlMap.queryForObject("getPerson", id);
18 System.out.println(person.getName()+ " " + person.getAge());
19 }catch(Exception e){
20 e.printStackTrace();
21 throw new RuntimeException("Error testibatis Cause:"+ e);
22 }
23 }
24
25}
26