修改数据
1.修改数据时判断name参数不为空且非空字符串,判断salary是不为空则添加记录
<update id="editStaffItem">update staff<set><if test='name!=null and name!=""'>name=#{name},</if><if test="salary!=null">salary=#{salary},</if></set><where>id=#{id}</where> </update>
2.在dao层声明该方法
int editStaffItem(Staff staff);
3.在controller类中,编写代码来接收http请求
@PutMapping("staffitem") public String editStaffItem(Staff staff) {dao.editStaffItem(staff);return "success"; }
批量添加数据
1.在mapper.xml文件中写入映射语句,定义一个名为addList的插入语句,用于向staff表中批量插入多条数据; <foreach>
元素来循环处理列表中的每一项,并为每一条记录构建一个插入语句;
<foreach>
的属性如下:
- collection: 指定要循环遍历的集合变量。在这个例子中,变量名为
list
,通常这个变量应该是在 MyBatis 映射文件对应的接口方法中传递过来的一个 List 对象。 - item: 指定在循环体内部对单个元素的引用变量。在这个例子中,变量名为
it
,表示当前迭代的元素。 - separator: 在每次循环之间添加的分隔符。在这个例子中,使用
,
作为分隔符,这意味着每条插入语句之间将用逗号,
分隔。
<insert id="addList">insert into staff(code,name,salary,username,userpass)<!--循环 -->values<foreach collection="list" item="it" separator=",">(#{it.code},#{it.name},#{it.salary},#{it.username},#{it.userpass})</foreach></insert>
2.在Dao层声明该方法
int addList(List<Staff> list);
3.在Controller层编写代码接收请求
@PostMapping("staff")public String addStaff(Staff staff) {staff=new Staff();staff.setCode("10001");staff.setName("李思思");staff.setSalary(new BigDecimal(2000));staff.setUsername("lisisi");staff.setUserpass("123123");List list=new ArrayList();list.add(staff);staff=new Staff();staff.setCode("10002");staff.setName("小甜甜");staff.setSalary(new BigDecimal(2000));staff.setUsername("牛夫人");staff.setUserpass("123123");list.add(staff);// dao.addStaff(staff);dao.addList(list);return "success";}
模糊查询
1.mapper
这里定义了一个名为 getStaff
的查询语句,用于从 staff
表中查询员工信息,并将结果映射到 com.easy.bean.Staff
类型的对象上。使用了 <where>
和 <if>
元素来动态构建 SQL 语句中的 WHERE
子句。<if>
元素用于根据传入的参数 checktext
是否存在和非空来决定是否包含 WHERE
子句中的条件.
<if test="checktext!=null and checktext !=''">
:如果checktext
参数不为空且非空字符串,则在 SQL 语句中添加name like #{liketext}
。这里使用了and
运算符来确保checktext
不仅不为null
,而且也不是空字符串。<bind name="liketext" value="'%'+checktext+'%'">
:这里使用<bind>
元素来定义一个新的变量liketext
,其值为%
加上checktext
再加上%
。这是为了进行模糊查询,即查询名字中包含checktext
的所有员工
<select id="getStaff" resultType="com.easy.bean.Staff">select * from staff<!-- 根据参数不同组合出不同的·SQL语句 动态SQL语句 标签--><where><!-- 编写条件语句 如果where标签中有内容 会自动添加关键字--><if test="checktext!=null and checktext !=''"><!-- 重新定义参数内容--><bind name="liketext" value="'%'+checktext+'%'"></bind>name like #{liketext}</if></where></select>
2.Dao层
List<Staff> getStaff(String checktext);
3.Controller层
@GetMapping("staff")public CommonResult getStraff(String checktext) {List<Staff> list=dao.getStaff(checktext)return CommonResult.success(list);}
筛选范围查询
1.定义了一个名为 getStaffBySalary
的查询语句,用于从 staff
表中查询员工信息,并将结果映射到 com.easy.bean.Staff
类型的对象上。
使用了 <choose>
, <when>
, 和 <otherwise>
元素来动态构建 SQL 语句中的 WHERE
子句。这些元素用于根据传入的参数 salarytext
来决定如何构造查询条件。
<choose>
:这是一个选择器,它会在多个<when>
条件中选择第一个满足条件的<when>
,如果没有<when>
条件满足,则执行<otherwise>
中的代码。<when test='salarytext=="低"'>
:如果salarytext
的值为"低"
,则在 SQL 语句中添加salary <= 300
。<when test='salarytext=="中"'>
:如果salarytext
的值为"中"
,则在 SQL 语句中添加salary > 300 and salary <= 500
。<otherwise>
:如果salarytext
的值既不是"低"
也不是"中"
(即默认情况下),则在 SQL 语句中添加salary > 500
。
<select id="getStaffBySalary" resultType="com.easy.bean.Staff">select * from staff<where><!--参数名 salarytext--><choose><when test='salarytext=="低"'><!--<在这里是小于 -->salary <= 300</when><when test='salarytext=="中"'>salary >300 and salary <= 500</when><otherwise>salary > 500</otherwise></choose></where></select>
2.声明 List<Staff> getStaffBySalary(String salarytext);
是一个接口方法定义,描述一个名为 getStaffBySalary
的方法,该方法接受一个字符串参数 salarytext
并返回一个 Staff
对象列表。
List<Staff> getStaffBySalary(String salarytext);
3.List<Staff> list=dao.getStaffBySalary(salarytext);
: 这一行代码调用了 DAO (Data Access Object) 层的方法 getStaffBySalary
,该方法接受 salarytext
参数并返回一个 List<Staff>
类型的对象。
return CommonResult.success(list);
: 这一行代码创建了一个 CommonResult
对象,使用 success
静态方法,并将 list
作为参数传递
@GetMapping("staff/salary")public CommonResult getStaffBySalary(String salarytext) {List<Staff> list=dao.getStaffBySalary(salarytext);return CommonResult.success(list);}
关联对象结果集
1.定义了一个名为 staffAndDep
的 resultMap
,用于将查询结果映射到 Staff
类型的对象上
将查询结果中的 dep_id
列的值作为参数传递给另一个映射器方法 getStaffDep
,该方法返回一个部门对象 (Department
),这个对象将被映射到 Staff
对象的 dep
属性上
定义getStaffDep方法:查询语句,用于根据员工的部门 ID (dep_id
) 查询对应的部门信息。其中 #{dep_id}
是一个预编译参数,它的值将由调用 getStaffDep
方法时传入的参数决定
定义getStaffAndDep方法
1<select id="getStaffAndDep" resultMap="staffAndDep">
2 select * from staff
3</select>
这是一个查询语句,用于获取所有的员工信息。这里使用了前面定义的 resultMap
(staffAndDep
) 来映射查询结果
过程:
- 定义了一个
resultMap
,用于将员工查询结果映射到Staff
对象上。 - 通过子查询(
getStaffDep
)获取每个员工对应的部门信息,并将其映射到Staff
对象的dep
属性上。 - 定义了一个查询所有员工的 SQL 语句,并使用上述
resultMap
来映射查询结果。
通过这种方式,当调用 getStaffAndDep
方法时,将会返回一个包含员工信息及其对应部门的 Staff
对象列表。
<!--一对一或一对多查询需要制定映射方式 --><resultMap id="staffAndDep" type="com.easy.bean.Staff"><association column="dep_id" select="getStaffDep" property="dep"></association></resultMap><select id="getStaffDep" resultType="com.easy.bean.Department">select * from department where id=#{dep_id}</select><select id="getStaffAndDep" resultMap="staffAndDep">select * from staff</select>
2.Dao层
List<Staff> getStaffAndDep();
3.Controller层
@GetMapping("staff")public CommonResult getStraff(String checktext) {List<Staff> list=dao.getStaffAndDep();return CommonResult.success(list);}
缓存
/*Mybatis一级缓存(默认生效,SQL session级别):SQL session
---前提:同一次sql会话(设置为同一个事务)
--具体操作:@Transactional 标注为事务,此外启动类也加注解 EnableTransactionManagement
* Mybatis二级缓存(mapper级别):
缓存失效时机:刷新时间到达,或执行增删改查操作时清空二级缓存,请求再次查询
*缓存意义:让数据达到重用,减少mysql的负担*/
1.一级缓存: 加注解,在启动类上加@EnableTransactionManagement注解,
在要实现的方法上加@Transactional注解
在Controller层实现
@GetMapping("dep")@Transactionalpublic CommonResult getDep(){List<Department> list=dao.getDep();System.out.println("------------------");
// list=dao.getDep();System.out.println(list.get(0));System.out.println(list.get(1));return CommonResult.success(list);
// return CommonResult.success();}
在mapper文件中写
<resultMap id="departmentAndStaff" type="com.easy.bean.Department"><!--把某个属性映射到某一个列上去<id column="" property="depid"></id><result column="name" property="depname"></result> --><result column="id" property="id"></result><collection column="id" select="getDepStaff" property="staffList"></collection></resultMap
2.二级缓存 :在Controller
@GetMapping("dep")
// @Transactionalpublic CommonResult getDep(){List<Department> list=dao.getDep();System.out.println("------------------");
// list=dao.getDep();System.out.println(list.get(0));System.out.println(list.get(1));
// return CommonResult.success(list);return CommonResult.success();}
在mapper中
<!-- 一级缓存默认开启 二级缓存 需要编写--><cache eviction="FIFO" flushInterval="60000" size="512" readOnly="true" /><resultMap id="departmentAndStaff" type="com.easy.bean.Department"><!--把某个属性映射到某一个列上去<id column="" property="depid"></id><result column="name" property="depname"></result> --><result column="id" property="id"></result><collection fetchType="lazy" column="id" select="getDepStaff" property="staffList"></collection></resultMap>