Mybatis
一、lombok
二、 Mybatis的增删改查
@Mapper
public interface EmpMapper {
// 删除数据根据ID
@Delete("delete from emp where id=#{id}")
public void deleteUserById(Integer id);
// 新增员工
@Insert("insert into emp(username,name,gender,image,job,entrydate,dept_id,create_time,update_time)" +
"values (#{username},#{name},#{gender},#{image},#{job},#{entrydate},#{deptId},#{createTime},#{updateTime})")
public void insert(Emp emp);
// 根据id搜索员工
public List<Emp> select(Integer id);
// 更新员工信息
public void update(Emp emp);
// 批量删除员工信息
public void deleteByids(List<Integer> ids);
}
参数占位符 #{}
三、日志输出
主键返回
数据封装
方案一:对属性名和字段名不一致的使用注解进行关联
@Results({
@Result(column = "class",property = "class_"),
@Result(column = "student_id",property = "studentId"),
@Result(column = "create_time",property = "createTime"),
@Result(column = "update_time",property = "updateTime")
})
@Select("select * from student_info where id=#{id}")
public Student selectById_3(Integer id);
方案二:起别名
方案三:开启mybatis的驼峰命名开关
推荐使用,前提是实体类和数据库字段名都要遵循各自的命名规范。
参数传递
四、XMl映射文件
同包同名