用mybatis做数据库处理 代码中的字段大小写 要和mapper映射设置的大小写一致(这TM不废话么,原谅我渣!~~)

简单描述情况:其实这个问题怎么说呢,有人给你说,你肯定能意识到,必须大小写对应的嘛。emmmm我现在才意识到是因为:自己在下边敲代码,配的mapper文件并没有把属性变量,和数据库里的段单独提出来做映射,基本上都是嵌在sql语句中的,大小写也都一样,所以从来没遇到过这种弱智白痴加213的问题。进公司之后呢,字段已经不是我原来自己做的东西那么少了,十几个算是少的,所以公司都是在mapper中单独拎出来做了集中处理,这里就是我的错误所在了,这里mapper设置的字段是大写的,而我class 以及各层的属性变量都是小写的!~~   废话不多说,下边贴代码,先贴以前的,再贴现在的。放心这些字段都不是公司的,都是我自己瞎起名的,想表达的就是一个字段提取出来进行映射的格式而已  。

代码部分:以前的自己写的:

<insert id="insertEvent" parameterType="map">
insert into event(id,user_id,title,event,e_date,s_date,end_date) values (#{id},#{user_id},#{title},#{event},#{e_date},#{s_date},#{end_date})
</insert>

<select id="selectEvent" parameterType="map" resultType="map">
select * from event where user_id=#{user_id}
</select>

现在的:

结果集映射:

<resultMap id="ProductResultMap" type="SysDict">
<result column="product_id" property="productId" jdbcType="CHAR" />
<result column="product_name" property="productName" jdbcType="VARCHAR" />
<result column="product_code" property="productCode" jdbcType="VARCHAR" />
<result column="sort_no" property="sortNo" jdbcType="TINYINT" />
<result column="parent_id" property="parentId" jdbcType="CHAR" />
<result column="description" property="description" jdbcType="VARCHAR" />
</resultMap>

表映射:

<sql id="tableName">
product
</sql>

字段映射:

<sql id="Field">
product_id,
product_name,
product_code,
sort_no,
parent_id,
description
</sql>

数据库字段映射:

<sql id="FieldValue">
#{productId},
#{productName},
#{productCode},
#{sortNo},
#{parentId},
#{description}
</sql>

语句:

<select id="find" resultMap="ProductResultMap" >
select <include refid="Field" />from
<include refid="tableName"></include>
where parent_Id is null or parent_Id = ''
</select>
<insert id="save" parameterType="Product">
insert into
<include refid="tableName"></include>
(
<include refid="Field"></include>
)
values
(
<include refid="FieldValue"></include>
)
</insert>

简单总结一下:可以看出来,这样做的好处就时sql语句与字段的耦合度明显降低了,可以进行统一的管理,而且都是自动映射,需要注意的就是前边的字段映射的大小写,要和代码中的一致,千万记住,别犯这种低级错误!!!

原文地址:https://www.cnblogs.com/xuchao0506/p/9558507.html