저는 IN 절에 정의 된 기준에 따라 객체 컬렉션을 얻으려고 MyBatis에서 간단한 @Select
주석을 정의하려고합니다. SQL은 다음과 같습니다.List를 매개 변수로 MyBatis 매퍼에 전달할 수 있습니까?
SELECT * FROM employees WHERE employeeID IN (1, 2, 3);
목록이 동적으로 생성되므로 목록의 개수가 얼마인지 알 수 없습니다.
@Select("SELECT * FROM employees WHERE employeeID IN(#{employeeIds})")
List<Employee> selectSpecificEmployees(@Param("employeeIds") List<Integer> employeeIds);
나는 다음과 같이 호출 위의 주석이 정의 된 Mapper
의 인스턴스를 생성하고 있어요 : 난 그냥 값의 List
에 뭔가를 통과하고 싶습니다
List<Integer> empIds = Arrays.asList(1, 2, 3);
List<Employee> result = mapper.selectSpecificEmployees(empIds);
나는 이것이 작동하지 않는다는 것을 발견했다.
org.apache.ibatis.exceptions.PersistenceException :
### 오류 쿼리 데이터베이스. 원인 : java.lang.NullPointerException이
### 오류가
com.mycompany.MySourceMapper.selectSpecificEmployees 인라인 ### 오류가 설정 파라미터가 원인 ### 동안 발생
를 포함 할 수있다 : java.lang.NullPointerException이 을 org.apache.ibatis.security.wrapException (ExceptionFactory.java:8) at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList (DefaultSqlSession.java:77) at org.apache.ibatis.session .defaults.DefaultSqlSession.selectList (DefaultSqlSession.java:69) at org.apache.ibatis.binding.MapperMethod.executeForList (MapperMethod.java:85) at org.apache.ibatis.binding.MapperMethod.execute (M apperMethod.java:65) com.mycompany.MySourceMapperDebug.testSelectSpecificEmployees에서 org.apache.ibatis.binding.MapperProxy.invoke (MapperProxy.java:35) $에서 Proxy23.selectSpecificProductTypes (알 수없는 소스) (MySourceMapperDebug.java에서 : 60)에서 sun.reflect.DelegatingMethodAccessorImpl.invoke sun.reflect.NativeMethodAccessorImpl.invoke (알 소스)에 sun.reflect.NativeMethodAccessorImpl.invoke0 (원시 메소드) (알 소스)에 에서 java.lang.reflect.Method의 .invoke (알 수없는 출처) at junit.framework.TestCase.runTest (TestCase.java:154) at junit.framework.TestCase.runBare (TestCase.java:127) at junit.framework.TestResult $ 1.protect (TestResult .java : 106)junit.framework.TestCase.run에서 junit.framework.TestResult.run (TestResult.java:109) 에서 junit.framework.TestResult.runProtected (TestResult.java:124) (TestCase.java:118) 에서 515,에서 (TestSuite.java:203) at junit.framework.TestSuite.run (TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests (RemoteTestRunner. java : 467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests (RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit. org.apache에서 java.lang.NullPointerException이 : runner.RemoteTestRunner.run (RemoteTestRunner.java:390)는 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main (RemoteTestRunner.java:197) 에 의해 발생 .ibatis.type.UnknownTypeHandler.setNonNullParameter (UnknownTypeHandler.java : 21) at org.apache.ibatis.type.BaseTypeHandler.setParameter (BaseTypeHandler.java:23) at org.apache.ibatis.executor.parameter.DefaultParameterHandler.setParameters (DefaultParameterHandler.java:73) at org. apache.ibatis.executor.statement.PreparedStatementHandler.parameterize (PreparedStatementHandler.java:61) at org.apache.ibatis.executor.statement.RoutingStatementHandler.parameterize (RoutingStatementHandler.java:43) at org.apache.ibatis.executor. SimpleExecutor.prepareStatement (SimpleExecutor.java:56) at org.apache.ibatis.executor.SimpleExecutor.doQuery (SimpleExecutor.java:40) at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase (BaseExecutor.java:216) at org.apache.ibatis.executor.BaseExecutor.query (BaseExecutor.java : 95) sun.reflect.NativeMethodAccessorImpl.invoke에서 sun.reflect.NativeMethodAccessorImpl.invoke0 (기본 방법) 에서 org.apache.ibatis.executor.CachingExecutor.query (CachingExecutor.java:72) (알 수없는 소스) 에서 에서 (알 수없는 소스) at org.apache.ibatis.plugin.Invocation.proceed (Invocation.java:31)
.. . 36 more
문제는 주석 자체에 있다고 생각합니다. 이것은 꽤 일반적인 요구 사항처럼 보입니다. List
을 직접 String
으로 변환하고 List<Integer>
대신 String
매개 변수로 전달해야합니까? 또는 List
을 MyBatis 주석에 매개 변수로 전달하기위한 다른 구문이 있습니까?
당신은 함께이 게시물을보고 XML과 어노테이션 기반 설정을 모두 사용할 수 있습니다 http://stackoverflow.com/questions/8788250/xml-annotation-based-configuration-for-mybatis/9076435 –