MyBatis第二天笔记

3.1 什么是CRUD

CRUD是数据库基本操作的缩写:

  • C:Create(创建)- 对应SQL的INSERT操作
  • R:Retrieve(查询)- 对应SQL的SELECT操作
  • U:Update(更新)- 对应SQL的UPDATE操作
  • D:Delete(删除)- 对应SQL的DELETE操作

3.2 insert操作(Create)

3.2.1 基本的insert语句
xml 复制代码
<insert id="insertCar">
    insert into t_car(id,car_num,brand,guide_price,produce_time,car_type)
    values(null,'1003','丰田霸道',30.0,'2000-10-11','燃油车');
</insert>

这种写法的问题是值被硬编码到配置文件中,实际开发中需要动态传值。

3.2.2 使用占位符

MyBatis中使用#{}作为占位符,等同于JDBC中的?

xml 复制代码
<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>
3.2.3 使用Map传值
java 复制代码
// 准备数据
Map<String, Object> map = new HashMap<>();
map.put("carNum", "1111");
map.put("brand", "比亚迪汉");
map.put("guidePrice", 10.0);
map.put("produceTime", "2020-11-11");
map.put("carType", "电车");

// 获取SqlSession对象
SqlSession sqlSession = SqlSessionUtil.openSession();
// 执行SQL语句
int count = sqlSession.insert("insertCar", map);

注意事项:

  • #{}中必须填写map集合的key
  • 如果key不存在,不会报错,但会插入NULL值
  • 建议map的key命名要见名知意
3.2.4 使用POJO对象传值
java 复制代码
// 创建POJO对象
Car car = new Car(null, "3333", "比亚迪秦", 30.0, "2020-11-11", "新能源");

// 获取SqlSession对象
SqlSession sqlSession = SqlSessionUtil.openSession();
// 执行SQL语句
int count = sqlSession.insert("insertCarByPOJO", car);
xml 复制代码
<insert id="insertCarByPOJO">
    insert into t_car(id,car_num,brand,guide_price,produce_time,car_type)
    values(null,#{carNum},#{brand},#{guidePrice},#{produceTime},#{carType})
</insert>

注意事项:

  • #{}中写的是POJO类的属性名

  • 严格来说,#{}中写的是get方法名去掉get后首字母小写的结果

  • 如果属性名不存在(没有对应的getter方法),会报错:

    复制代码
    There is no getter for property named 'xyz' in 'class com.powernode.mybatis.pojo.Car'
  • MyBatis底层是通过调用getter方法获取属性值的

3.3 delete操作(Delete)

3.3.1 根据id删除数据
xml 复制代码
<delete id="deleteById">
    delete from t_car where id = #{id}
</delete>
java 复制代码
// 获取SqlSession对象
SqlSession sqlSession = SqlSessionUtil.openSession();
// 执行SQL语句
int count = sqlSession.delete("deleteById", 59);

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

3.4 update操作(Update)

3.4.1 根据id修改数据
xml 复制代码
<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>
java 复制代码
// 准备数据
Car car = new Car(4L, "9999", "凯美瑞", 30.3, "1999-11-10", "燃油车");
// 获取SqlSession对象
SqlSession sqlSession = SqlSessionUtil.openSession();
// 执行SQL语句
int count = sqlSession.update("updateById", car);

3.5 select操作(Retrieve)

3.5.1 查询单条数据
xml 复制代码
<select id="selectById" resultType="com.powernode.mybatis.pojo.Car">
    select * from t_car where id = #{id}
</select>
java 复制代码
// 获取SqlSession对象
SqlSession sqlSession = SqlSessionUtil.openSession();
// 执行SQL语句
Object car = sqlSession.selectOne("selectById", 1);

注意事项:

  • select标签必须指定resultType属性,用于告诉MyBatis查询结果封装成什么类型的Java对象
  • resultType通常写全限定类名
3.5.2 列名和属性名不一致的问题

当数据库表的列名与Java类的属性名不一致时,查询结果无法正确映射,例如:

数据库列名:car_num, guide_price, produce_time, car_type

类的属性名:carNum, guidePrice, produceTime, carType

解决方案是使用SQL别名:

xml 复制代码
<select id="selectById" resultType="com.powernode.mybatis.pojo.Car">
    select
        id,car_num as carNum,brand,guide_price as guidePrice,
        produce_time as produceTime,
        car_type as carType
    from
        t_car
    where
        id = #{id}
</select>
3.5.3 查询多条数据
xml 复制代码
<select id="selectAll" resultType="com.powernode.mybatis.pojo.Car">
    select
        id,car_num as carNum,brand,guide_price as guidePrice,
        produce_time as produceTime,
        car_type as carType
    from
        t_car
</select>
java 复制代码
// 获取SqlSession对象
SqlSession sqlSession = SqlSessionUtil.openSession();
// 执行SQL语句
List<Object> cars = sqlSession.selectList("selectAll");

注意:

  • resultType指定的是List集合中元素的类型,而不是List类型
  • selectList方法会自动返回List集合

3.6 namespace的作用

namespace是SQL Mapper配置文件中<mapper>标签的一个属性,用于防止sqlId冲突。

xml 复制代码
<mapper namespace="car">
    <select id="selectAll" resultType="com.powernode.mybatis.pojo.Car">
        select * from t_car
    </select>
</mapper>

当有多个Mapper文件中存在相同id的SQL语句时,需要使用namespace进行区分:

java 复制代码
// 完整写法:namespace.id
List<Object> cars = sqlSession.selectList("car.selectAll");

实际上,MyBatis中sqlId的完整写法是:namespace.id

相关推荐
唐僧洗头爱飘柔95271 分钟前
【SSM-SSM整合】将Spring、SpringMVC、Mybatis三者进行整合;本文阐述了几个核心原理知识点,附带对应的源码以及描述解析
java·spring·mybatis·springmvc·动态代理·ioc容器·视图控制器
moxiaoran57532 小时前
uni-app学习笔记五-vue3响应式基础
笔记·学习·uni-app
饕餮争锋2 小时前
org.slf4j.MDC介绍-笔记
java·开发语言·笔记
weixin_448119942 小时前
Datawhale 5月llm-universe 第1次笔记
笔记
Python ml2 小时前
Tomcat与纯 Java Socket 实现远程通信的区别
java·开发语言·tomcat
chennalC#c.h.JA Ptho3 小时前
lubuntu 系统详解
linux·经验分享·笔记·系统架构·系统安全
m0_689618284 小时前
从海洋生物找灵感:造个机器人RoboPteropod,它能在水下干啥?
笔记·机器人
龙湾开发5 小时前
轻量级高性能推理引擎MNN 学习笔记 02.MNN主要API
人工智能·笔记·学习·机器学习·mnn
铁锚5 小时前
一个WordPress连续登录失败的问题排查
java·linux·服务器·nginx·tomcat
生命不息战斗不止(王子晗)6 小时前
mybatis中${}和#{}的区别
java·服务器·tomcat