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<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>6.接下来运行junit就可以了
如果是在直接在应用中使用,把juinit内容直接放入service就可以了
到此这篇关于ssm框架调用mysql存储过程的文章就介绍到这了,更多相关ssm框架mysql存储过程内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论