首页 > 代码库 > mybatis mysql 调用视图

mybatis mysql 调用视图

 

java代码

@RequestMapping(value = "http://www.mamicode.com/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后面的参数条件一定是在视图中返回的,不然会报错。

 

mybatis mysql 调用视图