【使用mybatis完成CRUD

1.什么是CRUD

C:Create(增加)
R:Retrieve(检索)
U:Update(修改)
D:Delete(删除)

2.insert

在JDBC中占位符采用的是“?”,在mybatis中使用“#{}”。
Java程序中使用Map可以给SQL语句的占位符传值:

Map<String, Object> map = new HashMap<String,Object>();
        map.put("carNum","1111");
        map.put("brand","比亚迪王");
        map.put("guidePrice","100000.0");
        map.put("produceTime","2020-11-11");
        map.put("carType","电车");
<insert id="insertCar">
        INSERT INTO t_car (id,car_num, brand, guide_price, produce_time, car_type) VALUES (null,#{carNum}, #{brand}, #{guidePrice}, #{produceTime}, #{carType});
    </insert>

注意:#{}里写map集合的key,如果key不存在,获取的是空


Java程序中使用POJO类给SQL语句的占位符传值:

Car car = new Car(null, "3333", "比亚迪王1", 30.0, "2023-11-11", "燃油车");
<insert id="insertCar">
        INSERT INTO t_car (id,car_num, brand, guide_price, produce_time, car_type) VALUES (null,#{carNum}, #{brand}, #{guidePrice}, #{produceTime}, #{carType});
</insert>

注意:如果使用POJO对象传递值的话,占位符#{}里面写pojo类的属性名对应的get方法的方法名去掉get,然后将剩下的单词首字母小写,然后方进去。
例如:getCarNum() --> #{carNum}
例如:getEmail() --> #{email}

也就是说mybatis在底层给?传值的时候,先调用pojo的get方法。例如:getCarNum(),getBrand(),getGuidePrice()。。。

3.delete

例:删除 id=20 的数据

<delete id="deleteByID">
    delete from t_car where id = #{id}
</delete>
    @Test
    public void testDeleteById(){
        SqlSession sqlSession = SqlSessionUtil.openSession();
        //执行sql
        int count = sqlSession.delete("deleteByID", 20);
        System.out.println(count);
        sqlSession.commit();
        sqlSession.close();
         
    }

注意:如果占位符只有一个,那么#{}里可以随便写,但是最好见名知意。

4.update

<update id="updateById">
        update t_car set
          car_num = #{carNum},
          brand = #{brand},
          guide_price = #{guidePrice},
          produce_time = #{produceTime},
          car_type = #{carType}
        where
          id = #{id}
    </update>
    @Test
    public void testUpdateById(){
        SqlSession sqlSession = SqlSessionUtil.openSession();
        Car car = new Car(19L, "6666", "桑塔拉", 8.0, "2013-11-11", "燃油车");
        //执行sql
        int count = sqlSession.delete("updateById", car);
        System.out.println(count);
        sqlSession.commit();
        sqlSession.close();
    }

在这里插入图片描述

5.update(根据主键id查询一条)

    <select id="selectById" resultType="com.powernode.mybatis.pojo.Car">
        select * from t_car where id = #{id}
    </select>

resultType:上面标签中这个属性用来告诉mybatis,查询结果集封装成什么类型的Java对象,对应的值一般是全限定类名。

    @Test
    public void testSelectById(){
        SqlSession sqlSession = SqlSessionUtil.openSession();
        Object car = sqlSession.selectOne("selectById",19);
        System.out.println(car);
        sqlSession.close();
    }

注意:数据库中表的字段名和对应的类中的属性名最好完全一致,如果不一致,在查询的SQL语句中可以给该出列字段起别名(和类的属性名一致)。

6.update(查询所有)

注意:resulType还是指定要封装的结果集的类型。

    <select id="selectAll" resultType="com.powernode.mybatis.pojo.Car">
        select * from t_car
    </select>
    @Test
    public void testSelectAll(){
        SqlSession sqlSession = SqlSessionUtil.openSession();
        List<Car> cars = sqlSession.selectList("selectAll");
        //遍历
        //cars.forEach(car -> System.out.println(car));
        for(Car car:cars) {
            System.out.println(car);
        }
        sqlSession.close();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值