<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC"-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mappernamespace="com.oeong.repository.AccountRepository"><insertid="save"parameterType="com.oeong.entity.Account">
insert into t_account(username, password, age) value (#{username}, #{password}, #{age})
</insert><updateid="update"parameterType="com.oeong.entity.Account">
update t_account set username=#{username}, password=#{password}, age=#{age} where id=#{id}
</update><deleteid="deleteById"parameterType="long">
delete from t_account where id=#{id}
</delete><selectid="findAll"resultType="com.oeong.entity.Account">
select * from t_account
</select><selectid="findById"parameterType="long"resultType="com.oeong.entity.Account">
select * from t_account where id = #{id}
</select></mapper>
<selectid="findById"parameterType="long"resultType="com.oeong.entity.Account">
select * from t_account where id = #{id}
</select>
2、String 类型,通过 name 查询 Account
<selectid="findByName"parameterType="java.lang.String"resultType="com.oeong.entity.Account">
select * from t_account where username = #{username}
</select>
3、 包装类
<selectid="findById2"parameterType="java.lang.Long"resultType="com.oeong.entity.Account">
select * from t_account where id = #{id}
</select>
4、多个参数,通过 name 和 age 查询 Account
<selectid="findByNameAndAge"resultType="com.oeong.entity.Account">
select * from t_account where username = #{arg0} and age = #{arg1}
</select>
5、Java Bean
<updateid="update"parameterType="com.oeong.entity.Account">
update t_account set username = #{username},password = #{password},age = #{age} where id = #{id}
</update>
resultType:结果类型
1、基本数据类型,统计 Account 总数
<selectid="count"resultType="int">
select count(id) from t_account
</select>
2、包装类,统计 Account 总数
<selectid="count2"resultType="java.lang.Integer">
select count(id) from t_account
</select>
3、String 类型,通过 id 查询 Account 的 name
<selectid="findNameById"resultType="java.lang.String">
select username from t_account where id = #{id}
</select>
4、Java Bean
<selectid="findById"parameterType="long"resultType="com.oeong.entity.Account">
select * from t_account where id = #{id}
</select>
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC"-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mappernamespace="com.oeong.repository.StudentRepository"><resultMapid="studentMap"type="com.oeong.entity.Student"><idcolumn="id"property="id"></id><resultcolumn="name"property="name"></result><associationproperty="classes"javaType="com.oeong.entity.Classes"><idcolumn="cid"property="id"></id><resultcolumn="cname"property="name"></result></association></resultMap><selectid="findById"parameterType="long"resultMap="studentMap">
select s.id,s.name,c.id as cid,c.name as cname from student s,classes c
where s.id = #{id} and s.cid = c.id
</select></mapper>
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC"-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mappernamespace="com.oeong.repository.ClassesRepository"><resultMapid="classesMap"type="com.oeong.entity.Classes"><idcolumn="cid"property="id"></id><resultcolumn="cname"property="name"></result><collectionproperty="students"ofType="com.oeong.entity.Student"><idcolumn="id"property="id"/><resultcolumn="name"property="name"/></collection></resultMap><selectid="findById"parameterType="long"resultMap="classesMap">
select s.id,s.name,c.id as cid,c.name as cname from student s,classes c
where c.id = #{id} and s.cid = c.id
</select></mapper>
<resultMapid="studentMapLazy"type="com.oeong.entity.Student"><idcolumn="id"property="id"></id><resultcolumn="name"property="name"></result><associationproperty="classes"javaType="com.oeong.entity.Classes"select="com.oeong.repository.ClassesRepository.findByIdLazy"column="cid"></association></resultMap><selectid="findByIdLazy"parameterType="long"resultMap="studentMapLazy">
select * from student where id = #{id}
</select>
ClassesRepository
public Classes findByIdLay(long id);
ClassesRepository.xml
<selectid="findByIdLazy"parameterType="long"resultType="com.oeong.entity.Classes">
select * from classes where id = #{id}
</select>
<selectid="findByAccount"parameterType="com.oeong.entity.Account"resultType="com.oeong.entity.Account">
select * from t_account where
<iftest="id!=0">
id = #{id}
</if><iftest="username!=null">
and username = #{username}
</if><iftest="password!=null">
and password = #{password}
</if><iftest="age!=0">
and age = #{age}
</if></select>
if 标签可以⾃动根据表达式的结果来决定是否将对应的语句添加到 SQL 中,如果条件不成⽴则不添加, 如果条件成⽴则添加。
where标签
<selectid="findByAccount"parameterType="com.oeong.entity.Account"resultType="com.oeong.entity.Account">
select * from t_account
<where><iftest="id!=0">
id = #{id}
</if><iftest="username!=null">
and username = #{username}
</if><iftest="password!=null">
and password = #{password}
</if><iftest="age!=0">
and age = #{age}
</if></where></select>
where 标签可以⾃动判断是否要删除语句块中的 and 关键字,如果检测到 where 直接跟 and 拼接,则 ⾃动删除 and,通常情况下 if 和 where 结合起来使⽤。
choose、when标签
<selectid="findByAccount"parameterType="com.oeong.entity.Account"resultType="com.oeong.entity.Account">
select * from t_account
<where><choose><whentest="id!=0">
id = #{id}
</when><whentest="username!=null">
and username = #{username}
</when><whentest="password!=null">
and password = #{password}
</when><whentest="age!=0">
and age = #{age}
</when></choose></where></select>
<selectid="findByAccount"parameterType="com.oeong.entity.Account"resultType="com.oeong.entity.Account">
select * from t_account
<trimprefix="where"prefixOverrides="and"><iftest="id!=0">
id = #{id}
</if><iftest="username!=null">
and username = #{username}
</if><iftest="password!=null">
and password = #{password}
</if><iftest="age!=0">
and age = #{age}
</if></trim></select>
set标签
set 标签⽤于 update 操作,会⾃动根据参数选择⽣成 SQL 语句。
<updateid="update"parameterType="com.oeong.entity.Account">
update t_account
<set><iftest="username!=null">
username = #{username},
</if><iftest="password!=null">
password = #{password},
</if><iftest="age!=0">
age = #{age}
</if></set>
where id = #{id}
</update>
<selectid="findByIds"parameterType="com.oeong.entity.Account"resultType="com.oeong.entity.Account">
select * from t_account
<where><foreachcollection="ids"open="id in ("close=")"item="id"separator=",">
#{id}
</foreach></where></select>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!