目录
1.单个参数:------------>八大基础类型与String类型
1.传递String的参数,必须使用@Param方式,并且指明参数名
2.在service接口,方法应该要将@param()去除掉,只留下变量名,在mapper层可以留下@param(),以防万一,防止报错
注: (1)mybatis中使用OGNL表达式传递参数 (2) 优先使用#{...} (3) ${...}方式存在SQL注入风险
案例③:使用resultType返回Map,适用于多表查询返回结果集,object>
为什么要重写mybatis的分页? Mybatis的分页功能很弱,它是基于内存的分页(查出所有记录再按偏移量offset和边界limit取结果),在大数据量的情况下这样的分页基本上是没有用的
5.在BookMapper.xml文件中进行sql语句的编写
8.在你需要进行分页的Mybatis方法前调用PageHelper.startPage静态方法即可,紧跟在这个方法后的第一个Mybatis查询方法会被进行分页
allowMultiQueries=true 允许一条SQL语句包含多个执行SQL以分号;分隔,是必须添加的
一:动态sql的基本概念
动态sql是指在进行sql操作的时候,传入的参数对象或者参数值,根据匹配的条件,有可能需要动态的去判断是否为空,循环,拼接等情况;
二:基本的动态sql标签
if标签
①:在BookMapper类定义包含if标签方法
//if标签
List<Book> queryBookByIf(Book book);
②:在BookMapper.xml文件中进行sql语句的编写
<select id="queryBookByIf" resultType="com.zking.model.Book">
select
<include refid="Base_Column_List"></include>
from t_book where 1=1
<if test="null!=btype and ''!=btype">
and btype= #{btype}
</if>
</select>
当满足test条件时,才会将if
标签内的sql语句拼接上去
③:在iBookService类编写代码
//mybatis之if标签
List<Book> queryBookByIf(Book book);
④:进行 junit4单元测试
------测试带type类型的查询:
@Test
//mybatis之if标签
public void queryBookByIf(){
List<Book> bookList = iBookService.queryBookByIf(Book.builder().btype("历史").build());
bookList.forEach(System.out::println);
}
测试结果如下:
查看sql语句:
---测试不type类型的查询:
@Test
//mybatis之if标签
public void queryBookByIf(){
List<Book> bookList = iBookService.queryBookByIf(Book.builder().build());
bookList.forEach(System.out::println);
}
测试结果如下:
查看sql语句:
trim标签
trim标签属性:
prefix | 前缀 |
suffix | 后缀 |
suffixOverride | 去除后缀指定的字符 |
prefixOverrides | 去除前缀指定的字符 |
①:在BookMapper类定义包含trim标签方法
int insertSelective(Book record);
②:在BookMapper.xml文件中进行sql语句的编写
<insert id="insertSelective" parameterType="com.zking.model.Book" >
insert into t_book
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="bid != null" >
bid,
</if>
<if test="bname != null" >
bname,
</if>
<if test="bprice != null" >
bprice,
</if>
<if test="btype != null" >
btype,
</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="bprice != null" >
#{bprice,jdbcType=INTEGER},
</if>
<if test="btype != null" >
#{btype,jdbcType=VARCHAR},
</if>
</trim>
</insert>
- foreach标签
foreach标签属性:
collection | 被遍历的集合或数组 |
item | 每次循环遍历的集合名 |
separator | 每次循环的分隔符 |
index | 下标 |
open | 开始位置 |
close | 关闭位置 |
①:编写一个类,储存需要遍历的集合或者数组
package com.zking.vo;
import com.zking.model.Book;
import lombok.Data;
import java.util.List;
/**
* @author 唐渊
* @create 2022-07-23 9:26
*/
@Data //getter/setter/toString
public class BookVo extends Book {
//foreach标签需要遍历的list集合
private List<Integer> ids;
}
②:在BookMapper类定义包含foreach标签方法
//foreach标签
List<Book> queryBookByForeach(BookVo bookVo);
③:在BookMapper.xml文件中进行sql语句的编写
(1)方式一:
<select id="queryBookByForeach" resultType="com.zking.model.Book">
select
<include refid="Base_Column_List"></include>
from t_book where 1=1 and bid in
<foreach collection="ids" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</select>
(2)方式二:
select
<include refid="Base_Column_List"></include>
from t_book where 1=1
<foreach collection="ids" item="id" separator="," open=" and bid in (" close=")">
#{id}
</foreach>
(3)方式三:
select
<include refid="Base_Column_List"></include>
from t_book where 1=1 and bid in
(
<foreach collection="ids" item="id" separator=",">
#{id}
</foreach>
④:在iBookService类编写代码
//foreach标签
List<Book> queryBookByForeach(BookVo bookVo);
⑤:进行 junit4单元测试
@Test
//foreach标签
public void queryBookByForeach(){
BookVo bookVo=new BookVo();
//Arrays.asList--->将数组转换为集合
bookVo.setIds(Arrays.asList(new Integer[]{13,17,3}));
List<Book> books = iBookService.queryBookByForeach(bookVo);
books.forEach(System.out::println);
}
测试结果如下:
查看sql语句:
choose标签
choose 标签作用是通过条件判断来拼接 SQL 语句,类似于 Java 中的 switch 语句,从上到下,当有匹配的条件时,跳出 choose 语句;如果所有条件都不成立则执行 otherwise 标签中的内容,使用 choose 标签的时候,一定要注意最先需要判断的条件要放在前面,当匹配到某个条件时,就不会判断后面的语句了
语法格式:
<choose>
<when test=条件1>
...
</when>
<when test=条件2>
...
</when>
<when test=条件3>
...
</when>
...
<otherwise>
...
</otherwise>
</choose>
①:在BookMapper类定义包含choose标签方法
//choose 标签
List<Book> queryBookByChoose(Book book);
②:在BookMapper.xml文件中进行sql语句的编写
<select id="queryBookByChoose" resultType="com.zking.model.Book">
select <include refid="Base_Column_List"/> from t_book where 1=1
<choose>
<when test="null!=bname and ''!=bname">
and bname like concat('%',#{bname},'%')
</when>
<when test="null!=btype and ''!=btype">
and btype like concat('%',#{btype},'%')
</when>
<otherwise>
and bid=#{bid}
</otherwise>
</choose>
</select>
③:在iBookService类编写代码
List<Book> queryBookByChoose(Book book);
④:实现接口所定义的方法
@Override
public List<Book> queryBookByChoose(Book book) {
return bookMapper.queryBookByChoose(book);
}
⑤:进行 junit4单元测试
------测试带name类型的查询:
@Test
//choose 标签
public void queryBookByChoose(){
List<Book> books = iBookService.queryBookByChoose(Book.builder().bname("之").build());
books.forEach(System.out::println);
}
测试结果如下:
查看sql语句:
------测试带type类型的查询:
@Test
//choose 标签
public void queryBookByChoose(){
List<Book> books = iBookService.queryBookByChoose(Book.builder().btype("历史").build());
books.forEach(System.out::println);
}
测试结果如下:
查看sql语句:
------测试带id类型的查询:
@Test
//choose 标签
public void queryBookByChoose(){
List<Book> books = iBookService.queryBookByChoose(Book.builder().bid(30).build());
books.forEach(System.out::println);
}
测试结果如下:
查看sql语句:
set标签
set用于更新的sql语句中,在update时,多条件更新,每个属性后面要加逗号“,”,这个时候可能会出现多一个“,”的情况,此时我们就可以使用set去掉后边的“,”修改方法,假如实体类属性为空就不修改此属性所对于的字段
①:在BookMapper类定义包含set标签方法
//set标签
int updateByPrimaryKeySelective(Book record);
②:在BookMapper.xml文件中进行sql语句的编写
<update id="updateByPrimaryKeySelective" parameterType="com.zking.model.Book" >
update t_book
<set >
<if test="bname != null" >
bname = #{bname,jdbcType=VARCHAR},
</if>
<if test="bprice != null" >
bprice = #{bprice,jdbcType=INTEGER},
</if>
<if test="btype != null" >
btype = #{btype,jdbcType=VARCHAR},
</if>
</set>
where bid = #{bid,jdbcType=INTEGER}
</update>
③:在iBookService类编写代码
int updateByPrimaryKeySelective(Book record);
④:实现接口所定义的方法
@Override
public int updateByPrimaryKeySelective(Book record) {
return bookMapper.updateByPrimaryKeySelective(record
);
}
⑤:进行 junit4单元测试
public void updateByPrimaryKeySelective(){
book.setBid(27);
book.setBname("剑与荣光");
book.setBprice(45);
book.setBtype("文学");
iBookService.updateByPrimaryKeySelective(book);
}
测试结果如下:
查看sql语句:
where标签
where
标签只会在至少有一个子元素返回了SQL语句时, 才会向SQL语句中添加WHERE,并且如果WHERE之后是以AND或OR开头,会自动将其删掉。
①:在BookMapper类定义包含where标签方法
//where标签
List<Book> queryBookWhere(Book book);
②:在BookMapper.xml文件中进行sql语句的编写
<select id="queryBookWhere" resultType="com.zking.model.Book">
select <include refid="Base_Column_List"/> from t_book
<where>
<if test="bname!=null and bname!=''">
and bname like concat('%',#{bname},'%')
</if>
<if test="btype!=null and btype!=''">
and btype like concat('%',#{btype},'%')
</if>
</where>
</select>
③:在iBookService类编写代码
//where标签
List<Book> queryBookWhere(Book book);
④:实现接口所定义的方法
@Override
public List<Book> queryBookWhere(Book book) {
return bookMapper.queryBookWhere(book);
}
⑤:进行 junit4单元测试
------测试带name类型的查询:
@Test
//where标签
public void queryBookWhere(){
List<Book> books = iBookService.queryBookWhere(Book.builder().bname("的").build());
books.forEach(System.out::println);
}
测试结果如下:
查看sql语句:
------测试带type类型的查询:
@Test
//where标签
public void queryBookWhere(){
List<Book> books = iBookService.queryBookWhere(Book.builder().btype("文学").build());
books.forEach(System.out::println);
}
测试结果如下:
查看sql语句: