根据主键删除数据
插入数据
-- ================插入=================
insert into emp(username, name, gender, image, job, entrydate, dept_id, create_time, update_time) values ('tom','塔姆','tom',1 , '1.png' ,now(),1,now(),now()
@Options(keyProperty = "id",useGeneratedKeys = true)
@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);
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Emp {private Integer id; // Idprivate String username; //用户名private String password; //用户密码private String name ; //姓名private short gender; //性别 1男 2女private String image; //头像 urlprivate short job ; // 工作职位 1班主任 2讲师 3 学工主管 4 教研主管 5 咨询师private LocalDate entrydate ; //入职时间private Integer deptId; // 部门IDprivate LocalDateTime createTime; //创建时间private LocalDateTime updateTime; // 修改时间
@Test
public void testInsert(){//构造员工对象Emp emp =new Emp();emp.setUsername("tom2");emp.setName("塔姆");emp.setImage("1.jpg");emp.setGender((short)1);emp.setJob((short)1);emp.setEntrydate(LocalDate.of(2000,1,1));emp.setCreateTime(LocalDateTime.now());emp.setUpdateTime(LocalDateTime.now());emp.setDeptId(1);empMapper.insert(emp);System.out.println(emp.getId());
}
更新数据
查询数据
如果数据里的名字和实体类的名·不一样重名为新的名字重名
//的一种
@Select("select id, username, password, name, gender, image, job, entrydate, dept_id deptId, create_time createTime, update_time updateTime from emp where id=#{di}")public Emp getById (Integer id);
//第二种@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);
#开启 mybatis的驼峰命名自动映射开关 变成 a_column--->aColumn
mybatis.configuration.map-underscore-to-camel-case=true
条件查询
@Select("select * from emp where name like concat('%','#{name}','%') and " +"gender=#{gender} and entrydate between#{begin} and #{end} order by update_time desc ;")public List<Emp> list(String name, Short gender, LocalDate begin ,LocalDate end);
@Testpublic void testList(){List<Emp> empList= empMapper.list("张",(short)1,LocalDate.of(2010,1,1),LocalDate.of(2020,1,1));System.out.println(empList);}