0

Mybatis throws an error which says there is problem with setting params. What could be wrong? I tested the SQL query and it's fine. I'm using graddle with spring.

Error querying database.
Cause: org.postgresql.util.PSQLException: ERROR: syntax error at or near "customer" Position: 245
The error may exist in services/CustomerService.java (best guess)
The error may involve services.CustomerService.findByPersonalCodeAndCountry-Inline
The error occurred while setting parameters

Code:

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);
}

Controller

@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);
    }
}

Table

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
jodymilers
  • 15
  • 5
  • This error usually happens if the query is syntactically incorrect, e.g. `select * customer where ...`. May it be that you had incorrect query, then fix it but still are running the old code? I would suggest to enable queries logging (in mybatis or in postgres) and check what query is really sent and executed in DB. – Roman-Stop RU aggression in UA Oct 16 '18 at 14:01
  • try using db level logging,useful link https://stackoverflow.com/questions/41001188/spring-boot-with-spring-mybatis-how-to-force-it-to-logging-all-sql-queries/41234831 – Akhil S Kamath Oct 16 '18 at 14:12

1 Answers1

-1

Can you try below code ?

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);

where id, personalCode,country : variables in customer POJO Table-> id(Integer),cust_personal_code (String ),cust_bank_country(String ) : columns in table

Akhil S Kamath
  • 1,012
  • 13
  • 23