mybatis调用存储过程

mybatis调用存储过程

         1. 新建存储过程并且通过测试
         2. 创建相应表的实体类
         3. 创建工具bean
         4. 创建dao接口
         5. 创建dao接口的xml文件
         6. 创建dao接口的相应实现类
         7. 创建测试


业务逻辑以后会出现的两种情况:
        1. 将业务逻辑写在BIZ层
        2. 将业务逻辑写在存储过程中

 

 

//实体类

package com.vo;

import java.util.HashSet;
import java.util.Set;


public class Houseinfos implements java.io.Serializable {

 // Fields

 private Integer houseid;
 private Territories territories;
 private Custominfos custominfos;
 private String housetitle;
 private String houseexplain;
 private Double monthlyfee;
 private Double buildarea;
 private String housetype;
 private String housepurpose;
 private Short buildingno;
 private Short unitno;
 private Short floorno;
 private String renttype;
 private String paytype;
 private String housedirection;
 private String remark;
 private Set orderinfoses = new HashSet(0);
 private Set housepictureses = new HashSet(0);

 // Constructors

 /** default constructor */
 public Houseinfos() {
 }

 /** minimal constructor */
 public Houseinfos(Integer houseid, String housetitle, String houseexplain,
   Double monthlyfee, Double buildarea, String housetype,
   Short buildingno, Short unitno, Short floorno, String renttype,
   String paytype, String housedirection) {
  this.houseid = houseid;
  this.housetitle = housetitle;
  this.houseexplain = houseexplain;
  this.monthlyfee = monthlyfee;
  this.buildarea = buildarea;
  this.housetype = housetype;
  this.buildingno = buildingno;
  this.unitno = unitno;
  this.floorno = floorno;
  this.renttype = renttype;
  this.paytype = paytype;
  this.housedirection = housedirection;
 }

 /** full constructor */
 public Houseinfos(Integer houseid, Territories territories,
   Custominfos custominfos, String housetitle, String houseexplain,
   Double monthlyfee, Double buildarea, String housetype,
   String housepurpose, Short buildingno, Short unitno,
   Short floorno, String renttype, String paytype,
   String housedirection, String remark, Set orderinfoses,
   Set housepictureses) {
  this.houseid = houseid;
  this.territories = territories;
  this.custominfos = custominfos;
  this.housetitle = housetitle;
  this.houseexplain = houseexplain;
  this.monthlyfee = monthlyfee;
  this.buildarea = buildarea;
  this.housetype = housetype;
  this.housepurpose = housepurpose;
  this.buildingno = buildingno;
  this.unitno = unitno;
  this.floorno = floorno;
  this.renttype = renttype;
  this.paytype = paytype;
  this.housedirection = housedirection;
  this.remark = remark;
  this.orderinfoses = orderinfoses;
  this.housepictureses = housepictureses;
 }

 // Property accessors

 public Integer getHouseid() {
  return this.houseid;
 }

 public void setHouseid(Integer houseid) {
  this.houseid = houseid;
 }

 public Territories getTerritories() {
  return this.territories;
 }

 public void setTerritories(Territories territories) {
  this.territories = territories;
 }

 public Custominfos getCustominfos() {
  return this.custominfos;
 }

 public void setCustominfos(Custominfos custominfos) {
  this.custominfos = custominfos;
 }

 public String getHousetitle() {
  return this.housetitle;
 }

 public void setHousetitle(String housetitle) {
  this.housetitle = housetitle;
 }

 public String getHouseexplain() {
  return this.houseexplain;
 }

 public void setHouseexplain(String houseexplain) {
  this.houseexplain = houseexplain;
 }

 public Double getMonthlyfee() {
  return this.monthlyfee;
 }

 public void setMonthlyfee(Double monthlyfee) {
  this.monthlyfee = monthlyfee;
 }

 public Double getBuildarea() {
  return this.buildarea;
 }

 public void setBuildarea(Double buildarea) {
  this.buildarea = buildarea;
 }

 public String getHousetype() {
  return this.housetype;
 }

 public void setHousetype(String housetype) {
  this.housetype = housetype;
 }

 public String getHousepurpose() {
  return this.housepurpose;
 }

 public void setHousepurpose(String housepurpose) {
  this.housepurpose = housepurpose;
 }

 public Short getBuildingno() {
  return this.buildingno;
 }

 public void setBuildingno(Short buildingno) {
  this.buildingno = buildingno;
 }

 public Short getUnitno() {
  return this.unitno;
 }

 public void setUnitno(Short unitno) {
  this.unitno = unitno;
 }

 public Short getFloorno() {
  return this.floorno;
 }

 public void setFloorno(Short floorno) {
  this.floorno = floorno;
 }

 public String getRenttype() {
  return this.renttype;
 }

 public void setRenttype(String renttype) {
  this.renttype = renttype;
 }

 public String getPaytype() {
  return this.paytype;
 }

 public void setPaytype(String paytype) {
  this.paytype = paytype;
 }

 public String getHousedirection() {
  return this.housedirection;
 }

 public void setHousedirection(String housedirection) {
  this.housedirection = housedirection;
 }

 public String getRemark() {
  return this.remark;
 }

 public void setRemark(String remark) {
  this.remark = remark;
 }

 public Set getOrderinfoses() {
  return this.orderinfoses;
 }

 public void setOrderinfoses(Set orderinfoses) {
  this.orderinfoses = orderinfoses;
 }

 public Set getHousepictureses() {
  return this.housepictureses;
 }

 public void setHousepictureses(Set housepictureses) {
  this.housepictureses = housepictureses;
 }

}

//bean调用存储过程用的工具类

