1.在idea中配置好数据源
2、视频案例中只给出了查询所有结果的示例,请自己完成添加、删除、修改操作的代码。以下供参
考。
@Delete("delete from emp where id=#{id}")
public void delete(Integer id);
测试代码
@Test
public void testDelete(){
empMapper.delete(17);
}
@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);
测试代码
@Test
public void testInsert(){
Emp emp=new Emp();
emp.setUsername("Tom");
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);
}
@Update("update emp set username=#{username},name=#{name},gender=#{gender}," +
"image=#{image},job=#{job},entrydate=#{entrydate},dept_id=#{deptId},update_time=#{updateTime} where id=#{id}")
public void update(Emp emp);
测试代码
@Test
public void testUpdate(){
Emp emp=new Emp();
emp.setId(17);
emp.setUsername("Tom1");
emp.setName("汤姆1");
emp.setImage("1.jpg");
emp.setGender((short)1);
emp.setJob((short)1);
emp.setEntrydate(LocalDate.of(2000,1,1));
emp.setUpdateTime(LocalDateTime.now());
emp.setDeptId(1);
empMapper.update(emp);
}
@Select("select * from emp where name like '%${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);
测试代码
@Test
public void testList(){
empMapper.list("张",(short)1,LocalDate.of(2010,1,1),LocalDate.of(2020,1,1));
System.out.println(empList);
}
3、lombok库的使用。尝试将实体类采用注解的方式来实现。
import lombok.*;
//@Getter
//@Setter
//@ToString
//@EqualsAndHashCode
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private Integer id;
private String name;
private Short age;
private Short gender;
private String phone;
4、学习idea的调试技巧,并尝试使用。
5、对以下案例使用mybatis进行添加、删除、修改、更新的操作。
@Mapper
public interface PoetMapper {
@Delete("delete from poet where id=#{id}")
public void delete(Integer id);
@Insert("insert into poet(name,gender,dynasty,title,style)" +
"values (#{name},#{gender},#{dynasty},#{title},#{style})")
public void insert(Poet poet);
@Update("update poet set name=#{name},gender=#{gender}," +
"dynasty=#{dynasty},title=#{title},style=#{style} where id=#{id}")
public void update(Poet poet);
}
测试代码
@SpringBootTest
class SpringbootMybatisCrudApplicationTests {
@Autowired
private PoetMapper poetMapper;
@Test
public void testInsert(){
Poet poet=new Poet();
poet.setName("汤姆");
poet.setGender((short)1);
poet.setDynasty("元代");
poet.setTitle("诗词爱好者");
poet.setStyle("无");
poetMapper.insert(poet);
}
@Test
public void testDelete(){
poetMapper.delete(7);
}
@Test
public void testUpdate(){
Poet poet=new Poet();
poet.setId(18);
poet.setName("汤姆1");
poet.setGender((short)1);
poet.setDynasty("清代");
poet.setTitle("无");
poet.setStyle("无");
poetMapper.update(poet);
}