ssm框架調用mysql存儲過程

1.建表

/*
Navicat MySQL Data Transfer
Source Server         : localMysql
Source Server Version : 50628
Source Host           : 127.0.0.1:3306
Source Database       : testmysql
Target Server Type    : MYSQL
Target Server Version : 50628
File Encoding         : 65001
Date: 2017-06-19 09:29:34
*/
 
SET FOREIGN_KEY_CHECKS=0;
 
-- ----------------------------
-- Table structure for t_user
-- ----------------------------
DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user` (
  `id` bigint(11) NOT NULL AUTO_INCREMENT,
  `user_name` varchar(255) DEFAULT NULL COMMENT '用戶名',
  `user_phone` varchar(20) DEFAULT NULL COMMENT '手機號',
  `user_email` varchar(255) DEFAULT NULL COMMENT '郵箱地址',
  `user_pwd` varchar(32) DEFAULT NULL COMMENT '加鹽後用戶密碼',
  `pwd_salt` varchar(6) DEFAULT NULL COMMENT 'MD5鹽',
  `create_time` datetime DEFAULT NULL COMMENT '創建時間',
  `modify_time` datetime DEFAULT NULL COMMENT '最後修改時間',
  `is_delete` tinyint(4) DEFAULT NULL COMMENT '是否刪除,0-未刪除;1-已刪除',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COMMENT='用戶登錄表';
 
-- ----------------------------
-- Records of t_user
-- ----------------------------
INSERT INTO `t_user` VALUES ('1', '子了a', '13285250574', '[email protected]', '05126a423a9379d529e4ee61a212fa55', 'KJUYT5', '2016-07-15 23:38:56', '2016-07-15 23:39:09', '0');
INSERT INTO `t_user` VALUES ('2', 'bbbb', '159852505743', '[email protected]', '98bd3a1bebde01ad363d3c5a0d1e56da', '656JHU', '2016-07-15 23:39:01', '2016-07-15 23:39:13', '0');
INSERT INTO `t_user` VALUES ('3', '王尼瑪', '13685250574', '[email protected]', '5470db9b63c354f6c8d628b80ae2f3c3', '89UIKQ', '2016-07-15 23:39:05', '2016-07-15 23:39:16', '0');

2.創建存儲過程

CREATE DEFINER=root@localhost PROCEDURE UPDATE_USER(IN in_id integer,IN in_user_name varchar(20),OUT out_user_phone varchar(20))
BEGIN
update t_user set user_name = in_user_name WHERE t_user.id = in_id;
select user_phone INTO out_user_phone from t_user where id = in_id;

3.service調用

package cn.demo.service;
 
import cn.demo.dao.AccountDao;
import cn.demo.model.Account;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import static junit.framework.TestCase.assertEquals;
 
/**
 * Created by Administrator on 2017/3/22.
 */
@RunWith(SpringJUnit4ClassRunner.class)
//這個是用來加載寫好的配置文件,傳入的值是數組形式多個配置文件如下 {"····","·······"}
@ContextConfiguration({"classpath:spring/spring-dao-config.xml"})
public class AccountServiceTest {
 
    @Autowired
    private AccountDao accountDao;
 
//    @Test
    public void getAllAccount() throws Exception {
        List<Account> accountList = accountDao.getAllAccount();
        System.out.println("accountList=" + accountList.toString());
    }
 
    @Test
    public void testGetNamesAndItems() {
        Map<String, Object> parms = new HashMap<String, Object>();
        parms.put("in_id", 1);
        parms.put("in_user_name", "子了");
        parms.put("out_user_phone", new String());
        accountDao.updateUser(parms);
        assertEquals("13285250574", parms.get("out_user_phone"));
 
 
    }
 
 
}

4.dao層

package cn.demo.dao;
/**
 * Created by chengcheng on 2017/6/2 0002.
 */
 
 
import cn.demo.model.Account;
import org.springframework.stereotype.Repository;
 
import java.util.List;
import java.util.Map;
 
/**
 * Created by Administrator on 2017/3/22.
 */
@Repository
public interface AccountDao {
 
    
    String updateUser(Map<String, Object> parms);
}

5.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.demo.dao.AccountDao">
   
    <select id ="updateUser" parameterType= "map" statementType="CALLABLE" >
        <!--註明statementType="CALLABLE"表示調用存儲過程-->
        {call UPDATE_USER(
        #{in_id, jdbcType=INTEGER, mode=IN},
        #{in_user_name, jdbcType= VARCHAR, mode=IN},
        #{out_user_phone, mode=OUT, jdbcType= VARCHAR}
        )}
        <!--傳入傳出參數要註明mode=IN/OUT 並要註明jdbcType(在網上可以查詢mybatis支持哪些jdbcType類型),返回參數要註明對應的resultMap-->
    </select >
 
</mapper>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章