Mybatis中动态的<trim>标签作用,以及#{jdbcType}的用处

本文详细解析了MyBatis中的trim元素,包括prefix前缀、suffix后缀、suffixOverrides去除逗号以及jdbcType的作用。trim元素主要用于动态SQL拼接,避免空值时引发的类型转换错误。理解jdbcType的重要性在于确保null值转换时不出现类型混淆。

例子

select * from user
<trim perfix='values (' suffix=')' suffixOverrides=','>

	   <if test="userId != null">
            #{userId,jdbcType=CHAR},
        </if>
        <if test="loginName != null">
            #{loginName,jdbcType=VARCHAR},
        </if>

</trim>
perfix 表示前面加什么
suffix 表示后面加什么
suffixOverrides 表示最后一项去掉什么
perfixOverrides 表示第一项去掉什么

jdbcType

在执行SQL时MyBatis会自动通过对象中的属性给SQL中参数赋值,它会自动将Java类型转换成数据库的类型。而一旦传入的是null 程序就无法准确判断这个类型应该是什么(是Integer?是VARCHAR?还是别的?),就有可能将类型转换错误,从而报错。
加入jdbcType正是为了解决这样的报错,需要针对这些可能为空的字段,手动指定其转换时用到的类型。
一般情况下,我们没有必要按个字段去识别/判断它是否可以为空,而是将所有的字段都当做可以为空,全部手动设置转换类型。
在这里插入图片描述

总结

Mybatis的补充

<?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.rabbiter.bms.mapper.UserMapper"> <resultMap id="BaseResultMap" type="com.rabbiter.bms.model.User"> <id column="userId" jdbcType="INTEGER" property="userid" /> <result column="userName" jdbcType="VARCHAR" property="username" /> <result column="userPassword" jdbcType="VARCHAR" property="userpassword" /> <result column="isAdmin" jdbcType="TINYINT" property="isadmin" /> </resultMap> <sql id="Base_Column_List"> userId, userName, userPassword, isAdmin </sql> <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from user where userId = #{userid,jdbcType=INTEGER} </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer"> delete from user where userId = #{userid,jdbcType=INTEGER} </delete> <insert id="insert" parameterType="com.rabbiter.bms.model.User"> insert into user (userId, userName, userPassword, isAdmin) values (#{userid,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{userpassword,jdbcType=VARCHAR}, #{isadmin,jdbcType=TINYINT}) </insert> <insert id="insertSelective" parameterType="com.rabbiter.bms.model.User"> insert into user <trim prefix="(" suffix=")" suffixOverrides=","> <if test="userid != null"> userId, </if> <if test="username != null"> userName, </if> <if test="userpassword != null"> userPassword, </if> <if test="isadmin != null"> isAdmin, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides=","> <if test="userid != null"> #{userid,jdbcType=INTEGER}, </if> <if test="username != null"> #{username,jdbcType=VARCHAR}, </if> <if test="userpassword != null"> #{userpassword,jdbcType=VARCHAR}, </if> <if test="isadmin != null"> #{isadmin,jdbcType=TINYINT}, </if> </trim> </insert> <update id="updateByPrimaryKeySelective" parameterType="com.rabbiter.bms.model.User"> update user <set> <if test="username != null"> userName = #{username,jdbcType=VARCHAR}, </if> <if test="userpassword != null"> userPassword = #{userpassword,jdbcType=VARCHAR}, </if> <if test="isadmin != null"> isAdmin = #{isadmin,jdbcType=TINYINT}, </if> </set> where userId = #{userid,jdbcType=INTEGER} </update> <update id="updateByPrimaryKey" parameterType="com.rabbiter.bms.model.User"> update user set userName = #{username,jdbcType=VARCHAR}, userPassword = #{userpassword,jdbcType=VARCHAR}, isAdmin = #{isadmin,jdbcType=TINYINT} where userId = #{userid,jdbcType=INTEGER} </update> <select id="selectByUsernameAndPasswordAndIsAdmin" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from user where userName = #{username} and userPassword = #{password} and isAdmin = #{isAdmin} limit 1 </select> <select id="selectByUsername" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from user where userName = #{username} limit 1 </select> <select id="selectAllByLimit" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from user limit #{begin}, #{size} </select> <select id="selectCount" resultType="int"> select count(*) from user </select> <select id="selectAll" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from user </select> <select id="selectCountBySearch" resultType="int"> select count(*) from user <where> <if test="username != null and username != &#39;&#39; "> and userName like concat(&#39;%&#39;,#{username},&#39;%&#39;) </if> </where> </select> <select id="selectBySearch" resultMap="BaseResultMap"> select userId, userName, userPassword, isAdmin from user <where> <if test="username != null and username != &#39;&#39; "> and userName like concat(&#39;%&#39;,#{username},&#39;%&#39;) </if> </where>这里有修改密码的相关代码吗 limit #{begin}, #{size} </select> </mapper>
06-26
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

商朝

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值