mybatis IDEA——存储过程的使用以及 Parameter number x is not an OUT parameter错误

1、根据用户id查询用户其他信息

创建存储过程

CREATE PROCEDURE  sp_query_by_id(
	IN  i_userid BIGINT, 
	OUT userName VARCHAR(50), 
	OUT userPassword VARCHAR (50), 
	OUT userEmail VARCHAR(50), 
	OUT createTime DATETIME
) 
BEGIN 
    select user_name,
		   user_password,
		   user_email,
		   create_time
       into userName,userPassword,userEmail,createTime
		from sys_user
    where id = i_userid;
END

存储过程测试

call sp_query_by_id(1,@userName,@userPassword, @userEmail, @createTime);
select @userName;

接口

    void queryUserByProcedure(SysUser user);

在调用存储过程的方法中, 需要把statementType设置为CALLABLE,在使用select标签调用存储过程时,由于存储过程方式不支持MyBatis的二级缓存,因此为了避免缓存配置出错,直接将select标签的useCache属性设置为false。
在存储过程中使用参数时, 除了写上必要的属性名,还必须指定参数的mode (模式),可选值为IN、 OUT、INNOUT三种。入参使用IN,出参使用OUT, 输入输出参数使用INOUT。 IN 和OUT 两种模式的区别是,OUT 模式的参数必须指定jdbcType。 这是因为在IN模式下,MyBatis提供了默认的jdbcType,在OUT模式下没有提供。另外在使用Oracle数据库时,如果入参存在null的情况, 那么入参也必须指定jdbcType。

    <select id="queryUserByProcedure" statementType="CALLABLE" useCache="false">
      {
        call sp_query_by_id(
          #{id,mode=IN},
          #{userName,mode=OUT,jdbcType=VARCHAR},
          #{userPassword,mode=OUT,jdbcType=VARCHAR},
          #{userEmail,mode=OUT,jdbcType=VARCHAR},
          #{createTime1,mode=OUT,jdbcType=TIMESTAMP}
        )
      }
    </select>

测试类,存储过程没有返回值(不要和存储过程的出参混淆),所以返回值类型使用void。

我们使用出参的方式得到了该用户的信息。
使用出参方式时,通常情况下会使用对象中的属性接收出参的值,或者使用Map类型接收返回值。这两种情况有很大的区别。当使用JavaBean 对象接收出参时,必须保证所有出参在JavaBean中都有对应的属性存在,否则就会抛出类似“Could not set property ’xxx’ 这样的错误。这是由于JavaBean对象中不存在出参对应的setter方法,使用Map类型时就
不需要保证所有出参都有对应的属性,当Map接收了存储过程的出参时,可以通过Map对象的get(“属性名”)方法获取出参的值。

    @Test
    public void testQueryUserByProcedure(){
        try(SqlSession session = this.getSqlSession()){
            SysUserDao dao = session.getMapper(SysUserDao.class);
            SysUser user = new SysUser();
            user.setId(1L);
            dao.queryUserByProcedure(user);
            System.out.println(user);
        }
    }

日志

Fri Sep 07 21:25:39 CST 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
DEBUG [main] - ==>  Preparing: { call sp_query_by_id( ?, ?, ?, ?, ? ) } 
DEBUG [main] - ==> Parameters: 1(Long)
2018-01-01 00:00:00.0
SysUser{id=1, userName='admin', userPassword='123456', userEmail='[email protected]', createTime1=2018-01-01 00:00:00.0, roles=null, role=null}

2、分页查询

创建存储过程

CREATE PROCEDURE  sp_query_user_page
(
	IN  i_userName VARCHAR(50), 
	IN  i_offset BIGINT, 
	IN  i_limit BIGINT, 
	OUT o_total BIGINT
) 
BEGIN 
	#查询数据总数
	select count(*)  INTO  o_total 
	from  sys_user 
	where user_name like concat('%',i_userName,'%');

	#分页查询数据
	select * 
	from sys_user 
	where user_name like concat('%',i_userName,'%')
	limit i_offset, i_limit;
END;

存储过程测试

call sp_query_user_page('ad',0,1,@total);
select @total;

接口

    List<SysUser> queryUserlistByPage(Map<String,Object> map);
    <select id="queryUserlistByPage" statementType="CALLABLE" useCache="false" resultMap="userMap">
    {
        call sp_query_user_page(
           #{userName,mode=IN},
           #{offset,mode=IN},
           #{limit,mode=IN},
           #{total,mode=OUT,jdbcType=BIGINT}
        )
    }
    </select>

测试类

    @Test
    public void testQueryUserlistByPage(){
        try(SqlSession session = this.getSqlSession()){
            SysUserDao dao = session.getMapper(SysUserDao.class);
            Map<String,Object> map = new HashMap<>();
            map.put("userName","");
            map.put("offset",0);
            map.put("limit",10);
            map.put("total",0);
            List<SysUser> users = dao.queryUserlistByPage(map);
            System.out.println(users);
            System.out.println(map.get("total"));
        }
    }

日志

Fri Sep 07 21:45:06 CST 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
DEBUG [main] - ==>  Preparing: { call sp_query_user_page( ?, ?, ?, ? ) } 
DEBUG [main] - ==> Parameters: (String), 0(Integer), 10(Integer)
TRACE [main] - <==    Columns: id, user_name, user_password, user_email, create_time
TRACE [main] - <==        Row: 1, admin, 123456, [email protected], 2018-01-01 00:00:00.0
Mon Jan 01 00:00:00 CST 2018
TRACE [main] - <==        Row: 1001, test, 123456, [email protected], 2018-01-01 00:00:00.0
Mon Jan 01 00:00:00 CST 2018
TRACE [main] - <==        Row: 1006, test2, 654321, [email protected], null
TRACE [main] - <==        Row: 1007, test3, 654321, [email protected], null
DEBUG [main] - <==      Total: 4
DEBUG [main] - <==    Updates: 0
[SysUser{id=1, userName='admin', userPassword='123456', userEmail='[email protected]', createTime1=Mon Jan 01 00:00:00 CST 2018, roles=null, role=null}, SysUser{id=1001, userName='test', userPassword='123456', userEmail='[email protected]', createTime1=Mon Jan 01 00:00:00 CST 2018, roles=null, role=null}, SysUser{id=1006, userName='test2', userPassword='654321', userEmail='[email protected]', createTime1=null, roles=null, role=null}, SysUser{id=1007, userName='test3', userPassword='654321', userEmail='[email protected]', createTime1=null, roles=null, role=null}]
4

如报以下错误,原因是存储过程名称写错

 Parameter number x is not an OUT parameter

猜你喜欢

转载自blog.csdn.net/Milan__Kundera/article/details/82502958
今日推荐