假如在where条件中使用if标签的SQL语句中,where 1=1这个条件是不希望存在的,此类问题就可以用trim、where、set三个标签来解决。
where标签和set标签都是trim标签的一种类型,下面首先介绍where标签和set标签。
1. where如果where标签包含的元素有返回值,就插入一个where语句;
如果where标签后面的字符串是以and和or开头的,就将它们剔除。
(1)查询条件:根据输入的学生信息进行条件检索。
① 当只输入用户名时,使用用户名进行模糊检索。
② 当只输入邮箱时,使用邮箱进行完全匹配。
③ 当用户名和邮箱都存在时,使用这两个条件进行匹配查询。
提示:不使用where 1=1。
(2)动态SQL。
① 当条件都不满足时,SQL中不能有where,否则会导致出错。
② 当if语句中有条件满足时,SQL中需要有where,且第一个成立的if标签下的and或or等要去掉。
项目情况截图


数据库,建表
create table z_student(
id int not null auto_increment primary key,
name varchar(20) not null,
phone varchar(20) null,
email varchar(50) null,
sex tinyint(4) null comment '0女,1男',
locked tinyint null comment '状态,0正常,1锁定',
gmt_created datetime default CURRENT_TIMESTAMP comment '存入库的时间',
gmt_modified datetime default CURRENT_TIMESTAMP comment '修改时间',
deletes int(11) null
) comment '学生表'
insert into z_student values(null,'tom','130','xx@xx.com',1,0,null,null,0);
insert into z_student values(null,'tom2','130','xx@xx.com',1,0,null,null,0);
insert into z_student values(null,'tom3','130','xx@xx.com',1,0,null,null,0);
insert into z_student values(null,'tom4','130','xx@xx.com',1,0,null,null,0);
Student.java
package com.shrimpking.code05;
import java.util.Date;
/**
* @author user1
*/
public class Student
{
private int id;
private String name;
private String phone;
private String email;
private int sex;
private int locked;
private Date gmtCreated;
private Date gmtModified;
private int deletes;
public Student()
{
}
public Student(int id, String name, String phone, String email, int sex, int locked, Date gmtCreated, Date gmtModified, int deletes)
{
this.id = id;
this.name = name;
this.phone = phone;
this.email = email;
this.sex = sex;
this.locked = locked;
this.gmtCreated = gmtCreated;
this.gmtModified = gmtModified;
this.deletes = deletes;
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getPhone()
{
return phone;
}
public void setPhone(String phone)
{
this.phone = phone;
}
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
public int getSex()
{
return sex;
}
public void setSex(int sex)
{
this.sex = sex;
}
public int getLocked()
{
return locked;
}
public void setLocked(int locked)
{
this.locked = locked;
}
public Date getGmtCreated()
{
return gmtCreated;
}
public void setGmtCreated(Date gmtCreated)
{
this.gmtCreated = gmtCreated;
}
public Date getGmtModified()
{
return gmtModified;
}
public void setGmtModified(Date gmtModified)
{
this.gmtModified = gmtModified;
}
public int getDeletes()
{
return deletes;
}
public void setDeletes(int deletes)
{
this.deletes = deletes;
}
@Override
public String toString()
{
return "Student{" + "id=" + id + ", name='" + name + '\'' + ", phone='" + phone + '\'' + ", email='" + email + '\'' + ", sex=" + sex + ", locked=" + locked + ", gmtCreated=" + gmtCreated + ", gmtModified=" + gmtModified + ", deletes=" + deletes + '}';
}
}
StudentMapper.java
package com.shrimpking.code05;
import java.util.List;
public interface StudentMapper
{
public List<Student> selectByWhereTag(Student student);
}
StudentMapper.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.shrimpking.code05.StudentMapper">
<sql id="base_sql">
id,name,phone,email,sex,locked,gmt_created,gmt_modified
</sql>
<select id="selectByWhereTag" parameterType="Student" resultType="Student">
select
<include refid="base_sql"/>
from z_student
<where>
<if test="name != null and name != ''">
and name like concat('%',#{name},'%')
</if>
<if test="email != null and name != '' ">
and email = #{email}
</if>
</where>
</select>
</mapper>
mybatis.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 起别名 -->
<typeAliases>
<package name="com.shrimpking.code05"/>
</typeAliases>
<!-- 环境 -->
<environments default="development">
<environment id="development">
<!-- 默认事务管理 -->
<transactionManager type="JDBC"/>
<!-- 默认的数据库连接 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimeZone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="mysql123"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/shrimpking/code05/StudentMapper.xml"/>
</mappers>
</configuration>
StudentTest.java
package com.shrimpking.code05;
import com.shrimpking.utils.DaoUtils;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.io.IOException;
import java.util.List;
public class StudentTest
{
@Test
public void test() throws IOException
{
SqlSession sqlSession = DaoUtils.getSqlSession("code05/mybatis.xml");
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
Student stu = new Student();
//
stu.setEmail("zz@zz.com");
System.out.println("只有邮箱查询");
List<Student> students = mapper.selectByWhereTag(stu);
for (Student student : students)
{
System.out.println(student);
}
//
stu.setName("虾米");
stu.setEmail(null);
System.out.println("只有用户名查询");
List<Student> students2 = mapper.selectByWhereTag(stu);
for (Student student : students2)
{
System.out.println(student);
}
//
stu.setName(null);
stu.setEmail(null);
System.out.println("没有任何条件查询");
List<Student> students3 = mapper.selectByWhereTag(stu);
for (Student student : students3)
{
System.out.println(student);
}
sqlSession.close();
}
}
DaoUtils.java
package com.shrimpking.utils;
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 java.io.IOException;
import java.io.Reader;
/**
* @author user1
*/
public class DaoUtils
{
private static Reader reader;
private static SqlSessionFactory sqlSessionFactory;
public static SqlSession getSqlSession(String config) throws IOException
{
try
{
reader = Resources.getResourceAsReader(config);
//
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
reader.close();
}
return sqlSessionFactory.openSession();
}
}
运行截图


1万+

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



