web第10次作业——mybatis实现数据库的增删改查操作

        1.删除操作

@Mapper
public interface EmpMapper {

    //删除操作
    //#可以动态获取数据
    @Delete("delete from emp where id=#{id}")
    public void delete(Integer id);
}

        首先我们在idea中的Mapper接口中新建一个类叫delete类,作用是根据数据表的主键来删除数据表中的元素,主要依靠delete from emp where id=#{id}这一条sql语句实现。

@SpringBootTest
class SpringProject06Database02ApplicationTests {

    @Autowired
    private EmpMapper empMapper;

    @Test
    public void testDeleteEmp() {
        empMapper.delete(16);
    }
}

        接着在测试中条用empdelete接口,并将16作为整数型参数传递进来,从而实现数据的删除。

        如果想在idea中看到代码的具体实现过程,可以在properties文件中加入显示日志到控制台的操作,具体代码块为

mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

        这样在新运行一个代码块后我们可以在控制台发现他的操作路径,例如我们要将emp数据表的一项数据进行删除,我们可以看见控制台输出了这样的信息。

        而在代码中我们采用#+{}的形式来动态获取数据表的主键,因此相对应的在控制台中,idea会使用?来占用一个数据的位置,当我们传进来的参数是16时,idea就会将?替换成16去数据表中寻找数据。这也就是预编译sql方法,可以提高数据安全性和效率。

        以上操作参考黑马程序员视频,接下来先进行自己的举一反三操作,有报错再根据视频学习。

        2.增加操作

@Insert("INSERT INTO emp(username,password,name,gender,image,job,entrydate,dept_id,create_time,update_time) values (#{username},#{password},#{name},#{gender},#{image},#{job},#{entrydate},#{dept_id},#{create_time},#{update_time})")
    public void insert(String username, String password, String name, short gender, String image, short job, LocalDate entrydate, int dept_id, LocalDateTime create_time, LocalDateTime update_time);


@Test
    public void testInsertEmp() {
        empMapper.insert("Yu33x","123456","Ruby",(short) 2,"1.jpg",(short)1, LocalDate.of(2023,6,1),1, LocalDateTime.now(),LocalDateTime.now());
    }

        我采用这样直接的方法也可以运行成功,但是弊端是显而易见的,首先就是在插入内容时要输入的数据实在是太多了,做起来非常痛苦,正常的应该不是这样。果然回看视频发现,我们可以将这所有的东西封装在一个类当中,到时候直接传类就行了。

@Insert("INSERT INTO emp(username,password,name,gender,image,job,entrydate,dept_id,create_time,update_time) values (#{username},#{password},#{name},#{gender},#{image},#{job},#{entrydate},#{deptId},#{createTime},#{updateTime})")
    public void insert(Emp emp);


@Test
    public void testInsertEmp() {

        Emp emp = new Emp();
        emp.setUsername("Yux");
        emp.setName("Jennie");
        emp.setPassword("123456");
        emp.setImage("image.jpg");
        emp.setGender((short)2);
        emp.setEntrydate(LocalDate.of(2023,6,1));
        emp.setDeptId(1);
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());

        empMapper.insert(emp);
    }

        果然,这样就方便的多了,至少在敲代码时还有idea给的提示。

        3.修改操作

@Update("UPDATE emp set username=#{username},password=#{password},name=#{name},gender=#{gender},image=#{image},job=#{job},dept_id=#{deptId},update_time=#{updateTime} WHERE id=#{id}")
    public void update(Emp emp);

@Test
    public void testUpdateEmp() {

        Emp emp = new Emp();
        emp.setId(19);
        emp.setUsername("Yux1");
        emp.setName("Jenny");
        emp.setPassword("123456");
        emp.setImage("image.jpg");
        emp.setGender((short)2);
        emp.setDeptId(1);
        emp.setUpdateTime(LocalDateTime.now());

        empMapper.update(emp);
    }

        原理同上

        4.查询操作

@Select("SELECT * from emp where id=#{id}")
    public Emp select(Integer id);

@Test
    public void testSelectEmp() {

        Emp emp=empMapper.select(19);
        System.out.println(emp);
    }

        验证成功,操作台也显示了相应的数据

        同时我们也可以看到代码执行的过程,但有一些属性是null值,原因是实体类的属性名与数据表的字段名不符合,所以无法进行封装。相应的也有几个解决方案:

  1. 给字段起别名
  2. 通过@Result手动封装
  3. 开启mybatis驼峰命名自动映射

第三种我认为是最方便的,方法是在properties文件中插入

mybatis.configuration.map-underscore-to-camel-case=true

这样mybatis就可以自动的给我们转化成驼峰命名法

//用此方法无法获取完整数据
//    @Select("SELECT * from emp where id=#{id}")
//    public Emp select(Integer id);

    //方法二
//    @Results({
//            @Result(column = "dept_id",property = "deptId"),
//            @Result(column = "create_time",property = "createTime"),
//            @Result(column = "update_time",property = "updateTime")
//    })
//    @Select("select * from emp where id=#{id}")
//    public Emp select(Integer id);

    //方法三:开启自动驼峰命名
    @Select("SELECT * from emp where id=#{id}")
    public Emp select(Integer id);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值