mybatis调用存储过程

来源:互联网 发布:magic mouse 2 知乎 编辑:程序博客网 时间:2024/04/19 07:16

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());
  }
 }

}

 

 

 

 

 

0 0
原创粉丝点击