首页 > 解决方案 > 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);
    }
}

桌子

标签: javaspringmybatis

解决方案


你可以试试下面的代码吗?

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 ) : 表中的列


推荐阅读