首页 > 代码库 > 【Mybaits学习】07_ 调用存储过程
【Mybaits学习】07_ 调用存储过程
查询得到男性或女性的数量, 如果传入的是0就女性否则是男性
1、创建表与存储过程
参数:IN `sex_id` int,OUT `user_count` int
2、创建表所对应的实体类
package nh.ui.automation.tools.Mybaits;/** * 项目 :UI自动化测试 Mybaits 类描述: * * @author Eric * @date 2017年3月5日 nh.ui.automation.tools.Mybaits */public class PUser { private int id; private String name; private String sex; /** * */ public PUser() { super(); } /* * (non-Javadoc) * * @see java.lang.Object#toString() */ @Override public String toString() { return "PUser [id=" + id + ", name=" + name + ", sex=" + sex + "]"; } /** * @param id * @param name * @param sex */ public PUser(int id, String name, String sex) { super(); this.id = id; this.name = name; this.sex = sex; } /** * @return the id */ public int getId() { return id; } /** * @param id * the id to set */ public void setId(int id) { this.id = id; } /** * @return the name */ public String getName() { return name; } /** * @param name * the name to set */ public void setName(String name) { this.name = name; } /** * @return the sex */ public String getSex() { return sex; } /** * @param sex * the sex to set */ public void setSex(String sex) { this.sex = sex; }}
3、创建puserMapper文件
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="nh.ui.automation.tools.Mybaits.User.puserMapper"> <select id="getCount" statementType="CALLABLE" parameterMap="getCountMap"> CALL mybaits.ges_user_count(?,?) </select> <parameterMap type="java.util.Map" id="getCountMap"> <parameter property="sex_id" mode="IN" jdbcType="INTEGER" /> <parameter property="user_count" mode="OUT" jdbcType="INTEGER" /> </parameterMap> <!-- 注意:此处定义的Map字段属性名必须与引用时一致 Map<String, Integer> paramMap = new HashMap<String, Integer>(); paramMap.put("sex_id", 0); paramMap.put("user_count", 0); --></mapper>
4、在Mybaits文件中注册puserMapper
’
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration> <properties resource="db.properties"></properties> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="${db.driver}" /> <property name="url" value="${db.url}" /> <property name="username" value="${db.username}" /> <property name="password" value="${db.password}" /> </dataSource> </environment> </environments> <mappers> <mapper resource="Mappings/userMapper.xml" /> <mapper resource="Mappings/orderMapper.xml" /> <mapper resource="Mappings/employMapper.xml" /> <mapper resource="Mappings/department2Mapper.xml" /> <mapper resource="Mappings/userMapper2.xml" /> <mapper class="nh.ui.automation.tools.mapper.UserMapper" /> </mappers></configuration>
5、测试代码
package nh.ui.automation.tools.Mybaits;import java.io.InputStream;import java.util.HashMap;import java.util.List;import java.util.Map;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;/** * Hello world! * */public class EmployeeTest { public static void main(String[] args) { /** * 1,加载mybaits的配置文件 2,构建sqlsession工厂 3,创建能执行sql的会话 4,映射sql的标识字符串 5,执行sql * 6,打印结果 */ String myBaitsConifg = "Mybaits.xml"; InputStream loadConfig = EmployeeTest.class.getClassLoader().getResourceAsStream(myBaitsConifg); SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(loadConfig); SqlSession sqlSession = sessionFactory.openSession(true); String statement = "nh.ui.automation.tools.Mybaits.User.puserMapper.getCount"; Map<String, Integer> paramMap = new HashMap<String, Integer>(); paramMap.put("sex_id", 1); paramMap.put("user_count", 0); sqlSession.selectOne(statement, paramMap); Integer integer = paramMap.get("user_count"); System.out.println(integer); }}
6、查看结果
2017-03-05 14:36:58,774 [main] DEBUG [nh.ui.automation.tools.Mybaits.User.puserMapper.getCount] - ==> Preparing: CALL mybaits.ges_user_count(?,?) 2017-03-05 14:36:58,811 [main] DEBUG [nh.ui.automation.tools.Mybaits.User.puserMapper.getCount] - ==> Parameters: 1(Integer)2
【Mybaits学习】07_ 调用存储过程
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。