1.Spring-mybatis-动态SQL-if&where&set标签
This commit is contained in:
@@ -28,8 +28,8 @@ public interface EmpMapper {
|
|||||||
public void insert(Emp emp);
|
public void insert(Emp emp);
|
||||||
|
|
||||||
//更新员工
|
//更新员工
|
||||||
@Update("update emp set username = #{username}, name = #{name}, gender = #{gender}, image = #{image}, job = #{job}" +
|
/*@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}")
|
", entrydate = #{entrydate}, dept_id = #{deptId},update_time = #{updateTime} where id = #{id}")*/
|
||||||
public void update(Emp emp);
|
public void update(Emp emp);
|
||||||
|
|
||||||
|
|
||||||
|
@@ -3,9 +3,54 @@
|
|||||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||||
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||||
<mapper namespace="com.inmind.mapper.EmpMapper">
|
<mapper namespace="com.inmind.mapper.EmpMapper">
|
||||||
|
|
||||||
<!--条件查询操作-->
|
<!--条件查询操作-->
|
||||||
<select id="list" resultType="com.inmind.pojo.Emp">
|
<select id="list" resultType="com.inmind.pojo.Emp">
|
||||||
select * from emp where name like concat('%',#{name},'%')
|
select * from emp
|
||||||
and gender = #{gender} and entrydate between #{begin} and #{end} order by update_time desc
|
<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>
|
</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>
|
</mapper>
|
@@ -71,4 +71,23 @@ class SpringbootMybatisCrudApplicationTests {
|
|||||||
List<Emp> list = empMapper.list("张", (short) 1, LocalDate.of(2010, 1, 1), LocalDate.of(2020, 1, 1));
|
List<Emp> list = empMapper.list("张", (short) 1, LocalDate.of(2010, 1, 1), LocalDate.of(2020, 1, 1));
|
||||||
System.out.println(list);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user