解决com.mysql.cj.exceptions.DataConversionException: Cannot determine value type

本文探讨了在使用Spring框架整合MyBatis时遇到的两个常见问题:实体类缺少必要的构造方法和setter方法,以及DAO层mapper配置中类型转换错误。通过具体案例,详细解释了如何检查和修正这些问题,确保数据库查询正确执行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天使用spring整合mybatis时查询数据库出错

提供两种方向:

1: 实体类缺少空构造方法和setter
2:dao.mapper类型转换错误

对于2:举个例子:

<select id="selectInStudent" parameterType="User" resultType="java.lang.Boolean">
        select * from student where username = #{username} and password = #{password}
    </select>

这里我的返回值为Boolean,然而sql语句的返回值并不是Boolean

改成以下即可:

<select id="selectInStudent" parameterType="User" resultType="java.lang.Boolean">
        select count(*) from student where username = #{username} and password = #{password}
    </select>
public class Test4 { public static void main(String[] args){ Connection connection = null; Statement statement = null; ResultSet resultSet = null; String dirver = "com.mysql.cj.jdbc.Driver"; try { Class.forName(dirver); String url = "jdbc:mysql://localhost:3307/epet";// 数据库地址 String user = "root";// 数据库用户名 String password = "123456";// 数据库密码 connection = DriverManager.getConnection(url,user,password); if(connection != null){ System.out.println("链接成功!!");// 建议改为"连接成功" } // 3. 创建Statement对象(用于执行SQL语句) statement = connection.createStatement(); resultSet = statement.executeQuery("select*from pet"); while (resultSet.next()){ System.out.print(resultSet.getInt(1)+"\t"); System.out.print(resultSet.getInt(2)+"\t"); System.out.print(resultSet.getInt("health")+"\t"); System.out.println(); } //System.out.println(result == 1 ? "执行成功" : "执行失败"); } catch (SQLException e) { System.out.println("数据库链接失败"); e.printStackTrace(); }catch (ClassNotFoundException e){ e.printStackTrace(); }finally { // 7. 资源释放(按打开顺序反向关闭) if(resultSet != null){ try { resultSet.close(); } catch (SQLException e) { e.printStackTrace(); } } if(statement != null){ try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } } if(connection != null){ try { connection.close(); }catch (SQLException e){ e.printStackTrace(); } } } } } 链接成功!! 1 数据库链接失败 java.sql.SQLDataException: Cannot determine value type from string '小黑' at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:114) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73) at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:96) at com.mysql.cj.jdbc.result.ResultSetImpl.getObject(ResultSetImpl.java:1431) at com.mysql.cj.jdbc.result.ResultSetImpl.getInt(ResultSetImpl.java:830) at JDBC.Test4.main(Test4.java:25) Caused by: com.mysql.cj.exceptions.DataConversionException: Cannot determine value type from string '小黑' at com.mysql.cj.result.AbstractNumericValueFactory.createFromBytes(AbstractNumericValueFactory.java:65) at com.mysql.cj.protocol.a.MysqlTextValueDecoder.decodeByteArray(MysqlTextValueDecoder.java:143) at com.mysql.cj.protocol.result.AbstractResultsetRow.decodeAndCreateReturnValue(AbstractResultsetRow.java:135) at com.mysql.cj.protocol.result.AbstractResultsetRow.getValueFromBytes(AbstractResultsetRow.java:243) at com.mysql.cj.protocol.a.result.ByteArrayRow.getValue(ByteArrayRow.java:91) at com.mysql.cj.jdbc.result.ResultSetImpl.getObject(ResultSetImpl.java:1324) ... 2 more
最新发布
04-03
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值