关键词搜索挖掘爱网站站长工具查询

张小明 2026/1/1 14:28:28
关键词搜索挖掘爱网站,站长工具查询,网站建设这门课,各大网站收录提交入口MyBatis实战教程#xff1a;使用Map与POJO类实现CRUD操作详解本文将通过实际案例#xff0c;详细讲解在MyBatis中如何使用Map集合和POJO类两种方式实现数据库的增删改查操作#xff0c;解决常见映射问题#xff0c;提高开发效率。一、MyBatis简介与CRUD基础MyBatis是一款优…MyBatis实战教程使用Map与POJO类实现CRUD操作详解本文将通过实际案例详细讲解在MyBatis中如何使用Map集合和POJO类两种方式实现数据库的增删改查操作解决常见映射问题提高开发效率。一、MyBatis简介与CRUD基础MyBatis是一款优秀的持久层框架它支持自定义SQL、存储过程以及高级映射。MyBatis避免了几乎所有的JDBC代码和手动设置参数以及获取结果集的工作。1.1 传统JDBC的问题在传统JDBC编程中我们经常遇到以下问题java// JDBC中繁琐的结果集处理 while(rs.next()) { User user new User(); user.setId(rs.getString(id)); user.setIdCard(rs.getString(idCard)); user.setUsername(rs.getString(username)); // ... 需要为每个属性手动赋值 userList.add(user); }这种方式存在以下缺点代码冗余每个字段都需要手动映射容易出错字段名写错会导致运行时错误维护困难表结构变化时需要修改大量代码二、MyBatis中使用Map实现CRUD2.1 使用Map进行数据插入Map集合提供了灵活的键值对存储方式适合不确定参数数量或临时性的数据操作。2.1.1 基础Map传参javaTest public void testInsertCarByMap() { SqlSession sqlSession SqlSessionUtil.openSession(); // 使用Map封装前端传递的数据 MapString, Object map new HashMap(); map.put(carNum, 11111); map.put(brand, 比亚迪汉); map.put(guidePrice, 10.0); map.put(produceTime, 2020-11-11); map.put(carType, 电车); // 执行SQL插入操作 int count sqlSession.insert(insertCar, map); System.out.println(插入记录数 count); sqlSession.commit(); sqlSession.close(); }2.1.2 Mapper XML配置xml!-- CarMapper.xml -- ?xml version1.0 encodingUTF-8? !DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//EN http://mybatis.org/dtd/mybatis-3-mapper.dtd mapper namespacecom.example.mapper.CarMapper insert idinsertCar insert into t_car(id, car_num, brand, guide_price, produce_time, car_type) values(null, #{carNum}, #{brand}, #{guidePrice}, #{produceTime}, #{carType}) /insert /mapper2.1.3 注意事项键名匹配原则#{}中的名称必须与Map中的key完全一致空值处理如果key不存在对应的值将为null命名规范建议使用有意义的key名称提高代码可读性2.2 使用Map进行数据查询javaTest public void testSelectByMap() { SqlSession sqlSession SqlSessionUtil.openSession(); MapString, Object paramMap new HashMap(); paramMap.put(minPrice, 10.0); paramMap.put(brand, 宝马); ListMapString, Object result sqlSession.selectList(selectCarByCondition, paramMap); for (MapString, Object car : result) { System.out.println(car); } sqlSession.close(); }三、MyBatis中使用POJO类实现CRUD3.1 POJO类定义java// Car.java 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; } // Getter和Setter方法 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; } // ... 其他getter和setter }3.2 使用POJO进行数据插入javaTest public void testInsertCarByPOJO() { SqlSession sqlSession SqlSessionUtil.openSession(); // 使用POJO对象封装数据 Car car new Car(null, 3333, 比亚迪秦, 30.0, 2020-11-11, 新能源); // 执行SQL int count sqlSession.insert(insertCar, car); System.out.println(插入记录数 count); sqlSession.commit(); sqlSession.close(); }3.3 POJO传参原理重要规则#{}占位符中的名称对应的是POJO类的getter方法名去掉get并将首字母小写后的名称。例如getCarNum()→#{carNum}getGuidePrice()→#{guidePrice}getUsername()→#{username}3.4 使用POJO进行数据查询3.4.1 查询单个对象xml!-- 根据id查询汽车信息 -- select idselectById resultTypecom.example.pojo.Car select * from t_car where id #{id} /selectjavaTest public void testSelectById() { SqlSession sqlSession SqlSessionUtil.openSession(); // 查询单个对象 Car car sqlSession.selectOne(selectById, 1); System.out.println(car); sqlSession.close(); }3.4.2 查询所有记录xml!-- 查询所有汽车信息 -- select idselectAll resultTypecom.example.pojo.Car select id, car_num as carNum, brand, guide_price as guidePrice, produce_time as produceTime, car_type as carType from t_car /selectjavaTest public void testSelectAll() { SqlSession sqlSession SqlSessionUtil.openSession(); // 查询所有记录返回List集合 ListCar carList sqlSession.selectList(selectAll); for (Car car : carList) { System.out.println(car); } sqlSession.close(); }四、解决字段名与属性名映射问题4.1 问题现象当数据库字段名与Java类属性名不一致时会出现属性值为null的情况sql-- 数据库表结构 ----------------------------------------------------------------- | id | car_num | brand | guide_price | produce_time | car_type | ----------------------------------------------------------------- | 1 | 1001 | 宝马520Li | 10.00 | 2020-10-11 | 燃油车 | ----------------------------------------------------------------- -- 直接查询会导致Car对象的carNum、guidePrice等属性为null -- 因为数据库字段是car_num而Java属性是carNum4.2 解决方案方案一SQL中使用AS别名推荐xmlselect idselectById resultTypecom.example.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方案二配置驼峰命名自动映射在MyBatis配置文件中开启驼峰命名自动映射xml!-- mybatis-config.xml -- configuration settings !-- 开启驼峰命名自动映射 -- setting namemapUnderscoreToCamelCase valuetrue/ /settings /configuration开启后MyBatis会自动将下划线命名的数据库字段映射到驼峰命名的Java属性car_num→carNumguide_price→guidePriceproduce_time→produceTime方案三使用resultMap自定义映射xmlresultMap idcarResultMap typecom.example.pojo.Car id propertyid columnid/ result propertycarNum columncar_num/ result propertybrand columnbrand/ result propertyguidePrice columnguide_price/ result propertyproduceTime columnproduce_time/ result propertycarType columncar_type/ /resultMap select idselectById resultMapcarResultMap select * from t_car where id #{id} /select五、Map与POJO对比与选择5.1 使用场景对比特性Map集合POJO类灵活性高适合动态参数低结构固定类型安全低运行时才能发现错误高编译时检查代码可读性低需要查看Map键名高属性明确IDE支持有限无法自动提示好有代码提示适合场景临时查询、参数不固定业务实体、固定结构5.2 最佳实践建议业务实体操作使用POJO用户、订单、商品等核心业务实体需要频繁操作和传递的数据有利于代码维护和重构临时查询使用Map动态条件查询参数不固定统计报表等临时性数据操作快速原型开发阶段混合使用策略java// 示例使用Map封装查询条件返回POJO列表 public ListCar findCars(MapString, Object params) { return sqlSession.selectList(findCarsByCondition, params); }六、完整示例与总结6.1 完整Mapper示例xml?xml version1.0 encodingUTF-8? !DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//EN http://mybatis.org/dtd/mybatis-3-mapper.dtd mapper namespacecom.example.mapper.CarMapper !-- 插入操作 -- insert idinsertCar insert into t_car(id, car_num, brand, guide_price, produce_time, car_type) values(null, #{carNum}, #{brand}, #{guidePrice}, #{produceTime}, #{carType}) /insert !-- 根据ID查询 -- select idselectById resultTypecom.example.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 !-- 查询所有 -- select idselectAll resultTypecom.example.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 !-- 更新操作 -- update idupdateCar update t_car set car_num #{carNum}, brand #{brand}, guide_price #{guidePrice}, produce_time #{produceTime}, car_type #{carType} where id #{id} /update !-- 删除操作 -- delete iddeleteById delete from t_car where id #{id} /delete /mapper6.2 关键点总结#{}与${}的区别#{}是预编译处理防止SQL注入${}是字符串替换有SQL注入风险resultType与resultMapresultType自动映射要求字段名与属性名一致或配置别名resultMap自定义映射处理复杂映射关系占位符命名规则Map传参#{}中写Map的keyPOJO传参#{}中写getter方法对应的属性名性能优化建议尽量使用POJO享受编译时检查的好处复杂查询考虑使用resultMap提高可读性批量操作使用批量API提高性能通过本文的学习你应该掌握了MyBatis中使用Map和POJO实现CRUD操作的核心技术。在实际开发中根据具体场景选择合适的方式既能提高开发效率又能保证代码质量。
版权声明:本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!

