mybatis--Mybatis-动态SQL-if-where-set-foreach标签
This commit is contained in:
@@ -26,8 +26,8 @@ public interface EmpMapper {
|
|||||||
/*
|
/*
|
||||||
更新员工
|
更新员工
|
||||||
*/
|
*/
|
||||||
@Update("update emp set username = #{username}, name = #{name}, gender = #{gender} , image = #{image} , job = #{job}, entrydate = #{entrydate}," +
|
/*@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}")
|
" dept_id = #{deptId}, update_time = #{updateTime} where id = #{id}")*/
|
||||||
public void update(Emp emp);
|
public void update(Emp emp);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -58,4 +58,9 @@ public interface EmpMapper {
|
|||||||
// @Select("select * from emp where name like concat('%',#{name},'%') and gender = #{gender} and entrydate between #{begin} and #{end}")
|
// @Select("select * from emp where name like concat('%',#{name},'%') and gender = #{gender} and entrydate between #{begin} and #{end}")
|
||||||
public List<Emp> list1(String name,Short gender,LocalDate begin,LocalDate end);
|
public List<Emp> list1(String name,Short gender,LocalDate begin,LocalDate end);
|
||||||
|
|
||||||
|
/*
|
||||||
|
-- 批量删除
|
||||||
|
delete from emp where id in (19,21,22) ;
|
||||||
|
*/
|
||||||
|
public void deleteByIds(List<Integer> ids);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,10 +3,55 @@
|
|||||||
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="list1" resultType="com.inmind.pojo.Emp"><!--注意:resultType定义的是单条记录的java封装类型-->
|
<select id="list1" resultType="com.inmind.pojo.Emp"><!--注意:resultType定义的是单条记录的java封装类型-->
|
||||||
select * from emp where
|
select * from emp
|
||||||
|
<where>
|
||||||
|
<if test="name != null">
|
||||||
name like concat('%',#{name},'%')
|
name like concat('%',#{name},'%')
|
||||||
|
</if>
|
||||||
|
<if test="gender != null">
|
||||||
and gender = #{gender}
|
and gender = #{gender}
|
||||||
|
</if>
|
||||||
|
<if test="begin != null and end != null">
|
||||||
and entrydate between #{begin} and #{end}
|
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>
|
||||||
|
|
||||||
|
|
||||||
|
<!--批量删除员工-->
|
||||||
|
<!--
|
||||||
|
collection:遍历的集合
|
||||||
|
item:遍历出来的元素
|
||||||
|
separator:分隔符
|
||||||
|
open:遍历开始前拼接的sql片段
|
||||||
|
close:遍历结束后拼接的sql片段
|
||||||
|
-->
|
||||||
|
<delete id="deleteByIds">
|
||||||
|
<!-- delete from emp where id in (19,21,22) ;-->
|
||||||
|
delete from emp where id in
|
||||||
|
<foreach collection="ids" item="id" separator="," open="(" close=")">
|
||||||
|
#{id}
|
||||||
|
</foreach>
|
||||||
|
</delete>
|
||||||
</mapper>
|
</mapper>
|
||||||
@@ -9,6 +9,7 @@ import org.springframework.boot.test.context.SpringBootTest;
|
|||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
@@ -74,8 +75,31 @@ class SpringbootMybatisCrudApplicationTests {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testList1() {
|
void testList1() {
|
||||||
List<Emp> list = empMapper.list1("张", (short) 1, LocalDate.of(2010, 1, 1), LocalDate.of(2020, 1, 1));
|
//条件查询
|
||||||
|
// List<Emp> list = empMapper.list1("张", (short) 1, LocalDate.of(2010, 1, 1), LocalDate.of(2020, 1, 1));
|
||||||
|
// List<Emp> list = empMapper.list1("张", null,null, null);
|
||||||
|
List<Emp> list = empMapper.list1(null, (short)1,null, null);
|
||||||
System.out.println(list);
|
System.out.println(list);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testUpdate2() {
|
||||||
|
Emp emp = new Emp();
|
||||||
|
emp.setId(22);
|
||||||
|
emp.setName("张四111");
|
||||||
|
emp.setGender((short) 1);
|
||||||
|
emp.setImage("3.jpg");
|
||||||
|
// emp.setUpdateTime(LocalDateTime.now());
|
||||||
|
empMapper.update(emp);
|
||||||
|
System.out.println(emp);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testdeleteByIds() {
|
||||||
|
// List<Integer> ids = Arrays.asList(12, 13, 18);
|
||||||
|
List<Integer> ids = Arrays.asList(11);
|
||||||
|
empMapper.deleteByIds(ids);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user