15
回答
MyBatis String数组作为参数传递的问题
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   

@verra 你好,想跟你请教个问题:

此处传的参数是String数组,请问这里的parameterType是否要写?要写的话是不是写成 parameterType=“String”呢?
还有,我的in里面的条件是字符串的  也就是这样  in ('a','b','c')  但按照我下面的那样写是查不出来,所以在这里想请教一下,多谢了
<select id="getViewHistory" resultMap="productResultMap">
select * from product where id in
<foreach collection="array" item="item" index="index" open="(" separator="," close=")">
#{item}
</foreach>
</select>









<无标签>
举报
子柚only
发帖于2年前 15回/11K+阅
共有15个答案 最后回答: 2年前
少了个
getTotalPage方法吧,并没有看到在哪里去处理页数,分页语句调用后,查询列表的语句未执行

1.需要传数组时,一般为java.util.List做参数, parameterType=java.util.List

2.<select id="getViewHistory" resultMap="productResultMap">缺少parameterType


引用来自“Ascend_”的评论

1.需要传数组时,一般为java.util.List做参数, parameterType=java.lang.List

2.<select id="getViewHistory" resultMap="productResultMap">缺少parameterType


依旧报错!
List<Product> getViewHistory(List<String> pro);
<select id="getViewHistory" resultMap="productResultMap" parameterType="java.util.List">


select * from product where id in 
<foreach collection="list" item="item" index="index" open="(" separator="," close=")">
#{item}
</foreach>
</select>
这种情况可以不传数组,直接传"a,b,c"这样的字符串,然后select * from product where id in (${id})
--- 共有 7 条评论 ---
sxgkwei回复 @小镇刁民 : 1,# 正式SQL中会是 ?,先加载SQL,后填充要查询的值进去;自然可以避免注入;2,他是想让你看看他配置有什么问题导致自己不能接收到返回值,这个要具体mybatis版本具体参考当前版本提供的说明的(版本间都稍有不同),关键点不是in这块,应该是XML本身还却什么配置项目。就in这块来说,他配置的应该没问题。 2年前 回复
小镇刁民回复 @sxgkwei : 的确是太年轻,你说的这种情况我的确没有想过,不过楼主这样的方式就能避免注入吗?还有你说的配置的共性问题是什么意思呢? 2年前 回复
sxgkwei回复 @小镇刁民 : 你还是太年轻啊。啊。啊。啊。这是在问一个配置的共性问题,你打算怎么过滤传入的值呢?万一人家是技术性论坛,里面的数据中本身就保存有sql语句,现在就是有人要根据一句sql查询答案呢? 2年前 回复
小镇刁民回复 @子柚only : 为啥呢 2年前 回复
小镇刁民回复 @sxgkwei : sql注入完全可以在操作数据库之前进行过滤啊 2年前 回复

引用来自“Ascend_”的评论

1.需要传数组时,一般为java.util.List做参数, parameterType=java.lang.List

2.<select id="getViewHistory" resultMap="productResultMap">缺少parameterType


引用来自“子柚only”的评论

依旧报错!
List<Product> getViewHistory(List<String> pro);
<select id="getViewHistory" resultMap="productResultMap" parameterType="java.util.List">


select * from product where id in 
<foreach collection="list" item="item" index="index" open="(" separator="," close=")">
#{item}
</foreach>
</select>
报什么错

引用来自“Ascend_”的评论

1.需要传数组时,一般为java.util.List做参数, parameterType=java.lang.List

2.<select id="getViewHistory" resultMap="productResultMap">缺少parameterType


引用来自“子柚only”的评论

依旧报错!
List<Product> getViewHistory(List<String> pro);
<select id="getViewHistory" resultMap="productResultMap" parameterType="java.util.List">


select * from product where id in 
<foreach collection="list" item="item" index="index" open="(" separator="," close=")">
#{item}
</foreach>
</select>

引用来自“Ascend_”的评论

报什么错
EBUG 2016-03-01 17:05:59,316 ziyou.zixi.mapper.ProductMapper.getViewHistory.http-8000-6 ==>  Preparing: select count(1) from (select * from product where id in ( ? , ? , ? )) tmp_count 
DEBUG 2016-03-01 17:05:59,318 ziyou.zixi.mapper.ProductMapper.getViewHistory.http-8000-6 ==> Parameters: a(String), b(String), c(String)
DEBUG 2016-03-01 17:05:59,330 org.mybatis.spring.SqlSessionUtils.http-8000-6 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@8ba0dd4]
==============异常开始=============
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: java.lang.NullPointerException
### The error may exist in mybatis/Product.xml
### The error may involve ziyou.zixi.mapper.ProductMapper.getViewHistory-Inline
### The error occurred while setting parameters
### Cause: java.lang.NullPointerException
--- 共有 1 条评论 ---
panda大侠0把异常贴全点 2年前 回复

