Mybatis问题

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中将数据库名或表名作为参数传入时要用$而不是常用的#

    使用#会在传入参数上加引号,如果是表名,运行时会抛异常

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值