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',"男");
insert into p_user(name,sex) values('B',"女");
insert into p_user(name,sex) values('C',"男");
-- 创建存储过程(查询得到男性或女性的数量, 如果传入的是0就女性否则是男性)
DELIMITER $
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;
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(1, @user_count);
SELECT @user_count;
2.userMapper.xml:
<?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">
<!-- 以下namespace名字可以随便写,但是一般都写成实体类的名字-->
<mapper namespace="mapper.userMapper">
<!-- resultMap标签:映射实体与表
type属性:表示实体全路径名
id属性:为实体与表的映射取一个任意的唯一的名字
-->
<select id="getUserCount" parameterMap="getUserCountMap" statementType="CALLABLE">
CALL mybatis.ges_user_count(?,?)
</select>
<!--
parameterMap.put("sexid", 0);
parameterMap.put("usercount", -1);
-->
<parameterMap type="java.util.Map" id="getUserCountMap">
<parameter property="sexid" mode="IN" jdbcType="INTEGER"/>
<parameter property="usercount" mode="OUT" jdbcType="INTEGER"/>
</parameterMap>
</mapper>
3.mybatis.xml
<?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"/>-->
<!-- 环境配置 -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<!-- 数据库连接相关配置 ,这里动态获取config.properties文件中的内容-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8&useSSL=false" />
<property name="username" value="root" />
<property name="password" value="wscjwacs" />
</dataSource>
<!--<dataSource type="pooled">-->
<!--<!– 配置与数据库交互的4个必要属性 –>-->
<!--<property name="driver" value="${mysql.driver}"/>-->
<!--<property name="url" value="${mysql.url}"/>-->
<!--<property name="username" value="${mysql.username}"/>-->
<!--<property name="password" value="${mysql.password}"/>-->
<!--</dataSource>-->
</environment>
</environments>
<!-- mapping文件路径配置 -->
<mappers>
<mapper resource="mapper/userMapper.xml"/>
</mappers>
<!-- 在配置文件中 关联包下的 接口类-->
<!--<mappers>-->
<!--<mapper class="com.flyz.UserMapper"/>-->
<!--</mappers>-->
</configuration>
4测试代码如下:
@Test
public void Process(){
SqlSessionFactory factory=MybatisUtils.getFactory();
SqlSession session=factory.openSession(true);
/**
* 映射sql的标识字符串,
* me.gacl.mapping.userMapper是userMapper.xml文件中mapper标签的namespace属性的值,
* getUserCount是select标签的id属性值,通过select标签的id属性值就可以找到要执行的SQL
*/
String statement = "mapper.userMapper.getUserCount";//映射sql的标识字符串
Map<String, Integer> parameterMap = new HashMap<String, Integer>();
parameterMap.put("sexid", 1);
parameterMap.put("usercount", -1);
session.selectOne(statement, parameterMap);
Integer result = parameterMap.get("usercount");
System.out.println(result);
session.close();
}
MybatisUtils.java
package com.flyz;
import java.io.InputStream;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class MybatisUtils {
public static SqlSessionFactory getFactory(){
String resource="mybatis.xml";
//加载mybatis 的配置文件(它也加载关联的映射文件)
InputStream is=MybatisUtils.class.getClassLoader().getResourceAsStream(resource);
//构建sqlSession 的工厂
SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(is);
return factory;
}
}
测试结果如下:
hhhhh.png
网友评论