MyBatis初阶
概述mybatis就是使用注释就能够完成JDBC需要大量做的事情但终归会有不能SQL语句,参数赋值,结果映射Mapper public interface UserInfoMapper { Select(select * from user_info) ListUserInfo selectAll(); }此时需要测试一下是否成功只要有mybatis依赖,就需要添加数据库信息基础操作打印日志打印出来的信息可以比作调试,能够看到sql语句的执行、执行传递的参数以及执行结果.在配置文件中进行配置mybatis: configuration: # 配置打印 MyBatis⽇志 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl参数传递以及赋值方法中的参数传递到此方法之后,使用#{}获取方法中的参数,进行赋值Select(select * from user_info where id #{id}) ListUserInfo selectById(Integer id);参数重命名#{}必须和Param中的名字一致Select(select * from user_info where id #{id} and age #{age}) ListUserInfo selectByIdAndAge(Param(id) Integer id, Integer age);增Insert(insert into user_info (username,password,age,gender) values (#{username},#{password},#{age},#{gender})) Integer insertUser(UserInfo userInfo);//添加Param注释之后userInfo就不能自动赋值了,生成参数 Insert(insert into user_info (username,password, age ,gender) values (#{uerInfo.username},#{userInfo.password},#{userInfo.age},#{userInfo.gender})) Integer insertUser2(Param(userInfo) UserInfo userInfo);返回主键//插入想要得到id,keyProperty需要一个属性来接收 Options(useGeneratedKeys true,keyProperty id) Insert(insert into user_info (username,password,age,gender) values (#{username},#{password},#{age},#{gender})) Integer insertUser(UserInfo userInfo);删Delete(delete from user_info where id#{id}) Integer deleteById(Integer id);改Update(update user_info set username#{username} where id#{id}) void updateUsername(String username,Integer id);查使用SQL语句查询时,通过测试方法发现, 发现有一些字段没有进行赋值,只有Java对象属性和数据库字段一模一样时,才会赋值,那么我们通过什么方法才能够赋值?对SQL语句起别名// Select(select id,username,password,age,gender,phone,delete_flag as deleteFlag, // create_time as createTime,update_time as updateTime // from user_info)结果映射数据库查询到的结果与java对象属性进行映射Results({ Result(column delete_flag ,property deleteFlag), Result(column create_time ,property createTime), Result(column update_time ,property updateTime) }) Select(select id, username, password, age, gender, phone, delete_flag, create_time, update_time from user_info) ListUserInfo selectAll();如果其他SQL也想复用这个映射关系就需要:Results(id resultMap,value { Result(column delete_flag ,property deleteFlag), Result(column create_time ,property createTime), Result(column update_time ,property updateTime) }) Select(select id, username, password, age, gender, phone, delete_flag, create_time, update_time from user_info) ListUserInfo selectAll(); ResultMap(value resultMap) Select(select * from user_info where id #{id}) ListUserInfo selectById(Integer id);开启驼峰命名数据库一般时蛇形命名,而Java属性一般通过驼峰命名,怎么才能够自动启动映射呢?D:在配置文件添加mybatis: configuration: map-underscore-to-camel-case: true #配置驼峰⾃动转换MyBatisXMl配置文件mybatis开发有两种方式:1.使用注解2.使用XML配置文件MyBatis XML方式需要两步1.配置连接字符串(和注解的配置文件一样)和MyBatismybatis: mapper-locations: classpath:mybatis/**Mapper.xml2.写持久层代码方法定义:interfaceMapper public interface UserInfoXmlMapper { ListUserInfo selectAll(); ListUserInfo selectById(Integer id); ListUserInfo selectByIdAndGender(Integer id,Integer gender); Integer insertUser(UserInfo userInfo); Integer insertUser2(Param(userInfo) UserInfo userInfo); Integer deleteById(Integer id); void updateUsername(String username,Integer id); }方法实现:XXX.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.Kon.mybatis.mapper.UserInfoXmlMapper !--只有select需要对象映射,resultType -- select idselectAll resultTypecom.Kon.mybatis.entity.UserInfo select id, username, password, age, gender, phone, delete_flag, create_time, update_time from user_info /select select idselectById resultTypecom.Kon.mybatis.entity.UserInfo select * from user_info where id #{id} /select select idselectByIdAndGender resultTypecom.Kon.mybatis.entity.UserInfo select * from user_info where id #{id} and gender #{gender} /select insert idinsertUser useGeneratedKeystrue keyPropertyid insert into user_info (username,password,age,gender) values (#{username},#{password},#{age},#{gender}) /insert insert idinsertUser2 insert into user_info (username,password,age,gender) values (#{userInfo.username},#{userInfo.password},#{userInfo.age},#{userInfo.gender}) /insert delete iddeleteById delete from user_info where id#{id} /delete update idupdateUsername update user_info set username#{username} where id#{id} /update /mapperXML起别名和注释是非常相似的#{}和${}的区别#{}是预编译SQL,${}是即时SQL性能即时SQL每次都需要进行SQL语法解析、SQL优化、SQL编译,而预编译SQL再进行编译参数传递为String时,#{}会加引号,而${}不会安全使用${}可能会导致SQL注入SQL注入:通过改变事先预定好的参数的SQL语句,以达到执行代码对服务器进行攻击的方法select * from user_info where username or 1 1即时SQL是将参数进行拼接,而预编译SQL就已经确定这个位置应该放入哪些具体的数据排序功能${}还是有一些用处的,此处的${order}是不需要加引号的,因为排序不需要加 Select(select * from user_info order by id ${order}) ListUserInfo selectUserByOrder(String order);有 $ 的地方就会有SQL注入的问题,解决办法:规定只能有asc或者desc,定义为枚举就可以了Like查询使用#{}查不出来自动会加引号,但使用${}可以查出来但是依旧会有SQL注入的问题,那么我们就使用mysql的内置函数来解决Select(select * from user_info where username like concat(%,#{key},%)) ListUserInfo queryAllUserByLike(String key);数据库连接池用户多次创建和销毁连接会消耗比较多的资源,那么数据库连接池会提前创建多个连接,也就是多个连接对象,用户请求连接时,将连接对象connection获取给用户,SQL语句执行完之后再还给数据库连接池如果要切换连接池,只需要引入相关依赖即可

