二十四、Mybatis-基础操作-删除(预编译SQL)

发布于:2025-08-16 ⋅ 阅读:(17) ⋅ 点赞:(0)

mybatis环境准备概述与注意事项(springboot项目引入三项必要的起步依赖)

mybatis环境准备概述与注意事项(引入三项必要依赖)

项目目录结构

项目目录结构

mybatis基础操作-删除

mybatis基础操作-删除

对应EmpMapper(接口)代码

package com.itheima.mapper;

import org.apache.ibatis.annotations.*;

@Mapper
public interface EmpMapper {
    //根据TD删除数据
    @Delete("delete from emp where id=#{id}")
    public void delete(Integer id);
}

对应SpringbootMybatisCrudApplicationTests(测试类)代码

package com.itheima;

import com.itheima.mapper.EmpMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SpringbootMybatisCrudApplicationTests {

    @Autowired
    private EmpMapper empMapper;
    @Test
    public void testDelete(){
        empMapper.delete(17);
    }

}

mybatis基础操作-日志输出-可直观看到预编译SQL的效果

mybatis基础操作-日志输出-预编译SQL

对应application.properties的配置内容

#下面这些内容是为了让MyBatis映射
#指定MybatisMapper文件
mybatis.mapper-locations=classpath:mappers/*xml
#指定Mybatis的实体目录
mybatis.type-aliases-package=com.itheima.mybatis.entity
#驱动类名称
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#数据库连接的url
spring.datasource.url=jdbc:mysql://localhost:3306/db02
#连接数据库的用户名
spring.datasource.username=root
#连接数据库的密码
spring.datasource.password=root
#配置mybatis的日志,指定输出到控制台
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl


测试结果

测试结果

预编译SQL优势

预编译SQL优势

SQL注入介绍

SQL注入介绍

mybatis参数占位符-(使用‘#{}'才能有SQL预编译效果)

mybatis参数占位符-(使用‘#{}'才能有SQL预编译效果)

mybatis参数占位符总结

mybatis参数占位符总结