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

}

相关推荐
小黄编程快乐屋1 小时前
各个排序算法基础速通万字介绍
java·算法·排序算法
材料苦逼不会梦到计算机白富美3 小时前
贪心算法-区间问题 C++
java·c++·贪心算法
羚羊角uou4 小时前
【C++】list模拟实现(详解)
开发语言·c++
Peter_chq4 小时前
【计算机网络】多路转接之select
linux·c语言·开发语言·网络·c++·后端·select
小小李程序员7 小时前
LRU缓存
java·spring·缓存
cnsxjean7 小时前
SpringBoot集成Minio实现上传凭证、分片上传、秒传和断点续传
java·前端·spring boot·分布式·后端·中间件·架构
CRMEB-嘉嘉7 小时前
如何优化 PHP 性能?
开发语言·php
hadage2337 小时前
--- stream 数据流 java ---
java·开发语言
xnuscd7 小时前
Milvus概念
数据库·学习·mysql