这个错误可能是因为在使用 MyBatis 时,将 Druid 连接池中的连接对象直接传递给了 Mapper 的方法,导致类型转换错误。解决方法是在 Mapper 的方法中使用 SqlSession 对象来获取连接,而不是直接使用连接池中的连接。可以按照以下步骤解决:

  1. 在 Mapper 接口中添加 SqlSession 参数,例如:
public interface MyMapper {
    void myMethod(SqlSession sqlSession, MyParam myParam);
}
  1. 在 Mapper.xml 文件中使用 SqlSession 对象来获取连接,例如:
<insert id="myMethod" parameterType="MyParam">
    <selectKey keyProperty="id" resultType="Long" order="BEFORE">
        SELECT nextval('my_sequence')
    </selectKey>
    INSERT INTO my_table(id, name) VALUES(#{id}, #{name})
</insert>
  1. 在调用 Mapper 方法时,传递 SqlSession 对象,例如:
SqlSession sqlSession = sqlSessionFactory.openSession();
MyMapper myMapper = sqlSession.getMapper(MyMapper.class);
MyParam myParam = new MyParam();
myParam.setId(1L);
myParam.setName("test");
myMapper.myMethod(sqlSession, myParam);
sqlSession.commit();
sqlSession.close();

这样就可以避免类型转换错误,正常执行 MyBatis 操作。

MyBatis 使用 Druid 连接池时出现 'com.alibaba.druid.pool.DruidPooledConnection cannot be cast to org.apache.ibatis.mapping.MappedStatement' 错误的解决方法

原文地址: https://www.cveoy.top/t/topic/ovUl 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录