abel533 / Mapper

Mybatis Common Mapper - Easy to use
https://mybatis.io
MIT License
7.32k stars 1.63k forks source link

Sybase ase 数据表,主键自增报错 #191

Closed CarlJia closed 6 years ago

CarlJia commented 6 years ago

c.t.r.mapper.OrderMergeMapper.insert : ==> Preparing: INSERT INTO plugin_order_merge ( id ) VALUES( ? )

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='id', mode=IN, javaType=class java.lang.Integer, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. Cause: org.apache.ibatis.type.TypeException: JDBC requires that the JdbcType must be specified for all nullable parameters.

配置如下: mapper: mappers:

abel533 commented 6 years ago

配置 settings 中 的 jdbcTypeForNull 试试。

CarlJia commented 6 years ago

@abel533 这个加过了,现在改成

mapper:
  mappers:
    - tk.mybatis.mapper.common.Mapper
  not-empty: false
  identity: SYBASE
  before: true

报错

SQL: INSERT INTO plugin_order_merge ( id ) VALUES( ? )

Cause: java.sql.SQLException: Explicit value specified for identity field in table 'plugin_order_merge' when 'SET IDENTITY_INSERT' is OFF.

abel533 commented 6 years ago

按照sybase要求,你可能需要把 id 的 insertable=false

CarlJia commented 6 years ago

这样是可以的,但是回写 id 就不知道如何弄了,我主要想是插入主表和子表,根据主键关联,这样的操作

abel533 commented 6 years ago

@CarlJia 你直接 getId 得不到回写的值吗?

CarlJia commented 6 years ago

@abel533 getId 是 null

@RequestMapping(value = "/save", method = RequestMethod.POST)
    public ModelAndView save(Country country) {
        ModelAndView result = new ModelAndView("view");
        String msg = country.getId() == null ? "新增成功!" : "更新成功!";
        countryService.save(country);
        System.out.println("id="+country.getId());
        result.addObject("country", country);
        result.addObject("msg", msg);
        return result;
    }

配置

# mybatis \u914D\u7F6E
mybatis.type-aliases-package=tk.mybatis.springboot.model
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.configuration.jdbc-type-for-null=null
# \u901A\u7528 Mapper \u914D\u7F6E
mapper.mappers=tk.mybatis.springboot.util.MyMapper
mapper.not-empty=false
mapper.identity=select @@IDENTITY
mapper.before=true
CarlJia commented 6 years ago

2018-01-08 09:28:59.813 DEBUG 58332 --- [nio-9090-exec-9] o.m.s.t.SpringManagedTransaction : JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@1213d742] will not be managed by Spring 2018-01-08 09:28:59.814 DEBUG 58332 --- [nio-9090-exec-9] t.m.s.mapper.CountryMapper.insert : ==> Preparing: INSERT INTO country ( countryname,countrycode ) VALUES( ?,? ) 2018-01-08 09:28:59.815 DEBUG 58332 --- [nio-9090-exec-9] t.m.s.mapper.CountryMapper.insert : ==> Parameters: test(String), tts(String) 2018-01-08 09:28:59.831 DEBUG 58332 --- [nio-9090-exec-9] t.m.s.mapper.CountryMapper.insert : <== Updates: 1 2018-01-08 09:28:59.832 DEBUG 58332 --- [nio-9090-exec-9] org.mybatis.spring.SqlSessionUtils : Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4cb13f4a] 2018-01-08 09:28:59.832 DEBUG 58332 --- [nio-9090-exec-9] o.s.jdbc.datasource.DataSourceUtils : Returning JDBC Connection to DataSource

abel533 commented 6 years ago

是自增的吧?

把 mapper.before=true 改成 false.

CarlJia commented 6 years ago

before false 后 id 也回写不了 唉,不搞了,让他们装个 MySQL 数据库了,这个 Sybase 真的是反人类啊,怎么诞生出来的。。。