接口配置:
package mybatis.mapper;
import java.util.List;
public interface TestMapper {
public List<String> selectAllInfo();
}
mapper.xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="mybatis.mapper.TestMapper">
<select id="selectAllInfo" resultType="java.util.List">
SELECT
ID
FROM TEST
</select>
</mapper>
在使用junit测试时出现以下错误:
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: java.lang.UnsupportedOperationException
### The error may exist in mybatis/mapper/TestMapper.xml
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: SELECT ID FROM TEST
### Cause: java.lang.UnsupportedOperationException
大致意思就是设置的参数有问题。
问题原因:
原因就在于mapper.xml的resultType代表的是List中的元素类型,而不应该是List本身,所以修改mapper.xml文件为:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="mybatis.mapper.TestMapper">
<select id="selectAllInfo" resultType="java.lang.String">
SELECT
ID
FROM TEST
</select>
</mapper>