基于Mysql的存储过程的 SSM实现

9 篇文章 0 订阅

首先创建存储过程,如下:

获取总条目:

drop procedure if exists sp_total;
create procedure sp_total(IN $name varchar(20))
begin
  if($name is null) then
   select count(id) from t_user;
  else
   select count(id) from t_user where name like concat('%',$name,'%');
  end if;
end
call sp_total('是');

分页查询:

drop procedure if exists sp_plist;
create procedure sp_plist(IN $offset int,IN $pagesize int,IN $name varchar(20))
begin
  if($name is null) then
     select * from t_user limit $offset,$pagesize;
   else
      select * from t_user where name like  concat('%',$name,'%')
       limit $offset,$pagesize;
  end if;
end
call sp_plist(5,3,'否');

添加和修改:

drop procedure if exists sp_save;
create procedure sp_save(IN $id int,IN $name varchar(20),IN $age int)
begin
  if exists (select * from t_user where id=$id) then
     update t_user set name=$name,age=$age where id=$id;
  else
      insert into t_user(name,age) values($name,$age);
 end if;
end
 
call sp_save(6,'猪八戒',21);

删除:

BEGIN
  if exists (select * from t_user where id=$id)
  then
    delete from t_user where id=$id;
 end if;	

END

查询ID:

drop procedure if exists sp_find;
create procedure sp_find(IN $id int)
 select * from t_user where id=$id;

call sp_find(3);

实体类:

public class User {

	private Integer id;
	private String name;
	private Integer age;
   //省去 get set
}

Mapper:

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.mapping.StatementType;

import com.bw.ssm.entity.User;
public interface UserMapper {

	@Select("{ call sp_total(#{name})}")
	@Options(statementType=StatementType.CALLABLE)
	int getTotal(String name);
	
	
	@Select("{call sp_plist(#{offset},#{pagesize},#{mohu})}")
	@Options(statementType=StatementType.CALLABLE)
	List<User> plist(@Param("offset")int offset,@Param("pagesize")int pagesize,@Param("mohu")String mohu);
	
	@Select("{ call sp_list()}")
	@Options(statementType=StatementType.CALLABLE)
	List<User> findAll();
	
	@Options(statementType=StatementType.CALLABLE)
	@Insert("{call sp_save(#{id},#{name},#{age})}")
	int save(User user);
	
	@Options(statementType= StatementType.CALLABLE)
	@Select("{call sp_find(#{id})}")
	User findById(int id);
	
	@Options(statementType= StatementType.CALLABLE)
	@Delete("{call sp_del(#{id})}")
	int delete(int id);
}

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值