您的位置:首页 > 文旅 > 旅游 > 【Day09】

【Day09】

2025/2/24 13:03:44 来源:https://blog.csdn.net/2201_75414908/article/details/141872607  浏览:    关键词:【Day09】

目录

Mybatis-基础操作-环境准备

Mybatis-基础操作-删除

Mybatis-基础操作-删除(预编译SQL)

Mybatis-基础操作-新增

Mybatis-基础操作-新增(主键返回)

Mybatis-基础操作-更新

Mybatis-基础操作-查询(根据ID查询)

Mybatis-基础操作-查询(条件查询)

Mybatis-XML映射文件

Mybatis-动态SQL-if

Mybatis-动态SQL-foreach

Mybatis-动态SQL-sql和include


Mybatis-基础操作-环境准备

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {private Integer id;private String name;private Short age;private Short gender;private String phone;
}
@Mapper //在运行时,会自动生成该接口的实现类对象,并且会将对象1交给IOC容器处理
public interface UserMapper {//查询全部用户信息@Select(" select * from user ")public List<User> list();}
@SpringBootTest //springboot整合单元测试的注解
class SpringbootMybatisQuickstartApplicationTests {@Autowiredprivate UserMapper userMapper;@Testpublic void testListUser(){List<User> userList = userMapper.list();userList.stream().forEach(user -> {System.out.println(user);});}}

Mybatis-基础操作-删除

@Mapper
public interface EmpMapper {//根据ID删除数据@Delete("delete from emp where id = #{id}")public void delete(Integer id);
}
@SpringBootTest //springboot整合单元测试的注解
class SpringbootMybatisQuickstartApplicationTests {@Autowiredprivate EmpMapper empMapper;@Testpublic void testDelete(){empMapper.delete(17);}}

Mybatis-基础操作-删除(预编译SQL)

Mybatis-基础操作-新增

@Data
@AllArgsConstructor
@NoArgsConstructor
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; // 职位private LocalDate entrydate; // 日志日期private Integer deptId; // 部门IDprivate LocalDateTime createTime; // 创建时间private LocalDateTime updateTime; // 修改时间
}
@Mapper
public interface EmpMapper {//新增员工@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);}
@SpringBootTest //springboot整合单元测试的注解
class SpringbootMybatisQuickstartApplicationTests {@Autowiredprivate EmpMapper empMapper;@Testpublic 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);}
}

Mybatis-基础操作-新增(主键返回)

Mybatis-基础操作-更新

@Mapper
public interface EmpMapper {//更新员工@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);}
@SpringBootTest //springboot整合单元测试的注解
class SpringbootMybatisQuickstartApplicationTests {@Autowiredprivate EmpMapper empMapper;@Testpublic void testUpdate(){//构造员工记录Emp emp = new Emp();emp.setId(18);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.setCreateTime(LocalDateTime.now());emp.setUpdateTime(LocalDateTime.now());emp.setDeptId(1);//执行更新员工操作empMapper.update(emp);}}

Mybatis-基础操作-查询(根据ID查询)

Mybatis-基础操作-查询(条件查询)

Mybatis-XML映射文件

Mybatis-动态SQL-if

Mybatis-动态SQL-foreach

Mybatis-动态SQL-sql和include

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com