【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 {

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

}

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

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

Mybatis-XML映射文件

Mybatis-动态SQL-if

Mybatis-动态SQL-foreach

Mybatis-动态SQL-sql和include

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

看未来捏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值