目录
1.forEach循环
@Test
public void test3() {
int[] ints={1,2,3,4,5,6};
// 将数据编程字符串 1,2,3,4,5,6
StringBuffer sb=new StringBuffer();
for (int i:ints){
sb.append(",").append(i);
}
String s=sb.toString();
System.out.println(s.substring(1));
}
BookMapper.xml:
<select id="selectByIn" resultType="com.xbb.model.Book" parameterType="java.util.List">
select
<include refid="Base_Column_List" />
from t_mvc_book
where bid in
<foreach collection="bookIds" open="(" close=")" separator="," item="bid">
#{bid}
</foreach>
</select>
BookMapper.java:
// 通过in关键字进行查询,讲解foreach标签的使用
// 如果说参数是非实体类(book,Order,...),那么集合加上注解 @param
List<Book> selectByIn(@Param("bookIds") List bookIds);
BookBiz :
List<Book> selectByIn(List bookIds);
BookBizImpl :
public List<Book> selectByIn(List bookIds){
return bookMapper.selectByIn(bookIds);
}
效果展示:
2.模糊查询
MyBatis中#和$的区别:
1. 将传入的数据都当成一个字符串,会对自动传入的数据加一个双引号。
如:order by #user_id#,如果传入的值是111,那么解析成sql时的值为order by '111',
如果传入的值是id,则解析成的sql为order by "id".2. $ 将传入的数据直接显示生成在sql中。
如:order by $user_id$,如果传入的值是111,那么解析成sql时的值为order by user_id,
如果传入的值是id,则解析成的sql为order by id.
3. 方式能够很大程度防止sql注入。
4. $ 方式无法防止Sql注入。
5. $ 方式一般用于传入数据库对象,例如传入表名.。
6. 一般能用 # 的就别用 $ 。
bookmapper.xml:
<select id="selectBooksLike1" resultType="com.xbb.model.Book" parameterType="java.lang.String">
select * from t_mvc_book where bname like #{bname}
</select>
<select id="selectBooksLike2" resultType="com.xbb.model.Book" parameterType="java.lang.String">
select * from t_mvc_book where bname like '${bname}'
</select>
<select id="selectBooksLike3" resultType="com.xbb.model.Book" parameterType="java.lang.String">
select * from t_mvc_book where bname like concat('%',#{bname},'%')
</select>
BookMapper:
package com.xbb.mapper;
import com.xbb.model.Book;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface BookMapper {
int deleteByPrimaryKey(Integer bid);
int insert(Book record);
int insertSelective(Book record);
Book selectByPrimaryKey(Integer bid);
int updateByPrimaryKeySelective(Book record);
int updateByPrimaryKey(Book record);
// 通过in关键字进行查询,讲解foreach标签的使用
// 如果说参数是非实体类(book,Order,....),那么记得加上注解 @param,bookIds是对应collection属性的
List<Book> selectByIn(@Param("bookIds") List bookIds);
List<Book> selectBooksLike1(@Param("bname") String bname);
List<Book> selectBooksLike2(@Param("bname") String bname);
List<Book> selectBooksLike3(@Param("bname") String bname);
}
测试类:
@Test
public void selectBooksLike1(){
bookBiz.selectBooksLike1("%圣墟%").forEach(System.out::println);
}
3.结果集处理
1 使用resultMap返回自定义类型集合
2 使用resultType返回List<T>
3 使用resultType返回单个对象
4 使用resultType返回List<Map>,适用于多表查询返回结果集
5 使用resultType返回Map<String,Object>,适用于多表查询返回单个结果集
resultMap:适合使用返回值是自定义实体类的情况
resultType:适合使用返回值的数据类型是非自定义的,即jdk的提供的类型
4.第三方插件集成myabtis的应用
导入pom依赖:
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
导入pagebean:
package com.xbb.pagination.entity;
import java.io.Serializable;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
public class PageBean implements Serializable {
private static final long serialVersionUID = 2422581023658455731L;
//页码
private int page=1;
//每页显示记录数
private int rows=10;
//总记录数
private int total=0;
//是否分页
private boolean isPagination=true;
//上一次的请求路径
private String url;
//获取所有的请求参数
private 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() {
return isPagination;
}
public void setPagination(boolean isPagination) {
this.isPagination = isPagination;
}
public void setPagination(String isPagination) {
if(null!=isPagination&&!"".equals(isPagination.trim()))
this.isPagination = Boolean.parseBoolean(isPagination);
}
/**
* 获取分页起始标记位置
* @return
*/
public int getStartIndex() {
//(当前页码-1)*显示记录数
return (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
+ "]";
}
}
5.特殊字符处理
BookVo:
public class BookVo extends Book{
private List bookIds;
private int min;
private int max;
public int getmin(){
return min;
}
public void setmin(int min){
this.min=min;
}
public int getmax(){
return max;
}
public void setmax(int max){
this.max=max;
}
BookMapper.xml :
<select id="list6" resultType="com.xbb.model.Book"
parameterType="com.xbb.model.BookVo">
select * from t_mvc_book
<where>
<if test="null != min and min != ''">
<![CDATA[ and #{min} < price ]]>
</if>
<if test="null != max and max != ''">
<![CDATA[ and #{max} > price ]]>
</if>
<![CDATA[ and #{max} < price and #{min} > price ]]>
</where>
</select>
<select id="list7" resultType="com.xbb.model.Book" parameterType="com.xbb.model.BookVo">
select * from t_mvc_book
<where>
<if test="null != min and min != ''">
and #{min} < price
</if>
<if test="null != max and max != ''">
and #{max} > price
</if>
</where>
</select>