【SSM】MyBatis(二.使用MyBatis完成CRUD)
创始人
2025-05-30 14:52:35
0

文章目录

  • 1.前期准备
  • 2.insert
    • 2.1 完成insert使用Map集合传参
    • 2.2 完成insert使用Pojo传参
  • 3.delete
  • 4.update
  • 5.retrieve
    • 5.1 查一个
    • 5.2 查所有
  • 6.关于SQL Mapper的namespace

1.前期准备

添加依赖

org.mybatismybatis3.5.10
mysqlmysql-connector-java5.1.37
ch.qos.logbacklogback-examples1.2.11
junitjunit4.13.2test

引入之前文件
在这里插入图片描述

2.insert

2.1 完成insert使用Map集合传参

CarMapper.xml

    insert into t_car(id, car_num, brand, guide_price, produce_time, car_type)values (null, #{car_num}, #{brand}, #{guide_price}, #{produce_time}, #{car_type})

测试

    @Testpublic void testInsertCar(){SqlSession sqlSession = SqlSessionUtil.openSession();Map map = new HashMap<>();map.put("car_num", 1111);map.put("brand", "比亚迪汉");map.put("guide_price", 10.0);map.put("produce_time", 2020-11-11);map.put("car_type", "电车");int count = sqlSession.insert("insertCar", map);System.out.println(count);sqlSession.commit();sqlSession.close();}

2.2 完成insert使用Pojo传参

Car.java

package com.sdnu.mybatis.pojo;public class Car {private Long id;private String carNum;private String brand;private Double guidePrice;private String produceTime;private String carType;public Car() {}public Car(Long id, String carNum, String brand, Double guidePrice, String produceTime, String carType) {this.id = id;this.carNum = carNum;this.brand = brand;this.guidePrice = guidePrice;this.produceTime = produceTime;this.carType = carType;}public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getCarNum() {return carNum;}public void setCarNum(String carNum) {this.carNum = carNum;}public String getBrand() {return brand;}public void setBrand(String brand) {this.brand = brand;}public Double getGuidePrice() {return guidePrice;}public void setGuidePrice(Double guidePrice) {this.guidePrice = guidePrice;}public String getProduceTime() {return produceTime;}public void setProduceTime(String produceTime) {this.produceTime = produceTime;}public String getCarType() {return carType;}public void setCarType(String carType) {this.carType = carType;}@Overridepublic String toString() {return "Car{" +"id=" + id +", carNum='" + carNum + '\'' +", brand='" + brand + '\'' +", guidePrice=" + guidePrice +", produceTime='" + produceTime + '\'' +", carType='" + carType + '\'' +'}';}
}

CarMapper.xml

    insert into t_car(id, car_num, brand, guide_price, produce_time, car_type)values (null, #{carNum}, #{brand}, #{guidePrice}, #{produceTime}, #{carType})

测试类

    @Testpublic void testInsertCarByPojo(){SqlSession sqlSession = SqlSessionUtil.openSession();Car car = new Car(null, "1001", "比亚迪泰", 12.00, "2000-02-09", "电车");int count = sqlSession.insert("insertCar", car);System.out.println(count);sqlSession.commit();sqlSession.close();}

使用POJO对象传递值的话,#{}这个大括号中写的是get方法的方法名去掉get,然后将剩下的单词首字母小写,然后放进去。

 例如:getUsername() --> #{username}例如:getEmail() --> #{email}

3.delete

CarMapper.xml

    delete from t_car where id = #{id}

测试类

    @Testpublic void testDeleteCar(){SqlSession sqlSession = SqlSessionUtil.openSession();int count = sqlSession.delete("deleteById", 10);System.out.println(count);sqlSession.commit();sqlSession.close();}

4.update

CarMapper.xml

    update t_car setcar_num=#{carNum},brand=#{brand},guide_price=#{guidePrice},produce_time=#{produceTime},car_type=#{carType}whereid=#{id}

测试类

    @Testpublic void updateCarById(){SqlSession sqlSession = SqlSessionUtil.openSession();Car car = new Car(4L, "11111", "奔驰", 112.00, "2020-09-09", "燃油车");int count = sqlSession.update("updateById",car);System.out.println(count);sqlSession.commit();sqlSession.close();}

5.retrieve

5.1 查一个

CarMapper.xml

    

测试类

    @Testpublic void selectById(){SqlSession sqlSession = SqlSessionUtil.openSession();Object car = sqlSession.selectOne("selectById", 3);System.out.println(car);sqlSession.commit();sqlSession.close();}

在这里插入图片描述

5.2 查所有

CarMapper.xml

    

测试类

    @Testpublic void  selectAll(){SqlSession sqlSession = SqlSessionUtil.openSession();List car = sqlSession.selectList("selectAll");for (Car car1 : car){System.out.println(car1);}sqlSession.close();}

在这里插入图片描述

6.关于SQL Mapper的namespace

在SQL Mapper配置⽂件中标签的namespace属性可以翻译为命名空间,这个命名空间主要是为了防⽌sqlId冲突的。

相关内容

热门资讯

家电新动能:国补与出海共振 家... 2024年下半年尤其是四季度以后,家用电器以旧换新等国补政策显著提升了家电上市公司业绩,2025年一...
政策端持续释放积极信号 支持提... 政策端不断释放积极信号,全力支持稳定资本市场,进一步明确做强做优的目标。在保持稳定的基础上,增强市场...
翠微股份:公司近年零售业务和收... 6月3日,北京翠微大厦股份有限公司(翠微股份,603123.SH)发布股票交易风险提示公告,公司股票...
6月券商金股出炉,海大集团最受... 6月首个交易日,A股市场全天震荡反弹,三大指数小幅上涨。截至收盘,沪指涨0.43%,深成指涨0.16...
步长制药:控股子公司撤回药品临... 新京报贝壳财经讯 6月3日,步长制药(603858.SH)公告称,近日,公司控股子公司浙江天元收到国...
秦洪看盘|配置型资金发力,A股... 李晶昀 AI图周二A股市场出现了震荡回稳的态势。其中,得益于银行股的再度强势,上证综指、上证50指数...
V观财报|五连板德邦股份:暂未...   中新经纬6月3日电 6月3日,连续五日涨停的德邦物流股份有限公司(下称“德邦股份”)发布股票交易...
干细胞移植到底是不是美容保健的... 近日,悉达多·穆克吉(印度裔美国医生、科学家和作家,以其对癌症研究的贡献以及他的畅销书而闻名)的《癌...
优必选创始人周剑成为腾势N9第... 近日,腾势汽车在深圳全球总部举行腾势N9第10000辆交付仪式,比亚迪集团总裁王传福亲自操刀交付仪式...
远东股份:股东国远投资拟减持不... 6月3日,远东智慧能源股份有限公司(远东股份,600869.SH)公告,因其自身资金需求,公司股东宜...
“苏超”火了!金陵体育直线拉升... “友谊第一,比赛第二”等等!重来!“友谊第一,比赛第十四!”不对!再来!“比赛第一,友谊第十四”图片...
盘中涨停!中科金财回应布局稳定... 6月3日,中科金财(002657.SZ)股价盘中快速拉升,并一度触及涨停。截至当日收盘,中科金财股价...
人形机器人量产颠覆职场:谁会被... 本文来自微信公众号:沈素明,作者:非职能管理专家,原文标题:《人形机器人量产颠覆职场:谁会被替代?企...
深物业A副总经理陈鸿基涉嫌职务... 6月2日,深物业A(000011.SZ)突发公告,公司副总经理陈鸿基因涉嫌职务违法被深圳市监察委员会...
拒绝自我关闭,不听人类命令的A... 第一,机器人不得伤害人类个体,或因不作为使人类受到伤害。第二,机器人必须服从人类命令,除非该命令与第...
沪农商行涨停!银行股集体走强,... 南方财经全媒体记者 吴霜 上海报道6月3日,A股银行板块涨幅居前,多数成份股飘红,沪农商行、渝农商行...
口号喊得响亮,但从数据层面来看... 日前,在A股消费板块的整体增长下,主营啤酒业务的乐惠国际股价也迎来一波涨停。行情显示,5月26日至2...
疯炒泡泡玛特,股市最大的博傻游... 2025年,随着电影《哪吒2》票房的大爆,哪吒无疑成了中国国内最火的IP。《哪吒2》的票房,绝大部分...
舒泰神:注射用STSP-060... 6月3日,舒泰神(北京)生物制药股份有限公司(舒泰神,300204.SZ)公告,公司子公司江苏贝捷泰...
国补激活换新消费,618第一周... 618大促过半,各大电商平台第一周期“成绩单”出炉。5月30日,研究机构易观发布《2025年“618...