宝安关于网站建设个人网站搭建模拟感想

张小明 2026/1/4 13:35:40
宝安关于网站建设,个人网站搭建模拟感想,网站变移动网站,大连建设网信息公开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进行投诉反馈,一经查实,立即删除!

网站左侧浮动代码合肥网站推广哪家好

LangFlow 草稿保存功能使用建议 在构建大语言模型(LLM)应用的实践中,开发者常常面临一个现实问题:如何在频繁调试和迭代中避免“一关机就白干”的窘境?尤其是在使用像 LangChain 这类复杂框架时,哪怕只是调…

张小明 2025/12/29 7:27:03 网站建设

深圳的网站建设公司那家好专门做任务的网站

引言 在Python编程中,字符串是最基础也是最常用的数据类型之一。[1]然而,许多开发者对于字符串的不可变性(immutability)特性理解不足,这导致了各种难以察觉的错误和性能问题。字符串的不可变性看似简单,实…

张小明 2025/12/29 7:25:01 网站建设

微网站预览优化网站性能监测

5分钟掌握ADK-Python:突破AI Agent开发瓶颈的实战指南 【免费下载链接】adk-python 一款开源、代码优先的Python工具包,用于构建、评估和部署灵活可控的复杂 AI agents 项目地址: https://gitcode.com/GitHub_Trending/ad/adk-python 你是否曾经在…

张小明 2025/12/29 7:20:58 网站建设

专门做童装的网站有哪些网上青团智慧团建官网

在数字化浪潮席卷全球的今天,信息安全已成为每个组织必须面对的核心挑战。为了帮助您系统化地应对这一挑战,我们特别推出了ISO/IEC 27005:2022英文PDF原版下载服务,为您提供权威的信息安全风险管理指南。 【免费下载链接】ISOIEC270052022英文…

张小明 2025/12/29 7:16:55 网站建设

做企业网站需要准备什么资料公司建设网站制作

一文读懂深度学习:深度学习的前世今生 作者:Weisian | AI探索者 用通俗语言拆解硬核技术,理清深度学习的前世今生 如果你用过 ChatGPT 写文案、用 Midjourney 画插画,或是惊叹于 AI 能精准识别图片里的物体、听懂你的语音指令&am…

张小明 2025/12/29 7:14:53 网站建设

个人备案网站盈利网上墓地 wordpress

简介 人工智能正深刻改变全球十大行业,包括制造、医疗、金融、交通、教育、能源、农业、零售、建筑和公共管理。AI通过数据分析、智能预测和自动化技术推动各行业向智能化、高效化方向发展,从精准医疗到自动驾驶,从个性化教育到智慧农业&…

张小明 2025/12/29 7:12:51 网站建设