MySQL使用存储过程

题目:根据图片所给的表做一个简单的能实现增删查改的项目

(要求:增删改要有存储过程,查询要用到视图查询

项目名:ConsPrc

MySQL使用存储过程_第1张图片

工具:MyEclipse

数据库:MySQL

存储过程:

Oracle:Oracle存储过程

MySQL:下面会讲

1、创表

c_cons_prc表.sql

CREATE DATABASE `work`;
USE `work`;

DROP TABLE IF EXISTS `c_cons_prc`;

CREATE TABLE `c_cons_prc` (
  `tariff_id` INT(16) NOT NULL AUTO_INCREMENT COMMENT '用户电价标识',
  `sp_id` INT(16) DEFAULT NULL COMMENT '受电点标识',
  `cons_id` INT(16) DEFAULT NULL COMMENT '用户标识',
  `pf_std_code` VARCHAR(20) DEFAULT NULL COMMENT '功率因数标准',
  `trade_code` VARCHAR(20) DEFAULT NULL COMMENT '电价行业类别',
  `prc_code` VARCHAR(20) DEFAULT NULL COMMENT '电价码',
  `ts_flag` VARCHAR(20) DEFAULT NULL COMMENT '是否执行峰谷标志',
  `fix_ratio` FLOAT DEFAULT NULL COMMENT '固定力率',
  PRIMARY KEY (`tariff_id`)
) ENGINE=INNODB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;

INSERT  INTO `c_cons_prc`(`tariff_id`,`sp_id`,`cons_id`,`pf_std_code`,`trade_code`,`prc_code`,`ts_flag`,`fix_ratio`) 
	VALUES (1,1001,123456,'1001','生活用电','1001','1001',1001),
		(2,1002,111111,'1002','生活用电','1002','1002',1002),
		(4,1004,1004,'1004','商业用电','1004','1004',1004),
		(5,1005,1005,'1005','生活用电','1005','1005',1005),
		(6,1006,1006,'1006','商业用电','1006','否',1006),
		(7,1007,1007,'1007','生活用电','1007','是',1007),
		(8,1008,1008,'1008','商业用电','1008','1008',1008),
		(9,1009,1009,'1009','商业用电','1009','1009',1009),
		(10,1010,1010,'1010','生活用电','1010','否',1010);
		

2、项目结构

MySQL使用存储过程_第2张图片

3、导入jar包

jar包导入到上方的lib文件夹中

4、实体类pojo

C_Cons_Prc.java

因为只有一张表,所以我将视图的字段写的和原表一模一样,这样方便好记

package com.pojo;
/**
 * 用户电价表
 * @author Administrator
 *
 */
public class C_Cons_Prc {

	private int tariff_id;//用户电价标识
	private int sp_id;//受电点标识
	private int cons_id;//用户标识
	private String pf_std_code;//功率因数标准
	private String trade_code;//电价行业类别
	private String prc_code;//电价码
	private String ts_flag;//是否执行峰谷标志
	private float fix_ratio;//固定力率
	
	public int getTariff_id() {
		return tariff_id;
	}
	public void setTariff_id(int tariff_id) {
		this.tariff_id = tariff_id;
	}
	public int getSp_id() {
		return sp_id;
	}
	public void setSp_id(int sp_id) {
		this.sp_id = sp_id;
	}
	public int getCons_id() {
		return cons_id;
	}
	public void setCons_id(int cons_id) {
		this.cons_id = cons_id;
	}
	public String getPf_std_code() {
		return pf_std_code;
	}
	public void setPf_std_code(String pf_std_code) {
		this.pf_std_code = pf_std_code;
	}
	public String getTrade_code() {
		return trade_code;
	}
	public void setTrade_code(String trade_code) {
		this.trade_code = trade_code;
	}
	public String getPrc_code() {
		return prc_code;
	}
	public void setPrc_code(String prc_code) {
		this.prc_code = prc_code;
	}
	public String getTs_flag() {
		return ts_flag;
	}
	public void setTs_flag(String ts_flag) {
		this.ts_flag = ts_flag;
	}
	public float getFix_ratio() {
		return fix_ratio;
	}
	public void setFix_ratio(float fix_ratio) {
		this.fix_ratio = fix_ratio;
	}
	
}


5、dao层

C_Cons_PrcMapper.java

package com.dao;

import java.util.List;

import org.apache.ibatis.annotations.Param;

import com.pojo.C_Cons_Prc;

public interface C_Cons_PrcMapper {
	
	public List findAll();
	public int findCount();
	public List findList(@Param("currPage")int currPage,
									   @Param("pageSize")int pageSize);
	
	public C_Cons_Prc findById(int tariff_id);
	public int addC_Cons_Prc(C_Cons_Prc p);
	public int delC_Cons_Prc(int tariff_id);
	public int updC_Cons_Prc(C_Cons_Prc p);

	public List findByCondition(String trade_code);
}

C_Cons_PrcMapper.xml





	
	
	
	
	
	
	
	
	
	



	
        
        
    
	

	
		{call addC_Cons_Prc(
			#{tariff_id,mode=OUT,jdbcType=INTEGER},#{sp_id,mode=IN},
			#{cons_id,mode=IN},#{pf_std_code,mode=IN},#{trade_code,mode=IN},
			#{prc_code,mode=IN},#{ts_flag,mode=IN},#{fix_ratio,mode=IN}
		)}
	
	
	
		{call delC_Cons_Prc(
			#{tariff_id,mode=IN}
		)}
	
	
	
		{call updC_Cons_Prc(
			#{tariff_id,mode=IN},#{sp_id,mode=IN},#{cons_id,mode=IN},#{pf_std_code,mode=IN},
			#{trade_code,mode=IN},#{prc_code,mode=IN},#{ts_flag,mode=IN},#{fix_ratio,mode=IN}
		)}
	
	
	
	
	
	


6、controller

C_Cons_PrcController.java

package com.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.pojo.C_Cons_Prc;
import com.service.C_Cons_PrcService;

@Controller
@RequestMapping("/pc")
public class C_Cons_PrcController {
	@Autowired
	private C_Cons_PrcService objService;
	
	@RequestMapping("/index")
	public String index(Model model){
		return "index";
	}
	
	@RequestMapping("/add")//显示添加页面
	public String addShow(){
		return "add";
	}
	
	@RequestMapping("/addCons.do")//显示添加页面
	@ResponseBody//ajax的方式
	public String addP(C_Cons_Prc p){
		int res = objService.addC_Cons_Prc(p);
		return JSON.toJSONString(res);
	}
	
	@RequestMapping("/findAll")
	public String findAll(C_Cons_Prc p){
		List list = objService.findAll();
		return "index";
	}
	
	

	@RequestMapping(value="/findCons.do")
	@ResponseBody
	public Object findPerpay(@RequestParam(value="page",required=false)Integer currPage,
						@RequestParam(value="limit",required=false)Integer pageSize){
			System.out.println("findCons");
			if (currPage==null) {
				currPage=1;
			}
			if (pageSize==null) {
				pageSize=10;
			}
			int limitOne=(currPage-1)*pageSize;
			//记录数
			List list = objService.findList(limitOne, pageSize);
			//总记录数
			int count=objService.findCount();
			System.out.println(count);
			
			String strjson=JSON.toJSONString(list);
			String json = "{\"code\":0,\"msg\":\"\",\"count\":" + count + ",\"data\":" + strjson + "}";
			System.out.println(JSONObject.toJSON(list));
			return JSONObject.toJSON(list);
	}
	
	

	
	@RequestMapping("/del")
	@ResponseBody
	public String del(Integer id){
		int res = objService.delC_Cons_Prc(id);
		return JSON.toJSONString(res);
	}
	
	@RequestMapping("/update")
	public String update(){
		return "update";
	}
	
	
	@RequestMapping("/updSel")
	public String updSel(Integer id,Model model){
		System.out.println(id);
		C_Cons_Prc cons = objService.findById(id);
		model.addAttribute("cons",cons);
		return "update";
	}

	@RequestMapping("/updCons")
	@ResponseBody
	public String updCons(C_Cons_Prc cons){
		int res = objService.updC_Cons_Prc(cons);
		System.out.println(JSON.toJSONString(res));
		return JSON.toJSONString(res);
	}

	//条件查询
	@RequestMapping("/findByCondition")
	@ResponseBody
	public String findByCondition(String trade_code,Model model){
		List list = objService.findByCondition(trade_code);
		return JSON.toJSONString(list);
	}
	

}

7、jsp页面

查询页面:index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

  
    
    查询
      
    
    
    
	

	
	
	


	

用户电价信息

电价行业类别:
用户电价标识 受电点标识 用户标识 功率因数标准 电价行业类别 电价码 是否执行峰谷标志 固定力率 操作

添加页面:add.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>


    添加
	
	
	
	
	
	

	
新增电价信息

更新页面:update.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>


    更新
	
	
	
	
	
	


	

页面中导入的包,在我的资源中可以下载

存储过程

c_cons_prc存储过程.sql


#查看视图结果
SELECT * FROM `c_cons_prc_view`

#创建视图
CREATE VIEW `c_cons_prc_view`
AS
  SELECT `tariff_id` AS 用户电价标识,`sp_id` AS 受电点标识,`cons_id` AS 用户标识,`pf_std_code` AS 功率因数标准,
	`trade_code` AS 电价行业类别,`prc_code` AS 电价码,`ts_flag` AS 是否执行峰谷标志,`fix_ratio` AS 固定力率
    FROM `c_cons_prc`

#存储过程
USE `work`;

#添加
DROP PROCEDURE IF EXISTS addC_Cons_Prc;
###
DELIMITER $
CREATE PROCEDURE addC_Cons_Prc(OUT c_tariff_id INTEGER,
			IN c_sp_id INTEGER,
			IN c_cons_id INTEGER,
			IN c_pf_std_code VARCHAR(20),
			IN c_trade_code VARCHAR(20),
			IN c_prc_code VARCHAR(20),
			IN c_ts_flag VARCHAR(20),
			IN c_fix_ratio INTEGER)
BEGIN
	INSERT INTO c_cons_prc(sp_id,cons_id,pf_std_code,trade_code,prc_code,ts_flag,fix_ratio) 
		VALUES (c_sp_id,c_cons_id,c_pf_std_code,c_trade_code,c_prc_code,c_ts_flag,c_fix_ratio);
	SET c_tariff_id=LAST_INSERT_ID();
END $
DELIMITER; 
###



#删除
DROP PROCEDURE IF EXISTS delC_Cons_Prc;
###
DELIMITER $
CREATE PROCEDURE delC_Cons_Prc(IN c_tariff_id INTEGER)
BEGIN
	DELETE FROM c_cons_prc WHERE tariff_id=c_tariff_id;
END $
DELIMITER; 
###



#更新
DROP PROCEDURE IF EXISTS updC_Cons_Prc;
###
DELIMITER $
CREATE PROCEDURE updC_Cons_Prc(IN c_tariff_id INTEGER,
			IN c_sp_id INTEGER,
			IN c_cons_id INTEGER,
			IN c_pf_std_code VARCHAR(20),
			IN c_trade_code VARCHAR(20),
			IN c_prc_code VARCHAR(20),
			IN c_ts_flag VARCHAR(20),
			IN c_fix_ratio INTEGER)
BEGIN
	UPDATE c_cons_prc SET 
		sp_id=c_sp_id,cons_id=c_cons_id,pf_std_code=c_pf_std_code,
		trade_code=c_trade_code,prc_code=c_prc_code,ts_flag=c_ts_flag,fix_ratio=c_fix_ratio
	WHERE tariff_id=c_tariff_id;
END $
DELIMITER; 
###

题外话

第一次在博客上写一整个练习项目,写的不好勿喷,还有因为老师没有布置分页,所以controller的分页我也没去实现,各位大佬还是自行解决吧,溜了溜了

你可能感兴趣的:(积累)