相关新闻

Claude中的表格怎么导出?别崩溃了,AI导出鸭一键搞定乱码与错乱,真香警告!

Claude中的表格怎么导出?别崩溃了,AI导出鸭一键搞定乱码与错乱,真香警告!

Claude中的表格怎么导出?AI导出鸭实测:乱码终结者,数据迁徙竟如此硬核 痛点驱动:当AI输出遇见“数据沼泽” 作为技术架构师,我曾在深夜被研发同事的哀嚎炸醒——“Claude给的表格,复制到Excel全是乱码&…

2026/11/2 9:07:49 阅读更多 →
淘系应用抓包技术:突破SSL Pinning与加密校验

淘系应用抓包技术:突破SSL Pinning与加密校验

1. 淘系应用抓包的核心挑战与解决思路淘宝/天猫等淘系应用作为国内头部电商平台,其客户端采用了多重防护机制来防止数据被轻易抓包分析。经过实测,淘系App主要存在以下三类防护手段:SSL Pinning证书绑定:应用内置了官方证书指纹验…

2026/11/2 6:55:48 阅读更多 →
ChatGPT中的表格怎么导出?AI导出鸭实测:3秒拯救乱码公式,Markdown排版稳如磐石

ChatGPT中的表格怎么导出?AI导出鸭实测:3秒拯救乱码公式,Markdown排版稳如磐石

ChatGPT中的表格怎么导出?AI导出鸭实测:3秒拯救乱码公式,Markdown排版稳如磐石技术架构师硬核测评:四种主流方案横向对比,白皮书数据专家QA用户实测,破解结构化数据流转痛点一、痛点驱动:当“复…

2026/11/3 13:23:15 阅读更多 →

最新新闻

Electron桌面应用开发实战:从环境搭建到打包发布全流程

Electron桌面应用开发实战:从环境搭建到打包发布全流程

做前端这些年,我大部分时间都在跟浏览器标签页打交道,直到第一次把桌面应用跑起来,才发现原来自己一直被限制在地址栏和刷新按钮围起来的框框里。Electron这个词,最开始是在某段时间频繁出现在同事的讨论里,一个用HTML…

2026/11/4 1:35:24 阅读更多 →
滑盖智能锁T109深度解析:结构原理、安装调试与故障排查

滑盖智能锁T109深度解析:结构原理、安装调试与故障排查

简介:亚太天能T109滑盖智能锁产品说明书是一份PDF格式的官方技术文档,主要面向智能锁安装人员、终端用户及售后维护者,帮助快速掌握产品安装、配置与常见故障处理。全包仅1个PDF文件,大小4.37MB,内容结构清晰&#xff…

2026/11/5 1:08:50 阅读更多 →
PMSM弱磁控制原理与Simulink建模:高速AGV驱动系统设计指南

PMSM弱磁控制原理与Simulink建模:高速AGV驱动系统设计指南

上个月我在调试一台高速AGV的驱动系统,整车要求空载跑到8m/s,电机转速已经拉到额定点,驱动器的输出电压也顶到了母线电压极限附近。再往上踩速度指令,转速纹丝不动,三相电流反而开始震荡。那会儿我的第一反应就是&…

2026/11/3 11:35:49 阅读更多 →
C++核心知识点:从内存管理到STL的实战指南

C++核心知识点:从内存管理到STL的实战指南

C 这门语言我前前后后折腾了十几年,用它写过嵌入式驱动、做过游戏服务端,也搭过基础组件库。每次看到新人一头扎进"C 核心知识点"这个搜索词,我其实挺感慨的——这门语言的难点从来不是语法多,而是知识点之间的耦合太深…

2026/11/4 6:23:50 阅读更多 →
T109滑盖智能锁怎么装?安装调试与故障排查全攻略

T109滑盖智能锁怎么装?安装调试与故障排查全攻略

简介:亚太天能T109滑盖智能锁产品说明书以PDF格式发布,适合智能锁用户、安装人员与售后维修人员阅读。内容围绕T109系列指纹密码刷卡锁展开,先介绍前面板、后面板、锁体、机械钥匙、电池盒盖等部件名称与功能,再给出完整安装步骤&…

2026/11/5 0:54:28 阅读更多 →
LLM知识降熵:从信息垄断到理解权下放

LLM知识降熵:从信息垄断到理解权下放

你有没有过这种经历:翻开源码注释、论文摘要或者一份专业报告,每个字都认识,连在一起却像天书。想找人聊聊,身边要么没人懂,要么懂的懒得跟你解释。后来我试着把同一段内容丢给一个大语言模型,几秒钟之后&a…

2026/11/4 19:19:54 阅读更多 →

日新闻

周新闻

月新闻