mybatis-mysql操作存储过程
java代码:
@RequestMapping(value = "/testProcedure", method = RequestMethod.GET) public @ResponseBody String testProcedure(HttpServletRequest request, HttpServletResponse response) throws IOException { Map<String, Object> paraMap = new HashMap<String, Object>(); //输出参数可以不传 //paraMap.put("out_ret", -1); //paraMap.put("out_desc", ""); //paraMap.put("userId", null); paraMap.put("user_pwd", "123456"); paraMap.put("nickname", UUID.randomUUID().toString().replace("-", "").substring(0, 10)); procedureMapper.testProcedure(paraMap); System.out.println(paraMap); return null; }
dao方法:
Map<String, Object> testProcedure(Map<String, Object> paraMap);
ProcedureMapper.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="cn.zsmy.mapper.ProcedureMapper" > <select id="testProcedure" parameterType="java.util.Map" statementType="CALLABLE" resultType="java.util.HashMap"> {call loginandreg( #{out_ret,mode=OUT,javaType=java.lang.Integer,jdbcType=INTEGER}, #{out_desc,mode=OUT,javaType=java.lang.String,jdbcType=VARCHAR}, #{userId,jdbcType=VARCHAR,mode=OUT}, #{user_pwd,jdbcType=VARCHAR,mode=IN}, #{nickname,jdbcType=VARCHAR,mode=IN} ) } </select> </mapper>
存储过程:
DELIMITER // DROP PROCEDURE IF EXISTS loginandreg // CREATE PROCEDURE loginandreg( OUT out_ret INT, OUT out_desc VARCHAR(100), OUT userId VARCHAR(32), IN user_pwd VARCHAR(32), IN nickname VARCHAR(16) ) BEGIN DECLARE u_id VARCHAR(32); SET u_id=(SELECT uuid_short()); SET out_ret = -1; SET out_desc = '执行失败'; IF EXISTS(SELECT * FROM tb_user u WHERE u.NICKNAME=nickname) THEN SELECT u.ID INTO userId FROM tb_user u WHERE u.NICKNAME=nickname AND u.PAY_PASSWORD=user_pwd; IF FOUND_ROWS() < 1 THEN SELECT '-1' INTO userId; END IF; ELSE INSERT INTO tb_user(ID, NICKNAME, PAY_PASSWORD) VALUES (u_id, nickname,user_pwd); SELECT u_id INTO userId; END IF; SET out_ret = 1; SET out_desc ='SUCC'; END // DELIMITER ;
看完打开支付宝扫一扫领个红包吧!