1. 数据库表中设置了自增主键id,insert的时候,在标签中写了
keyProperty="id" keyColumn="id" useGeneratedKeys="true"
这两个属性用于新增记录后获取主键id返回的配置,结果数据添加成功,但是报错Error getting generated key or setting result to parameter object. Cause: org.apache.ibatis....
去掉这部分后成功执行
mybatis的配置文件中如果已经配置了useGeneratedKeys="true", insert方法中应该不再写useGeneratedKeys="true"
2. resultMap中对数据对应类型
<resultMap>
<result column="test" jdbcType="VARCHAR" property="test" />
</resultMap>
其中jdbcType是Java数据库连接(JDBC)中的数据类型,javaType是Java编程语言中的数据类型
这两个写错了程序执行时会报错
3. 自定义Mybatis的拦截器不起作用
@Intercepts(
{
@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}),
@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}),
@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class}),}
)
@Component
public class SqlInterceptor implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Throwable {
Object[] args = invocation.getArgs();
Method method = invocation.getMethod();
Object obj = invocation.getTarget();
return null;
}
@Override
public Object plugin(Object target) {
return Plugin.wrap(target, this);
}
@Override
public void setProperties(Properties properties) {
}
}
在自定义拦截器的类上加注解,标记为spring的bean,就可以拦截到了
4. 在mybatis中将数据库名或表名作为参数传入时要用$而不是常用的#
使用#会在传入参数上加引号,如果是表名,运行时会抛异常