mybatis 注解 动态SQL 问题

BryceLoski 发布于 2016/08/25 11:59
阅读 1K+
收藏 0

使用mybatis注解开发。想要产生一次insert多条记录的sql,例如 insert into 表名(字段1,字段2,字段3) values(值1,值2,值3),values(值1,值2,值3), ... values(值1,值2,值3); 但是执行时mybatis抛出异常,还没有找到原因,有大神来看看么?

Mapper 代码:

@InsertProvider(type=ProviderLoanInfoSqlProvider.class,method = "insertManyRecord")
    int insertManyRecord(List<ProviderLoanInfo> providerLoanInfoList);



SqlProvider 代码:

public String insertManyRecord(List<ProviderLoanInfo> providerLoanInfoList)
    {
        StringBuilder sqlBuffer = new StringBuilder("insert into 91credit_provider_customer_loaninfo(");
        sqlBuffer.append("id,trxNo,borrowType,borrowState,borrowAmount,contractDate,loanPeriod,repayState,arrearsAmount,companyCode)");
        int size = providerLoanInfoList.size();
        for(int i = 0; i < size; i++)
        {
            ProviderLoanInfo loanInfo = providerLoanInfoList.get(i);
            sqlBuffer.append(" values(").append(null + ",").append(loanInfo.getTrxNo() + ",")
                    .append(loanInfo.getBorrowType() + ",").append(loanInfo.getBorrowState() + ",")
                    .append(loanInfo.getBorrowAmount() + ",").append(loanInfo.getContractDate() + ",")
                    .append(loanInfo.getLoanPeriod() + ",").append(loanInfo.getRepayState() + ",")
                    .append(loanInfo.getArrearsAmount() + ",");
            if(i == size - 1)
            {
                sqlBuffer.append(loanInfo.getCompanyCode() + ");");
            }
            else
            {
                sqlBuffer.append(loanInfo.getArrearsAmount() + "),");
            }
        }
        return sqlBuffer.toString();
    }



异常:

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.builder.BuilderException: Error invoking SqlProvider method (com.yuanheng100.credit.credit91.mapper.ProviderLoanInfoSqlProvider.insertManyRecord).  Cause: java.lang.IllegalArgumentException: argument type mismatch
	at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:75)
	at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:371)
	at com.sun.proxy.$Proxy21.insert(Unknown Source)
	at org.mybatis.spring.SqlSessionTemplate.insert(SqlSessionTemplate.java:240)
	at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:51)
	at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:52)
	at com.sun.proxy.$Proxy37.insertManyRecord(Unknown Source)




加载中
0
c
catlover
你看看你数据库里的字段跟你代码里的字段类型是否一致~~
c
catlover
回复 @j4love : 不过你的报错信息里面的确是报字段不匹配的错误,详见这里:Cause: java.lang.IllegalArgumentException: argument type mismatch,还有种情况就是数字型的字段,在java里面定义的是int或者long型,在mysql里面定义成了Unsigned int/long,这样也是会报错的
BryceLoski
BryceLoski
回复 @catlover : 在没有执行sqlProvider的中那个方法的时候,就抛出了那个异常,所以应该不是你所说的那个问题导致的。我还是不知道为什么会这样,明天休息的时候仔细研究下。
c
catlover
回复 @j4love : 存在的,java也是拼接sql语句,然后去数据库执行查找操作,所以sql的编写规范还是遵守下吧~
BryceLoski
BryceLoski
回复 @catlover : 但是我这是用java在操作数据库,不是我自己写sql去跑,不会存在这个问题吧?
c
catlover
回复 @j4love : contractDate,看看这个字段吧,一般insert的时候要对date类型的字段做 to_date处理,例如:insert into tabname(datecol) value(to_date('2016-08-25,'yyyy-mm-dd'))
下一页
0
开心的如来
开心的如来
argument type mismatch 看下类型匹配么
0
BryceLoski
BryceLoski
@Insert({"<script>",
            "INSERT INTO 91credit_consumer_customer_loaninfo (",
            "id, consumerCustomerId, borrowType, borrowState, borrowAmount, ",
            "contractDate, loanPeriod, repayState, arrearsAmount, companyCode)",
            "VALUES",
            "<foreach collection =\"consumerLoanInfoList\" item=\"consumerLoanInfo\" index= \"index\" separator =\",\">",
            "(#{consumerLoanInfo.id},#{consumerLoanInfo.consumerCustomerId},#{consumerLoanInfo.borrowType},",
            "#{consumerLoanInfo.borrowState},#{consumerLoanInfo.borrowAmount},#{consumerLoanInfo.contractDate},",
            "#{consumerLoanInfo.loanPeriod},#{consumerLoanInfo.repayState},#{consumerLoanInfo.arrearsAmount},",
            "#{consumerLoanInfo.companyCode})",
            "</foreach>",
            "</script>"})
    @Options(useGeneratedKeys = false)


注解用这种方式可以实现,不过还是有很多奇怪的地方
返回顶部
顶部