这是dao方法
int insertRecord(CustmoerDownRecordDO custmoerDownRecordDO,String taskType);
}
这是xml
<insert id="insertRecord" parameterType="com.yz.api.pojo.CustmoerDownRecordDO"> insert into custmoer_down_record(task_type,customer_id,file_info_id, bucket_name,object_name,gmt_create,created_by,gmt_modify) values ( #{taskType,jdbcType=VARCHAR}, #{customerId,jdbcType=VARCHAR}, #{fileInfoId,jdbcType=BIGINT}, #{bucketName,jdbcType=VARCHAR}, #{objectName,jdbcType=VARCHAR}, #{gmtCreate,jdbcType=TIMESTAMP}, #{createdBy,jdbcType=VARCHAR}, #{gmtModify,jdbcType=TIMESTAMP} ) </insert>
这是service
public int insertRecord(CustmoerDownRecordDO custmoerDownRecord, String taskType) {
try {
//记录客户下载文件信息
FileInfoDO fileInfoMessage = fileInfoDao.select(taskType);
custmoerDownRecord.setTaskType(taskType);
custmoerDownRecord.setCustomerId(taskType);
custmoerDownRecord.setFileInfoId(fileInfoMessage.getId());
custmoerDownRecord.setBucketName((fileInfoMessage.getBucketName()));
custmoerDownRecord.setObjectName(fileInfoMessage.getObjectName());
custmoerDownRecord.setGmtCreate(fileInfoMessage.getGmtCreate());
custmoerDownRecord.setGmtModify(fileInfoMessage.getGmtModify());
custmoerDownRecordDao.insertRecord(custmoerDownRecord, taskType);
}catch(Exception e){
logger.error("记录用户下载文件异常",e);
}
return 0;
这是contorller
@Autowired
private ApiService apiService;
@RequestMapping("/api/v1")
public ResultJson selectByType(HttpServletRequest request,
HttpServletResponse response)throws Exception{
String key = request.getHeader(ParamConstants.X_ACCESS_LKEY);
String task_type = request.getHeader(ParamConstants.TASK_TYPE);
ResultJson result = null;
result = ResultJson.ok().data(apiService.selectByType(task_type));
CustmoerDownRecordDO custmoerDownRecordDO = new CustmoerDownRecordDO();
apiService.insertRecord(custmoerDownRecordDO,task_type);
return result;
大概就是我要根据http请求头里面的tasktype来判断用户下载的是哪文件 然后把一些属性拿出来记录一下
问题是insertRecord(CustmoerDownRecordDO custmoerDownRecord, String taskType)这个方法如果不传
tasktype这个参数 没法知道是哪个文件 问题加上就一直报错
抱着个错误
nested exception is org.apache.ibatis.binding.BindingException: Parameter 'taskType' not found. Available parameters are [custmoerDownRecordDO, task_type, param1, param2]
dao中给tasktype前面添加@param(“tasktype”)。
int insertRecord(@Param("do") CustmoerDownRecordDO custmoerDownRecordDO, @Param("taskType") String taskType);
<insert id="insertRecord" parameterType="com.yz.api.pojo.CustmoerDownRecordDO"> insert into custmoer_down_record(task_type,customer_id,file_info_id, bucket_name,object_name,gmt_create,created_by,gmt_modify) values ( #{taskType,jdbcType=VARCHAR}, #{do.customerId,jdbcType=VARCHAR}, #{do.fileInfoId,jdbcType=BIGINT}, #{do.bucketName,jdbcType=VARCHAR}, #{do.objectName,jdbcType=VARCHAR}, #{do.gmtCreate,jdbcType=TIMESTAMP}, #{do.createdBy,jdbcType=VARCHAR}, #{do.gmtModify,jdbcType=TIMESTAMP} ) </insert>
int insertRecord(@Param("do") CustmoerDownRecordDO custmoerDownRecordDO, @Param("taskType") String taskType);
<insert id="insertRecord" parameterType="com.yz.api.pojo.CustmoerDownRecordDO">
insert into custmoer_down_record(task_type,customer_id,file_info_id,
bucket_name,object_name,gmt_create,created_by,gmt_modify)
values (
#{taskType,jdbcType=VARCHAR},
#{do.customerId,jdbcType=VARCHAR},
#{do.fileInfoId,jdbcType=BIGINT},
#{do.bucketName,jdbcType=VARCHAR},
#{do.objectName,jdbcType=VARCHAR},
#{do.gmtCreate,jdbcType=TIMESTAMP},
#{do.createdBy,jdbcType=VARCHAR},
#{do.gmtModify,jdbcType=TIMESTAMP}
)
</insert>
这就涉及到mybatis mapper传参问题,只有一个参数是没什么争议的。参数过多时,就需要给每个参数都起一个别名,也就是每个参数钱都加上@param("别名")注解。mapper.xml中信用参数时加上别名前缀。这个前面几个回答写的比较清楚了。重要的事说三遍:给每个参数都起一个别名。给每个参数都起一个别名。给每个参数都起一个别名。