MyBatis高级查询 存储过程

1.第一个存储过程  根据用户id查询用户其他信息

#第一个存储过程
#根据用户id查询用户其他信息
DROP PROCEDURE IF EXISTS `select_user_by_id`;
DELIMITER ;;
CREATE PROCEDURE `select_user_by_id` (
    IN userId BIGINT,
    OUT userName VARCHAR (50),
    OUT userPassword VARCHAR (50),
    OUT userEmail VARCHAR (50),
    OUT userInfo TEXT,
    OUT headImg BLOB,
    OUT createTime DATETIME
)
BEGIN
    #根据用户id查询其他数据
    SELECT user_name,user_password,user_email,user_info,head_img,create_time 
    INTO userName,userPassword,userEmail,userInfo,headImg,createTime
    FROM sys_user
    WHERE id = userId;
END
;;
DELIMITER ;
#sql中调用
SET @userId = 1;

CALL select_user_by_id (
    @userId ,@userName ,@userPassword ,@userEmail,@userInfo,@headImg,@createTime
);

SELECT
    @userName AS userName ,@userPassword AS userPassword,@userEmail as userEmail,@userInfo as userInfo,@headImg as headImg,@createTime as createTime;

 

SysUserMapper.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">
<mapper namespace="test.dao.SysUserMapper">
  <resultMap id="BaseResultMap" type="test.model.SysUser">
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="user_password" jdbcType="VARCHAR" property="userPassword" />
    
    <result column="user_name" jdbcType="VARCHAR" property="userName" />
    <result column="user_email" jdbcType="VARCHAR" property="userEmail" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="user_info" jdbcType="LONGVARCHAR" property="userInfo" />
    <result column="head_img" jdbcType="LONGVARBINARY" property="headImg" />
    
  </resultMap>
  
  
  <!-- 调用存储过程 -->
  <select id="selectUserById" statementType="CALLABLE" useCache="false">
      {call select_user_by_id(
          #{id,mode=IN},
          #{userName,mode=OUT,jdbcType=VARCHAR},
          #{userPassword,mode=OUT,jdbcType=VARCHAR},
          #{userEmail,mode=OUT,jdbcType=VARCHAR},
          #{userInfo,mode=OUT,jdbcType=VARCHAR},
          #{headImg,mode=OUT,jdbcType=BLOB,javaType=_byte[]},
          #{createTime,mode=OUT,jdbcType=TIMESTAMP}
      )}
  </select>
</mapper>
//dao层
void selectUserById(SysUser user);

//测试 ;返回结果存储在参数对象中;因为在存储过程中使用了别名user_name;
//如若不使用别名,则在mapper中可以自定义resultMap="BaseResultMap"
@Test public void testSelectUserById() { SqlSession sqlSession = getSqlSession(); SysUserMapper sysUserMapper = sqlSession.getMapper(SysUserMapper.class); SysUser user = new SysUser(); user.setId(1L); sysUserMapper.selectUserById(user); Assert.assertNotNull(user); System.out.println(user.getUserName()); }

 

2第二个存储过程     简单根据用户名和分页参数进行查询,返回总数和分页数据

#第二个存储过程
#简单根据用户名和分页参数进行查询,返回总数和分页数据
DROP PROCEDURE IF EXISTS `select_user_page`;
DELIMITER ;;
CREATE PROCEDURE `select_user_page`(
  IN  userName VARCHAR(50),
    IN  _offset BIGINT,
    IN  _limit BIGINT,
    OUT total BIGINT)
BEGIN
#查询数据总数
SELECT count(*) INTO total FROM sys_user 
WHERE user_name like concat('%', userName ,'%');
#分页查询数据
SELECT * FROM sys_user 
WHERE user_name like concat('%', userName ,'%') 
limit _offset,_limit;
END
;;
DELIMITER ;
#存储过程调用
SET @userName='ad',@_offset=0,@_limit=1;
CALL select_user_page(
    @userName,@_offset,@_limit,@total
);

SELECT @total as total;

SysUserMapper.xml

<select id="selectUserPage" statementType="CALLABLE" useCache="false" resultMap="BaseResultMap">
      {call select_user_page(
          #{userName,mode=IN},
          #{offset,mode=IN},
          #{limit,mode=IN},
          #{total,mode=OUT,jdbcType=BIGINT}
      )}
  </select>
//dao
List<SysUser> selectUserPage(Map<String,Object> params);

//测试
@Test
    public void testSelectUserPage() {
        SqlSession sqlSession = getSqlSession();
        try {
        SysUserMapper sysUserMapper = sqlSession.getMapper(SysUserMapper.class);
        Map<String,Object> params = new HashMap<String, Object>();
        params.put("userName", "ad");
        params.put("offset", "0");
        params.put("limit", "1");
        List<SysUser> userList = sysUserMapper.selectUserPage(params);
        Long total = (Long) params.get("total");
        System.out.println("总数:"+total);
        for (SysUser sysUser : userList) {
            
            System.out.println("用户名:"+sysUser.getUserName());
        }
        }
        finally {
            sqlSession.close();
        }
    }

 

posted @ 2017-09-28 12:01  watermelonBan  阅读(4331)  评论(0编辑  收藏  举报