Mybatis的key、value结构以及BLOB类型字段保存与读取

 
1、如果你确定返回的数据只有一条,你可以这样整
xml中:
<select id="searchncomedateByInvestID" resultMap="java.util.HashMap">
    select
    t1.invest_id ,
    cast(t1.modify_time AS DATE) modify_time
    from t_c_wh_redeeminfo t1
    where 1=1
    and t1.invest_id =#{investId}
</select>
dao中:
Map<String,Object> searchncomedateByInvestID(investId);
 
 
2、如果返回的是多条数据,可以这样整
xml中:
<resultMap id="getAllSetDaysResult"   type="HashMap">
        <result property="investid" column="invest_id" jdbcType="VARCHAR" />
        <result property="modifytime" column="modify_time" jdbcType="DATE"/>
 
</resultMap>
<select id="searchncomedateByInvestID" parameterType="java.util.List" resultMap="getAllSetDaysResult">
    select
    t1.invest_id                      ,
    cast(t1.modify_time AS DATE) modify_time
    from t_c_wh_redeeminfo t1
    where 1=1
    and t1.invest_id in
    <foreach collection="list" item="investId" index="index"  open="(" close=")" separator=",">
        #{investId}
    </foreach>
</select>
dao中:
List<Map<String, Object>> searchncomedateByInvestID(List<String> preinvestList);
 
值得注意的要点:
1)mybatis中的oracel中某个字段允许为空,后面一定要接上类型 jdbcType=VARCHAR
2)mybatis中in (${arrayIds}),dao中一定要绑定参数
 
 
 
 
<?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.test.dao.BlobFieldDao">
     <sql id="blobFieldColumns">
         a.ID AS id,
         a.TAB_NAME AS tabName,
         a.TAB_PKID_VALUE AS tabPkidValue,
         a.BLOB_COL_NAME AS blobColName,
         a.BLOB_COL_VALUE AS blobColValue
     </sql>
     <sql id="blobFieldJoins">
     </sql>
     <select id="get" resultType="blobField">
         SELECT
         <include refid="blobFieldColumns" />
         FROM BLOB_FIELD a
         <include refid="blobFieldJoins" />
         WHERE a.ID = #{id}
     </select>
     <select id="findList" resultType="blobField">
         SELECT
         <include refid="blobFieldColumns" />
         FROM BLOB_FIELD a
         <include refid="blobFieldJoins" />
     </select>
     <insert id="insert">
         INSERT INTO BLOB_FIELD(
         ID ,
         TAB_NAME ,
         TAB_PKID_VALUE ,
         BLOB_COL_NAME ,
         BLOB_COL_VALUE
         ) VALUES (
         #{id},
         #{tabName},
         #{tabPkidValue},
         #{blobColName},
         #{blobColValue,jdbcType=BLOB}
         )
     </insert>
     <update id="update">
         UPDATE BLOB_FIELD SET
         TAB_NAME = #{tabName},
         TAB_PKID_VALUE = #{tabPkidValue},
         BLOB_COL_NAME = #{blobColName},
         BLOB_COL_VALUE = #{blobColValue}
         WHERE ID = #{id}
     </update>
     <delete id="delete">
         DELETE FROM BLOB_FIELD
         WHERE ID = #{id}
     </delete>
     
</mapper>
 
------------ 保存blob字段
/**
     * 附件上传
     *
     * @param testId
     *            主表Id
     * @param request
     * @return
     * @throws UnsupportedEncodingException
     */
    @RequiresPermissions("exc:exceptioninfo:feedback")
    @RequestMapping(value = "attachment", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> uploadAttachment(@RequestParam(value = "testId", required = true) String testId,
 
HttpServletRequest request)
            throws UnsupportedEncodingException {
        Map<String, Object> result = new HashMap<String, Object>();
 
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        // 获得文件
        MultipartFile multipartFile = multipartRequest.getFile("Filedata");// 与前端设置的fileDataName属性值一致
        String filename = multipartFile.getOriginalFilename();// 文件名称
        InputStream is = null;
        try {
            //读取文件流
            is = multipartFile.getInputStream();
            byte[] bytes = FileCopyUtils.copyToByteArray(is);
            BlobField blobField = new BlobField();
            blobField.setTabName("testL");
            blobField.setTabPkidValue(testId);
            blobField.setBlobColName("attachment");
            blobField.setBlobColValue(bytes);
            //保存blob字段
            this.testService.save(blobField, testId, filename);
            result.put("flag", true);
            result.put("attachmentId", blobField.getId());
            result.put("attachmentName", filename);
        } catch (IOException e) {
            e.printStackTrace();
            result.put("flag", false);
        } finally {
            IOUtils.closeQuietly(is);
        }
        return result;
    }
-------------------- 读取blob字段
/**
     * 下载附件
     *
     * @param attachmentId
     * @return
     */
    @RequiresPermissions("exc:exceptioninfo:view")
    @RequestMapping(value = "download", method = RequestMethod.GET)
    public void download(@RequestParam(value = "attachmentId", required = true) String attachmentId,
            @RequestParam(value = "attachmentName", required = true) String attachmentName, HttpServletRequest
 
request, HttpServletResponse response) {
        ServletOutputStream out = null;
        try {
            response.reset();
            String userAgent = request.getHeader("User-Agent");
            byte[] bytes = userAgent.contains("MSIE") ? attachmentName.getBytes() : attachmentName.getBytes("UTF-
 
8"); // fileName.getBytes("UTF-8")处理safari的乱码问题
            String fileName = new String(bytes, "ISO-8859-1");
            // 设置输出的格式
            response.setContentType("multipart/form-data");
            response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(attachmentName,
 
"UTF-8"));
            BlobField blobField = this.blobFieldService.get(attachmentId);
            //获取blob字段
            byte[] contents = blobField.getBlobColValue();
            out = response.getOutputStream();
            //写到输出流
            out.write(contents);
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
知人者智,自知者明,胜人者有力,自胜者强。
原文地址:https://www.cnblogs.com/nanfengxiangbei/p/14182956.html