java - How to use IN clause with Mybatis Annotation inside SQL Provider -
i have seen links pointing solution relevant how use annotations ibatis (mybatis) in query? doesn't provide solution oracle driver.
public string getemployees(map<string, object> params){ //value hold params params={empid={123,345,667,888}} stringbuilder sql=new stringbuilder(); sql.append("select * employee emp_id in (#{empid}");
mybatis substitute values params. when value substituted query becomes thing below.
select * employee emp_id in ('123,345,667,888');
which invalid query mybatis has added single quotes in query.
how should handle issue fix? cannot concatenate values because prevent sql injection.
the accepted answer in how use annotations ibatis (mybatis) in query? gives solution working postgres, string representation of list/array passed , converted database. oracle not support this. list must iterared bind every value.
in opinion, looking dynamic sql, explained lordofthepigs in next answer. adapted case be:
@select({"<script>", "select *", "from employee", "where emp_id in", "<foreach item='emp' collection='empid'", "open='(' separator=', ' close=')'>", "#{emp}", "</foreach>", "</script>"}) list<employee selectemployees(map<string, object> params);
@selectprovider provides sql string built in java. binding parameters become more tedious.
Comments
Post a Comment