java - MyBatis 无法在查询中设置参数
问题描述
Mybatis 报错,说设置参数有问题。有什么问题?我测试了 SQL 查询,没问题。我正在使用带有弹簧的渐变。
查询数据库时出错。
原因:org.postgresql.util.PSQLException: ERROR: syntax error at or near "customer" Position: 245
错误可能存在于 services/CustomerService.java 中(最佳猜测)
错误可能涉及 services.CustomerService.findByPersonalCodeAndCountry-Inline
错误设置参数时发生
代码:
import com.luminor.dc.ccc.contracts.dao.Customer;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface CustomerService {
@Select("Select * FROM customer WHERE cust_personal_code= #{personalCode} AND cust_bank_country = #{country}")
List<Customer> findByPersonalCodeAndCountry(@Param("personalCode") String personalCode,@Param("country") String country);
}
控制器
@RestController
@RequestMapping(value = "/v1/customers", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class CustomerController {
private CustomerService customerService;
public CustomerController(@Autowired CustomerService customerService) {
this.customerService = customerService;
}
@GetMapping("/{personalCode}")
public List<Customer> getCustomersByPersonalCode(@PathVariable String personalCode, @RequestHeader String country) {
return customerService.findByPersonalCodeAndCountry(personalCode, country);
}
}
桌子
解决方案
你可以试试下面的代码吗?
Controller method :
@GetMapping("/{personalCode}")
public List<Customer> getCustomersByPersonalCode(@PathVariable String personalCode, @RequestHeader(value="country") String country) {
return customerService.findByPersonalCodeAndCountry(personalCode, country);
}
@Results(value = { @Result(column = "id", property = "id"),
@Result(column = "cust_personal_code", property = "personalCode"),
@Result(column = "cust_bank_country ", property = "country")
)}
@Select("Select * FROM customer WHERE cust_personal_code= #{personalCode} AND cust_bank_country = #{country}")
List<Customer> findByPersonalCodeAndCountry(@Param("personalCode") String personalCode, @Param("country") String country);
其中 id, personalCode,country : 客户 POJO 表中的变量 -> id(Integer),cust_personal_code (String ),cust_bank_country(String ) : 表中的列
推荐阅读
- c++ - 为什么这个参数包不能直接解包到向量初始化列表中?
- angular - 清理组件时出错 this._app.unregisterRootNav 不是 NavControllerBase.destroy 的函数
- python - 如何获取选定复选框的总和并将选定项目保存在数据库 mysql 中
- php - 修剪/减少PHP中的多维数组到一定深度
- hyperledger-fabric-ca - 如何远程访问 Fabric-ca 服务器?
- angular - 需要为许多属性中的特定属性应用管道
- docker - 每当挂载的代码更改时,盖茨比都不会重建
- sql - Json 数组 - 选择左侧数组中不在右侧数组中的元素
- xpages - 过滤掉 XPage 视图中的数据
- sql - 尝试在嵌套循环中使用两个硬编码值