ibatis3.0存储过存的一些细节。

1.因为支持注释和代码方式生成mapper,所以原来的 parameterMap反对使用,也就是不建议用parameter的顺序支对应"?" 方式的参数。

因为mapper的数据是一个纯xml的,而xml的元素如果没有标记为序列是没有顺序的,所以用

<parameterMap>

<parameter property="p1" .../>

<parameter property="p2" .../>

<parameterMap>

这样的顺序关系对应procedure(?,?)的参数是不可靠的。

所以3.0直接使用#{p1},#{p2,otherAttribute........}这样的命令参数可以直接和map的KEY以及BEAN的属性名称相匹配,所以应该使用

parameterType来指定一个绑定了命名参数的Bean或map.

2.直接看例子:

table: Person

id int(8) auto_increment pk

name varchar(20)

ago int(8)

SessionFactoryConf.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="org.gjt.mm.mysql.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/ibatis"/>
<property name="username" value="ibts_root"/>
<property name="password" value="coffee&amp;tea"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/axman/PersonMapper.xml"/>
</mappers>
</configuration>

a:无返回值

在navicat中建立一个存储过程,proc_test(pname,pago):

BEGIN
#Routine body goes here...
insert into Person (name,ago) values (pname,pago);
END

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="com.axman.PersonMapper">
<select id="getPerson" parameterType="int" resultType="com.axman.Person">
select * from Person where id = #{id}
</select>
<update id="execProcedure" statementType="CALLABLE" parameterType="com.axman.Person">
{ call proc_test(#{name},#{ago})}
</update>
</mapper>

String resource = "com/axman/SessionFactoryConf.xml";
Reader reader = Resources.getResourceAsReader(resource);
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = factory.openSession();
try {
// Map m = new HashMap();
// m.put("name","ibatisVersion");
// m.put("ago",40);
Person p = new Person();
p.setName("ibatisVerion");
p.setAgo(40);


session.update("com.axman.PersonMapper.execProcedure", p);
} finally {
session.close();
}

无返回值调用时使用update,不要用select[ForXXX],否则会死等

b:返回一个值,这例用返回Key来说只是说明返回单值,实际上的返回KEY可以直接注册AutoGenrateKeys获取,这里只是为了返回一个单一对象,和“select 1 ”一样。

BEGIN
#Routine body goes here...
insert into Person (name,ago) values (pname,pago);

select last_insrt_id();
END

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="com.axman.PersonMapper">
<select id="getPerson" parameterType="int" resultType="com.axman.Person">
select * from Person where id = #{id}
</select>
<select id="execProcedure" statementType="CALLABLE" parameterType="com.axman.Person">
{ call proc_test(#{name},#{ago})}
</select>
</mapper>

这时不需要指定resultTyep

String resource = "com/axman/SessionFactoryConf.xml";
Reader reader = Resources.getResourceAsReader(resource);
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = factory.openSession();
try {
// Map m = new HashMap();
// m.put("name","ibatisVersion");
// m.put("ago",40);
Person p = new Person();
p.setName("ibatisVerion");
p.setAgo(40);


Integer i = (Integer)session.selectOne("com.axman.PersonMapper.execProcedure", p);
} finally {
session.close();
}

c: 返回结果维集:

BEGIN
#Routine body goes here...
insert into Person (name,ago) values (pname,pago);

select * from Person;

END

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="com.axman.PersonMapper">
<select id="getPerson" parameterType="int" resultType="com.axman.Person">
select * from Person where id = #{id}
</select>
<select id="execProcedure" statementType="CALLABLE" parameterType="com.axman.Person" resultType="hashmap">
{ call proc_test(#{name},#{ago})}
</select>
</mapper>

这时需要指定resultTyep

String resource = "com/axman/SessionFactoryConf.xml";
Reader reader = Resources.getResourceAsReader(resource);
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = factory.openSession();
try {
// Map m = new HashMap();
// m.put("name","ibatisVersion");
// m.put("ago",40);
Person p = new Person();
p.setName("ibatisVerion");
p.setAgo(40);


List<map> l = session.selectList("com.axman.PersonMapper.execProcedure", p);

for(Map m : l){

System.out.println(m.get("id")+":"+m.get("name")+":"m.get("ago");

}
} finally {
session.close();
}

d:对于多结果集,需要修改源码。即使拦截注入也无法一次返回多结果和输出参数,因为多个结果和输出参数需要一个对象做为容器把它们同时返回出来,所以只能修改它的源代码。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值