MyBatis動態sql
在接口中定義方法
然后alt加回車在xml中如圖:
1.if 語句 (簡單的條件判斷)
2. choose (when,otherwize) ,相當於java 語言中的 switch ,與 jstl 中的choose 很類似
3. trim (對包含的內容加上 prefix,或者 suffix 等,前綴,后綴)
4. where (主要是用來簡化sql語句中where條件判斷的,能智能的處理 and or ,不必擔心多余導致語法錯誤)、
5. set (主要用於更新時)
6. foreach (在實現 mybatis in 語句查詢時特別有用)
測試:
@Test public void selectByIn() { List list = new ArrayList(); list.add(1); list.add(2); list.add(3); list.add(10003); List<Book> books = this.bookService.selectByIn(list); for(Book b : books){ System.out.println(b); } }
展示:
模糊查詢
這里演示3種:
接口:
List<Book> selectBylike1(@Param("bname") String bname); List<Book> selectBylike2(@Param("bname") String bname); List<Book> selectBylike3(@Param("bname") String bname);
映射文件
<select id="selectBylike1" resultType="com.cjh.model.Book" parameterType="java.lang.String"> select * from t_mvc_book where bname like #{bname} </select> <select id="selectBylike2" resultType="com.cjh.model.Book" parameterType="java.lang.String"> select * from t_mvc_book where bname like '${bname}' </select> <select id="selectBylike3" resultType="com.cjh.model.Book" parameterType="java.lang.String"> select * from t_mvc_book where bname like concat(concat('%',#{bname},'%')) </select>
注意:#{...}自帶引號,${...}有sql注入的風險
測試:
@Test public void selectBylike() { // List<Book> books = this.bookService.selectBylike1(StringUtils.toLikeStr("聖墟")); // List<Book> books = this.bookService.selectBylike2("%聖墟 or bid!=1%");//這種方式存在sql攻擊 List<Book> books = this.bookService.selectBylike3("聖墟"); for(Book b : books){ System.out.println(b); } }
其中StringUtils:
public class StringUtils { public static String toLikeStr(String str){ return "%"+str+"%"; } }
結果:
查詢返回結果集的處理
resultMap:適合使用返回值是自定義實體類的情況
resultType:適合使用返回值的數據類型是非自定義的,即jdk的提供的類型
3.1 使用resultMap返回自定義類型集合
3.2 使用resultType返回List<T>
3.3 使用resultType返回單個對象
3.4 使用resultType返回List<Map>,適用於多表查詢返回結果集
3.5 使用resultType返回Map<String,Object>,適用於多表查詢返回單個結果集
接口:
// 3.1 使用resultMap返回自定義類型集合 List<Book> list1(); // 3.2 使用resultType返回List<T> List<Book> list2(); // 3.3 使用resultType返回單個對象 Book list3(BookVo bookVo); // 3.4 使用resultType返回List<Map>,適用於多表查詢返回結果集 List<Map> list4(Map map); // 3.5 使用resultType返回Map<String,Object>,適用於多表查詢返回單個結果集 Map list5(Map map);
BookVo類:
public class BookVo extends Book{ private List<String> bookIds; public List<String> getBookIds() { return bookIds; } public void setBookIds(List<String> bookIds) { this.bookIds = bookIds; } }
映射文件:
<select id="list1" resultMap="BaseResultMap"> select * from t_mvc_book </select> <select id="list2" resultType="com.cjh.model.Book"> select * from t_mvc_book </select> <select id="list3" resultType="com.cjh.model.Book" parameterType="com.cjh.model.BookVo"> select * from t_mvc_book where bid in <foreach collection="bookIds" open="(" close=")" separator="," item="bid"> #{bid} </foreach> </select> <select id="list4" resultType="java.util.Map" parameterType="java.util.Map"> select * from t_mvc_book <where> <if test="null !=bname and bname !=''"> and bname like #{bname} </if> </where> </select> <select id="list5" resultType="java.util.Map" parameterType="java.util.Map"> select * from t_mvc_book <where> <if test="null !=bid and bid !=''"> and bid = #{bid} </if> </where> </select>
測試:
@Test public void list() { //返回resultMap但是使用list<T> // List<Book> books = this.bookService.list1(); //返回resulttype使用list<T>接收 // List<Book> books = this.bookService.list2(); // for(Book b : books){ // System.out.println(b); // } // //返回的是resulttype使用T接收 // BookVo bookVo = new BookVo(); // List list = new ArrayList(); // list.add(1); // bookVo.setBookIds(list); // Book book = this.bookService.list3(bookVo); // System.out.println(book); //返回的是resultTypr ,然后用list<Map>進行接收 Map map = new HashMap(); // map.put("bname", StringUtils.toLikeStr("聖墟")); // List<Map> list = this.bookService.list4(map); // for (Map m : list) { // System.out.println(m); // } //返回的是resultTypr ,然后用Map進行接收 map.put("bid",2); Map k = this.bookService.list5(map); System.out.println(k); }
分頁查詢
為什么要重寫mybatis的分頁?
Mybatis的分頁功能很弱,它是基於內存的分頁(查出所有記錄再按偏移量offset和邊界limit取結果),在大數據量的情況下這樣的分頁基本上是沒有用的
使用分頁插件步奏
1、導入pom依賴
2、Mybatis.cfg.xml配置攔截器
3、使用PageHelper進行分頁
4、處理分頁結果
Pom依賴:
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.2</version> </dependency>
Mybatis.cfg.xml配置攔截器
<plugins> <!-- 配置分頁插件PageHelper, 4.0.0以后的版本支持自動識別使用的數據庫 --> <plugin interceptor="com.github.pagehelper.PageInterceptor"> </plugin> </plugins>
然后在實現類中寫方法:
@Override public List<Map> listPagers(Map map, PageBean pageBean) { if (pageBean != null && pageBean.isPagination()){ PageHelper.startPage(pageBean.getPage(),pageBean.getRows()); } List<Map> list = this.BookMapper.list4(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; }
測試:
@Test public void listPage() { Map map = new HashMap(); map.put("bname", StringUtils.toLikeStr("聖墟")); PageBean pageBean = new PageBean(); // pageBean.setPage(3); //不分頁 pageBean.setPagination(false); List<Map> list = this.bookService.listPagers(map, pageBean); for (Map m : list){ System.out.println(m); } }
特殊字符處理:
方式一:
大於:>
小於:<
空格:&
方式二:<![CDATA[ <= ]]>
接口:
List<Map> list6(BookVo bookVo);
List<Map> list7(BookVo bookVo);
映射文件:
<select id="list6" resultType="java.util.Map" parameterType="com.cjh.model.BookVo"> select * from t_mvc_book <where> <if test="null !=min and min !=''"> and price > #{min} </if> <if test="null !=max and max !=''"> and price < #{max} </if> </where> </select> <select id="list7" resultType="java.util.Map" parameterType="com.cjh.model.BookVo"> select * from t_mvc_book <where> <if test="null !=min and min !=''"> <![CDATA[ and price > #{min}]]> </if> <if test="null !=max and max !=''"> <![CDATA[ and price < #{max}]]> </if> </where> </select>
測試:
@Test public void sqlSpecial() { BookVo bookVo = new BookVo(); bookVo.setMax(80); bookVo.setMin(20); /* List<Map> maps = this.bookService.list6(bookVo);*/ List<Map> maps = this.bookService.list7(bookVo); for (Map map : maps) { System.out.println(map); } }