这是一个流行的例子。
<!-- Insert example, using the Account parameter class -->
<insert id="insertAccount" parameterType="Account">
insert into ACCOUNT (
ACC_ID,
ACC_FIRST_NAME,
ACC_LAST_NAME,
ACC_EMAIL
)values (
#{id}, #{firstName}, #{lastName}, #{emailAddress}
)
我想手动为其中一个字段插入 null。
<!-- Insert example, using the Account parameter class -->
<insert id="insertAccount" parameterType="Account">
insert into ACCOUNT (
ACC_ID,
ACC_FIRST_NAME,
ACC_LAST_NAME,
ACC_EMAIL
)values (
#{id}, #{firstName}, #{lastName}, #{null}
) 但是我得到了这个异常(exception)
org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'null'
谁能帮忙。
请您参考如下方法:
MyBatis 使用#{propertyName} 来定义属性。如果您使用名称“NULL”,它会查找 getNull() 和 setNull(...) 或命名参数或映射。但是,如果在您的情况下该值曾经为 null,则可以省略该值,就像您在数据库中没有该列的默认值一样。
<insert id="insertAccount" parameterType="Account">
insert into ACCOUNT (
ACC_ID,
ACC_FIRST_NAME,
ACC_LAST_NAME
)values (
#{id}, #{firstName}, #{lastName}
);
或者以与 SQL 命令相同的方式输入准确的值:
<insert id="insertAccount" parameterType="Account">
insert into ACCOUNT (
ACC_ID,
ACC_FIRST_NAME,
ACC_LAST_NAME,
ACC_EMAIL
)values (
#{id}, #{firstName}, #{lastName}, null
);