java - How to make a dynamic query for MySQL -
i used spring mvc, how can made dynamic query mysql? have in controller that: public string getnewsbydateandauthor(model model, httpservletrequest request) {}
how can used parameters url request.getparametermap()
, , map <string, list<string>>
create dynamic query that?
for example, if have 3 parameters want generate 1 query that:
select * news parameter = in(val1, val2...) , parameter2 = in(val1, val2...) , parameter3 = in(val1, val2...)
or have 1 parameter;
select * news parameter = in(val1, val2...)
if know please show me how can that.
i want code that:
public string getquery(map<string,list<string>> parameters){ list<string> author = new arraylist<>(); list<string> startdate = new arraylist<>(); list<string> enddate = new arraylist<>(); list<string> categories = new arraylist<>(); list<string> = new arraylist<>(); list<string> soureid = new arraylist<>(); if (!parameters.get("author").isempty()) (int = 0; < parameters.get("author").size(); i++) { author.add(parameters.get("author").get(i)); } if (!parameters.get("startdate").isempty()) (int = 0; < parameters.get("startdate").size(); i++) { startdate.add(parameters.get("startdate").get(i)); } if (!parameters.get("enddate").isempty()) (int = 0; < parameters.get("enddate").size(); i++) { enddate.add(parameters.get("enddate").get(i)); } if (!parameters.get("categories").isempty()) (int = 0; < parameters.get("categories").size(); i++) { categories.add(parameters.get("categories").get(i)); } if (!parameters.get("about").isempty()) (int = 0; < parameters.get("about").size(); i++) { about.add(parameters.get("about").get(i)); } if (!parameters.get("soureid").isempty()) (int = 0; < parameters.get("soureid").size(); i++) { soureid.add(parameters.get("soureid").get(i)); } stringbuilder querybuilder = new stringbuilder(); querybuilder.append("select * news where"); if (!author.isempty()) { string authors = author.tostring(); authors.replace(" ", " , "); querybuilder.append(" author = in ( " + authors + " ) and"); } if (!startdate.isempty()) { string startdates = startdate.tostring(); startdates.replace(" ", " , "); querybuilder.append(" author = in ( " + startdates + " ) and"); } if (!enddate.isempty()) { string enddates = enddate.tostring(); enddates.replace(" ", " , "); querybuilder.append(" author = in ( " + enddates + " ) and"); } if (!categories.isempty()) { string categoriesr = categories.tostring(); categoriesr.replace(" ", " , "); querybuilder.append(" author = in ( " + categoriesr + " ) and"); } if (!about.isempty()) { string abouts = about.tostring(); abouts.replace(" ", " , "); querybuilder.append(" author = in ( " + abouts + " ) and"); } if (!soureid.isempty()) { string sourceids = soureid.tostring(); sourceids.replace(" ", " , "); querybuilder.append(" author = in ( " + sourceids + " ) and"); } querybuilder.delete(querybuilder.length() - 3, querybuilder.length()); string query = querybuilder.tostring(); return query; }
you don't want method that, don't want use string concats create dynamic query. want use criteria
api that.
something following
criteriabuilder cb = em.getcriteriabuilder(); criteriaquery<news> cq = cb.createquery(news.class); root<news> news = cq.from(news.class); if (!parameters.get("author").isempty()) { cq.where(cb.in(news.get("author"), parameters.get("author")); } typedquery<news> query = em.createquery(cq); return query.getresultlist();
as have multiple results first want create list of predicates
, apply them using cq.where(cb.and(predicates.toarray(new predicate[predicates.size()]);
.
criteriabuilder cb = em.getcriteriabuilder(); criteriaquery<news> cq = cb.createquery(news.class); root<news> news = cq.from(news.class); list<predicate> predicates = new arraylist<predicate>(); if (!parameters.get("author").isempty()) { predicates.add(cb.in(news.get("author"), parameters.get("author"))); } // other params here. cq.where(cb.and(predicates.toarray(new predicate[predicates.size()])); typedquery<news> query = em.createquery(cq); return query.getresultlist();
Comments
Post a Comment