mybatis获得对象参数

1
2
@Insert("insert into data_info_all(operator,operation,oper_time,type_id) values (#{operator},#{operation},#{operTime},#{typeId}) ")
Integer addOperationInfo(DataInfo dataInfo);
正常运行

1
2
@Insert("insert into data_info_all(operator,operation,oper_time,type_id) values (#{operator},#{operation},#{operTime},#{typeId}) ")
Integer addOperationInfo(@Param("dataInfo")DataInfo dataInfo);

测试代码

1
2
3
4
5
6
7
@Test
void addOperationInfoTest222222(){
LocalDateTime ldt1 = LocalDateTime.of(2017, Month.JANUARY, 4, 17, 23, 52);
DataInfo dataInfo=new DataInfo("王五6","创建了一个队伍",ldt1,1);
dataInfoDao.addOperationInfo(dataInfo);

}
报错

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter 'operator' not found. Available parameters are [dataInfo, param1]

修改为下面的代码
1
2
@Insert("insert into data_info_all(operator,operation,oper_time,type_id) values (#{dataInfo.operator},#{dataInfo.operation},#{dataInfo.operTime},#{dataInfo.typeId}) ")
Integer addOperationInfo(@Param("dataInfo")DataInfo dataInfo);
正常运行

当传入的参数是单个对象时,如果没用@Param可以直接使用属性名引用,如果用了@Param,要用"对象.属性名"获取

参考 https://stackoverflow.com/questions/59668117/how-to-properly-use-the-param-annotation-of-mybatis