我正在尝试@Select在MyBatis中定义一个简单的注释,以根据IN子句定义的条件获取对象的集合.SQL看起来像:
SELECT * FROM employees WHERE employeeID IN (1, 2, 3);
Run Code Online (Sandbox Code Playgroud)
该列表是动态生成的,因此我不知道它将包含多少参数.我想传递一个List值,例如:
@Select("SELECT * FROM employees WHERE employeeID IN( #{employeeIds} )")
List<Employee> selectSpecificEmployees(@Param("employeeIds") List<Integer> employeeIds);
Run Code Online (Sandbox Code Playgroud)
我正在创建一个Mapper定义上面注释的实例,并按如下方式调用它:
List<Integer> empIds = Arrays.asList(1, 2, 3);
List<Employee> result = mapper.selectSpecificEmployees(empIds);
Run Code Online (Sandbox Code Playgroud)
我发现这不起作用.
org.apache.ibatis.exceptions.PersistenceException:
###查询数据库时出错.原因:java.lang.NullPointerException
###错误可能涉及
com.mycompany.MySourceMapper.selectSpecificEmployees-Inline
###设置参数时发生错误###原因:org.apache.ibatis.exceptions中的java.lang.NullPointerException位于org.apache.ibatis.session.defaults.DefaultSqlSession.select上的org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:77)的.ExceptionFactory.wrapException(ExceptionFactory.java:8)(DefaultSqlSession.java) :69)org.apache.ibatis.binding.binperMethod.executeForList(MapperMethod.java:85)位于org.apache.ibatis.binding的org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:65).位于sun.reflect的sun.reflect.NativeMethodAccessorImpl.invoke0(本地方法)的com.mycompany.MySourceMapperDebug.testSelectSpecificEmployees(MySourceMapperDebug.java:60)处的$ Proxy23.selectSpecificProductTypes(未知来源)的MapperProxy.invoke(MapperProxy.java:35) sun.reflect中的.NativeMethodAccessorImpl.invoke(未知来源).在junit.framework.TestCase.runBare(testCase.java:127)的junit.framework.TestCase.runTest(TestCase.java:154)的java.lang.reflect.Method.invoke(未知来源)中委托MethodAethodAccessorImpl.invoke(未知来源) )在junit.framework.TestResult $ 1.protect(TestResult.java:106)的junit.framework.TestResult.runProtected(TestResult.java:124)junit.framework.TestResult.run(TestResult.java:109)junit. framework.TestCase.run(TestCase.java:118)位于junit.framework.TestSuite.runTest(TestSuite.java:208),位于org.eclipse.jdt.internal的junit.framework.TestSuite.run(TestSuite.java:203)位于org.eclipse.jdt.internal.junit的org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)的.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130) .runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)atg.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)org.eclipse.jdt.internal.junit.runner.RemoteTestRunner .RUN(RemoteTestRu nner.java:390)at or.e.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)引起:org.apache.ibatis.type.UnknownTypeHandler.setNonNullParameter的java.lang.NullPointerException( UnknownTypeHandler.java:21)atg.apache.ibatis.type.BaseTypeHandler.setParameter(BaseTypeHandler.java:23)atg.apache.ibatis.executor.parameter.DefaultParameterHandler.setParameters(DefaultParameterHandler.java:73)org.apache位于org.apache.ibatis.executor.SimpleExecutor.prepareStatement的org.apache.ibatis.executor.statement.RoutingStatementHandler.parameterize(RoutingStatementHandler.java:43)中的.ibatis.executor.statement.PreparedStatementHandler.parameterize(PreparedStatementHandler.java:61) (SimpleExecutor.java:56)位于org.apache的org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:40)org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:216). org.apache.ibatis.executor上的ibatis.executor.BaseExecutor.query(BaseExecutor.java:95).cachingExecutor.query(CachingExecutor.java:72)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)at java. org.apache.ibatis.plugin.Invocation.proceed(Invocation.java:31)中的lang.reflect.Method.invoke(未知来源)
... 36更多
我认为问题在于注释本身.这似乎是一个相当普遍的要求.我是否需要将其转换List为String自己并将其作为String参数传递而不是List<Integer> …
我的Mybatis中有一个包含IN子句的查询,它基本上是一组Id(整数)
我现在停留在如何将一个Integer数组传递给这个IN子句,以便它提取正确的记录.通过将包含ID的String传递给IN子句,但这没有按预期工作.
代码示例如下
使用注释的Mybatis方法
@Select(SEL_QUERY)
@Results(value = {@Result(property="id",column="ID")})
List<Integer> getIds(@Param("usrIds") Integer[] usrIds);
Run Code Online (Sandbox Code Playgroud)
询问
select distinct ID from table a where a.id in ( #{usrIds} )
Run Code Online (Sandbox Code Playgroud)
方法调用
Integer[] arr = new Integer[2];
arr[0] = 1;
arr[1] = 2;
mapper.getIds(arr)
Run Code Online (Sandbox Code Playgroud)
这不起作用,当我调用mapper方法时,Mybatis会抛出一个错误
请给我任何建议
我试图避免使用额外的xml来定义mybatis3中的映射器.注释适合于.
我对@ SelectProvider/@ InsertProvider /等的使用感到有点困惑.不要以为网上有很多资源可以指导我完成这项工作.
基本上,我想在mybatis3中找到替代的注释版本.
例如,我有一个xml映射器,我想将其转换为使用注释
<select ...>
<where>
<if cause.....>
</if>
<if cause......>
</if>
</where>
</select>
Run Code Online (Sandbox Code Playgroud)
任何人都可以提供包含代码的具体答案/解决方案吗?
提前致谢!