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

环境准备创建表

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

@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

@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
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值