使用注解的方式定义SQL语句 与 使用Mybatis映射文件xml的方式定义SQL语句的具体实现

1.使用注解的方式定义SQL语句:

Controller层:

java 复制代码
@RequestMapping("/emps")
@Slf4j
@RestController
public class EmpController {

    @Autowired
    private EmpService empService;

    //根据ID修改员工数据
    @PutMapping()
    public Result update(@RequestBody Emp emp){
        log.info("修改员工数据:{}", emp);
        empService.update(emp);
        return Result.success();
    }
}

Service接口层:

java 复制代码
public interface EmpService {
    /**
     * 修改员工数据
     */
    void update(Emp emp);
}

Service接口实现层:

java 复制代码
@Service
public class EmpServiceImpl implements EmpService{
    @Autowired
    private EmpMapper empMapper;

    @Override
    public void update(Emp emp) {
        empMapper.update(emp.getUsername(), emp.getName(), emp.getGender(), emp.getImage(), emp.getId(), LocalDateTime.now());
    }
}

Mapper接口层:

java 复制代码
@Mapper
public interface EmpMapper {
    /**
     * 根据ID修改员工数据
     */
    @Update("update emp set username = #{username},name = #{name},gender=#{gender},update_Time=#{updateTime} ,image=#{image} where id=#{id}")
    void update(String username, String name, Short gender, String image, Integer id, LocalDateTime updateTime);
}
2.使用Mybatis映射文件xml的方式:

Controller层(相同):

java 复制代码
@RequestMapping("/emps")
@Slf4j
@RestController
public class EmpController {

    @Autowired
    private EmpService empService;

    //根据ID修改员工数据
    @PutMapping()
    public Result update(@RequestBody Emp emp){
        log.info("修改员工数据:{}", emp);
        empService.update(emp);
        return Result.success();
    }
}
java 复制代码
public interface EmpService {
    /**
     * 修改员工数据
     */
    void update(Emp emp);
}

Service接口实现类:

java 复制代码
@Service
public class EmpServiceImpl implements EmpService{
    @Autowired
    private EmpMapper empMapper;

    //使用映射文件xml
    @Override
    public void update(Emp emp){
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.update(emp);
    }
}

Mapper层:

java 复制代码
@Mapper
public interface EmpMapper {

    //使用映射文件xml的方式
    void update(Emp emp);
}

Mapper映射文件xml:

XML 复制代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.itheima.mapper.EmpMapper">

    <!--    更新员工数据-->
    <update id="update">
        update emp
        <set>
            <if test="username != null and username != ''">
                username = #{username},
            </if>
            <if test="password!=null and passwprd !=''">
                password = #{passwprd},
            </if>
            <if test="name!=null and name!=''">
                name =#{name},
            </if>
            <if test="gender!=null">
                gender=#{gender},
            </if>
            <if test="image !=null and image !=''">
                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映射文件xml的路径要求如下:

  1. 放在项目的resources目录下。
  2. 在resources目录下创建一个与mapper接口全名相同的包名,例如:com.example.mapper。
  3. 在创建的包名目录下,创建一个与mapper接口同名的文件,例如:UserMapper.xml。
  4. 将mapper映射文件中的内容按照MyBatis的语法编写。
相关推荐
老纪的技术唠嗑局6 分钟前
重剑无锋,大巧不工 —— OceanBase 中的 Nest Loop Join 使用技巧分享
数据库·sql
BillKu34 分钟前
Java + Spring Boot + Mybatis 插入数据后,获取自增 id 的方法
java·tomcat·mybatis
全栈凯哥34 分钟前
Java详解LeetCode 热题 100(26):LeetCode 142. 环形链表 II(Linked List Cycle II)详解
java·算法·leetcode·链表
chxii35 分钟前
12.7Swing控件6 JList
java
寒山李白37 分钟前
MySQL复杂SQL(多表联查/子查询)详细讲解
sql·mysql·子查询·多表联查
全栈凯哥37 分钟前
Java详解LeetCode 热题 100(27):LeetCode 21. 合并两个有序链表(Merge Two Sorted Lists)详解
java·算法·leetcode·链表
YuTaoShao37 分钟前
Java八股文——集合「List篇」
java·开发语言·list
PypYCCcccCc42 分钟前
支付系统架构图
java·网络·金融·系统架构
华科云商xiao徐1 小时前
Java HttpClient实现简单网络爬虫
java·爬虫
扎瓦1 小时前
ThreadLocal 线程变量
java·后端