在编译阶段,就可以确定的SQL语句称为静态SQL;在程序运行阶段,根据条件不同才能确定下来的SQL叫动态SQL。如:学生表数据查询时,查询条件有年龄、姓名、专业、班级等,SQL语句条件根据当前的使用场景可能是这些条件的任意组合。Mybaits提供了一种基于OGNL(对象导航图语言)的表达式来完成动态SQL,极大方便了动态sql编程。
掌握基于OGNL表达式实现动态SQL的使用
以查询学生信息为例,学习if、choose(when、otherwise)、where、trim、set、forEach、bind等元素的使用。
字段名称 | 字段代码 | 数据类型 | 备注 |
学号 | sno | integer | 主键自增 |
学生姓名 | student_name | varchar(50) | |
年龄 | student_age | integer |
@Data
public class Student {
private Integer sno;
private String studentName;
private Integer studentAge;
}
public interface StudentDao {
List<StudentEntity> getStudentByCondition(Map<String,Object> param);
List<StudentEntity> getStudentByCondition2(Map<String,Object> param);
int UpdateStudentById(StudentEntity student);
int insertStudent(StudentEntity student);
int insertBatch(@Param("students") List<StudentEntity> students);
List<StudentEntity> getStudentByBind(StudentEntity student);
}
<select id="getStudentByCondition" resultType="com.bjwl.pojo7.StudentEntity">
select * from tb_student where 11=1
<if test="name !=null and name !=''">
and student_name like concat('%',#{name},'%')
if>
<if test="age !=null and age !=''">
and student_age =#{age}
if>
select>
<select id="getStudentByCondition2" resultMap="com.bjwl.pojo7.StudentEntity">
select * from tb_student where 11=1
<choose>
<when test="name !=null and name !=''">
and student_name like concat('%',#{name},'%')
when>
<when test="age !=null and age !=''">
and student_age =#{age}
when>
<otherwise>
and student_name = '王小丫'
otherwise>
choose>
select>
<select id="getStudentByCondition3" resultType="com.bjwl.pojo7.StudentEntity">
select * from tb_student
<where>
<if test="sname !=null and sname !=''">
and student_name like concat('%',#{sname},'%')
if>
<if test="age !=null and age !=''">
and student_age =#{age}
if>
where>
select>
<insert id="insertStudent">
insert into tb_student
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="sname != null">
student_name,
if>
<if test="sage != null">
student_age
if>
trim>
<trim prefix="values(" suffix=")" suffixOverrides=",">
<if test="sname != null">
#{sname},
if>
<if test="sage != null">
#{sage}
if>
trim>
insert>
Trim元素的有prefix、prefixOverrides、suffix、suffixOverrides四个属性,prefix开头添加一个,prefixOverrides开头去掉一个,suffix结尾添加一个,suffixOverrides结尾去掉一个,如代码的第3、12行,增加前缀和后缀,去掉多余的逗号
<update id="UpdateStudentById">
update tb_student
<set>
<if test="studentName!=null">
student_name=#{studentName},
if>
<if test="studentAge!=null">
student_age=#{studentAge},
if>
set>
where sno=#{sno}
update>
<insert id="insertBatch">
insert into tb_student(student_name,student_age)
values
<foreach collection="students" item="student" index="index" separator=",">
(#{student.studentName},#{student.studentAge})
foreach>
insert>
<select id="getStudentByBind" resultType="com.bjwl.pojo7.StudentEntity" >
<bind name="name" value="'%'+studentName"/>
select * from tb_student where student_name like #{name}
select>