实现思路:首先添加配置文件,以通过in关键字查询比例,通过佛reach标签查询,因为是返回一个集合,所有该方法返回java.util.List,需要注意foreach标签的属性item代表返回参数,而用==#来接收,还需要注意如果说参数是非实体类 那么记得加上注解 @Param(变量名–对应配置文件collection参数)==
例如:配置文件的查询配置
<!--
bokkIds就是数组
parameterType 参数类型
-->
<select id="selectByIn" resultMap="BaseResultMap" parameterType="java.util.List" >
select
<include refid="Base_Column_List" />
for t_mvc_book
where bid in
<foreach collection="bookIds" open="(" close= ")" item="bid" separator=",">
#{bid}
</foreach>
</select>
实现方法
//通过in关键字进行查询 了解forearm标签
// 如果说参数是非实体类 那么记得加上注解 @Param(变量名--对应配置文件collection参数)
List<Book> selectByIn(@Param("bookIds") List bookIds);
然后加到我们的BookBiz层,在这个层则我们可以去掉注解
List<Book> selectByIn(List bookIds);
然后写实现类
@Override
public List<Book> selectByIn(List bookIds) {
return bookMapper.selectByIn(bookIds);
}
最后写测试类,测试该方法
@Test
public void selectByIn() {
List<Integer> integers = Arrays.asList(new Integer[] {33,34,32});
bookBiz.selectByIn(integers).forEach(System.out::println);
}
<select id="selectBooksLike1" resultType="com.javaxl.model.Book" parameterType="java.lang.String">
select * from t_mvc_book where bname like #{bname}
</select>
实现类
List<Book> selectBooksLike1(@Param("bname") String bname);
接口类BookBiz
List<Book> selectBooksLike1(String bname);
实现接口层
public List<Book> selectBooksLike1(String bname){
return bookMapper.selectBooksLike1(bname);
}
最后测试类
@Test
public void selectBooksLike1() {
bookBiz.selectBooksLike1("%圣墟%").forEach(System.out::println);
}
<select id="selectBooksLike2" resultType="com.javaxl.model.Book" parameterType="java.lang.String">
select * from t_mvc_book where bname like '${bname}'
</select>
实现类
List<Book> selectBooksLike2(@Param("bname") String bname);
接口类BookBiz
List<Book> selectBooksLike2(String bname);
实现接口层
public List<Book> selectBooksLike2(String bname){
return bookMapper.selectBooksLike2(bname);
}
实现类:
@Test
public void selectBooksLike2() {
bookBiz.selectBooksLike2("%圣嘘%").forEach(System.out::println);
}
<select id="selectBooksLike3" resultType="com.javaxl.model.Book" parameterType="java.lang.String">
select * from t_mvc_book where bname like concat(concat('%',#{bname}),'%')
</select>
实现类
List<Book> selectBooksLike3(@Param("bname") String bname);
接口BookBiz
List<Book> selectBooksLike3( String bname);
实现接口层
public List<Book> selectBooksLike3(String bname){
return bookMapper.selectBooksLike3(bname);
}
测试类
@Test
public void selectBooksLike3() {
bookBiz.selectBooksLike3("圣嘘").forEach(System.out::println);
}
MyBatis中#和$的区别
- #将传入的数据都当成一个字符串,会对自动传入的数据加一个双引号。
如:order by #user_id#,如果传入的值是111,那么解析成sql时的值为order by ‘111’,
如果传入的值是id,则解析成的sql为order by “id”.
- $将传入的数据直接显示生成在sql中。
如:order by u s e r i d user_id userid,如果传入的值是111,那么解析成sql时的值为order by user_id,
如果传入的值是id,则解析成的sql为order by id.
- #方式能够很大程度防止sql注入。
- $方式无法防止Sql注入。
- $方式一般用于传入数据库对象,例如传入表名.
- 一般能用#的就别用$.
Concat可以直接传参,一般采用Concat方式
首先配置方法
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.xlb.mapper.BookMapper" >
<resultMap id="BaseResultMap" type="com.xlb.model.Book" >
<constructor >
<idArg column="bid" jdbcType="INTEGER" javaType="java.lang.Integer" />
<arg column="bname" jdbcType="VARCHAR" javaType="java.lang.String" />
<arg column="price" jdbcType="REAL" javaType="java.lang.Float" />
</constructor>
</resultMap>
<sql id="Base_Column_List" >
bid, bname, price
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from t_mvc_book
where bid = #{bid,jdbcType=INTEGER}
</select>
<!--
bokkIds就是数组
parameterType 参数类型
-->
<select id="selectByIn" resultMap="BaseResultMap" parameterType="java.util.List" >
select
<include refid="Base_Column_List" />
for t_mvc_book
where bid in
<foreach collection="bookIds" open="(" close= ")" item="bid" separator=",">
#{bid}
</foreach>
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from t_mvc_book
where bid = #{bid,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.xlb.model.Book" >
insert into t_mvc_book (bid, bname, price
)
values (#{bid,jdbcType=INTEGER}, #{bname,jdbcType=VARCHAR}, #{price,jdbcType=REAL}
)
</insert>
<insert id="insertSelective" parameterType="com.xlb.model.Book" >
insert into t_mvc_book
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="bid != null" >
bid,
</if>
<if test="bname != null" >
bname,
</if>
<if test="price != null" >
price,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="bid != null" >
#{bid,jdbcType=INTEGER},
</if>
<if test="bname != null" >
#{bname,jdbcType=VARCHAR},
</if>
<if test="price != null" >
#{price,jdbcType=REAL},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.xlb.model.Book" >
update t_mvc_book
<set >
<if test="bname != null" >
bname = #{bname,jdbcType=VARCHAR},
</if>
<if test="price != null" >
price = #{price,jdbcType=REAL},
</if>
</set>
where bid = #{bid,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.xlb.model.Book" >
update t_mvc_book
set bname = #{bname,jdbcType=VARCHAR},
price = #{price,jdbcType=REAL}
where bid = #{bid,jdbcType=INTEGER}
</update>
<select id="selectBooksLike1" resultType="com.xlb.model.Book" parameterType="java.lang.String">
select * from t_mvc_book where bname like #{bname}
</select>
<select id="selectBooksLike2" resultType="com.xlb.model.Book" parameterType="java.lang.String">
select * from t_mvc_book where bname like '${bname}'
</select>
<select id="selectBooksLike3" resultType="com.xlb.model.Book" parameterType="java.lang.String">
select * from t_mvc_book where bname like concat(concat('%',#{bname}),'%')
</select>
<select id="list1" resultMap="BaseResultMap">
select * from t_mvc_book
</select>
<select id="list2" resultType="com.xlb.model.Book">
select * from t_mvc_book
</select>
<select id="list3" resultType="com.xlb.model.Book" parameterType="com.xlb.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">
select * from t_mvc_book
</select>
<select id="list5" resultType="java.util.Map" parameterType="java.util.Map">
select * from t_mvc_book where bid = #{bid}
</select>
</mapper>
其中如果是单表查询则Mybatis对应ResultType
而多表查询则对应Mybatis中的ResultMap
如果是单表的情况下,ResultType和ResultMap都可以使用
需要一个工具类BookVo
package com.xlb.model;
import java.util.List;
/**
* @author 波哥
* @QQ 2212371722
* @company 波哥集团
* @create 2022-08-12 11:26
*/
public class BookVo {
private List booKIds;
public List getBooKIds() {
return booKIds;
}
public void setBooKIds(List booKIds) {
this.booKIds = booKIds;
}
}
验证单表查询可以用参数BaseResultMap和BaseResultType接收
方法层
//list1和list2结论是对于单表而言,可以用BaseResultMap和BaseResultType,但是多表可以用BaseResultMap
List<Book> list1();
List<Book> list2();
List<Book> list1();
List<Book> list2();
@Override
public List<Book> list1() {
return bookMapper.list1();
}
@Override
public List<Book> list2() {
return bookMapper.list2();
}
测试类
@Test
public void list1() {
bookBiz.list1().forEach(System.out::println);
}
@Test
public void list2() {
bookBiz.list2().forEach(System.out::println);
}
验证要传入多个查询条件,必须以对象的方式进行传递
方法层
List<Book> list3(BookVo vo);
List list3(BookVo vo);
@Override
public List<Book> list3(BookVo vo) {
return bookMapper.list3(vo);
}
结果
@Test
public void list3() {
BookVo vo = new BookVo();
vo.setBooKIds(Arrays.asList(new Integer[]{31,32,33,34}));
bookBiz.list3(vo).forEach(System.out::println);
}
验证不管返回1条数据,还是多条数据,都应该同java.util.Map进行接收
如果是一条,返回Map
如果是两条,返回值List
List<Map> list4();
Map list5(Map map);
List<Map> list4();
Map list5(Map map);
@Override
public List<Map> list4() {
return bookMapper.list4();
}
@Override
public Map list5(Map map) {
return bookMapper.list5(map);
}
测试类
@Test
public void list4() {
bookBiz.list4().forEach(System.out::println);
}
@Test
public void list5() {
Map map = new HashMap();
map.put("bid",32);
System.out.println(bookBiz.list5(map));
}
使用步骤
1.导入pom依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
2.Mybatis.cfg.xml配置拦截器
<plugins>
<!-- 配置分页插件PageHelper, 4.0.0以后的版本支持自动识别使用的数据库 -->
<plugin interceptor="com.github.pagehelper.PageInterceptor">
</plugin>
</plugins>
3.使用PageHelper进行分页
package com.zking.util;
/**
* 分页工具类
*
*/
public class PageBean {
private int page = 1;// 页码
private int rows = 10;// 页大小
private int total = 0;// 总记录数
private boolean pagination = true;// 是否分页
public PageBean() {
super();
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public int getRows() {
return rows;
}
public void setRows(int rows) {
this.rows = 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 pagination;
}
public void setPagination(boolean pagination) {
this.pagination = pagination;
}
/**
* 获得起始记录的下标
*
* @return
*/
public int getStartIndex() {
return (this.page - 1) * this.rows;
}
@Override
public String toString() {
return "PageBean [page=" + page + ", rows=" + rows + ", total=" + total + ", pagination=" + pagination + "]";
}
}
4.处理分页结果
使用方法
<select id="listPager" resultType="java.util.Map" parameterType="java.util.Map">
select * from t_mvc_book where bname like concat(concat('%',#{bname}),'%')
</select>
Mapper层
List<Map> listPager(Map map);
Service层
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;
}
测试类
@Text
public void listPager(){
Map map = new HashMap();
map.put("banme","圣墟");
//查询出第二页的20条数据
PageBean pageBean = new PageBean();
pageBean.setPage(2);
pageBean.setRows(20);
bookBiz.listPager(map,pageBean).forEach(System.out::printIn)
}
因为我们数据库有大于号和小于号,所有我们需要避免这种编译错误,就需要解决特殊字符的处理
配置
<select id="list6" resultType="com.xlb.model.Book" parameterType="com.xlb.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>
</where>
</select>
<select id="list7" resultType="com.xlb.model.Book" parameterType="com.xlb.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>
方法
/**
* 处理特殊字符
* @param bookVo
* @return
*/
List<Book> list6(BookVo bookVo);
/**
* 处理特殊字符
* @param bookVo
* @return
*/
List<Book> list7(BookVo bookVo);
接口
/**
* 处理特殊字符
* @param bookVo
* @return
*/
List<Book> list6(BookVo bookVo);
/**
* 处理特殊字符
* @param bookVo
* @return
*/
List<Book> list7(BookVo bookVo);
实现
@Override
public List<Book> list6(BookVo bookVo) {
return bookMapper.list6(bookVo);
}
@Override
public List<Book> list7(BookVo bookVo) {
return list7(bookVo);
}
BookVo
package com.xlb.model;
import java.util.List;
/**
* @author 波哥
* @QQ 2212371722
* @company 波哥集团
* @create 2022-08-12 11:26
*/
public class BookVo {
private List booKIds;
private int min;
private int max;
public int getMax() {
return max;
}
public void setMax(int max) {
this.max = max;
}
public int getMin() {
return min;
}
public void setMin(int min) {
this.min = min;
}
public List getBooKIds() {
return booKIds;
}
public void setBooKIds(List booKIds) {
this.booKIds = booKIds;
}
}
测试类
@Test
public void list6() {
BookVo vo = new BookVo();
vo.setMax(45);
vo.setMin(35);
bookBiz.list6(vo).forEach(System.out::println);
}
@Test
public void list7() {
BookVo vo = new BookVo();
vo.setMax(45);
vo.setMin(35);
bookBiz.list7(vo).forEach(System.out::println);
}