使用注解的方式定义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的语法编写。
相关推荐
n8n3 小时前
RabbitMQ全面详解:从核心概念到企业级应用
java·rocketmq
用户785127814703 小时前
实战代码:获取淘宝商品详情数据接口
java
Chan163 小时前
流量安全优化:基于 Sentinel 实现网站流量控制和熔断
java·spring boot·安全·sentinel·intellij-idea·进程
莫陌尛.4 小时前
xml 方式声明式事务案例
xml
源码7可4 小时前
Java高手速成--吃透源码+手写组件+定制开发
java
zjjuejin4 小时前
Maven 云原生时代面临的八大挑战
java·后端·maven
ZhengEnCi4 小时前
@RequestParam 注解完全指南-从参数绑定到接口调用的Web开发利器
java·spring boot
=>>漫反射=>>4 小时前
单元测试 vs Main方法调试:何时使用哪种方式?
java·spring boot·单元测试
初圣魔门首席弟子4 小时前
c++ bug 记录(merge函数调用时错误地传入了vector对象而非迭代器。)
java·c++·bug
cxyxiaokui0015 小时前
🔍 为什么我的日志在事务回滚后也没了?——揭秘 REQUIRES_NEW 的陷阱
java·后端·spring