克拉玛依市建设局网站网络营销推广方案整合

你是否曾经面对复杂的程序崩溃束手无策?是否想要深入了解软件运行的内在逻辑?x64dbg调试器就是你的终极解决方案!作为Windows平台最强大的开源调试工具,它不仅能帮你快速定位程序问题,还能让你轻松掌握逆向工程的核心技…

张小明 2025/12/24 8:52:02 网站建设

玩具 东莞网站建设 技术支持什么网站简单

终极指南:5个简单步骤掌握Dokploy容器化部署 【免费下载链接】dokploy Open Source Alternative to Vercel, Netlify and Heroku. 项目地址: https://gitcode.com/GitHub_Trending/do/dokploy 你是否正在寻找一个简单易用的容器化部署平台来替代Vercel、Netl…

张小明 2025/12/24 8:50:58 网站建设

wp网站建设网站调用115做云播

anything-llm中文支持现状与优化方案探讨 在企业知识管理日益依赖AI的今天,越来越多团队开始尝试将大语言模型(LLM)落地到内部系统中。然而,当面对中文文档时,许多看似强大的开源RAG应用却频频“翻车”:提问…

张小明 2025/12/24 8:49:55 网站建设

做网站总结体会php网站的推广方式

pyvideotrans:免费开源的视频翻译终极解决方案 【免费下载链接】pyvideotrans Translate the video from one language to another and add dubbing. 将视频从一种语言翻译为另一种语言,并添加配音 项目地址: https://gitcode.com/gh_mirrors/py/pyvid…

张小明 2025/12/24 8:47:50 网站建设

湖北可以做网站的公司开发公司电梯前室只给一楼吊顶

这个方法是设备状态巡检的核心线程控制逻辑,实现了 “安全重启旧线程、循环执行巡检、优雅终止、动态参数适配、异常容错” 的完整能力,以下是逐模块拆解:一、核心功能定位启动 / 重启后台巡检线程,定时向串口设备发送「状态查询指…

张小明 2025/12/24 8:44:41 网站建设

网站建设主要问题网站seo快速排名优化

一、明确核心定位:发挥Java的工程化优势 1. 专注大模型能力封装与集成 Java程序员的核心竞争力在于企业级系统架构能力。应重点将大模型作为“工具”嵌入现有业务系统,例如: 用Spring Boot封装大模型API为微服务,供其他模块调用…

张小明 2025/12/24 8:43:38 网站建设