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/7/18 5:50:11 阅读更多 →
淘系应用抓包技术:突破SSL Pinning与加密校验

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

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

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

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

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

2026/7/18 5:50:11 阅读更多 →

最新新闻

深度剖析OpenCode事件总线:如何构建高性能的AI编程助手通信架构

深度剖析OpenCode事件总线:如何构建高性能的AI编程助手通信架构

深度剖析OpenCode事件总线:如何构建高性能的AI编程助手通信架构 【免费下载链接】opencode The open source coding agent. 项目地址: https://gitcode.com/GitHub_Trending/openc/opencode OpenCode作为一款开源的AI编程助手,其核心的事件总线架…

2026/7/18 8:14:08 阅读更多 →
Python脚本驱动HY-Motion 1.0:实现3D动作批量自动化生成实战

Python脚本驱动HY-Motion 1.0:实现3D动作批量自动化生成实战

1. 项目概述:当Python脚本遇见3D动作生成最近在搞一个挺有意思的项目,用Python脚本去驱动腾讯开源的HY-Motion 1.0模型,实现批量化的3D动作自动生成。简单来说,就是你写一堆描述动作的文本,然后跑一个脚本,…

2026/7/18 8:14:08 阅读更多 →
打破音乐枷锁:ncmdump让你的网易云音乐真正属于你

打破音乐枷锁:ncmdump让你的网易云音乐真正属于你

打破音乐枷锁:ncmdump让你的网易云音乐真正属于你 【免费下载链接】ncmdump 项目地址: https://gitcode.com/gh_mirrors/ncmd/ncmdump 你是否曾在网易云音乐下载了心爱的歌曲,却发现它们被.ncm格式"囚禁"在特定平台中?ncmd…

2026/7/18 8:14:08 阅读更多 →
OpenCode事件总线架构深度解析:构建AI编程助手的神经网络系统

OpenCode事件总线架构深度解析:构建AI编程助手的神经网络系统

OpenCode事件总线架构深度解析:构建AI编程助手的神经网络系统 【免费下载链接】opencode The open source coding agent. 项目地址: https://gitcode.com/GitHub_Trending/openc/opencode OpenCode作为一款开源的AI编程助手,其核心架构设计体现了…

2026/7/18 8:14:08 阅读更多 →
OpenCore Legacy Patcher:数字遗产的守护者,让老Mac重获新生

OpenCore Legacy Patcher:数字遗产的守护者,让老Mac重获新生

OpenCore Legacy Patcher:数字遗产的守护者,让老Mac重获新生 【免费下载链接】OpenCore-Legacy-Patcher Experience macOS just like before 项目地址: https://gitcode.com/GitHub_Trending/op/OpenCore-Legacy-Patcher 在技术快速迭代的时代&am…

2026/7/18 8:14:08 阅读更多 →
基于Casbin和Midway实现高效接口鉴权方案

基于Casbin和Midway实现高效接口鉴权方案

1. 项目概述在后台管理系统开发中,接口鉴权是保障系统安全性的核心环节。传统鉴权方案往往存在代码臃肿、权限逻辑与业务代码耦合度高的问题。本文将基于Casbin这一开源访问控制框架,结合Midway全栈框架,展示如何实现优雅简洁的接口鉴权方案。…

2026/7/18 8:13:08 阅读更多 →

日新闻

从模糊意图到可执行指令:Claude PRD中Prompt Engineering与需求颗粒度的5级映射法则

从模糊意图到可执行指令:Claude PRD中Prompt Engineering与需求颗粒度的5级映射法则

更多请点击: https://kaifayun.com 第一章:从模糊意图到可执行指令:Claude PRD中Prompt Engineering与需求颗粒度的5级映射法则 在Claude驱动的产品需求文档(PRD)生成实践中,原始业务意图往往以自然语言片…

2026/7/18 0:00:38 阅读更多 →
Cursor配置生成失效?3大隐藏陷阱+4行修复代码,资深工程师连夜整理的紧急补救清单

Cursor配置生成失效?3大隐藏陷阱+4行修复代码,资深工程师连夜整理的紧急补救清单

更多请点击: https://codechina.net 第一章:Cursor配置生成失效?3大隐藏陷阱4行修复代码,资深工程师连夜整理的紧急补救清单 Cursor 配置生成突然失效,是近期高频报障场景。表面看是 cursor.config.json 未更新或 LSP…

2026/7/18 0:00:38 阅读更多 →
某智驾大牛创业

某智驾大牛创业

作者:钟声编辑:Mark出品:红色星际头图:智能驾驶图片据悉,国内某头部智驾公司端到端模型技术大牛Z投身创业,并且已经拿到融资。Z不仅是该头部公司内部最年轻的对标阿里P10级别技术负责⼈,更是业内…

2026/7/18 0:00:38 阅读更多 →

周新闻

月新闻