mybatis基础操作--CRUD(基于注解使用)

环境准备创建表

sql 复制代码
create table dept(id int unsigned primary key auto_increment comment '主键ID',
name varchar(10) not null unique comment '部门名称',
create_time datetime not null comment '创建时间',
update_time datetime not null comment '修改时间'
)comment '部门表';

insert into dept(id, name, create_time, update_time) VALUES
(1,'皇帝',now(),now()),(2,'后宫',now(),now()),(3,'文臣',now(),now()),(4,'武将',now(),now());

create table emp(
id int unsigned primary key auto_increment comment 'ID',
username varchar(20) not null  unique comment '用户名',
password varchar(32) default '123456' comment '密码',
gender tinyint unsigned not null comment '性别',
image varchar(300) comment '图像',
job tinyint unsigned comment '职位',
entrydate date comment '入职时间',
dept_id int unsigned comment '部门ID',
create_time datetime not null comment '创建时间',
update_time datetime not null comment '修改时间'
)comment '员工表'

增删改查

Emp.java

java 复制代码
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Emp {
    private Integer id;
    private String username;
    private String password;
    private Short gender;
    private String image;
    private Short job;
    private LocalDateTime entrydate;
    private Integer deptId;
    private LocalDateTime createTime;
    private LocalDateTime updateTime;
}

EmpMapper.interface

java 复制代码
@Mapper
public interface EmpMapper {
    @Delete("delete from emp where id=#{id}") //预编译 参数传递 防止sql注入
    public void delete(Integer id);

    @Options(keyProperty = "id",useGeneratedKeys = true)//会自动将生成主键值 赋值给emp对象的id属性
    @Insert("insert into emp(id, username, password, gender, image, job, entrydate, dept_id, create_time, update_time) VALUES ( #{id},#{username},#{password},#{gender},#{image},#{job},#{entrydate},#{deptId},#{createTime},#{updateTime})")
    public void insert(Emp emp);

    @Update("update emp set username =#{username},update_time =#{updateTime} where id =#{id}")
    public void update(Emp emp);

    @Select("select * from emp where id =#{id}") //数据封装 如果类属性名和数据库返回的字段名一致会自动封装
    public  Emp getById(Integer id);
    //1.解决方法给字段起别名
//    @Select("select update_time updateTime,dept_id deptId,create_time createTime from emp where id =#{id}") //数据封装 如果类属性名和数据库返回的字段名一致会自动封装
//    public  Emp getById(Integer id);
    //2.通过Result注解手动映射封装
//    @Results({
//            @Result(column = "dept_id",property = "deptId"),
//            @Result(column = "create_time",property = "createTime"),
//            @Result(column = "update_time",property = "updateTime")
//    })
//    @Select("select * from emp where id =#{id}") //数据封装 如果类属性名和数据库返回的字段名一致会自动封装
//    public  Emp getById(Integer id);
    //3.开启mybatis驼峰命名自动映射开关
    // mybatis.configuration.map-underscore-to-camel-case=true

//    @Select("select * from emp where username like '%${name}%' and gender = #{gender} order by create_time desc ")//${}存在sql注入问题
@Select("select * from emp where username like concat('%',#{name},'%') and gender = #{gender} order by create_time desc ")
    public List<Emp> select(String name,Short gender);
}
增删改查注意

查询开启驼峰命名

查询不到主键

模糊查询%朱%->%${朱}%存在sql注入问题 使用concat

mybatis log
java 复制代码
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
相关推荐
程序员清风2 分钟前
快手二面:乐观锁是怎么用它来处理多线程问题的?
java·后端·面试
一线大码17 分钟前
SpringBoot 优雅实现接口的多实现类方式
java·spring boot·后端
花伤情犹在22 分钟前
Java Stream 高级应用:优雅地扁平化(FlatMap)递归树形结构数据
java·stream·function·flatmap
yaoxin52112335 分钟前
212. Java 函数式编程风格 - Java 编程风格转换:命令式 vs 函数式(以循环为例)
java·开发语言
摇滚侠1 小时前
Spring Boot 3零基础教程,WEB 开发 Thymeleaf 属性优先级 行内写法 变量选择 笔记42
java·spring boot·笔记
滑水滑成滑头1 小时前
**发散创新:多智能体系统的探索与实践**随着人工智能技术的飞速发展,多智能体系统作为当今研究的热点领域,正受到越来越多关注
java·网络·人工智能·python
摇滚侠1 小时前
Spring Boot 3零基础教程,WEB 开发 Thymeleaf 总结 热部署 常用配置 笔记44
java·spring boot·笔记
十年小站1 小时前
一、新建一个SpringBoot3项目
java·spring boot
2401_841495641 小时前
【数据结构】最长的最短路径的求解
java·数据结构·c++·python·算法·最短路径·图搜索
麦麦鸡腿堡1 小时前
Java的代码块介绍与快速入门
java·开发语言