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
的数据,但是数据的content
为NULL
,调用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