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

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

Controller层:

@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接口层:

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

Service接口实现层:

@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接口层:

@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层(相同): 

@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();
    }
}

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

Service接口实现类:

@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层:

@Mapper
public interface EmpMapper {

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

Mapper映射文件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的语法编写。
  • 10
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值