Mybatis foreach標(biāo)簽使用不當(dāng)導(dǎo)致異常的原因淺析
異常產(chǎn)生場(chǎng)景及異常信息
上周,由于在Mybatis的Mapper接口方法中使用實(shí)現(xiàn)了Map.Entry接口的泛型類(lèi),同時(shí)此方法對(duì)應(yīng)的sql語(yǔ)句也使用了foreach標(biāo)簽,導(dǎo)致出現(xiàn)了異常。如下為異常信息:
org.apache.ibatis.exceptions.PersistenceException: ### Error updating database. Cause: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'key' in 'class java.lang.Integer' ### The error may involve org.guojing.test.spring.server.GoodsRoomnight30daysMapper.insertBatch-Inline ### The error occurred while setting parameters ### SQL: REPLACE INTO goods_roomnight_30days (goods_id, checkin_room_night_30days) VALUES (?, ?) , (?, ?), (?, ?), (?, ?) ### Cause: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'key' in 'class java.lang.Integer' at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30) at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:200) at org.apache.ibatis.session.defaults.DefaultSqlSession.insert(DefaultSqlSession.java:185) at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:57) at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:53) at com.sun.proxy.$Proxy4.insertBatch(Unknown Source) at org.guojing.test.spring.server.GoodsRoomnight30daysTest.test(GoodsRoomnight30daysTest.java:47) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.junit.runner.JUnitCore.run(JUnitCore.java:160) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) Caused by: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'key' in 'class java.lang.Integer' at org.apache.ibatis.reflection.Reflector.getGetInvoker(Reflector.java:409) at org.apache.ibatis.reflection.MetaClass.getGetInvoker(MetaClass.java:164) at org.apache.ibatis.reflection.wrapper.BeanWrapper.getBeanProperty(BeanWrapper.java:162) at org.apache.ibatis.reflection.wrapper.BeanWrapper.get(BeanWrapper.java:49) at org.apache.ibatis.reflection.MetaObject.getValue(MetaObject.java:122) at org.apache.ibatis.reflection.MetaObject.getValue(MetaObject.java:119) at org.apache.ibatis.mapping.BoundSql.getAdditionalParameter(BoundSql.java:75) at org.apache.ibatis.scripting.defaults.DefaultParameterHandler.setParameters(DefaultParameterHandler.java:72) at org.apache.ibatis.executor.statement.PreparedStatementHandler.parameterize(PreparedStatementHandler.java:93) at org.apache.ibatis.executor.statement.RoutingStatementHandler.parameterize(RoutingStatementHandler.java:64) at org.apache.ibatis.executor.SimpleExecutor.prepareStatement(SimpleExecutor.java:86) at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:49) at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:117) at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:198) ... 29 more
由于本人對(duì)Mybatis還不是非常的了解,導(dǎo)致用了很長(zhǎng)的時(shí)間去debug找具體的異常原因。
接下來(lái)介紹我將重現(xiàn)異常并分析導(dǎo)致異常的原因。
異常重現(xiàn)
為了重現(xiàn)上面的異常,寫(xiě)了demo。相關(guān)代碼如下:
數(shù)據(jù)庫(kù)表結(jié)構(gòu):
CREATE TABLE `goods_roomnight_30days` ( `goods_id` bigint(20) NOT NULL, `checkin_room_night_30days` int(11) NOT NULL DEFAULT '0' COMMENT '近30天消費(fèi)間夜', PRIMARY KEY (`goods_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='goods的近30天消費(fèi)間夜'
KeyValue.java 參數(shù)類(lèi):
public class KeyValue<K, V> implements Map.Entry<K, V> { private K key; private V value; public KeyValue() { } public KeyValue(K key, V value) { this.key = key; this.value = value; } @Override public K getKey() { return key; } @Override public V getValue() { return value; } @Override public V setValue(V value) { V oldValue = this.value; this.value = value; return oldValue; } public JSONObject toJSONObject() { return ReportJSONObject.newObject().append(String.valueOf(key), value); } @Override public String toString() { return toJSONObject().toJSONString(); } }
DAO類(lèi)GoodsRoomnight30daysMapper.java
public interface GoodsRoomnight30daysMapper { int deleteByExample(GoodsRoomnight30daysExample example); List<GoodsRoomnight30days> selectByExample(GoodsRoomnight30daysExample example); <K, V> int insertBatch(List<KeyValue<K, V>> records); }
mybatis-config.xml文件:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <setting name="cacheEnabled" value="false"/> </settings> <!-- 和spring整合后 environments配置將廢除,交給spring管理--> <environments default="development"> <environment id="development"> <!-- 使用jdbc事務(wù)管理--> <transactionManager type="JDBC" /> <!-- 數(shù)據(jù)庫(kù)連接池,整合后一般使用第三方的連接池--> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/hotel_report?characterEncoding=utf-8" /> <property name="username" value="test_user" /> <property name="password" value="user123" /> </dataSource> </environment> </environments> <mappers> <mapper resource="mybatis/GoodsRoomnight30daysMapper.xml"/> </mappers> </configuration>
GoodsRoomnight30daysMapper.xml文件的主要內(nèi)容:
<insert id="insertBatch" parameterType="list"> REPLACE INTO goods_roomnight_30days (goods_id, checkin_room_night_30days) VALUES <foreach collection="list" index="index" item="item" separator=","> (#{item.key}, #{item.value}) <!--<choose>--> <!--<when test="item.value == null">(#{item.key}, 0)</when>--> <!--<when test="item.value != null">(#{item.key}, #{item.value})</when>--> <!--</choose>--> </foreach> </insert>
以上為重現(xiàn)此異常的主要代碼,完整代碼可在Github查看:https://github.com/misterzhou/java-demo/tree/master/test-spring/spring-server
重現(xiàn)異常的測(cè)試代碼 GoodsRoomnight30daysTest.java:
package org.guojing.test.spring.server; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.guojing.spring.commons.KeyValue; import org.junit.After; import org.junit.Before; import org.junit.Test; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; /** * Created at: 2016-12-24 * * @author guojing */ public class GoodsRoomnight30daysTest { SqlSessionFactory sqlSessionFactory; SqlSession sqlSession; GoodsRoomnight30daysMapper goodsRoomnight30daysMapper; @Before public void init() throws IOException { String resource = "mybatis/mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); sqlSession = sqlSessionFactory.openSession(true); goodsRoomnight30daysMapper = sqlSession.getMapper(GoodsRoomnight30daysMapper.class); } @Test public void test() { List<KeyValue<Long, Integer>> records = new ArrayList<>(); records.add(new KeyValue<Long, Integer>(1725L, 5)); records.add(new KeyValue<Long, Integer>(1728L, 3)); records.add(new KeyValue<Long, Integer>(1730L, null)); records.add(new KeyValue<Long, Integer>(1758L, null)); int deleted = goodsRoomnight30daysMapper.deleteByExample(new GoodsRoomnight30daysExample()); System.out.println("----- deleted row size: " + deleted); int row = goodsRoomnight30daysMapper.insertBatch(records); System.out.println("----- affected row: " + row); List<GoodsRoomnight30days> result = goodsRoomnight30daysMapper.selectByExample(new GoodsRoomnight30daysExample()); for (GoodsRoomnight30days item : result) { System.out.println(item.toString()); } } @After public void after() { if (sqlSession != null) { sqlSession.close(); } } }
賣(mài)個(gè)關(guān)子,大家先不要往下看,想想導(dǎo)致異常的原因(熟練使用foreach標(biāo)簽的同學(xué)應(yīng)該能看出端倪)。
查找異常過(guò)程及異常分析
在項(xiàng)目中,由于DAO方法的參數(shù)類(lèi)和返回結(jié)果類(lèi)經(jīng)常會(huì)包含一個(gè)鍵和鍵對(duì)應(yīng)的值,為了避免重復(fù)定義類(lèi),我定義了一個(gè)實(shí)現(xiàn)了Map.Entry接口的KeyValue泛型類(lèi),具體請(qǐng)查看上節(jié)。
GoodsRoomnight30daysMapper.insertBatch()
方法就使用了此泛型類(lèi)。在運(yùn)行之后就拋出了本文開(kāi)始提到的異常信息。
看到異常信息后,就把重點(diǎn)放到了是不是Mybatis對(duì)泛型的支持不夠好上。問(wèn)了下同事(@勝男),同事在自己的機(jī)器上試了下,發(fā)現(xiàn)沒(méi)有異常。這就奇怪了。仔細(xì)看了下代碼,發(fā)現(xiàn)不同之處就是我的KeyValue泛型類(lèi)實(shí)現(xiàn)了Map.Entry接口。 此時(shí)還不知道m(xù)ybatis官網(wǎng)對(duì)于foreach標(biāo)簽的說(shuō)明:
可以將任何可迭代對(duì)象(如列表、集合等)和任何的字典或者數(shù)組對(duì)象傳遞給foreach作為集合參數(shù)。當(dāng)使用可迭代對(duì)象或者數(shù)組時(shí),index是當(dāng)前迭代的次數(shù),item的值是本次迭代獲取的元素。當(dāng)使用字典(或者M(jìn)ap.Entry對(duì)象的集合)時(shí),index是鍵,item是值。
接下來(lái)就是通過(guò)debug看看,異常產(chǎn)生的具體原因。于是就先用實(shí)現(xiàn)了Map.Entry接口的KeyValue類(lèi)的代碼進(jìn)行debug,通過(guò)異常日志可以看到異常是在 DefaultSqlSession.java:200 行拋出,那么將斷點(diǎn)打到 DefaultSqlSession.java:197行,然后一步步向下執(zhí)行,當(dāng)執(zhí)行到 ForEachSqlNode.java:73行時(shí),終于煥然大悟。先看下debug調(diào)用鏈圖:
再看看具體的代碼 ForEachSqlNode.java:73(此類(lèi)就是foreach標(biāo)簽對(duì)象的Node類(lèi)):
此時(shí)具體的異常原因就很明顯了,此處的 o 對(duì)象的所屬的類(lèi)就是KeyValue類(lèi),由于KeyValue類(lèi)實(shí)現(xiàn)了Map.Entry接口,所以 o instance Map.Entry 為true,mybatis就把key值賦給了foreach的index屬性,而把value值賦給了item屬性,此處也就是把值為5的Integer對(duì)象賦給了item屬性。所以 GoodsRoomnight30daysMapper.xml 中id為 insertBatch 的select標(biāo)簽的item屬性對(duì)應(yīng)的對(duì)象也就沒(méi)有 item.key 和 item.value 屬性,這就是最終導(dǎo)致異常的原因。
<insert id="insertBatch" parameterType="list"> REPLACE INTO goods_roomnight_30days (goods_id, checkin_room_night_30days) VALUES <foreach collection="list" index="index" item="item" separator=","> (#{item.key}, #{item.value}) </foreach> </insert>
以上所述是小編給大家介紹的Mybatis foreach標(biāo)簽使用不當(dāng)導(dǎo)致異常的原因淺析,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- 解決Mybatis中foreach嵌套使用if標(biāo)簽對(duì)象取值的問(wèn)題
- 深入淺析MyBatis foreach標(biāo)簽
- Mybatis動(dòng)態(tài)SQL foreach標(biāo)簽用法實(shí)例
- MyBatis動(dòng)態(tài)SQL foreach標(biāo)簽實(shí)現(xiàn)批量插入的方法示例
- mybatis foreach標(biāo)簽的使用詳解
- Mybatis中foreach標(biāo)簽帶來(lái)的空格\換行\(zhòng)回車(chē)問(wèn)題及解決方案
- mybatis的foreach標(biāo)簽語(yǔ)法報(bào)錯(cuò)的解決
相關(guān)文章
Java中ArrayList和LinkedList之間的區(qū)別_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了Java中ArrayList和LinkedList之間的區(qū)別,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05SpringMVC 使用JSR-303進(jìn)行校驗(yàn) @Valid示例
本篇文章主要介紹了SpringMVC 使用JSR-303進(jìn)行校驗(yàn) @Valid示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02

JavaCV實(shí)現(xiàn)圖片中人臉檢測(cè)的示例代碼

springboot整合mybatis plus與druid詳情