IT俱乐部 MySql ssm框架如何调用mysql存储过程

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', '1045221654@qq.com', '05126a423a9379d529e4ee61a212fa55', 'KJUYT5', '2016-07-15 23:38:56', '2016-07-15 23:39:09', '0');
INSERT INTO `t_user` VALUES ('2', 'bbbb', '159852505743', '1198224554@qq.com', '98bd3a1bebde01ad363d3c5a0d1e56da', '656JHU', '2016-07-15 23:39:01', '2016-07-15 23:39:13', '0');
INSERT INTO `t_user` VALUES ('3', '王尼玛', '13685250574', '1256221654@qq.com', '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 accountList = accountDao.getAllAccount();
        System.out.println("accountList=" + accountList.toString());
    }
    @Test
    public void testGetNamesAndItems() {
        Map parms = new HashMap();
        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 parms);
}

5.xml文件

        {call UPDATE_USER(
        #{in_id, jdbcType=INTEGER, mode=IN},
        #{in_user_name, jdbcType= VARCHAR, mode=IN},
        #{out_user_phone, mode=OUT, jdbcType= VARCHAR}
        )}
        
    

6.接下来运行junit就可以了

如果是在直接在应用中使用,把juinit内容直接放入service就可以了

到此这篇关于ssm框架调用mysql存储过程的文章就介绍到这了,更多相关ssm框架mysql存储过程内容请搜索IT俱乐部以前的文章或继续浏览下面的相关文章希望大家以后多多支持IT俱乐部!

本文收集自网络,不代表IT俱乐部立场,转载请注明出处。https://www.2it.club/database/mysql/11376.html
上一篇
下一篇
联系我们

联系我们

在线咨询: QQ交谈

邮箱: 1120393934@qq.com

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部