mybatis mysql 呼叫檢視

weixin_34088583發表於2017-01-06

 

java程式碼

@RequestMapping(value = "/testView", method = RequestMethod.GET)
    public @ResponseBody String testView(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Map<String, Object> paraMap = new HashMap<String, Object>();
        paraMap.put("userType", "doctor");
        List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
        list = procedureMapper.testView(paraMap);
        if(list != null && list.size() > 0){
            for (int i = 0; i < list.size(); i++) {
                System.out.println(list.get(i));
            }
        }
        return null;
    }

 

mapper

package cn.zsmy.mapper;

import java.util.List;
import java.util.Map;

import cn.zsmy.entity.User;
import cn.zsmy.form.UserForm;
import cn.zsmy.mapper.base.SimpleMapper;
import cn.zsmy.tmp.core.MyBatisRepository;


/**
 * @ClassName: ProcedureMapper
 * @Description: 呼叫儲存過程與檢視
 * @author 師海明
 * @date 2016年6月13日 上午11:52:38
 * 
*/
@MyBatisRepository
public interface ProcedureMapper extends SimpleMapper<UserForm,User>{
    
    Map<String, Object> testProcedure(Map<String, Object> paraMap);

    List<Map<String, Object>> testView(Map<String, Object> paraMap);
}

 

mapper.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.zsmy.mapper.ProcedureMapper" >


 <!-- 呼叫儲存過程查詢 -->
 <select id="testProcedure" parameterType="java.util.Map" statementType="CALLABLE" resultType="java.util.HashMap">  
      {call loginandreg(
                #{out_ret,mode=OUT,javaType=java.lang.Integer,jdbcType=INTEGER},
              #{out_desc,mode=OUT,javaType=java.lang.String,jdbcType=VARCHAR},
              #{userId,jdbcType=VARCHAR,mode=OUT},
              #{user_pwd,jdbcType=VARCHAR,mode=IN},
              #{nickname,jdbcType=VARCHAR,mode=IN}
          )
       }
  </select>
  
  <!-- 呼叫檢視查詢 -->
  <select id="testView" parameterType="java.util.Map" resultType="java.util.HashMap">
        select username, nickname, dept from test_view where user_type=#{userType}
  </select>
    
</mapper>

 

檢視

DROP VIEW IF EXISTS `palm_2_0_16`.`test_view`;
CREATE VIEW `palm_2_0_16`.`test_view` 
    AS
SELECT u.username,u.nickname,d.dept, u.user_type FROM tb_user u, tb_doctor d WHERE d.id=u.id ;

 

備註:

mapper.xml中where後面的引數條件一定是在檢視中返回的,不然會報錯。

 

相關文章