mybatis调用存储过程

  • 格式:txt
  • 大小:2.19 KB
  • 文档页数:1
import com.util.MybatisUtil;
public class Test8 {
public static void main(String[] args) {
SqlSessionFactory factory=MybatisUtil.getFactory();
ELSE
SELECT COUNT(*) FROM mybatis.p_user WHERE p_user.sex='男' INTO user_count;
END IF;
END
$
#调用存储过程
DELIMITER ;
SET @user_count = 0;
CALL mybatis.ges_user_count(0, @user_count);
insert into p_user(name,sex) values('B',"女");
insert into p_user(name,sex) values('C',"男");
#创建存储过程(查询得到男性或女性的数量, 如果传入的是0就女性否则是男性)
DELIMITER $
<parameterproperty="user_count"mode="OUT"jdbcType="INTEGER"/>
</parameterMap>
</mapper>
4.在conf.xml中ห้องสมุดไป่ตู้置mapper.xml
<mapper resource="com/fanjunkai9/UserMapper.xml"/>
paraMap.put("sex_id", 0);
paraMap.put("user_count", -1);
session.selectOne(stm, paraMap);
Integer userCount=paraMap.get("user_count");
session.close();
1.创建存储过程
create table p_user(
id int primary key auto_increment,
name varchar(10),
sex char(2)
);
insert into p_user(name,sex) values('A',"男");
System.out.println(userCount);
}
}
<!--
查询得到男性或女性的数量, 如果传入的是0就女性否则是男性
CALL mybatis.get_user_count(1, @user_count);
-->
<selectid="getCount"statementType="CALLABLE"parameterMap="getCountMap">
CREATE PROCEDURE mybatis.ges_user_count(IN sex_id INT, OUT user_count INT)
BEGIN
IF sex_id=0 THEN
SELECT COUNT(*) FROM mybatis.p_user WHERE p_user.sex='女' INTO user_count;
SELECT @user_count;
2.创建实体类
public class User {
private String id;
private String name;
private String sex;
}
3.创建mapper.xml
<mappernamespace="erMapper">
SqlSession session=factory.openSession(true);
String stm="erMapper.getCount";
Map<String,Integer> paraMap=new HashMap<String,Integer>();
5.测试类
package com.fanjunkai8;
import java.util.HashMap;
import java.util.Map;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
call mybatis.get_user_count(?,?)
</select>
<parameterMaptype="java.util.Map"id="getCountMap">
<parameterproperty="sex_id"mode="IN"jdbcType="INTEGER"/>

下载文档原格式

  / 1