package com.util;
/**
 * 用于封装传递到存储过程的工具类
 */
import java.util.ArrayList;
import java.util.List;

import com.vo.Houseinfos;

public class PageBean {
 //用于存放系统返回的值
 private List<Houseinfos> houseList=new ArrayList<Houseinfos>();
 //用于存放
 private Integer teid;
 private String  monthfee;
 private String  area;
 private float currentpage;
 private float pagesize;
 public float getCurrentpage() {
  return currentpage;
 }
 public void setCurrentpage(float currentpage) {
  this.currentpage = currentpage;
 }
 public float getPagesize() {
  return pagesize;
 }
 public void setPagesize(float pagesize) {
  this.pagesize = pagesize;
 }
 public List<Houseinfos> getHouseList() {
  return houseList;
 }
 public void setHouseList(List<Houseinfos> houseList) {
  this.houseList = houseList;
 }
 public Integer getTeid() {
  return teid;
 }
 public void setTeid(Integer teid) {
  this.teid = teid;
 }
 public String getMonthfee() {
  return monthfee;
 }
 public void setMonthfee(String monthfee) {
  this.monthfee = monthfee;
 }
 public String getArea() {
  return area;
 }
 public void setArea(String area) {
  this.area = area;
 }
 
 

}

 

//dao接口

package com.dao;

import com.util.PageBean;
/**
 * 处理房屋访问的dao接口
 * @author Administrator
 *
 */
public interface IHouseDao {
 /**
  * 调用存储过程
  * @param pageBean
  */
 public void callProc(PageBean pageBean);
}

 

//dao接口的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="com.dao.IHouseDao">
 <resultMap id="cursorMap" type="com.vo.Houseinfos" >
 </resultMap>
 <update id="callProc"  statementType="CALLABLE" parameterType="com.util.PageBean">
  {call proc_CP(
   #{houseList,jdbcType=CURSOR,mode=OUT,javaType=ResultSet,resultMap=cursorMap},
   #{teid,jdbcType=INTEGER},
   #{monthfee,jdbcType=VARCHAR},
   #{area,jdbcType=VARCHAR},
   #{currentpage,jdbcType=FLOAT},
   #{pagesize,jdbcType=FLOAT}
  )}
 </update>

</mapper>

 

//dao接口的相应实现类

 

 


 

package com.dao.impl;


import org.apache.ibatis.session.SqlSession;

import com.dao.IHouseDao;
import com.factory.BuildFactory;
import com.util.PageBean;

public class HouseDaoImpl implements IHouseDao {

 private IHouseDao hdao=null;
 private SqlSession session=null;
 public IHouseDao getHdao() {
  return hdao;
 }
 public void setHdao(IHouseDao hdao) {
  this.hdao = hdao;
 }
 public SqlSession getSession() {
  return session;
 }
 public void setSession(SqlSession session) {
  this.session = session;
 }
 @Override
 public void callProc(PageBean pageBean) {
  // TODO Auto-generated method stub
  
  try {
   session=BuildFactory.getFactory().openSession();
   hdao=session.getMapper(IHouseDao.class);
   hdao.callProc(pageBean);
  } catch (Exception e) {
   // TODO: handle exception
   e.printStackTrace();
  }finally{
   if(session!=null){
    session.close();
   }
   
  }
 }
}

}

 

//测试类

 

package com.test;

import java.util.List;

import com.dao.IHouseDao;
import com.dao.impl.HouseDaoImpl;
import com.util.PageBean;
import com.vo.Houseinfos;

public class HouseTest {
 public static void main(String[] args) {
  IHouseDao hdao=new HouseDaoImpl();
  PageBean pageBean=new PageBean();
  pageBean.setArea("50-60");
  pageBean.setCurrentpage(1);
  pageBean.setMonthfee("500-1000");
  pageBean.setPagesize(10);
  pageBean.setTeid(7);
  hdao.callProc(pageBean);
  List<Houseinfos> list=pageBean.getHouseList();
  for (Houseinfos house : list) {
   System.out.println(house.getHouseid());
  }
 }

}

 

 

 

 

 

MyBatis可以通过调用存储过程来执行数据库操作。下面是一个简单的示例: 1. 创建存储过程 在数据库中创建一个存储过程,例如: ```sql CREATE PROCEDURE get_user_by_id(IN id INT, OUT name VARCHAR(20)) BEGIN SELECT user_name INTO name FROM user WHERE user_id = id; END ``` 该存储过程接受一个输入参数id,一个输出参数name,根据id查询用户信息并将用户名赋值给name。 2. 编写Mapper文件 在MyBatis的Mapper文件中,定义一个<select>标签,调用存储过程,例如: ```xml <select id="getUserById" statementType="CALLABLE"> CALL get_user_by_id(#{id, mode=IN, jdbcType=INTEGER}, #{name, mode=OUT, jdbcType=VARCHAR}) </select> ``` 该<select>标签的id属性为getUserByIdstatementType属性为CALLABLE,表示调用存储过程。 3. 调用存储过程 通过SqlSession的selectOne方法调用存储过程,例如: ```java SqlSession sqlSession = sqlSessionFactory.openSession(); try { Map<String, Object> paramMap = new HashMap<String, Object>(); paramMap.put("id", 1); sqlSession.selectOne("getUserById", paramMap); String name = (String)paramMap.get("name"); // 处理返回值 } finally { sqlSession.close(); } ``` 该代码创建一个SqlSession对象,调用selectOne方法执行getUserById查询,将查询结果存储在paramMap中,最后从paramMap中获取name的值。 以上就是一个简单的MyBatis调用存储过程的示例,具体的实现方式还需要按照实际情况进行适当的调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值