MyBatis使用细节

1. 错误:with a primitive return type (int).

原因:返回值类型为int,但是查询出来的结果有空值(null),无法转换成基本类型。包括char,long,short都有可能。

解决方式:1. select ifnull(value, 0) from xxx;结果为空时默认返回返回值0。2. 如果where 条件命中不上,select语句则不会执行(where语句比select先执行),返回值仍为null。此情况一是使用 select case when then语句。二是修改Mapper接口中的返回值类型,改用包装类(Integer,Short,Long等),并在业务逻辑中处理null值。

select

CASE

WHEN (select provinceID from kdmc_t_province where name =#{province})is null

THEN 0
ELSE (select provinceID from kdmc_t_province where name =#{province})
END;

2. resultType和resultMap区别

查询出来的结果集只有一行且一列,可以使用简单类型进行输出映射。通过resultType映射。

如果查询结果复杂,需要封装成bean,则适合使用resultMap,定义映射规则。

3. 动态传入表名,例如数据记录日表

先区分#{}和${}的却别,简略来讲${}单纯填充字段,不做预编译;#{}有预编译过程,可以防止SQL注入。(#{} 是编译好SQL语句再取值,${} 这是取值以后再去编译SQL语句)

那么,动态传入表名则需要使用${}方式,例如select * from ${prefix}ACT_HI_PROCINST where PROC_INST_ID_ = #{processInstanceId},另外SQL语句配置为statementType="STATEMENT",即不进行预编译。

4. 多参数传递

单个参数配置文件映射时能自动识别,多参数则需要指定参数,一是通过Map传递,二是封装成bean,三是在接口通过注解标明。例如:

Integer getAccountPermissionValue(@Param("publicId") long publicId,
                                  @Param("prvCode") String prvCode) throws Exception;

 

4. 随机数生成

private String getFixLenthString(int strLength) {
        Random rm = new Random();
        // 获得随机数
        double pross = (1 + rm.nextDouble()) * Math.pow(10, strLength);
        // 将获得的获得随机数转化为字符串
        String fixLenthString = String.valueOf(pross);
        // 返回固定的长度的随机数
        return fixLenthString.substring(2, strLength + 2);

5. insert返回主键id

insert语句配置useGeneratedKeys="true" keyProperty="id"即可,对于insert ignore into,insert into on duplicate key update不生效。另外param需以bean的方式传入,且接口通过bean.getId()获取主键id,sql返回结果只表示执行的状态。

6. resultMap示例

<resultMap id="pubDownMaterial" type="com.cmb.zh.pms.mapper.pojo.message.PubDownImageText"
               autoMapping="true">
        <id column="id" property="id" jdbcType="INTEGER" />
        <result column="msg_id" property="msgId" jdbcType="INTEGER" />
        <result column="image_text_id" property="imageTextId" jdbcType="INTEGER" />
        <result column="material_id" property="materialId" jdbcType="INTEGER" />
        <result column="title" property="title" jdbcType="VARCHAR"/>
        <result column="content" property="content" jdbcType="VARCHAR"/>
        <result column="author" property="author" jdbcType="VARCHAR"/>
        <result column="summary" property="summary" jdbcType="VARCHAR"/>
        <result column="res_id" property="resId" jdbcType="INTEGER" />
        <result column="click_url" property="clickUrl" jdbcType="VARCHAR"/>
        <result column="show_order" property="showOrder" jdbcType="SMALLINT" />
        <result column="title_pic_show" property="showTitlePic" jdbcType="TINYINT" />
        <result column="link_url" property="linkUrl" jdbcType="VARCHAR"/>
        <result column="comment_flag" property="commentFlag" jdbcType="CHAR"/>

       <result column="res_create_time" property="resCreateTime" jdbcType="TIMESTAMP"/>
    </resultMap>

 

7. 批量插入示例

<insert id ="batchSavePubDownMsg" parameterType="java.util.List" >
        <selectKey resultType ="java.lang.Integer" keyProperty= "id"
                   order= "AFTER">
            SELECT LAST_INSERT_ID()
        </selectKey >
        insert into redeem_code
        (bach_id, code, type, facevalue,create_user,create_time)
        values
        <foreach collection ="list" item="reddemCode" index= "index" separator =",">
            (
            #{reddemCode.batchId}, #{reddemCode.code},
            #{reddemCode.type},
            #{reddemCode.facevalue},
            #{reddemCode.createUser}, #{reddemCode.createTime}
            )
        </foreach >
    </insert >

8. 文件操作 FileUtils(org.apache.commons.io)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值