mybatis的动态SQL
1. MyBatis 的动态 SQL 元素与 JSTL 或 XML 文本处理器相似,常用1. <if>、
2. <choose>、<when>、<otherwise>、(if-else)
3. <trim>、<where>、<set>、
4. <foreach>
重点掌握标签:<if>/<where>/<foreach>/<trim>
1.if标签:
作用:可以根据自己传递的条件进行多条件查询
属性:
test:判断的条件,一般判断该值是否不为空
案例:
<select id="findStu" resultMap="studentMap" parameterType="student">
SELECT * FROM student WHERE 1=1
<if test="sex != null">
and sex = #{sex}
</if>
<if test="name != null">
and name=#{name}
</if>
/*模糊查询方式一*/
<if test="stunum != null">
and stunum like #{stunum}
</if>
/*模糊查询方式二*/
<if test="stunum != null">
and stunum like concat("%",#{stunum},"%")
</if>
/*模糊查询方式三*/
<if test="stunum != null">
and stunum like "%"#{stunum}"%"
</if>
</select>
2.where标签:
作用:替换where条件的恒等式 1=1
用法:
将if标签写在where标签中即可
案例:
<select id="findStu" resultMap="studentMap" parameterType="student">
SELECT * FROM student
<where>
<if test="sex != null">
and sex = #{sex}
</if>
<if test="name != null">
and name=#{name}
</if>
<if test="stunum != null">
and stunum like "%"#{stunum}"%"
</if>
</where>
</select>
#SQL注入的万能密码
' or '1'='1
3. foreach:
概念:
<foreach> 元素主要用在构建 in 条件中,它可以在 SQL 语句中迭代一个集合。<foreach> 元素的属性主要有collection、item、index、open、separator、close。
collection:必填,值为所选迭代循环的属性名,值为mybatis内部定义的名字:list\array\key
item 表示集合中每一个元素进行迭代时的别名。
index 指定一个名字,用于表示在迭代过程中每次迭代到的位置。
open 表示该语句以什么开始。
separator 表示在每次进行迭代之间以什么符号作为分隔符。
close 表示以什么结束。
在使用 <foreach> 元素时,最关键、最容易出错的是 collection 属性,该属性是必选的,
但在不同情况下该属性的值是不一样的,主要有以下 3 种情况:
如果传入的是单参数且参数类型是一个 List,collection 属性值为 list。
如果传入的是单参数且参数类型是一个 array 数组,collection 的属性值为 array。
如果传入的参数是多个,需要把它们封装成一个 Map,当然单参数也可以封装成 Map。Map 的 key 是参数名,
collection 属性值是传入的 List 或 array 对象在自己封装的 Map 中的 key。
批量查询:
mapper接口:
List<Product> findProductByListId(List<String> pid);
mapper.xml配置文件:
<!-- 查询产品中编号为(2,9,25,100)商品信息 -->
<select id="findProductByListId" resultType="product">
SELECT * FROM product
<where>
pid in
<foreach collection="list" item="pid"
open="(" separator="," close=")">
#{pid}
</foreach>
</where>
</select>
测试实现类:
@Test
public void findProductByListIdTest() {
ProductMapper proMapper=session.getMapper(ProductMapper.class);
List<String> listPid=new ArrayList<String>();
listPid.add("2");
listPid.add("9");
listPid.add("25");
listPid.add("100");
List<Product> listPro=proMapper.findProductByListId(listPid);
System.out.println(listPro);
}
批量添加:
需求:向数据库中同时添加多条数据
mapper接口:
/*批量插入数据*/
int addProductForeach(List<Product> proList);
mapper.xml配置文件
<!-- 批量添加数据 -->
<insert id="addProductForeach" parameterType="List">
insert into product VALUES
<foreach collection="list" item="pro" separator=",">
(
#{pro.pid},
#{pro.pname},
#{pro.marketPrice},
#{pro.shopPrice},
#{pro.pimage},
#{pro.pdate},
#{pro.isHot},
#{pro.pdesc},
#{pro.pflag},
#{pro.cid}
)
</foreach>
</insert>
测试实现类:
@Test
public void addProductForeachTest() {
ProductMapper proMapper=session.getMapper(ProductMapper.class);
List<Product> proList=new ArrayList<Product>();
for (int i = 55; i < 60; i++) {
Product product = new Product();
product.setPid(String.valueOf(i));
product.setPname("Sony/索尼 ILCE-A6000L");
product.setMarketPrice(4999.00);
product.setShopPrice(4599.00);
product.setPimage("products/img/00013.jpg");
product.setPdate(new Date());
product.setIsHot(1);
product.setPdesc("Sony/索尼 ILCE-A6000L(E16-50/55-210)微单反长焦旅游双镜头相机\r\n" +
"随拍双镜头套机 全焦段覆盖 赠送微单大礼包");
product.setPflag(1);
product.setCid("2");
proList.add(product);
}
int i = proMapper.addProductForeach(proList);
System.out.println(i);
session.commit();
session.close();
}
批量修改:
/*
* 批量更新数据
*/
int updateProductForeach(List<Product> proList);
<!-- 批量更新数据 collection="list" item="pro" item可以看做是list集合的对象 -->
<update id="updateProductForeach" parameterType="List">
<foreach collection="list" item="pro" separator=";">
UPDATE product
<set>
pname = #{pro.pname},
market_price = #{pro.marketPrice},
shop_price = #{pro.shopPrice},
pimage = #{pro.pimage},
pdate = #{pro.pdate},
is_hot = #{pro.isHot},
pdesc = #{pro.pdesc},
pflag = #{pro.pflag},
cid = #{pro.cid}
</set>
<where>
pid = #{pro.pid}
</where>
</foreach>
</update>
测试类:
/*
* 批量更新
*/
@Test
public void updateProductForeachTest() {
ProductMapper proMapper=session.getMapper(ProductMapper.class);
List<Product> proList=new ArrayList<Product>();
for (int i = 55; i < 60; i++) {
Product product = new Product();
product.setPid(String.valueOf(i));
product.setPname("Sony/索尼 ILCE-A6000 Plus");
product.setMarketPrice(5999.00);
product.setShopPrice(5599.00);
product.setPimage("products/img/00013.jpg");
product.setPdate(new Date());
product.setIsHot(1);
product.setPdesc("Sony/索尼 ILCE-A6000L(E16-50/55-210)微单反长焦旅游双镜头相机\r\n" +
"随拍双镜头套机 全焦段覆盖 赠送微单大礼包");
product.setPflag(1);
product.setCid("2");
proList.add(product);
}
int i = proMapper.updateProductForeach(proList);
System.out.println(i);
session.commit();
session.close();
}
批量删除:
需求:对选中的多条数据进行一次性删除
QQ空间的照片管理:选中多个复选框进行删除
数据库分析:
#删除一条
DELETE FROM `user` WHERE id=126;
#删除多条
DELETE FROM `user` WHERE id=126;
DELETE FROM `user` WHERE id=127;
DELETE FROM `user` WHERE id=128;
DELETE FROM `user` WHERE id in(126,127,128);
mybatis实现:
<!-- 批量删除 -->
<delete id="deleteProductForeach">
DELETE FROM product
<where>
pid in
<foreach collection="list" item="pid" open="(" separator="," close=")">
#{pid}
</foreach>
</where>
</delete>
<!-- 批量删除2 -->
<delete id="deleteProductForeach2">
<foreach collection="list" item="pid" separator=";">
DELETE FROM product
<where>
pid=#{pid}
</where>
</foreach>
</delete>
3555

被折叠的 条评论
为什么被折叠?



