Mybatis之映射實(shí)體類中不區(qū)分大小寫(xiě)的解決
Mybatis 映射實(shí)體類中不區(qū)分大小寫(xiě)
做項(xiàng)目時(shí)候遇到一個(gè)Bug,實(shí)體類中有兩個(gè)字段,例如(addTime,addtime),進(jìn)行查詢搜索會(huì)發(fā)生神奇的事情
<?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.runlin.jetta.mapper.JettaUpgradeLogMapper">
<resultMap id="BaseResultMap" type="cn.runlin.jetta.entity.JettaUpgradeLog">
<id column="upgrade_id" jdbcType="INTEGER" property="upgradeId" />
<result column="task_id" jdbcType="INTEGER" property="taskId" />
<result column="task_name" jdbcType="VARCHAR" property="taskName" />
<result column="task_version" jdbcType="VARCHAR" property="taskVersion" />
<result column="project_id" jdbcType="INTEGER" property="projectId" />
<result column="project_name" jdbcType="VARCHAR" property="projectName" />
<result column="project_type" jdbcType="TINYINT" property="projectType" />
<result column="dealer_id" jdbcType="INTEGER" property="dealerId" />
<result column="dealer_name" jdbcType="VARCHAR" property="dealerName" />
<result column="service_code" jdbcType="VARCHAR" property="serviceCode" />
<result column="add_time" jdbcType="TIMESTAMP" property="addTime" />
<result column="addtime" jdbcType="INTEGER" property="addtime" />
<result column="status" jdbcType="INTEGER" property="status" />
<result column="reasonname" jdbcType="VARCHAR" property="reasonname" />
</resultMap>
<sql id="Base_Column_List">
upgrade_id, task_id, task_name, task_version, project_id, project_name, project_type,
dealer_id, dealer_name, service_code, add_time, addtime
</sql>
//映射到實(shí)體類 而不使用xml文件中的BaseResultMap
<select id="getJettaUpgradeLogList" resultType="cn.runlin.jetta.entity.JettaUpgradeLog" parameterType="Map">
select
jul.upgrade_id, task_id, task_name, task_version, project_id, project_name, project_type,
dealer_id, dealer_name, service_code, add_time, status, reasonname
from jetta_upgrade_log jul
LEFT OUTER JOIN jetta_upgrade_log_status juls
ON jul.upgrade_id=juls.upgrade_id
LEFT OUTER JOIN jetta_status_code jsc
ON juls.status_id= jsc.rid
<where>
<if test="serviceCode != null and serviceCode !='' ">
AND jul.service_code like concat("%",#{serviceCode},"%")
</if>
<if test="dealerName != null and dealerName !='' ">
AND jul.dealer_name like concat("%",#{dealerName},"%")
</if>
<if test="taskVersion != null and taskVersion !='' ">
AND jul.task_version like concat("%",#{taskVersion},"%")
</if>
<if test="status != null and status !='' ">
AND juls.status like concat("%",#{status},"%")
</if>
</where>
</select>
</mapper>

會(huì)報(bào)addTime是時(shí)間戳類型,不能轉(zhuǎn)換成INTEGER類型的問(wèn)題。原因就是mybatis映射實(shí)體類之后,不能區(qū)分大小寫(xiě),而造成字段類型對(duì)應(yīng)錯(cuò)誤的問(wèn)題
解決辦法
把映射實(shí)體類變成映射到xml中所設(shè)置的resultMap,就可以解決這個(gè)問(wèn)題
<?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.runlin.jetta.mapper.JettaUpgradeLogMapper">
<resultMap id="BaseResultMap" type="cn.runlin.jetta.entity.JettaUpgradeLog">
<id column="upgrade_id" jdbcType="INTEGER" property="upgradeId" />
<result column="task_id" jdbcType="INTEGER" property="taskId" />
<result column="task_name" jdbcType="VARCHAR" property="taskName" />
<result column="task_version" jdbcType="VARCHAR" property="taskVersion" />
<result column="project_id" jdbcType="INTEGER" property="projectId" />
<result column="project_name" jdbcType="VARCHAR" property="projectName" />
<result column="project_type" jdbcType="TINYINT" property="projectType" />
<result column="dealer_id" jdbcType="INTEGER" property="dealerId" />
<result column="dealer_name" jdbcType="VARCHAR" property="dealerName" />
<result column="service_code" jdbcType="VARCHAR" property="serviceCode" />
<result column="add_time" jdbcType="TIMESTAMP" property="addTime" />
<result column="addtime" jdbcType="INTEGER" property="addtime" />
<result column="status" jdbcType="INTEGER" property="status" />
<result column="reasonname" jdbcType="VARCHAR" property="reasonname" />
</resultMap>
<sql id="Base_Column_List">
upgrade_id, task_id, task_name, task_version, project_id, project_name, project_type,
dealer_id, dealer_name, service_code, add_time, addtime
</sql>
//映射到xml的BaseResultMap
<select id="getJettaUpgradeLogList" resultMap="BaseResultMap" parameterType="Map">
select
jul.upgrade_id, task_id, task_name, task_version, project_id, project_name, project_type,
dealer_id, dealer_name, service_code, add_time, status, reasonname
from jetta_upgrade_log jul
LEFT OUTER JOIN jetta_upgrade_log_status juls
ON jul.upgrade_id=juls.upgrade_id
LEFT OUTER JOIN jetta_status_code jsc
ON juls.status_id= jsc.rid
<where>
<if test="serviceCode != null and serviceCode !='' ">
AND jul.service_code like concat("%",#{serviceCode},"%")
</if>
<if test="dealerName != null and dealerName !='' ">
AND jul.dealer_name like concat("%",#{dealerName},"%")
</if>
<if test="taskVersion != null and taskVersion !='' ">
AND jul.task_version like concat("%",#{taskVersion},"%")
</if>
<if test="status != null and status !='' ">
AND juls.status like concat("%",#{status},"%")
</if>
</where>
</select>
</mapper>
問(wèn)題解決

Mybatis的一些小細(xì)節(jié)
Mybatis要解決的問(wèn)題:
1. 將sql語(yǔ)句硬編碼到j(luò)ava代碼中,如果修改sql語(yǔ)句,需要修改java代碼,重新編譯。系統(tǒng)可維護(hù)性不高。
設(shè)想如何解決?
能否將sql單獨(dú)配置在配置文件中。
2. 數(shù)據(jù)庫(kù)連接頻繁開(kāi)啟和釋放,對(duì)數(shù)據(jù)庫(kù)的資源是一種浪費(fèi)。
設(shè)想如何解決?
使用數(shù)據(jù)庫(kù)連接池管理數(shù)據(jù)庫(kù)連接。
3. 向preparedStatement中占位符的位置設(shè)置參數(shù)時(shí),存在硬編碼(占位符的位置,設(shè)置的變量值)
設(shè)想如何解決?
能否也通過(guò)配置的方式,配置設(shè)置的參數(shù),自動(dòng)進(jìn)行設(shè)置參數(shù)
4. 解析結(jié)果集時(shí)存在硬編碼(表的字段名、字段的類型)
設(shè)想如何解決?
能否將查詢結(jié)果集映射成java對(duì)象。

問(wèn)題一. #{}和${}的區(qū)別是什么?
#{}是預(yù)編譯處理,${}是字符串替換。
Mybatis在處理#{}時(shí),會(huì)將sql中的#{}替換為?號(hào),調(diào)用PreparedStatement的set方法來(lái)賦值;
Mybatis在處理時(shí),就是把{}替換成變量的值。
使用#{}可以有效的防止SQL注入,提高系統(tǒng)安全性。
問(wèn)題二. 當(dāng)實(shí)體類中的屬性名和表中的字段名不一樣,怎么辦
第1種: 通過(guò)在查詢的sql語(yǔ)句中定義字段名的別名,讓字段名的別名和實(shí)體類的屬性名一致
<select id=”selectorder” parametertype=”int” resultetype=”me.gacl.domain.order”>
select order_id id, order_no orderno ,order_price price form orders where order_id=#{id};
</select>
第2種: 通過(guò)<resultMap>來(lái)映射字段名和實(shí)體類屬性名的一一對(duì)應(yīng)的關(guān)系
<select id="getOrder" parameterType="int" resultMap="orderresultmap">
select * from orders where order_id=#{id}
</select>
<resultMap type=”me.gacl.domain.order” id=”orderresultmap”>
<!–用id屬性來(lái)映射主鍵字段–>
<id property=”id” column=”order_id”>
<!–用result屬性來(lái)映射非主鍵字段,property為實(shí)體類屬性名,column為數(shù)據(jù)表中的屬性–>
<result property = “orderno” column =”order_no”/>
<result property=”price” column=”order_price” />
</reslutMap>
問(wèn)題三. 模糊查詢like語(yǔ)句該怎么寫(xiě)
string wildcardname = “smi”;
list<name> names = mapper.selectlike(wildcardname);
<select id=”selectlike”>
select * from foo where bar like "%"#{value}"%"
</select>
1.表達(dá)式: name like"%"#{name}"%" #起到占位符的作用
2.表達(dá)式: name like '%${name}%' $進(jìn)行字符串的拼接,直接把傳入的值,拼接上去了,沒(méi)有任何問(wèn)題
表達(dá)式: name likeconcat(concat('%',#{username}),'%') 這是使用了cancat進(jìn)行字符串的連接,同時(shí)使用了#進(jìn)行占位
表達(dá)式:name like CONCAT('%','${name}','%') 對(duì)上面的表達(dá)式進(jìn)行了簡(jiǎn)化,更方便了
問(wèn)題四. 通常一個(gè)Xml映射文件
都會(huì)寫(xiě)一個(gè)Dao接口與之對(duì)應(yīng),請(qǐng)問(wèn),這個(gè)Dao接口的工作原理是什么?Dao接口里的方法,參數(shù)不同時(shí),方法能重載嗎?
Dao接口,就是人們常說(shuō)的Mapper接口,接口的全限名,就是映射文件中的namespace的值,接口的方法名,就是映射文件中MappedStatement的id值,接口方法內(nèi)的參數(shù),就是傳遞給sql的參數(shù)。
Mapper接口是沒(méi)有實(shí)現(xiàn)類的,當(dāng)調(diào)用接口方法時(shí),接口全限名+方法名拼接字符串作為key值,可唯一定位一個(gè)MappedStatement,舉例:com.mybatis3.mappers.StudentDao.findStudentById,可以唯一找到namespace為com.mybatis3.mappers.StudentDao下面id = findStudentById的MappedStatement。在Mybatis中,每一個(gè)<select>、<insert>、<update>、<delete>標(biāo)簽,都會(huì)被解析為一個(gè)MappedStatement對(duì)象。
Dao接口里的方法,是不能重載的,因?yàn)槭侨廾?方法名的保存和尋找策略。
Dao接口的工作原理是JDK動(dòng)態(tài)代理,Mybatis運(yùn)行時(shí)會(huì)使用JDK動(dòng)態(tài)代理為Dao接口生成代理proxy對(duì)象(如使用spring會(huì)注入到容器中),代理對(duì)象proxy會(huì)攔截接口方法,轉(zhuǎn)而執(zhí)行MappedStatement所代表的sql,然后將sql執(zhí)行結(jié)果返回。
問(wèn)題五. Mybatis是如何將sql執(zhí)行結(jié)果封裝為目標(biāo)對(duì)象并返回的
都有哪些映射形式?
答:第一種是使用<resultMap>標(biāo)簽,逐一定義列名和對(duì)象屬性名之間的映射關(guān)系。
第二種是使用sql列的別名功能,將列別名書(shū)寫(xiě)為對(duì)象屬性名,比如T_NAME AS NAME,對(duì)象屬性名一般是name,小寫(xiě),但是列名不區(qū)分大小寫(xiě),Mybatis會(huì)忽略列名大小寫(xiě),智能找到與之對(duì)應(yīng)對(duì)象屬性名,你甚至可以寫(xiě)成T_NAME AS NaMe,Mybatis一樣可以正常工作。
有了列名與屬性名的映射關(guān)系后,Mybatis通過(guò)反射創(chuàng)建對(duì)象,同時(shí)使用反射給對(duì)象的屬性逐一賦值并返回,那些找不到映射關(guān)系的屬性,是無(wú)法完成賦值的。
問(wèn)題六. 如何獲取自動(dòng)生成的(主)鍵值
insert 方法總是返回一個(gè)int值 - 這個(gè)值代表的是插入的行數(shù)。
而自動(dòng)生成的鍵值在 insert 方法執(zhí)行完后可以被設(shè)置到傳入的參數(shù)對(duì)象中。
示例:
<insert id="insertUserMessage" parameterType="com.xxx.xxx.model.UserMessage"
useGeneratedKeys="true" keyProperty="userMessage.id">
insert into my_news
(orderid,commentid,type,title,content,createtime)
values
(#{userMessage.orderid},#{userMessage.commentid},#{userMessage.type},#{userMessage.title}
,#{userMessage.content},#{userMessage.createtime})
</insert>
這里需要注意的是需要把實(shí)體類傳進(jìn)來(lái)。keyProperty為自增的id字段。調(diào)用insert后自動(dòng)將自增id賦值進(jìn)insert調(diào)用的實(shí)體類中
//新建對(duì)象 UserMessage userMessage = new UserMessage(); userMessage.setXxxxxx(xxxxxx); userMessageDao.insertUserMessage(userMessage); //這時(shí)userMessage.getId()就可以獲取到自增主鍵了 BigInteger id = userMessage.getId();
問(wèn)題七. 在mapper中如何傳遞多個(gè)參數(shù)
第1種:
//DAO層的函數(shù)
Public UserselectUser(String name,String area);
//對(duì)應(yīng)的xml,#{0}代表接收的是dao層中的第一個(gè)參數(shù),#{1}代表dao層中第二參數(shù),更多參數(shù)一致往后加即可。
<select id="selectUser"resultMap="BaseResultMap">
select * fromuser_user_t whereuser_name = #{0} anduser_area=#{1}
</select>
第2種: 使用 @param 注解:
import org.apache.ibatis.annotations.param;
public interface usermapper {
user selectuser(@param(“username”) string username,
@param(“hashedpassword”) string hashedpassword);
}
然后,就可以在xml像下面這樣使用(推薦封裝為一個(gè)map,作為單個(gè)參數(shù)傳遞給mapper
<select id=”selectuser” resulttype=”user”>
select id, username, hashedpassword
from some_table
where username = #{username}
and hashedpassword = #{hashedpassword}
</select>
問(wèn)題八. Mybatis動(dòng)態(tài)sql是做什么的
都有哪些動(dòng)態(tài)sql?能簡(jiǎn)述一下動(dòng)態(tài)sql的執(zhí)行原理不?
Mybatis動(dòng)態(tài)sql可以讓我們?cè)赬ml映射文件內(nèi),以標(biāo)簽的形式編寫(xiě)動(dòng)態(tài)sql,完成邏輯判斷和動(dòng)態(tài)拼接sql的功能。
Mybatis提供了9種動(dòng)態(tài)sql標(biāo)簽:trim|where|set|foreach|if|choose|when|otherwise|bind。
其執(zhí)行原理為,從sql參數(shù)對(duì)象中計(jì)算表達(dá)式的值,根據(jù)表達(dá)式的值動(dòng)態(tài)拼接sql,以此來(lái)完成動(dòng)態(tài)sql的功能。
比如:
<select id="findUserById" resultType="user">
select * from user where
<if test="id != null">
id=#{id}
</if>
and deleteFlag=0;
</select>
問(wèn)題九. Mybatis的Xml映射文件中
不同的Xml映射文件,id是否可以重復(fù)?
不同的Xml映射文件,如果配置了namespace,那么id可以重復(fù);如果沒(méi)有配置namespace,那么id不能重復(fù);畢竟namespace不是必須的,只是最佳實(shí)踐而已。
原因就是namespace+id是作為Map<String, MappedStatement>的key使用的,如果沒(méi)有namespace,就剩下id,那么,id重復(fù)會(huì)導(dǎo)致數(shù)據(jù)互相覆蓋。有了namespace,自然id就可以重復(fù),namespace不同,namespace+id自然也就不同。
問(wèn)題十. 為什么說(shuō)Mybatis是半自動(dòng)ORM映射工具
它與全自動(dòng)的區(qū)別在哪里?
Hibernate屬于全自動(dòng)ORM映射工具,使用Hibernate查詢關(guān)聯(lián)對(duì)象或者關(guān)聯(lián)集合對(duì)象時(shí),可以根據(jù)對(duì)象關(guān)系模型直接獲取,所以它是全自動(dòng)的。而Mybatis在查詢關(guān)聯(lián)對(duì)象或關(guān)聯(lián)集合對(duì)象時(shí),需要手動(dòng)編寫(xiě)sql來(lái)完成,所以,稱之為半自動(dòng)ORM映射工具。
問(wèn)題十一. 一對(duì)一、一對(duì)多的關(guān)聯(lián)查詢
<mapper namespace="com.lcb.mapping.userMapper">
<!--association 一對(duì)一關(guān)聯(lián)查詢 -->
<select id="getClass" parameterType="int" resultMap="ClassesResultMap">
select * from class c,teacher t where c.teacher_id=t.t_id and c.c_id=#{id}
</select>
<resultMap type="com.lcb.user.Classes" id="ClassesResultMap">
<!-- 實(shí)體類的字段名和數(shù)據(jù)表的字段名映射 -->
<id property="id" column="c_id"/>
<result property="name" column="c_name"/>
<association property="teacher" javaType="com.lcb.user.Teacher">
<id property="id" column="t_id"/>
<result property="name" column="t_name"/>
</association>
</resultMap>
<!--collection 一對(duì)多關(guān)聯(lián)查詢 -->
<select id="getClass2" parameterType="int" resultMap="ClassesResultMap2">
select * from class c,teacher t,student s where c.teacher_id=t.t_id and c.c_id=s.class_id and c.c_id=#{id}
</select>
<resultMap type="com.lcb.user.Classes" id="ClassesResultMap2">
<id property="id" column="c_id"/>
<result property="name" column="c_name"/>
<association property="teacher" javaType="com.lcb.user.Teacher">
<id property="id" column="t_id"/>
<result property="name" column="t_name"/>
</association>
<collection property="student" ofType="com.lcb.user.Student">
<id property="id" column="s_id"/>
<result property="name" column="s_name"/>
</collection>
</resultMap>
</mapper>
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Kafka中的producer攔截器與consumer攔截器詳解
這篇文章主要介紹了Kafka中的producer攔截器與consumer攔截器詳解,Producer 的Interceptor使得用戶在消息發(fā)送前以及Producer回調(diào)邏輯前有機(jī)會(huì)對(duì)消息做 一些定制化需求,比如修改消息等,需要的朋友可以參考下2023-12-12
Springboot項(xiàng)目刪除項(xiàng)目同步target文件問(wèn)題解決方案
這篇文章主要介紹了Springboot項(xiàng)目刪除項(xiàng)目同步target文件問(wèn)題解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12
SpringCloud Alibaba使用Seata處理分布式事務(wù)的技巧
在傳統(tǒng)的單體項(xiàng)目中,我們使用@Transactional注解就能實(shí)現(xiàn)基本的ACID事務(wù)了,隨著微服務(wù)架構(gòu)的引入,需要對(duì)數(shù)據(jù)庫(kù)進(jìn)行分庫(kù)分表,每個(gè)服務(wù)擁有自己的數(shù)據(jù)庫(kù),這樣傳統(tǒng)的事務(wù)就不起作用了,那么我們?nèi)绾伪WC多個(gè)服務(wù)中數(shù)據(jù)的一致性呢?跟隨小編一起通過(guò)本文了解下吧2021-06-06
IDEA整合Dubbo+Zookeeper+SpringBoot實(shí)現(xiàn)
初學(xué)者,想自己動(dòng)手做一個(gè)簡(jiǎn)單的demo,本文主要介紹了IDEA整合Dubbo+Zookeeper+SpringBoot實(shí)現(xiàn),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-06-06
使用mybatis報(bào)Invalid bound statement解決分析
這篇文章主要為大家介紹了使用mybatis報(bào)Invalid bound statement原因解決分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
Java中Stream的flatMap與map使用場(chǎng)景及區(qū)別詳解
這篇文章主要介紹了Java中Stream的flatMap與map使用場(chǎng)景及區(qū)別詳解,Stream 流式操作,一般用于操作集合即 List 一類的數(shù)據(jù)結(jié)構(gòu),簡(jiǎn)單來(lái)說(shuō) Stream 的 map 使得其中的元素轉(zhuǎn)為另一種元素的映射(map)方法,需要的朋友可以參考下2024-01-01
在Java的Spring框架的程序中使用JDBC API操作數(shù)據(jù)庫(kù)
這篇文章主要介紹了在Java的Spring框架的程序中使用JDBC API操作數(shù)據(jù)庫(kù)的方法,并通過(guò)示例展示了其存儲(chǔ)過(guò)程以及基本SQL語(yǔ)句的應(yīng)用,需要的朋友可以參考下2015-12-12

