foreach循环查询提示错误
Posted 月疯
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了foreach循环查询提示错误相关的知识,希望对你有一定的参考价值。
"C:\\Program Files\\Java\\jdk1.8.0_131\\bin\\java" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:F:\\开发工具\\idea\\IntelliJ IDEA 2017.2.6\\lib\\idea_rt.jar=53995:F:\\开发工具\\idea\\IntelliJ IDEA 2017.2.6\\bin" -Dfile.encoding=UTF-8 -classpath "F:\\开发工具\\idea\\IntelliJ IDEA 2017.2.6\\lib\\idea_rt.jar;F:\\开发工具\\idea\\IntelliJ IDEA 2017.2.6\\plugins\\junit\\lib\\junit-rt.jar;F:\\开发工具\\idea\\IntelliJ IDEA 2017.2.6\\plugins\\junit\\lib\\junit5-rt.jar;C:\\Program Files\\Java\\jdk1.8.0_131\\jre\\lib\\charsets.jar;C:\\Program Files\\Java\\jdk1.8.0_131\\jre\\lib\\deploy.jar;C:\\Program Files\\Java\\jdk1.8.0_131\\jre\\lib\\ext\\access-bridge-64.jar;C:\\Program Files\\Java\\jdk1.8.0_131\\jre\\lib\\ext\\cldrdata.jar;C:\\Program Files\\Java\\jdk1.8.0_131\\jre\\lib\\ext\\dnsns.jar;C:\\Program Files\\Java\\jdk1.8.0_131\\jre\\lib\\ext\\jaccess.jar;C:\\Program Files\\Java\\jdk1.8.0_131\\jre\\lib\\ext\\jfxrt.jar;C:\\Program Files\\Java\\jdk1.8.0_131\\jre\\lib\\ext\\localedata.jar;C:\\Program Files\\Java\\jdk1.8.0_131\\jre\\lib\\ext\\nashorn.jar;C:\\Program Files\\Java\\jdk1.8.0_131\\jre\\lib\\ext\\sunec.jar;C:\\Program Files\\Java\\jdk1.8.0_131\\jre\\lib\\ext\\sunjce_provider.jar;C:\\Program Files\\Java\\jdk1.8.0_131\\jre\\lib\\ext\\sunmscapi.jar;C:\\Program Files\\Java\\jdk1.8.0_131\\jre\\lib\\ext\\sunpkcs11.jar;C:\\Program Files\\Java\\jdk1.8.0_131\\jre\\lib\\ext\\zipfs.jar;C:\\Program Files\\Java\\jdk1.8.0_131\\jre\\lib\\javaws.jar;C:\\Program Files\\Java\\jdk1.8.0_131\\jre\\lib\\jce.jar;C:\\Program Files\\Java\\jdk1.8.0_131\\jre\\lib\\jfr.jar;C:\\Program Files\\Java\\jdk1.8.0_131\\jre\\lib\\jfxswt.jar;C:\\Program Files\\Java\\jdk1.8.0_131\\jre\\lib\\jsse.jar;C:\\Program Files\\Java\\jdk1.8.0_131\\jre\\lib\\management-agent.jar;C:\\Program Files\\Java\\jdk1.8.0_131\\jre\\lib\\plugin.jar;C:\\Program Files\\Java\\jdk1.8.0_131\\jre\\lib\\resources.jar;C:\\Program Files\\Java\\jdk1.8.0_131\\jre\\lib\\rt.jar;E:\\workplace\\notification\\SQL_MISS_W\\test01\\target\\test-classes;E:\\workplace\\notification\\SQL_MISS_W\\test01\\target\\classes;C:\\Users\\思尔\\.m2\\repository\\org\\mybatis\\mybatis\\3.5.7\\mybatis-3.5.7.jar;C:\\Users\\思尔\\.m2\\repository\\junit\\junit\\4.12\\junit-4.12.jar;C:\\Users\\思尔\\.m2\\repository\\org\\hamcrest\\hamcrest-core\\1.3\\hamcrest-core-1.3.jar;C:\\Users\\思尔\\.m2\\repository\\mysql\\mysql-connector-java\\5.1.3\\mysql-connector-java-5.1.3.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 PHTest,SeUserCell
org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 2
### The error may exist in mapper/UserMapper.xml
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: SELECT * from user_BAK where id in
### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 2at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:153)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:145)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:140)
at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:147)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:80)
at org.apache.ibatis.binding.MapperProxy$PlainMethodInvoker.invoke(MapperProxy.java:145)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:86)
at com.sun.proxy.$Proxy6.findPhocell(Unknown Source)
at PHTest.SeUserCell(PHTest.java:110)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 2
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:403)
at com.mysql.jdbc.Util.getInstance(Util.java:378)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1031)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3378)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3310)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1853)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1976)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2503)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1737)
at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:998)
at org.apache.ibatis.executor.statement.PreparedStatementHandler.query(PreparedStatementHandler.java:64)
at org.apache.ibatis.executor.statement.RoutingStatementHandler.query(RoutingStatementHandler.java:79)
at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:63)
at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:325)
at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:156)
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:109)
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:89)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:151)
... 30 more
Process finished with exit code -1
查询:
public interface UserMapper List<User> findPhocell(@Param("list") List<String> ids);
<select id="findPhocell" resultType="User"> SELECT * from user_BAK where id in <foreach collection="list" open="(" close=")" item="id" separator=","> '$id' </foreach> </select>
最后发现传入的List<String> list这个是空的,所以直接提示上面的报错。
注:
foreach三种循环:数组、list集合、map集合
foreach元素的属性主要有 item,index,collection,open,separator,close。
item表示集合中每一个元素进行迭代时的别名,
index指 定一个名字,用于表示在迭代过程中,每次迭代到的位置,
open表示该语句以什么开始,
separator表示在每次进行迭代之间以什么符号作为分隔 符,
close表示以什么结束。
1. 如果传入的是单参数且参数类型是一个List的时候,collection属性值为list
2. 如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array
3. 如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以封装成map,实际上如果你在传入参数的时候,在MyBatis里面也是会把它封装成一个Map的,map的key就是参数名,所以这个时候collection属性值就是传入的List或array对象在自己封装的map里面的key
1、list集合:
public List<User> selectByIds(List<Integer> list);
<select id="selectByIds" resultType="com.huitao.pojo.User">
select * from user where id in
<foreach collection="list" open="(" close=")" item="id" separator="," >
#id
</foreach>
</select>
2、list集合(插入)
public int saveAllUsers(List<User> users);
<insert id="saveUsers">
insert into user (id, username, password,address)
values
<foreach collection="list" item="user" separator=",">
(#user.id, #user.username, #user.password,#user.address)
</foreach>
</insert>
list集合(查询)(注意#拼接和‘$’的区别)
List<User> findPhocell(@Param("list") List<String> ids);
<select id="findPhocell" resultType="User"> SELECT * from user_BAK where id in <foreach collection="list" open="(" close=")" item="id" separator=","> '$id' </foreach> </select>
3、数组:
List selectByIdSet(String[] idList)
<select id="selectByIdSet" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List" />
from t_user
WHERE id IN
<foreach collection="array" item="id" index="index" open="(" close=")" separator=",">
#id
</foreach>
</select>
4、多参数
List<User> selectByIdSet(@Param("name")String name, @Param("ids")String[] idList);
<select id="selectByIdSet" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List" />
from t_user
WHERE name=#name,jdbcType=VARCHAR and id IN
<foreach collection="idList" item="id" index="index"
open="(" close=")" separator=",">
#id
</foreach>
</select>
5、map集合
Map<String, Object> params = new HashMap<String, Object>(2);
params.put("name", name);
params.put("idList", ids);
mapper.selectByIdSet(params);
<select id="selectByIdSet" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from t_user where
name = #name
and ID in
<foreach item="item" index="index" collection="idList" open="(" separator="," close=")">
#item
</foreach>
</select>
以上是关于foreach循环查询提示错误的主要内容,如果未能解决你的问题,请参考以下文章