SqlMapConfig.xml
1
<?
xml version="1.0" encoding="UTF-8"
?>
2 <! DOCTYPE sqlMapConfig
3 PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
4 "http://ibatis.apache.org/dtd/sql-map-config-2.dtd" >
5 < sqlMapConfig >
6 < properties resource ="com/sigon/ibatis/SqlMapConfig.properties" />
7 < settings cacheModelsEnabled ="true" enhancementEnabled ="true" lazyLoadingEnabled ="true" errorTracingEnabled ="true" maxRequests ="32" maxSessions ="10" maxTransactions ="5" useStatementNamespaces ="false" />
8
9 < transactionManager type ="JDBC" commitRequired ="false" >
10 < dataSource type ="SIMPLE" >
11 < property name ="JDBC.Driver" value ="${driver}" />
12 < property name ="JDBC.ConnectionURL" value ="${url}" />
13 < property name ="JDBC.Username" value ="${username}" />
14 < property name ="JDBC.Password" value ="${password}" />
15 < property name ="Pool.MaximumActiveConnections" value ="10" />
16 < property name ="Pool.MaximumIdleConnections" value ="5" />
17 < property name ="Pool.MaximumWait" value ="60000" />
18 </ dataSource >
19 </ transactionManager >
20 < sqlMap resource ="com/sigon/ibatis/maps/User.xml" />
21 </ sqlMapConfig >
22
user.xml
2 <! DOCTYPE sqlMapConfig
3 PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
4 "http://ibatis.apache.org/dtd/sql-map-config-2.dtd" >
5 < sqlMapConfig >
6 < properties resource ="com/sigon/ibatis/SqlMapConfig.properties" />
7 < settings cacheModelsEnabled ="true" enhancementEnabled ="true" lazyLoadingEnabled ="true" errorTracingEnabled ="true" maxRequests ="32" maxSessions ="10" maxTransactions ="5" useStatementNamespaces ="false" />
8
9 < transactionManager type ="JDBC" commitRequired ="false" >
10 < dataSource type ="SIMPLE" >
11 < property name ="JDBC.Driver" value ="${driver}" />
12 < property name ="JDBC.ConnectionURL" value ="${url}" />
13 < property name ="JDBC.Username" value ="${username}" />
14 < property name ="JDBC.Password" value ="${password}" />
15 < property name ="Pool.MaximumActiveConnections" value ="10" />
16 < property name ="Pool.MaximumIdleConnections" value ="5" />
17 < property name ="Pool.MaximumWait" value ="60000" />
18 </ dataSource >
19 </ transactionManager >
20 < sqlMap resource ="com/sigon/ibatis/maps/User.xml" />
21 </ sqlMapConfig >
22
1
<?
xml version="1.0" encoding="UTF-8"
?>
2 <! DOCTYPE sqlMap
3 PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
4 "http://ibatis.apache.org/dtd/sql-map-2.dtd" >
5 < sqlMap namespace ="User" >
6
7 < typeAlias alias ="user" type ="com.sigon.ibatis.User" />
8 < select id ="getUser" parameterClass ="java.lang.String" resultClass ="user" >
9 <![CDATA[
10 select
11 name,
12 sex
13 from t_user
14 where name = #name#
15 ]]>
16 </ select >
17
18 < select id ="getAllUser" resultClass ="user" >
19 <![CDATA[
20 select
21 id,
22 name,
23 sex
24 from t_user
25 ]]>
26 </ select >
27
28 < update id ="updateUser" parameterClass ="user" >
29 <![CDATA[
30 UPDATE t_user SET name=#name#, sex=#sex# WHERE id = #id# ]]>
31 </ update >
32
33 < insert id ="insertUser" parameterClass ="user" > INSERT INTO t_user ( name, sex) VALUES ( #name#, #sex# ) </ insert >
34
35 < delete id ="deleteUser" parameterClass ="java.lang.String" > delete from t_user where id=#value# </ delete >
36 <img src="http://www.blogjava.n
App.java
2 <! DOCTYPE sqlMap
3 PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
4 "http://ibatis.apache.org/dtd/sql-map-2.dtd" >
5 < sqlMap namespace ="User" >
6
7 < typeAlias alias ="user" type ="com.sigon.ibatis.User" />
8 < select id ="getUser" parameterClass ="java.lang.String" resultClass ="user" >
9 <![CDATA[
10 select
11 name,
12 sex
13 from t_user
14 where name = #name#
15 ]]>
16 </ select >
17
18 < select id ="getAllUser" resultClass ="user" >
19 <![CDATA[
20 select
21 id,
22 name,
23 sex
24 from t_user
25 ]]>
26 </ select >
27
28 < update id ="updateUser" parameterClass ="user" >
29 <![CDATA[
30 UPDATE t_user SET name=#name#, sex=#sex# WHERE id = #id# ]]>
31 </ update >
32
33 < insert id ="insertUser" parameterClass ="user" > INSERT INTO t_user ( name, sex) VALUES ( #name#, #sex# ) </ insert >
34
35 < delete id ="deleteUser" parameterClass ="java.lang.String" > delete from t_user where id=#value# </ delete >
36 <img src="http://www.blogjava.n
1
package
com.sigon.ibatis;
2
3 import java.sql.SQLException;
4 import java.util.List;
5
6 import com.sigon.ibatis.User;
7 import com.ibatis.sqlmap.client.SqlMapClientBuilder;
8
9 /** */ /**
10 *
11 * @author zhupan
12 */
13 public class App {
14
15 public static void update() {
16 //首先初始化iBatis获得一个SqlMapClient对象
17 String resource = "com/sigon/ibatis/maps/SqlMapConfig.xml";
18 com.ibatis.sqlmap.client.SqlMapClient sqlMap = null;
19 try {
20 java.io.Reader reader = com.ibatis.common.resources.Resources
21 .getResourceAsReader(resource);
22 sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
23 } catch (Exception e) {
24 e.printStackTrace();
25 }
26 // sqlMap系统初始化完毕,开始执行update操作
27 try {
28 sqlMap.startTransaction();
29 User user = new User();
30 user.setId(new Integer(1));
31 user.setName("zhupan");
32 user.setSex(new Integer(1));
33 sqlMap.update("updateUser", user);
34 sqlMap.commitTransaction();
35 } catch (SQLException e) {
36 System.out.println(e.getMessage());
37 } finally {
38 try {
39 sqlMap.endTransaction();
40 } catch (SQLException e) {
41 e.printStackTrace();
42 }
43 }
44 }
45 public static List getUser() {
46// 首先初始化iBatis获得一个SqlMapClient对象
47 String resource = "com/sigon/ibatis/maps/SqlMapConfig.xml";
48 com.ibatis.sqlmap.client.SqlMapClient sqlMap = null;
49 List list=null;
50 try {
51 java.io.Reader reader = com.ibatis.common.resources.Resources
52 .getResourceAsReader(resource);
53 sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
54 } catch (Exception e) {
55 e.printStackTrace();
56 }
57 // sqlMap系统初始化完毕,开始执行getAllUser操作
58 try {
59 sqlMap.startTransaction();
60 list=sqlMap.queryForList("getAllUser", null);
61 sqlMap.commitTransaction();
62 } catch (SQLException e) {
63 System.out.println(e.getMessage());
64 } finally {
65 try {
66 sqlMap.endTransaction();
67 } catch (SQLException e) {
68 e.printStackTrace();
69 }
70 }
71 return list;
72 }
73 public static void main(String[] args) {
74 //update();
75 List list=getUser();
76 User u = null;
77 for(int i=0;i<list.size(); i++)
78 {
79 u = (User)list.get(i);
80 System.out.println(u.getId()+" "+u.getName()+" "+u.getSex());
81 }
82 }
83}
2
3 import java.sql.SQLException;
4 import java.util.List;
5
6 import com.sigon.ibatis.User;
7 import com.ibatis.sqlmap.client.SqlMapClientBuilder;
8
9 /** */ /**
10 *
11 * @author zhupan
12 */
13 public class App {
14
15 public static void update() {
16 //首先初始化iBatis获得一个SqlMapClient对象
17 String resource = "com/sigon/ibatis/maps/SqlMapConfig.xml";
18 com.ibatis.sqlmap.client.SqlMapClient sqlMap = null;
19 try {
20 java.io.Reader reader = com.ibatis.common.resources.Resources
21 .getResourceAsReader(resource);
22 sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
23 } catch (Exception e) {
24 e.printStackTrace();
25 }
26 // sqlMap系统初始化完毕,开始执行update操作
27 try {
28 sqlMap.startTransaction();
29 User user = new User();
30 user.setId(new Integer(1));
31 user.setName("zhupan");
32 user.setSex(new Integer(1));
33 sqlMap.update("updateUser", user);
34 sqlMap.commitTransaction();
35 } catch (SQLException e) {
36 System.out.println(e.getMessage());
37 } finally {
38 try {
39 sqlMap.endTransaction();
40 } catch (SQLException e) {
41 e.printStackTrace();
42 }
43 }
44 }
45 public static List getUser() {
46// 首先初始化iBatis获得一个SqlMapClient对象
47 String resource = "com/sigon/ibatis/maps/SqlMapConfig.xml";
48 com.ibatis.sqlmap.client.SqlMapClient sqlMap = null;
49 List list=null;
50 try {
51 java.io.Reader reader = com.ibatis.common.resources.Resources
52 .getResourceAsReader(resource);
53 sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
54 } catch (Exception e) {
55 e.printStackTrace();
56 }
57 // sqlMap系统初始化完毕,开始执行getAllUser操作
58 try {
59 sqlMap.startTransaction();
60 list=sqlMap.queryForList("getAllUser", null);
61 sqlMap.commitTransaction();
62 } catch (SQLException e) {
63 System.out.println(e.getMessage());
64 } finally {
65 try {
66 sqlMap.endTransaction();
67 } catch (SQLException e) {
68 e.printStackTrace();
69 }
70 }
71 return list;
72 }
73 public static void main(String[] args) {
74 //update();
75 List list=getUser();
76 User u = null;
77 for(int i=0;i<list.size(); i++)
78 {
79 u = (User)list.get(i);
80 System.out.println(u.getId()+" "+u.getName()+" "+u.getSex());
81 }
82 }
83}