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

}

相关推荐
ps酷教程4 小时前
Jackson 解决没有无参构造函数的反序列化问题
java
NiceCloud喜云4 小时前
Opus 4.8 的 Effort Control 怎么选:Low 到 Max 五档策略
android·java·大数据·前端·c++·python·spring
AI玫瑰助手4 小时前
Python函数:默认参数的定义与注意事项
开发语言·python·信息可视化
油炸自行车4 小时前
Claude Code 错误:API Error: 400 Failed to deserialize the JSON body into the
开发语言·javascript·json·trae·claude code·api error 400
肩上风骋5 小时前
C++14特性
开发语言·c++·c++14特性
_日拱一卒5 小时前
LeetCode:994腐烂的橘子
java·数据结构·算法·leetcode·深度优先
隔窗听雨眠5 小时前
Nginx网关响应慢排查手记
java·服务器·nginx
智慧物业老杨5 小时前
智慧物业合同周期管理系统:从风险预警到智能交接的全流程数智化落地方案
java·人工智能·python
源码宝6 小时前
MES系统源码:Java8 + SpringBoot2.7 + MySQL8 + Redis,后端源码清爽易扩展
java·后端·源码·springboot·mes系统·源码二开·mes源码
JAVA社区6 小时前
Java高级全套教程(十)—— SpringCloudAlibaba超详细实战详解
java·开发语言·spring cloud·面试·职场和发展