目录
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 {
@Autowired
private UserMapper userMapper;
@Test
public 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 {
@Autowired
private EmpMapper empMapper;
@Test
public void testDelete(){
empMapper.delete(17);
}
}
Mybatis-基础操作-删除(预编译SQL)
Mybatis-基础操作-新增
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Emp {
private Integer id; // ID
private String username; // 用户名
private String password; // 密码
private String name; // 姓名
private Short gender; // 性别:1 男,2 女
private String image; // 图像url
private Short job; // 职位
private LocalDate entrydate; // 日志日期
private Integer deptId; // 部门ID
private 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 {
@Autowired
private EmpMapper empMapper;
@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);
}
}
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 {
@Autowired
private EmpMapper empMapper;
@Test
public 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);
}
}