MyBatis+mysql+text

本文记录了一次使用SpringMVC+MyBatis+Mysql时遇到的bug,具体表现为当数据字段类型为text时,执行查询操作出现异常。Mapper.xml中的配置、数据库中的实际数据都已描述,但异常出现在Mybatis框架内部,尤其在字段为空时。问题尚未解决。

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

MyBatis+mysql+text

记录一下这个bug,到现在还没有知道原因是什么。
我使用SpringMVC+Mybatis+Mysql, 定义了一个Model: Activity:

CREATE TABLE `tb_activity_info` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `type` varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '活动类型',
  `name` varchar(128) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '活动名称',
  `topic` varchar(128) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '活动主题',
  `created_by` varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `created_time` datetime DEFAULT NULL,
  `status` varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '状态:草稿,发布,待审核',
  `is_approved` varchar(1) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '后台管理系统是否已经审批通过',
  `description` varchar(256) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '活动描述',
  `people` int(11) DEFAULT NULL COMMENT '参与人数',
  `cover` varchar(128) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '封面',
  `start_time` datetime DEFAULT NULL COMMENT '开始时间',
  `end_time` datetime DEFAULT NULL COMMENT '结束时间',
  `location` varchar(45) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '活动地点',
  `association_id` int(11) DEFAULT NULL COMMENT '协会id',
  `visible` varchar(1) COLLATE utf8mb4_unicode_ci DEFAULT '1' COMMENT '是否对外可见',
  `content` text COLLATE utf8mb4_unicode_ci COMMENT '正文内容,html',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='活动/赛事表';

根据mybatis-generator生成的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="com.unionapp.activity.dao.ActivityMapper">
  <resultMap id="BaseResultMap" type="com.unionapp.activity.model.Activity">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="type" jdbcType="VARCHAR" property="type" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <result column="topic" jdbcType="VARCHAR" property="topic" />
    <result column="created_by" jdbcType="VARCHAR" property="createdBy" />
    <result column="created_time" jdbcType="TIMESTAMP" property="createdTime" />
    <result column="status" jdbcType="VARCHAR" property="status" />
    <result column="is_approved" jdbcType="VARCHAR" property="isApproved" />
    <result column="description" jdbcType="VARCHAR" property="description" />
    <result column="people" jdbcType="INTEGER" property="people" />
    <result column="cover" jdbcType="VARCHAR" property="cover" />
    <result column="start_time" jdbcType="TIMESTAMP" property="startTime" />
    <result column="end_time" jdbcType="TIMESTAMP" property="endTime" />
    <result column="location" jdbcType="VARCHAR" property="location" />
    <result column="association_id" jdbcType="INTEGER" property="associationId" />
    <result column="visible" jdbcType="VARCHAR" property="visible" />
  </resultMap>
  <resultMap extends="BaseResultMap" id="ResultMapWithBLOBs" type="com.unionapp.activity.model.Activity">
    <result column="content" jdbcType="LONGVARCHAR" property="content" />
  </resultMap>
  <sql id="Base_Column_List">
    id, type, name, topic, created_by, created_time, status, is_approved, description, 
    people, cover, start_time, end_time, location, association_id, visible
  </sql>
  <sql id="Blob_Column_List">
    content
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="ResultMapWithBLOBs">
    select 
    <include refid="Base_Column_List" />
    ,
    <include refid="Blob_Column_List" />
    from tb_activity_info
    where id = #{id,jdbcType=INTEGER}
  </select>
</mapper>

在数据库里,我有一条id=1的数据,但是数据的contentNULL,调用mapper.selectByPrimaryKey方法的时候,出现了NullPointException, 而这个Exception并没有发生在我的逻辑代码里,而是在mybatis的框架发生的。如果把content置为非NULL,就不会出现报错。至今未解。:

Caused by: org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: java.lang.NullPointerException
### The error may exist in file [E:\Project\IDEA\Union_App\UnionApp\target\UnionApp\WEB-INF\classes\com\unionapp\activity\mapper\ActivityMapper.xml]
### The error may involve com.unionapp.activity.dao.ActivityMapper.selectByPrimaryKey
### The error occurred while handling results
### SQL: select            id, type, name, topic, created_by, created_time, status, is_approved, description,      people, cover, start_time, end_time, location, association_id, visible         ,           content         from tb_activity_info     where id = ?
### Cause: java.lang.NullPointerException
    at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:26)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:111)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:102)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:66)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:358)
    ... 65 more
Caused by: java.lang.NullPointerException
    at net.sf.log4jdbc.sql.resultsetcollector.DefaultResultSetCollector.methodReturned(DefaultResultSetCollector.java:204)
    at net.sf.log4jdbc.sql.jdbcapi.ResultSetSpy.reportAllReturns(ResultSetSpy.java:100)
    at net.sf.log4jdbc.sql.jdbcapi.ResultSetSpy.reportReturn(ResultSetSpy.java:192)
    at net.sf.log4jdbc.sql.jdbcapi.ResultSetSpy.wasNull(ResultSetSpy.java:2497)
    at com.mchange.v2.c3p0.impl.NewProxyResultSet.wasNull(NewProxyResultSet.java:259)
    at org.apache.ibatis.type.BaseTypeHandler.getResult(BaseTypeHandler.java:56)
    at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.getPropertyMappingValue(DefaultResultSetHandler.java:393)
    at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.applyPropertyMappings(DefaultResultSetHandler.java:367)
    at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.getRowValue(DefaultResultSetHandler.java:341)
    at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleRowValuesForSimpleResultMap(DefaultResultSetHandler.java:294)
    at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleRowValues(DefaultResultSetHandler.java:269)
    at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleResultSet(DefaultResultSetHandler.java:239)
    at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleResultSets(DefaultResultSetHandler.java:153)
    at org.apache.ibatis.executor.statement.PreparedStatementHandler.query(PreparedStatementHandler.java:60)
    at org.apache.ibatis.executor.statement.RoutingStatementHandler.query(RoutingStatementHandler.java:73)
    at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:60)
    at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:267)
    at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:137)
    at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:96)
    at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:77)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:108)
    ... 72 more
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值