十、 持久层框架(MyBatis)

时间:2023-03-09 07:10:19
十、 持久层框架(MyBatis)

一、基于MyBatis动态SQL语句

1、if标签

实体类Product的字段比较多的时候,为了应付各个字段的查询,那么就需要写多条SQL语句,这样就变得难以维护。

此时,就可以使用MyBatis动态SQL里的if标签

<select id="listProduct" resultType="Product">
select * from product_table
<if test="name!=null">
where name like concat('%',#{name},'%')
</if>
</select>

这种写法,如果没有传递参数name,那么就查询所有,如果有name就进行模糊查询。

这样就定义了一条SQL语句,应付了多种情况。

TestMyBatis测试如下:

package com.demo;
import java.io.IOException;
import java.io.InputSteam;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import org.apache.ibatis.io.Resouces;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder; import com.demo.pojo.Product; public class TestMyBatis{
public static void main(String[] args) throws IOException{
String resouce="mybatis-config.xml";
InputSteam inputStream=Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory =new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session=sqlSessionFactory.openSession(); System.out.println("查询所有的数据");
List<Product> list1=session.selectList("listProduct");
for(Product p:list1){
System.out.println(p);
} System.out.println("模糊查询");
Map<String,Object> params=new HashMap<>();
params.put("name","a");
List<Product> list2=session.selectList("listProduct",params);
for(Product p:list2){
System.out.println(p);
} session.commit();
session.close(); }
}

2、where标签

where标签与if标签对应,如果要进行多个条件判断,就会写成这样

<select id="listProduct" resultType="Product">
select * from product_table
<if test="name!=null">
where name like concat('%',#{name},'%')
</if>
<if test="price!=0">
and price>#{price}
</if>
</select>

这种写法,会出现问题,当没有name参数,却有price参数的时候,执行的sql语句是:

select * from product_table and price>10

执行会报错的。

解决这种问题时,就要使用where标签

<select id="listProduct" resultType="Product">
select * from product_table
<where>
<if test="name!=null">
where name like concat('%',#{name},'%')
</if>
<if test="price!=0">
and price>#{price}
</if>
<where>
</select>

<where>标签会进行自动判断,如果任何条件都不成立,那么sql语句中就不会出现where关键字,如果有任何条件成立,会自动去掉多出来的and,or

3、set标签

与where标签类似,在update语句中碰到多个字段的问题。这种情况下,就会使用set标签

<update id="updateProduct" parameterType="Product">
update product_table
<set>
<if test="name!=null">name=#{name},</if>
<if test="price!=null">price=#{price}</if>
</set>
where id=#{id}
</update>

4、trim标签

用来制定想要的功能,

trim标签替换where标签如下:

<trim prefix="WHERE" prefixOverrides="AND|OR"></trim>

trim标签替换set标签如下:

<trim prefix="SET" suffixOverrides=","></trim>

使用原生的标签配置Product.xml如下:

<?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.demo.pojo">
<select id="listProduct" resultType="Product">
select * from product_table
<where>
<if test="name!=null">
and name like concat('%',#{name},'%')
</if>
<if test="price!=null and price!=0">
and price > #{price}
</if>
</where>
</select> <update id="updateProduct" parameterType="Product" >
update product_table
<set>
<if test="name != null">name=#{name},</if>
<if test="price != null">price=#{price}</if>
</set>
where id=#{id}
</update>
</mapper>

使用trim标签配置的Product.xml

<?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.demo.pojo">
<select id="listProduct" resultType="Product">
select * from product_table
<trim prefix="WHERE" prefixOverrides="AND|OR">
<if test="name!=null">
and name like concat('%',#{name},'%')
</if>
<if test="price!=null and price!=0">
and price > #{price}
</if>
</trim>
</select> <update id="updateProduct" parameterType="Product" >
update product_
<trim prefix="SET" suffixOverrides=",">
<if test="name != null">name=#{name},</if>
<if test="price != null">price=#{price}</if>
</trim>
where id=#{id}
</update>
</mapper>

5、choose标签

if-else时候的情况,因为MyBatis中没有else标签,可以使用when otherwise标签,但是要在choose标签包裹里

<select id="listProduct" resultType="Product">
select * from product_table
<where>
<choose>
<when test="name!=null">
and name like concat('%',#{name},'%')
</when>
<when test="price!=null and price!=0">
and price > #{price}
</when>
<otherwise>
and id>#{id}
</otherwise>
</choose>
</where>
</select>

6、foreach标签

用于sql中使用in的语法

例如sql:

--名字要么是apple要么是banana
select * from product_table where name in("apple,banana")
--薪水要么等于3000要么等于6000
select * from employees where salary in(3000,6000)

foreach标签使用

<select id="listProduct" resultType="Product">
select * from product_table
where id in
<foreach item="item" index="index" collection="list" open="(" separator="," close=")">#{item}</foreach>
</select>

7、bind标签

相当于做一次字符串拼接,方便后续使用

    <select id="listProduct" resultType="Product">
<bind name="likename" value="'%'+name+'%'" />
select * from product_table where name like #{likename}
</select>

本来的模糊查询是这样:

<select id="listProduct" resultType="Product">
select * from product_table where name like concat('%',#{0},'%')
</select>