引用来自“Ascend_”的评论

1.需要传数组时,一般为java.util.List做参数, parameterType=java.lang.List

2.<select id="getViewHistory" resultMap="productResultMap">缺少parameterType


引用来自“子柚only”的评论

依旧报错!
List<Product> getViewHistory(List<String> pro);
<select id="getViewHistory" resultMap="productResultMap" parameterType="java.util.List">


select * from product where id in 
<foreach collection="list" item="item" index="index" open="(" separator="," close=")">
#{item}
</foreach>
</select>

引用来自“Ascend_”的评论

报什么错

引用来自“子柚only”的评论

EBUG 2016-03-01 17:05:59,316 ziyou.zixi.mapper.ProductMapper.getViewHistory.http-8000-6 ==>  Preparing: select count(1) from (select * from product where id in ( ? , ? , ? )) tmp_count 
DEBUG 2016-03-01 17:05:59,318 ziyou.zixi.mapper.ProductMapper.getViewHistory.http-8000-6 ==> Parameters: a(String), b(String), c(String)
DEBUG 2016-03-01 17:05:59,330 org.mybatis.spring.SqlSessionUtils.http-8000-6 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@8ba0dd4]
==============异常开始=============
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: java.lang.NullPointerException
### The error may exist in mybatis/Product.xml
### The error may involve ziyou.zixi.mapper.ProductMapper.getViewHistory-Inline
### The error occurred while setting parameters
### Cause: java.lang.NullPointerException
EBUG 2016-03-01 17:35:26,468 org.mybatis.spring.SqlSessionUtils.http-8000-4 SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@75e79c77] was not registered for synchronization because synchronization is not active
DEBUG 2016-03-01 17:35:26,501 org.mybatis.spring.transaction.SpringManagedTransaction.http-8000-4 JDBC Connection [com.mysql.jdbc.JDBC4Connection@11b2c43e] will not be managed by Spring
DEBUG 2016-03-01 17:35:26,501 ziyou.zixi.mapper.ProductMapper.getViewHistory.http-8000-4 ooo Using Connection [com.mysql.jdbc.JDBC4Connection@11b2c43e]
DEBUG 2016-03-01 17:35:26,502 ziyou.zixi.mapper.ProductMapper.getViewHistory.http-8000-4 ==>  Preparing: select count(1) from (select * from product where id in ( ? , ? , ? )) tmp_count 
DEBUG 2016-03-01 17:35:26,503 ziyou.zixi.mapper.ProductMapper.getViewHistory.http-8000-4 ==> Parameters: a(String), b(String), c(String)
DEBUG 2016-03-01 17:35:26,509 org.mybatis.spring.SqlSessionUtils.http-8000-4 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@75e79c77]
==============异常开始=============
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: java.lang.NullPointerException
### The error may exist in mybatis/Product.xml
### The error may involve ziyou.zixi.mapper.ProductMapper.getViewHistory-Inline
### The error occurred while setting parameters
### Cause: java.lang.NullPointerException
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:73)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:365)
at $Proxy22.selectList(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:195)
at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:124)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:90)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:40)
at $Proxy25.getViewHistory(Unknown Source)
at ziyou.zixi.service.impl.ProductServiceImpl.getViewHistory(ProductServiceImpl.java:78)
at ziyou.zixi.service.impl.ProductServiceImpl$$FastClassByCGLIB$$9efef58b.invoke(<generated>)
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191)
at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:689)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:90)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:622)
at ziyou.zixi.service.impl.ProductServiceImpl$$EnhancerByCGLIB$$e0f44cde.getViewHistory(<generated>)
at ziyou.zixi.controller.client.WebController.getViewHistory(WebController.java:76)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:263)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:584)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)
Caused by: org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: java.lang.NullPointerException
### The error may exist in mybatis/Product.xml
### The error may involve ziyou.zixi.mapper.ProductMapper.getViewHistory-Inline
### The error occurred while setting parameters
### Cause: java.lang.NullPointerException
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:23)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:104)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:95)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:355)
... 50 more
Caused by: java.lang.NullPointerException
at ziyou.zixi.pulgin.mybatis.plugin.PagePlugin.intercept(PagePlugin.java:104)
at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:57)
at $Proxy50.prepare(Unknown Source)
at org.apache.ibatis.executor.SimpleExecutor.prepareStatement(SimpleExecutor.java:70)
at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:56)
at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:267)
at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:141)
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:105)
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:81)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:101)
... 56 more
==============异常结束=============

顶部