StoredProcedure是一个抽象类,必须写一个子类来继承它,这个类是用来简化JDBCTemplate执行存储过程 *** 作的。
首先我们写一个实现类:
<span >import<span > java.sql.Types;<span >import<span > java.util.HashMap;
<span >import<span > java.util.Map;
<span >import<span > org.springframework.jdbc.core.RowMapper;
<span >import<span > org.springframework.jdbc.core.sqlOutParameter;
<span >import<span > org.springframework.jdbc.core.sqlParameter;
<span >import<span > org.springframework.jdbc.core.sqlReturnResultSet;
<span >import<span > org.springframework.jdbc.object.StoredProcedure;
<span >/**<span >
name:StoredProcedureTemplate User: HP Date: 2007-7-21 Time: 7:40:01
Description:
<span >*/
<span >public <span >class StoredProcedureTemplate <span >extends<span > StoredProcedure {
<span >private HashMap<String,Object> map = <span >new HashMap<String,Object><span >();
<span >public<span > StoredProcedureTemplate() {
<span >super<span >();
}
<span >public<span > HashMap getMap()
{
<span >return <span >this<span >.map;
}
<span >public <span >voID<span > setValue(String key,Object obj) {
map.put(key,obj);
}
<span >public<span > Map execute() {
<span >if (<span >this.getsql() == <span >null || <span >this.getsql().equals(""<span >))
<span >return <span >null<span >;
<span >this<span >.compile();
<span >return<span > execute(map);
}
<span >public <span >voID<span > setvarcharParam(String param) {
<span >this.declareParameter(<span >new<span > sqlParameter(param,Types.VARCHAR));
}
<span >public <span >voID<span > setDoubleParam(String param) {
<span >this.declareParameter(<span >new<span > sqlParameter(param,Types.DOUBLE));
}
<span >public <span >voID<span > setIntegerParam(String param) {
<span >this.declareParameter(<span >new<span > sqlParameter(param,Types.INTEGER));
}
<span >public <span >voID<span > setvarcharOutParam(String param) {
<span >this.declareParameter(<span >new<span > sqlOutParameter(param,Types.VARCHAR));
}
<span >public <span >voID<span > setDoubleOutParam(String param) {
<span >this.declareParameter(<span >new<span > sqlOutParameter(param,Types.DOUBLE));
}
<span >public <span >voID<span > setIntegerOutParam(String param) {
<span >this.declareParameter(<span >new<span > sqlOutParameter(param,Types.INTEGER));
}
<span >public <span >voID setInParam(String param,<span >int<span > valueType)
{
<span >this.declareParameter(<span >new<span > sqlParameter(param,valueType));
}
<span >public <span >voID setoutParam(String param,<span >int<span > valueType)
{
<span >this.declareParameter(<span >new<span > sqlOutParameter(param,valueType));
}
<span >public <span >voID<span > setReturnParam(String param,RowMapper rowMapper) {
<span >this.declareParameter(<span >new<span > sqlReturnResultSet(param,rowMapper));
}
}
写一个测试:
= "classpath:spring/applicationContext-base.xml"= (JdbcTemplate) context.getBean("jdbcTemplate" StoredProcedureTemplate template </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">new</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> StoredProcedureTemplate(); template.setJdbcTemplate(jdbc); template.set<a href="https://www.jb51.cc/tag/sql/" target="_blank" >sql</a>(</span>"testproc"<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">注意有返回结果集的时候,第<a href="https://www.jb51.cc/tag/yige/" target="_blank" >一个</a><a href="https://m.jb51.cc/tag/canshu/" target="_blank" >参数</a>必须<a href="https://m.jb51.cc/tag/shezhi/" target="_blank" >设置</a>为返回结果集<a href="https://m.jb51.cc/tag/canshu/" target="_blank" >参数</a>,不然会<a href="https://m.jb51.cc/tag/baocuo/" target="_blank" >报错</a>。</span> template.setReturnP<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>m("rows",<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">new</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> F<a href="https://www.jb51.cc/tag/irs/" target="_blank" >irs</a>tReportRowMapper()); template.setIntegerP<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>m(</span>"@p<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>ma"<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">); template.setValue(</span>"@p<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>ma",9<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">); Map map </span>=<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> template.execute(); Object o </span>= map.get("rows"<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">); <a href="https://m.jb51.cc/tag/List/" target="_blank" >List</a></span><FirstReportVO> <a href="https://m.jb51.cc/tag/List/" target="_blank" >List</a> = (<a href="https://m.jb51.cc/tag/List/" target="_blank" >List</a><FirstReportVO><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">)o; </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">for</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> (F<a href="https://www.jb51.cc/tag/irs/" target="_blank" >irs</a>tReportVO vo : <a href="https://m.jb51.cc/tag/List/" target="_blank" >List</a>) { Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" >stem</a>.out.println(vo.getSort<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a>()</span>+","+<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">vo.getSort<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a>()); }}</span></pre>
唯一要注意的地方就是测试里备注的地方,我测试了好久才发现,郁闷的一塌糊涂,老是莫名其妙的错,原来将参数互换一下位置就OK了,比如你把
template.setIntegerParam("@parama");写在前面然后再写template.setReturnParam("rows",new FirstReportRowMapper());的话,就会报空指针错误。
这个“rows”可以随便取名字,不过下面map.get("rows")要和你取的名字一致,因为StoredProcedureTemplate会将结果集以这个名字保存在map中返回。
还有要注意的就是设置sqlparamter的顺序要和存储过程中参数的顺序要一致,不然也会报错.
原文:http://blog.csdn.net/xiao_jun_0820/article/details/7268219
http://forum.spring.io/forum/spring-projects/data/13984-multiple-calls-to-storedprocedure-using-same-connection
总结以上是内存溢出为你收集整理的spring 中StoredProcedure的用法--转载全部内容,希望文章能够帮你解决spring 中StoredProcedure的用法--转载所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)