• java学习记录


    一、修改数据

    1.当修改数据时,会先判断该属性是否存在或者是否为空串

    1. <update id="editStaffItem">
    2. update staff
    3. <set>
    4. <if test='name!=null and name!=""'>
    5. name=#{name},
    6. if>
    7. <if test="salary!=null">
    8. salary=#{salary},
    9. if>
    10. set>
    11. <where>
    12. id=#{id}
    13. where>
    14. update>

    2.在Dao层声明该方法

     int editStaffItem(Staff staff);

    3.在Controller类中,编写代码来接收http请求

    1. @PutMapping("staffitem")
    2. public String editStaffItem(Staff staff) {
    3. dao.editStaffItem(staff);
    4. return "success";
    5. }

    二、模糊查询

    1.在Dao层声明方法

     List getStaff(String checktext);

    将查询的结果放在一个List中

    2.在Mapper文件中编写数据库操作

    1. <select id="getStaff" resultType="com.easy.bean.Staff">
    2. select * from staff
    3. <where>
    4. <if test="checktext !=null and checktext !=''">
    5. <bind value="'%'+checktext+'%'" name="liketext">bind>
    6. name like #{liketext}
    7. if>
    8. where>
    9. select>

    3.控制类

    1. @GetMapping("staff")
    2. public CommonResult getStraff(String checktext) {
    3. List list=dao.getStaff(checktext);
    4. return CommonResult.success(list);
    5. }

    三、根据工资的等级来获取职员的信息

    1.Dao层

     List getStaffBySalary(String salarytext);

    2.Mapper文件

    1. <select id="getStaffBySalary" resultType="com.easy.bean.Staff">
    2. select * from staff
    3. <where>
    4. <choose>
    5. <when test='salarytext == "低"'>
    6. salary <= 5000
    7. when>
    8. <when test='salarytext == "中"'>
    9. salary >5000 and salary<=8000
    10. when>
    11. <otherwise>
    12. salary>8000
    13. otherwise>
    14. choose>
    15. where>
    16. select>

    resultType表示输出的对象类型

    3.控制类

    1. @GetMapping("staff/salary")
    2. public CommonResult getStaffBySalary(String salarytext) {
    3. List list=dao.getStaffBySalary(salarytext);
    4. return CommonResult.success(list);
    5. }

    四、一次添加多组数据

    1.Dao

    int addList(List list);

    2.Mapper文件

    1. <insert id="addList" >
    2. insert into staff(code,name,salary,username,userpass)
    3. values
    4. <foreach collection="list" item="it" separator=",">
    5. (#{it.code},#{it.name},#{it.salary},#{it.username},#{it.userpass})
    6. foreach>
    7. insert>

    3.控制类

    1. @PostMapping("staff")
    2. public String addStaff(Staff staff) {
    3. staff=new Staff();
    4. staff.setCode("10001");
    5. staff.setName("李思思");
    6. staff.setSalary(new BigDecimal(2000));
    7. staff.setUsername("lisisi");
    8. staff.setUserpass("123123");
    9. List list=new ArrayList();
    10. list.add(staff);
    11. staff=new Staff();
    12. staff.setCode("10002");
    13. staff.setName("小甜甜");
    14. staff.setSalary(new BigDecimal(2000));
    15. staff.setUsername("牛夫人");
    16. staff.setUserpass("123123");
    17. list.add(staff);
    18. // dao.addStaff(staff);
    19. dao.addList(list);
    20. return "success";
    21. }

    五、Dao中接收多个参数

    如果Dao中接收两个以上的参数就要使用@Param注解给参数起名字

    1.Dao

    int edit(@Param("id") int id,@Param("staff") Staff staff);

    2.Mapper

    1. <update id="edit">
    2. upadate staff code=#{staff.code},name=#{staff.name}set where id=#{id}
    3. update>

    六、多张表的映射

    查询Staff表时也会显示Department表的部门信息

    1.在Staff类中添加Department类的属性

    1. private Department dep;
    2. public Department getDep() {
    3. return dep;
    4. }
    5. public void setDep(Department dep) {
    6. this.dep = dep;
    7. }

    2.Dao

    List getStaffAndDep();

    3.Mapper

    1. <resultMap id="staffAndDep" type="com.easy.bean.Staff">
    2. <association column="dep_id" select="getStaffDep" property="dep" >association>
    3. resultMap>
    4. <select id="getStaffDep" resultType="com.easy.bean.Department">
    5. select * from department where id=#{dep_id}
    6. select>
    7. <select id="getStaffAndDep" resultMap="staffAndDep" >
    8. select * from staff
    9. select>

    resultMap来定制映射的方式

    type定义返回的对象是Staff类,通过dep_id来查询,将查询的内容放在dep列中,查询方法是select中指向的getStaffDep。

    一对一用association

    七、示例

    查询部门时能显示内部职员的信息,一对多

    1.Department

    1. package com.easy.bean;
    2. import java.io.Serializable;
    3. import java.util.List;
    4. public class Department implements Serializable {
    5. private int id;
    6. private String code;
    7. private String name;
    8. private List staffList;
    9. public List getStaffList() {
    10. return staffList;
    11. }
    12. public void setStaffList(List staffList) {
    13. this.staffList = staffList;
    14. }
    15. public int getId() {
    16. return id;
    17. }
    18. public void setId(int id) {
    19. this.id = id;
    20. }
    21. public String getCode() {
    22. return code;
    23. }
    24. public void setCode(String code) {
    25. this.code = code;
    26. }
    27. public String getName() {
    28. return name;
    29. }
    30. public void setName(String name) {
    31. this.name = name;
    32. }
    33. }

    2.Dao

    1. package com.easy.dao;
    2. import com.easy.bean.Department;
    3. import org.apache.ibatis.annotations.Mapper;
    4. import java.util.List;
    5. @Mapper
    6. public interface IDepartmentDao {
    7. int addDepartment(Department department);
    8. int delDepartment(int id);
    9. int editDepartment(Department department);
    10. List getDep();
    11. }

    3.Mapper

    1. "1.0" encoding="UTF-8"?>
    2. mapper
    3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    5. <mapper namespace="com.easy.dao.IDepartmentDao">
    6. <cache eviction="FIFO" flushInterval="60000" size="512" readOnly="true" />
    7. <insert id="addDepartment">
    8. insert into department(code,name)
    9. value(#{code},#{name})
    10. insert>
    11. <delete id="delDepartment">
    12. delete from department where id=#{id}
    13. delete>
    14. <update id="editDepartment">
    15. update department set name=#{name} where id=#{id}
    16. update>
    17. <resultMap id="departmentAndStaff" type="com.easy.bean.Department">
    18. <result column="id" property="id">result>
    19. <collection fetchType="lazy" property="staffList" column="id" select="getDepStaff">collection>
    20. resultMap>
    21. <select id="getDepStaff" resultType="com.easy.bean.Staff">
    22. select *from staff where dep_id=#{id}
    23. select>
    24. <select id="getDep" resultMap="departmentAndStaff">
    25. select * from department
    26. select>
    27. mapper>

    4.控制类

    1. package com.easy.controller;
    2. import com.easy.bean.Department;
    3. import com.easy.common.CommonResult;
    4. import com.easy.dao.IDepartmentDao;
    5. import org.springframework.beans.factory.annotation.Autowired;
    6. import org.springframework.transaction.annotation.Transactional;
    7. import org.springframework.web.bind.annotation.*;
    8. import java.util.List;
    9. @RestController
    10. public class DepartmentController {
    11. @Autowired
    12. IDepartmentDao dao;
    13. @PostMapping("department")
    14. public String addDepartment(Department department){
    15. dao.addDepartment(department);
    16. return "添加成功";
    17. }
    18. @DeleteMapping("department/{id}")
    19. public String delDepartment(@PathVariable int id){
    20. dao.delDepartment(id);
    21. return "删除成功";
    22. }
    23. @PutMapping("department")
    24. public String editDepartment(Department department){
    25. dao.editDepartment(department);
    26. return "修改成功";
    27. }
    28. @GetMapping ("dep")
    29. // @Transactional
    30. public CommonResult getDep(){
    31. List list =dao.getDep();
    32. // System.out.println("---------");
    33. // list =dao.getDep();
    34. return CommonResult.success(list);
    35. }
    36. }

    八、缓存

    运行一次,就会加载一次

    1.可以通过在Mapper文件中添加

    <cache eviction="FIFO" flushInterval="60000" size="512" readOnly="true"
    然后将在映射方式中加上fetchType属性
     <collection fetchType="lazy" property="staffList" column="id" select="getDepStaff">collection>

    只有在系统调用时才会加载

    2.@Transactional

    在需要缓存的方法上加上@Transactional注解,并在启动类上加上@EnableTransactionManagement

  • 相关阅读:
    如何保证语音芯片的稳定性能和延长使用寿命
    SpringBoot中幕——配置文件properties与yml
    leetcodeTop(100) 31链表排序
    Spring Boot面试必问:启动流程
    轻量版PDF批注编辑数字签名工具:SignFlow Mac
    深度学习重建 特点对比总结 MVSNet系列最新顶刊 特点对比总结
    论文解读(NGCF)《LightGCN: Simplifying and Powering Graph Convolution Network for Recommendation》
    常用安全产品系统默认口令
    FPGA-串口接收图像写入RAM并读出在TFT显示屏上显示
    成语词典查询易语言代码
  • 原文地址:https://blog.csdn.net/qq_62499218/article/details/140963678