Tôi đang cố gắng xác định chú thích đơn giản @Select
trong MyBatis để có được tập hợp các đối tượng dựa trên tiêu chí được xác định bởi mệnh đề IN. SQL trông giống như sau:Tôi có thể chuyển Danh sách dưới dạng thông số cho một người lập bản đồ MyBatis không?
SELECT * FROM employees WHERE employeeID IN (1, 2, 3);
Danh sách được tạo động, vì vậy tôi không biết có bao nhiêu thông số sẽ có. Tôi muốn chỉ cần vượt qua trong một List
các giá trị, một cái gì đó như:
@Select("SELECT * FROM employees WHERE employeeID IN(#{employeeIds})")
List<Employee> selectSpecificEmployees(@Param("employeeIds") List<Integer> employeeIds);
tôi là tạo ra một thể hiện của các Mapper
nơi chú thích trên được xác định và gọi đó là như sau:
List<Integer> empIds = Arrays.asList(1, 2, 3);
List<Employee> result = mapper.selectSpecificEmployees(empIds);
Tôi đã phát hiện ra rằng điều này không hoạt động.
org.apache.ibatis.exceptions.PersistenceException:
### Lỗi truy vấn cơ sở dữ liệu. Nguyên nhân: java.lang.NullPointerException
### Các lỗi có thể liên quan
com.mycompany.MySourceMapper.selectSpecificEmployees-Inline
### Các lỗi xảy ra trong khi thiết thông số ### Nguyên nhân: java.lang.NullPointerException tại org.apache.ibatis.exceptions.ExceptionFactory.wrapException (ExceptionFactory.java:8) tại org.apache.ibatis.session.defaults.DefaultSqlSession.selectList (DefaultSqlSession.java:77) tại org.apache.ibatis.session .defaults.DefaultSqlSession.selectList (DefaultSqlSession.java:69) tại org.apache.ibatis.binding.MapperMethod.executeForList (MapperMethod.java:85) tại org.apache.ibatis.binding.MapperMethod.execute (M apperMethod.java:65) tại org.apache.ibatis.binding.MapperProxy.invoke (MapperProxy.java:35) tại $ Proxy23.selectSpecificProductTypes (Nguồn không xác định) tại com.mycompany.MySourceMapperDebug.testSelectSpecificEmployees (MySourceMapperDebug.java: 60) tại sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method) tại sun.reflect.NativeMethodAccessorImpl.invoke (Unknown Source) tại sun.reflect.DelegatingMethodAccessorImpl.invoke (Unknown Source) tại java.lang.reflect.Method .invoke (Nguồn không xác định) tại junit.framework.TestCase.runTest (TestCase.java:154) tại junit.framework.TestCase.runBare (TestCase.java:127) tại junit.framework.TestResult $ 1.protect (TestResult .java: 106) tại junit.framework.TestResult.runProtected (TestResult.java:124) tại junit.framework.TestResult.run (TestResult.java:109) tại junit.framework.TestCase.run (TestCase.java:118) at junit.framework.TestSuite.runTest (TestSuite.java:208) tại junit.framework.TestSuite.run (TestSuite.java:203) tại org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run (JUnit3TestReference.java:130) tại org.eclipse.jdt.internal.junit.runner.TestExecution.run (TestExecution.java:38) tại org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests (RemoteTestRunner. java: 467) tại org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests (RemoteTestRunner.java:683) tại org.eclipse.jdt.internal.junit. runner.RemoteTestRunner.run (RemoteTestRunner.java:390) tại org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main (RemoteTestRunner.java:197) Gây ra bởi: java.lang.NullPointerException tại org.apache .ibatis.type.UnknownTypeHandler.setNonNullParameter (UnknownTypeHandler.java: 21) tại org.apache.ibatis.type.BaseTypeHandler.setParameter (BaseTypeHandler.java:23) tại org.apache.ibatis.executor.parameter.DefaultParameterHandler.setParameters (DefaultParameterHandler.java:73) tại tổ chức. apache.ibatis.executor.statement.PreparedStatementHandler.parameterize (PreparedStatementHandler.java:61) tại org.apache.ibatis.executor.statement.RoutingStatementHandler.parameterize (RoutingStatementHandler.java:43) tại org.apache.ibatis.executor. SimpleExecutor.prepareStatement (SimpleExecutor.java:56) tại org.apache.ibatis.executor.SimpleExecutor.doQuery (SimpleExecutor.java:40) tại org.apache.ibatis.executor.BaseExecutor.queryFromDatabase (BaseExecutor.java:216) tại org.apache.ibatis.executor.BaseExecutor.query (BaseExecutor.java: 95) tại org.apache.ibatis.executor.CachingExecutor.query (CachingExecutor.java:72) tại sun.reflect.NativeMethodAccessorImpl.invoke0 (Phương pháp gốc) tại sun.reflect.NativeMethodAccessorImpl.invoke (Nguồn không xác định) tại sun.reflect.DelegatingMethodAccessorImpl.invoke (Nguồn không rõ) tại java.lang.reflect.Method.invoke (Nguồn không xác định) tại org.apache.ibatis.plugin.Invocation.proceed (Invocation.java:31)
.. 36 thêm
Tôi nghĩ rằng vấn đề nằm trong chính chú thích. Điều này có vẻ như nó sẽ là một yêu cầu khá phổ biến. Tôi có cần phải tự chuyển đổi số List
thành một số String
và chuyển số đó thành thông số String
thay vì số List<Integer>
không? Hoặc có một số cú pháp khác để chuyển một số List
làm tham số cho chú thích MyBatis không?
Bạn có thể sử dụng cả cấu hình dựa trên xml và chú thích cùng nhau xem bài đăng này, vui lòng http://stackoverflow.com/questions/8788250/xml-annotation-based-configuration-for-mybatis/9076435 –