1 ibatis 调用oracle函数示例
2
3
4
5 利用空余时间写的一个简单例子,数据库是oracle10g.
6
7
8
9 配置文件:
10
11 OracleSqlMapConfig.xml
12 Xml代码
13
14 1 . <? xml version = " 1.0 " encoding = " UTF-8 " ?>
15 2 .
16 3 . <! DOCTYPE sqlMapConfig
17 4 . PUBLIC " -//ibatis.apache.org//DTD SQL Map Config 2.0//EN "
18 5 . " http://ibatis.apache.org/dtd/sql-map-config-2.dtd " >
19 6 .
20 7 . < sqlMapConfig >
21 8 .
22 9 . <!-- Configure a built - in transaction manager. If you ' re using an
23 10 . app server, you probably want to use its transaction manager
24 11 . and a managed datasource -->
25 12 . < transactionManager type = " JDBC " commitRequired = " false " >
26 13 . < dataSource type = " SIMPLE " >
27 14 . < property name = " JDBC.Driver "
28 15 . value = " oracle.jdbc.driver.OracleDriver " />
29 16 . < property name = " JDBC.ConnectionURL "
30 17 . value = " jdbc:oracle:thin:@localhost:1521:ORCL " />
31 18 . < property name = " JDBC.Username " value = " scott " />
32 19 . < property name = " JDBC.Password " value = " tiger " />
33 20 . < property name = " Pool.MaximumActiveConnections " value = " 10 " />
34 21 . < property name = " Pool.MaximumIdleConnections " value = " 5 " />
35 22 . < property name = " Pool.MaximumCheckoutTime " value = " 120000 " />
36 23 . < property name = " Pool.TimeToWait " value = " 500 " />
37 24 . < property name = " Pool.PingEnabled " value = " false " />
38 25 . < property name = " Pool.PingConnectionsOlderThan " value = " 1 " />
39 26 . < property name = " Pool.PingConnectionsNotUsedFor " value = " 1 " />
40 27 . </ dataSource >
41 28 . </ transactionManager >
42 29 .
43 30 . < sqlMap resource = " com/Emp.xml " />
44 31 .
45 32 .
46 33 . </ sqlMapConfig >
47
48 <? xml version = " 1.0 " encoding = " UTF-8 " ?>
49
50 <! DOCTYPE sqlMapConfig
51 PUBLIC " -//ibatis.apache.org//DTD SQL Map Config 2.0//EN "
52 " http://ibatis.apache.org/dtd/sql-map-config-2.dtd " >
53
54 < sqlMapConfig >
55
56 <!-- Configure a built - in transaction manager. If you ' re using an
57 app server, you probably want to use its transaction manager
58 and a managed datasource -->
59 < transactionManager type = " JDBC " commitRequired = " false " >
60 < dataSource type = " SIMPLE " >
61 < property name = " JDBC.Driver "
62 value = " oracle.jdbc.driver.OracleDriver " />
63 < property name = " JDBC.ConnectionURL "
64 value = " jdbc:oracle:thin:@localhost:1521:ORCL " />
65 < property name = " JDBC.Username " value = " scott " />
66 < property name = " JDBC.Password " value = " tiger " />
67 < property name = " Pool.MaximumActiveConnections " value = " 10 " />
68 < property name = " Pool.MaximumIdleConnections " value = " 5 " />
69 < property name = " Pool.MaximumCheckoutTime " value = " 120000 " />
70 < property name = " Pool.TimeToWait " value = " 500 " />
71 < property name = " Pool.PingEnabled " value = " false " />
72 < property name = " Pool.PingConnectionsOlderThan " value = " 1 " />
73 < property name = " Pool.PingConnectionsNotUsedFor " value = " 1 " />
74 </ dataSource >
75 </ transactionManager >
76
77 < sqlMap resource = " com/Emp.xml " />
78
79
80 </ sqlMapConfig >
81
82
83
84 Emp.xml
85 Xml代码
86
87 1 . <? xml version = " 1.0 " encoding = " UTF-8 " ?>
88 2 .
89 3 . <! DOCTYPE sqlMap
90 4 . PUBLIC " -//ibatis.apache.org//DTD SQL Map 2.0//EN "
91 5 . " http://ibatis.apache.org/dtd/sql-map-2.dtd " >
92 6 .
93 7 . < sqlMap namespace = " Emp " >
94 8 .
95 9 . <!-- 取别名 -->
96 10 . < typeAlias alias = " emp " type = " com.Emp " />
97 11 .
98 12 . < resultMap id = " emp-map " class = " emp " >
99 13 . < result property = " ename " column = " ENAME " />
100 14 . </ resultMap >
101 15 .
102 16 . < parameterMap id = " test " class = " java.util.HashMap " >
103 17 . < parameter property = " result " jdbcType = " ORACLECURSOR " javaType = " java.sql.ResultSet " mode = " OUT " resultMap = " emp-map " />
104 18 . < parameter property = " param1 " jdbcType = " VARCHAR " javaType = " java.lang.String " mode = " IN " />
105 19 . </ parameterMap >
106 20 .
107 21 . < procedure id = " testCur " parameterMap = " test " >
108 22 .
{ ? = call selectemp( ? )}
109 23 . </ procedure >
110 24 .
111 25 . </ sqlMap >
112
139 数据库脚本:
140 Sql代码
152 create or replace function scott.selectemp(
153 param1 in varchar2
154 ) return sys_refcursor
155 is
156 outcursor sys_refcursor;
157 begin
158 open outcursor for select ename from scott.emp where emp.ename like param1 ;
159 return outcursor;
160 end;
161
162 pojo类:
163 Java代码
164
165 1 . package com;
166 2 .
167 3 . public class Emp
{
168 4 .
169 5 . private int empno;
170 6 .
171 7 . private String ename;
172 8 .
173 9 . private String job;
174 10 .
175 11 . private float sal;
176 12 .
177 13 . private float comm;
178 14 .
179 15 . public float getComm()
{
180 16 . return comm;
181 17 . }
182 18 .
183 19 . public void setComm( float comm)
{
184 20 . this .comm = comm;
185 21 . }
186 22 .
187 23 . public int getEmpno()
{
188 24 . return empno;
189 25 . }
190 26 .
191 27 . public void setEmpno( int empno)
{
192 28 . this .empno = empno;
193 29 . }
194 30 .
195 31 . public String getEname()
{
196 32 . return ename;
197 33 . }
198 34 .
199 35 . public void setEname(String ename)
{
200 36 . this .ename = ename;
201 37 . }
202 38 .
203 39 . public String getJob()
{
204 40 . return job;
205 41 . }
206 42 .
207 43 . public void setJob(String job)
{
208 44 . this .job = job;
209 45 . }
210 46 .
211 47 . public float getSal()
{
212 48 . return sal;
213 49 . }
214 50 .
215 51 . public void setSal( float sal)
{
216 52 . this .sal = sal;
217 53 . }
218 54 .
219 55 . }
220
280
281 测试代码:
282 Java代码
283
284 1 . Reader reader = Resources.getResourceAsReader( " com/OracleSqlMapConfig.xml " );
285 2 . SqlMapClient client = SqlMapClientBuilder.buildSqlMapClient(reader);
286 3 . reader.close();
287 4 . Map < String, Object > p = new HashMap < String, Object > ();
288 5 . p.put( " param1 " , " %S% " );
289 6 . client.queryForObject( " testCur " , p);
290 7 . List list = (List) p.get( " result " );
291 8 . for ( int i = 0 ; i < list.size(); i ++ )
{
292 9 . Emp emp = (Emp) list.get(i);
293 10 . System.out.println(emp.getEname());
294 11 . }
ibatis存储过程调用(转载)
最新推荐文章于 2024-08-16 23:08:28 发布