如果可能,不要用电脑管家
因为很多配置文件将会被当作病毒给删除掉,还要来回的搞这些文件,心好累
文章目录
MyBatis 动态SQL
- 根据不同的条件拼接 SQL 语句是一件极其痛苦的工作,但是动态 SQL 很灵活哦
- MyBatis 的动态 SQL 如下所示:
元素 | 作用 | 使用场景 |
---|---|---|
if | 判断语句 | 单条件分支判断 |
choose(when、otherwise) | 相当于 Java 中的 switch case 语句 | |
trim、where | 辅助元素 | 用于处理一些SQL拼装问题 |
foreach | 循环语句 | 在in语句等列举条件常用 |
bind | 辅助元素 | 拼接参数 |
MyBatis if标签:条件判断
-
MyBatis if 类似于 Java 中的 if 语句,是 MyBatis 中最常用的判断语句
-
if 语句使用方法,常和test属性联合使用
<if test="判断条件"> // 当条件为true时,执行SQL语句 SQL语句 </if> // if 语句中包含 where 子句 <select id="selectAllWebsite" resultMap="myResult"> select id,name,url from website <if test="name != null"> where name like #{ name} </if> </select>
MyBatis choose、when和otherwise标签
-
MyBatis 中动态语句 choose-when-otherwise 类似于 Java 中的 switch-case-default 语句
-
MyBatis 并没有为 if 提供对应的 else 标签,如果想要达到
<if>...<else>...</else> </if> 的效果,可以借助 <choose>、<when>、<otherwise>
来实现。 -
动态语句 choose-when-otherwise 语法
<choose> <when test="判断条件1"> SQL语句1 </when > <when test="判断条件2"> SQL语句2 </when > <when test="判断条件3"> SQL语句3 </when > <otherwise> SQL语句4 </otherwise> </choose>
-
choose 标签按顺序判断其内部 when 标签中的判断条件是否成立,如果有一个成立,则执行相应的 SQL 语句,choose 执行结束;如果都不成立,则执行 otherwise 中的 SQL 语句。
-
WebsiteMapper.xml代码如下
<mapper namespace="net.biancheng.mapper.WebsiteMapper">
<select id="selectWebsite"
parameterType="net.biancheng.po.Website"
resultType="net.biancheng.po.Website">
SELECT id,name,url,age,country
FROM website WHERE 1=1
<choose>
<when test="name != null and name !=''">
AND name LIKE CONCAT('%',#{
name},'%')
</when>
<when test="url != null and url !=''">
AND url LIKE CONCAT('%',#{
url},'%')
</when>
<otherwise>
AND age is not null
</otherwise>
</choose>
</select>
</mapper>
- WebsiteMapper 类中方法
public List<Website> selectWebsite(Website website);
- 测试类代码
public class Test {
public static void main(String[] args) throws IOException {
// 读取配置文件mybatis-config.xml
InputStream config = Resources.getResourceAsStream("mybatis-config.xml"); // 根据配置文件构建
SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(config);
// 通过SqlSessionFactory创建SqlSession
SqlSession ss = ssf.openSession();
Website site = new Website();
site.setname("编程");
List<Website> siteList = ss.selectList("net.biancheng.mapper.WebsiteMapper.selectWebsite", site);
for (Website ws : siteList) {
System.out.println(ws);
}
}
}
MyBatis where标签
-
where 标签主要用来简化 SQL 语句中的条件判断,可以自动处理 AND/OR 条件,语法如下
<where> <if test="判断条件"> AND/OR ... </if> </where>
-
if 语句中判断条件为 true 时,where 关键字才会加入到组装的 SQL 里面,否则就不加入
-
where 会检索语句,它会将 where 后的第一个 SQL 条件语句的 AND 或者 OR 关键词去掉。
MyBatis where标签样例
- WebsiteMapper.xml
<select id="selectWebsite" resultType="net.biancheng.po.Website">
select id,name,url from website
<where>
<if test="name != null">
AND name like #{
name}
</if>
<if test="url!= null">
AND url like #{
url}
</if>
</where>
</select>
- WebsiteMapper 类
public List<Website> selectWebsite(Website website);
- 测试类代码
public class Test {
public static void main(String[] args) throws IOException {
// 读取配置文件mybatis-config.xml
InputStream config = Resources.getResourceAsStream("mybatis-config.xml"); // 根据配置文件构建
SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(config);
// 通过SqlSessionFactory创建SqlSession
SqlSession ss = ssf.openSession();
Website site = new Website();
site.setname("编程");
List<Website> siteList = ss.selectList("net.biancheng.mapper.WebsiteMapper.selectWebsite", site);
for (Website ws : siteList) {
System.out.println(ws);
}
}
}
MyBatis set标签
- 在 Mybatis 中,update 语句可以使用 set 标签动态更新列。
- set 标签可以为 SQL 语句动态的添加 set 关键字,剔除追加到条件末尾多余的逗号。
- WebsiteMapper.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="net.biancheng.mapper.WebsiteMapper">
<select id="selectWebsite" resultType="net.biancheng.po.Website">
SELECT * FROM website
<where>
<if test="id!=null and id!=''">
id=#{
id}
</if>
</where>
</select>
<!--使用set元素动态修改一个网站记录 -->
<update id="updateWebsite"
parameterType="net.biancheng.po.Website">
UPDATE website
<set>
<if test="name!=null">name=#{
name}</if>
<if test="url!=null">url=#{
url}</if>
</set>
WHERE id=#{
id}
</update>
</mapper>
- WebsiteMapper 类
package net.biancheng.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import net.biancheng.po.Website;
public interface WebsiteMapper {
public List<Website> selectWebsite(Website site);
public int updateWebsite(Website site);
}
- 测试类代码
package net.biancheng.test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import net.biancheng.mapper.WebsiteMapper;
import net.biancheng.po.Website;
public class Test {
public static void main(String[] args) throws IOException {
InputStream config = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(config);
SqlSession ss = ssf.openSession();
Website site = new Website();
site.setId(1);
site.setUrl("www.biancheng.net");
// 执行update语句前
List<Website> siteList = ss.getMapper(WebsiteMapper.class).selectWebsite(site);
for (Website st : siteList) {
System.out.println(st);
}
int num = ss.getMapper(WebsiteMapper.class).updateWebsite(site);
System.out.println("影响数据库行数" + num);
// 执行update语句后
List<Website> siteList2 = ss.getMapper(WebsiteMapper.class).selectWebsite(site);
for (Website st : siteList2) {
System.out.println(st);
}
ss.commit();
ss.close();
}
}