mybatis dynamic sql
If、trim、foreach
<select id="selectBooksIn" resultType="com.jt.model.Book" parameterType="java.util.List"> select * from t_mvc_book where bid in <foreach collection="bookIds" open="(" close=")" separator="," item="bid"> #{bid} </foreach> </select>
List<Book> selectBooksIn(@Param("bookIds") List bookIds);
Then look at the tools
Pagebean.java
Private static Final Long serialVersionUID = 2422581023658455731L ; // p Private int Page = . 1 ; // Number per page recording Private int rows = 10 ; // total number of records Private int Total = 0 ; // if the tab Private Boolean = isPagination to true ; // a path request Private String URL; // get all request parameters Private the Map <String, String []> Map; public PageBean() { super(); } //设置请求参数 public void setRequest(HttpServletRequest req) { String page=req.getParameter("page"); String rows=req.getParameter("rows"); String pagination=req.getParameter("pagination"); this.setPage(page); this.setRows(rows); this.setPagination(pagination); this.url=req.getContextPath()+req.getServletPath(); this.map=req.getParameterMap(); } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public Map<String, String[]> getMap() { return map; } public void setMap(Map<String, String[]> map) { this.map = map; } public int getPage() { return page; } public void setPage(int page) { this.page = page; } public void setPage(String page) { if(null!=page&&!"".equals(page.trim())) this.page = Integer.parseInt(page); } public int getRows() { return rows; } public void setRows(int rows) { this.rows = rows; } public void setRows(String rows) { if(null!=rows&&!"".equals(rows.trim())) this.rows = Integer.parseInt(rows); } public int getTotal() { return total; } public void setTotal(int total) { this.total = total; } public void setTotal(String total) { this.total = Integer.parseInt(total); } public boolean isPagination() { returnisPagination; } public void setPagination (Boolean isPagination) { the this .isPagination = isPagination; } public void setPagination (String isPagination) { IF ( null !! = isPagination && "" .equals (isPagination.trim ())) the this .isPagination = Boolean .parseBoolean (isPagination); } / * * * start mark position acquiring tab * @return * / public int getStartIndex () { // (current page -1) * number of records return ( the this.getPage()-1)*this.rows; } /** * 末页 * @return */ public int getMaxPage() { int totalpage=this.total/this.rows; if(this.total%this.rows!=0) totalpage++; return totalpage; } /** * 下一页 * @return */ public int getNextPage() { int nextPage=this.page+1; if(this.page>=this.getMaxPage()) nextPage=this.getMaxPage(); return nextPage; } /** * 上一页 * @return */ public int getPreivousPage() { int previousPage=this.page-1; if(previousPage<1) previousPage=1; return previousPage; } @Override public String toString() { return "PageBean [page=" + page + ", rows=" + rows + ", total=" + total + ", isPagination=" + isPagination + "]"; }
StringUtils
public class StringUtils { public static String toLikeStr(String str) { return "%"+str+"%"; } }
2, fuzzy query
#{…}
${…}
Concat
Note: # {...} comes with quotation marks, $ {...} There sql risk injected
<select id="selectBooksLike1" resultType="com.jt.model.Book" parameterType="java.lang.String"> select * from t_mvc_book where bname like #{bname} </select> <select id="selectBooksLike2" resultType="com.jt.model.Book" parameterType="java.lang.String"> select * from t_mvc_book where bname like '${bname}' </select> <select id="selectBooksLike3" resultType="com.jt.model.Book" parameterType="java.lang.String"> select * from t_mvc_book where bname like concat(concat('%',#{bname}),'%') </select>
List<Book> selectBooksLike1(@Param("bname")String bname); List<Book> selectBooksLike2(@Param("bname")String bname); List<Book> selectBooksLike3(@Param("bname")String bname);
Return result sets query processing
The resultMap : suitable return value is the case where the custom entity class
the resultType : suitable return value data type of non-custom, i.e. jdk type provided
3.1 Use resultMap return custom type set
3.2 Use resultType return List <T>
3.3 Use resultType return a single object
3.4 Use resultType return List <the Map> , the result set returned applies to multi-table query
3.5 Use resultType return to the Map <String, Object> , for multi-table queries return a single result set
<select id="list1" resultMap="BaseResultMap"> select * from t_mvc_book </select> <select id="list2" resultType="com.jt.model.Book"> select * from t_mvc_book </select> <select id="list3" resultType="com.jt.model.Book"ParameterType ="com.jt.model.vo.BookVo"> select * from t_mvc_book where bid in <foreach collection="bookIds" item="bid" open="(" close=")" separator=","> #{bid} </foreach> </select> <select id="list4" resultType="java.util.Map" parameterType="java.util.Map"> select * from t_mvc_book where bid in <foreach collection="bookIds" item="bid" open="(" close=")" separator=","> #{bid} </foreach> </select> <select id="list5" resultType="java.util.Map"> select * from t_mvc_book where bid = #{bid} </select>
public void list() { // List<Book> books = this.bookService.list1(); // List<Book> books = this.bookService.list2(); List list=new ArrayList(); list.add(1); list.add(12); list.add(16); // BookVo bookVo=new BookVo(); // bookVo.setBookIds(list); // List<Book> books = this.bookService.list3(bookVo); // // // for (Book b : books) { // System.out.println(b); // } // Map map=new HashMap(); // map.put("bookIds",list); // List<Map> mapList = this.bookService.list4(map); // for (Map m : mapList) { // System.out.println(m); // // } // map.put("bid",1); // System.out.println(this.bookService.list5(map)); }
Paging query
Why rewrite mybatis paging?
Mybatis paging function is weak, it is the memory-based page ( isolated then all records offsets offset and boundary limit take result ) , this tab is substantially not used in the case of a large amount of data
Use pagination plug-in step-outs
1, introduced pom dependent
2, Mybatis.cfg.xml arranged interceptors
3, using PageHelper paging
4, the processing result tab
pom-dependent
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.2</version> </dependency>
Mybatis.cfg.xml configured interceptor
<plugins> <-! widget configuring paging PageHelper, 4.0 future automatic identification .0 version supports the use of a database -> <= plugin Interceptor " com.github.pagehelper.PageInterceptor " > </ plugin> </ plugins>
Use pagination plug-in
<select id="listPager" resultType="java.util.Map" parameterType="java.util.Map"> select * from t_mvc_book where bname like concat(concat('%',#{bname}),'%') </select>
Mapper layer
List<Map> listPager(Map map);
Service Layer
List<Map> listPager(Map map, PageBean pageBean);
@Override public List<Map> listPager(Map map, PageBean pageBean) { if(pageBean != null && pageBean.isPagination()){ PageHelper.startPage(pageBean.getPage(),pageBean.getRows()); } List<Map> list = bookMapper.listPager(map); if(pageBean != null && pageBean.isPagination()){ PageInfo pageInfo = new PageInfo(list); System.out.println("页码:"+pageInfo.getPageNum()); System.out.println("页大小:"+pageInfo.getPageSize()); System.out.println("总记录:"+pageInfo.getTotal()); pageBean.setTotal(pageInfo.getTotal()+""); } return list; }
Special Character Handling
>(>) <(<) &(&) 空格( ) <![CDATA[ <= ]]>
The relevant code configuration
<select id="list6" resultType="com.jt.model.Book" parameterType="com.jt.model.vo.BookVo"> select * from t_mvc_book where <![CDATA[ price >#{min} and price <#{max} ]]> </select> <select id="list7" resultType="com.jt.model.Book" parameterType="com.jt.model.vo.BookVo"> select * from t_mvc_book where >#{min} and price <#{max} </select>