1.Spring-mybatis-动态SQL-if&where&set标签

This commit is contained in:
2025-09-26 14:57:15 +08:00
parent f6d39acad2
commit 0ec88d4aa3
3 changed files with 68 additions and 4 deletions

View File

@@ -28,8 +28,8 @@ public interface EmpMapper {
public void insert(Emp emp);
//更新员工
@Update("update emp set username = #{username}, name = #{name}, gender = #{gender}, image = #{image}, job = #{job}" +
", entrydate = #{entrydate}, dept_id = #{deptId},update_time = #{updateTime} where id = #{id}")
/*@Update("update emp set username = #{username}, name = #{name}, gender = #{gender}, image = #{image}, job = #{job}" +
", entrydate = #{entrydate}, dept_id = #{deptId},update_time = #{updateTime} where id = #{id}")*/
public void update(Emp emp);

View File

@@ -3,9 +3,54 @@
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.inmind.mapper.EmpMapper">
<!--条件查询操作-->
<select id="list" resultType="com.inmind.pojo.Emp">
select * from emp where name like concat('%',#{name},'%')
and gender = #{gender} and entrydate between #{begin} and #{end} order by update_time desc
select * from emp
<where>
<if test="name != null">
name like concat('%',#{name},'%')
</if>
<if test="gender != null">
and gender = #{gender}
</if>
<if test="begin != null and end != null">
and entrydate between #{begin} and #{end}
</if>
</where>
order by update_time desc
</select>
<!--动态更新员工信息-->
<update id="update">
update emp
<set>
<if test=" username != null">
username = #{username},
</if>
<if test="name != null">
name = #{name},
</if>
<if test="gender != null">
gender = #{gender},
</if>
<if test="image != null">
image = #{image},
</if>
<if test="job != null">
job = #{job},
</if>
<if test="entrydate != null">
entrydate = #{entrydate},
</if>
<if test="deptId != null">
dept_id = #{deptId},
</if>
<if test="updateTime != null">
update_time = #{updateTime}
</if>
</set>
where id = #{id}
</update>
</mapper>

View File

@@ -71,4 +71,23 @@ class SpringbootMybatisCrudApplicationTests {
List<Emp> list = empMapper.list("", (short) 1, LocalDate.of(2010, 1, 1), LocalDate.of(2020, 1, 1));
System.out.println(list);
}
//条件查询测试
@Test
void testList1(){
// List<Emp> list = empMapper.list("张", null, null,null);
List<Emp> list = empMapper.list(null, (short)1, null,null);
System.out.println(list);
}
@Test
void testUpdate1(){
Emp emp = new Emp();
emp.setId(20);
emp.setGender((short)1);
emp.setUsername("ls6");
emp.setName("李四6");
emp.setUpdateTime(LocalDateTime.now());
empMapper.update(emp);
}
}