本文主要是介绍【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
这篇关于【Day09】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!