Java web第五次作业

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);

}

相关推荐
我科绝伦(Huanhuan Zhou)6 分钟前
MySQL极端场景数据丢失防护:从Crash-Safe到异地备份
数据库·mysql
辞旧 lekkk7 分钟前
【Redis初阶】常见数据类型
开发语言·数据库·c++·redis·学习·缓存·bootstrap
帅次39 分钟前
Kotlin 与 Java 互操作:混合工程里的平台类型与 API 边界
java·开发语言·kotlin·suspend·nullable
笨蛋不要掉眼泪1 小时前
MySQL架构揭秘:MVCC解决读一致性问题
数据库·mysql·架构
X-⃢_⃢-X1 小时前
一、第一阶段:认识 Spring Boot
java·spring boot·后端
前端工作日常1 小时前
我学习到的Java程序的生命周期
java·后端
SunnyDays10112 小时前
Java 实现 PDF 页面删除、排序、旋转和裁剪
java·pdf
人工干智能2 小时前
科普:Pandas 索引器 loc 与 iloc 的编程思维
java·人工智能·pandas
dtq04242 小时前
C语言-结构体详解
c语言·开发语言·学习
梅雅达编程笔记2 小时前
编程启蒙|Scratch 转 Python 系列第9天:字典/哈希表积木双向对照(AI大模型参数配置表实战)
开发语言·人工智能·python·numpy·pandas