简体   繁体   English

使用Java和MyBatis将NULL长插入PostgreSQL的bigint列中

[英]Insert long as NULL into bigint column in PostgreSQL using Java and MyBatis

I have a POJO with a long portNumber field. 我有一个带有long portNumber字段的POJO。 During instantiation that field defaults to 0 if it is not set. 在实例化期间,如果未设置该字段,则默认为0 This maps to a nullable and unique column in the database. 这映射到数据库中可为空唯一的列。 I'm using mybatis xml for my database layer. 我正在将mybatis xml用于我的数据库层。 How can I insert portNumber as NULL if it is set to 0 ? 如果将portNumber设置为0如何将其插入为NULL

I'm using JDBC as the database driver for my PostgreSQL database. 我正在使用JDBC作为PostgreSQL数据库的数据库驱动程序。

The class 班级

class MyPojo {
    long portNumber;
    long id;

    public MyPojo(long portNumber) {
        this.portNumber = portNumber;
    }
}

The XML insert statement XML插入语句

<insert id="insert" parameterType="MyPojo" useGeneratedKeys="true" keyColumn="primary_key"
        keyProperty="id">
    INSERT INTO some_table (port_number)
    VALUES (#{portNumber})
</insert>
<insert id="insert" parameterType="MyPojo" useGeneratedKeys="true" 
        keyColumn="primary_key" keyProperty="id">
INSERT INTO some_table (port_number)
VALUES (
<dynamic>
    <isEqual property="portNumber" compareValue="0">
    null
</isEqual>
<isNotEqual property="portNumber" compareValue="0">
    #portNumber#    
</isNotEqual>
</dynamic>
)
</insert>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM