第七章续版,mapper.xml,这是重点,一对多,一对一,动态SQL,foreach...

embedded/2025/2/7 7:07:12/
xmlns="http://www.w3.org/2000/svg" style="display: none;">

xml_0">mapper,mapper.xml

1.单表查询

<select id="接口名" parameterType="参数类型,单个参数时写对应类型,两个及以上用Map" resultType="返回值类型,查询必须写">这里写SQL语句
</select>
Map时测试类写法
Map<String, Object> map = new HashMap<>();map.put("goodsName", "牛奶");map.put("id", 1);
Goods goods = goodsMapper.selectGoodsByNameAndId(map);

1.查询

 User getUserById(int id);<select id="getUserById" parameterType="int" resultType="com.hz.pojo.User">select id, name, pwd from `user` where id = #{id} limit 1</select>
2.新增
int addUser(User user);
<insert id="addUser" parameterType="com.hz.pojo.User">insert into `user`(id, name, pwd) values (#{id},#{name},#{pwd})</insert>3.删除
int deleteUser(int id);
<delete id="deleteUser" parameterType="int">delete from `user` where id = #{id}</delete>
4.修改int updateUser(User user);  <update id="updateUser" parameterType="com.hz.pojo.User">update `user` set name=#{name},pwd=#{pwd} where id = #{id}</update>

Map

接口int addUser2(Map<String, Object> map);实现<insert id="addUser2" parameterType="map">insert into `user`(id, name, pwd) values (#{userId},#{userName},#{password})</insert>
测试@Testpublic void addUser2() {SqlSession sqlSession = null;try {Map<String, Object> map = new HashMap<>();map.put("userId", 5);map.put("userName", "hz");map.put("password", "123456");sqlSession = MybatisUtils.getSqlSession();UserMapper userMapper = sqlSession.getMapper(UserMapper.class);int num = userMapper.addUser2(map);if (num > 0) {System.out.println("插入成功!");}sqlSession.commit();} finally {if (sqlSession != null) {sqlSession.close();}}}

2.1一对一

Mapper.xml里内容
<resultMap id="随意起,后面用到注意保持一致" type="用到的实体类的包名到类名"><result property="实体类的列名" column="查询语句返回的列的名字"></result><association property="这里是将类当作属性的对应属性名" javaType="对应实体类的包名到类名"><result property="实体类的列名" column="查询语句返回的列的名字"></result></association >
</resultMap>
<select id="这是mapper里需要绑定的方法名" resultMap="上面随意起的id名">这里写SQL语句
</select>

2.2一对多


<resultMap id="随意起,后面用到注意保持一致" type="用到的实体类的包名到类名"><result property="实体类的列名" column="查询语句返回的列的名字"></result><collection property="这里是将类当作属性的对应属性名"ofType="对应实体类的包名到类名"><result property="实体类的列名" column="查询语句返回的列的名字"></result></collection >
</resultMap>
<select id="这是mapper里需要绑定的方法名" resultMap="上面随意起的id名">这里写SQL语句
</select>
另一种写法
<resultMap id="myMap" type="用到的类的包名到类名"><result property="与实体类对应" column="跟查询结果返回的列对应"/>
</resultMap><resultMap id="usedMap" type="用到的类的包名到类名"><result property="与实体类对应" column="跟查询结果返回的列对应"/><collection property="被当作属性放在实体类的属性名" ofType="myMap用到的实体类名" tesultMap="myMap"/>
</resultMap>
<select id="这是mapper里需要绑定的方法名" resultMap="usedMap">这里写SQL语句
</select>

3.1自增主键

useGeneratedKeys="true" keyProperty="id"
获取只需要在调用方法时用对象点属性即可

sql_110">4.动态sql

4.1查询
<selsct id="方法名" resultType="返回值类型">select * from <where><if test="name!=null and name!='' ">and name=#{name}</if></where></select>
4.2修改
第一种trim实现
<update id="方法名" parameterType="参数类型">update 表名 <trim prefix="set" suffixOverrides=","><if test="name!=null and name!='' ">name=#{name},</if><if test="hobby!=null and hobby!='' ">hobby=#{hobby},</if></trim>where id=#{id}
</update >
第二种set实现
<update id="接口名" parameterType="参数类型">update 表名<set><if test="name!=null and name!='' ">name=#{name},</if></set>where id=#{id}
</update>
4.3新增
<insert id="接口名" parameterType="参数类型,包名到类名">insert into 表名<trim prefix="(" suffix=")" suffixOverrider=","><if test="name!=null and name!='' ">name,</if><trim>values<trim prefix="(" suffix=")" suffixOverrider=","><if test="name!=null and name!='' ">#{name},</if></trim>
</insert>

SQL片段

将一些功能的部分抽取出来,方便复用

<!-- 1.创建模板 -->
<sql id="customSql">select id,customerName,birthday,phone,level,sum
</sql>
<select id="selectCustom" resultType="bean.Custom">
<!-- 2.使用模板 --><include refid="customSql"/> from custom<where><if test="customerName !=null and customerName!=''">and customerName like concat('%',#{customerName},'%')</if><if test="phone !=null and phone!=''">and phone like concat('%',#{phone},'%')</if></where>
</select>
foreach
<!-- 定义接口 -->List<Custom> selectCustomByIds(@Param("ids")List<Integer> ids);
<!-- mapper.xml -->
<sql id="customSql">select id,customerName,birthday,phone,level,sum</sql>
<select id="selectCustomByIds" resultType="bean.Custom"><include refid="customSql"></include>from custom where id in<!-- 下面的意思是将参数ids每次拿出一个放在cusId,以'('开始遍历结束后以')'结束 --><foreach collection="ids" item="cusId" open="(" close=")" separator=",">#{cusId}</foreach></select>
//当参数是对象时
int addListCustom(@Param("Listcustom") List<Custom> Listcustom);<insert id="addListCustom">insert into custom(customerName,phone) values<foreach collection="Listcustom" item="cus"  separator=",">(#{cus.customerName},#{cus.phone})</foreach></insert>

choose

<!--  满足name后即使phone满足也不会拼在sql上,执行的只会是其中一个,都不满足执行otherwise--><select id="selectCustomChoose" resultType="com.j4061.springmybatis.bean.Custom"><include refid="customSql"></include>from custom<where><choose><when test="customerName !=null and customerName!=''">and customerName like concat('%',#{customerName},'%')</when><when test="phone !=null and phone!=''">and phone like concat('%',#{phone},'%')</when><otherwise>and 1=1</otherwise></choose></where></select>

http://www.ppmy.cn/embedded/144178.html

相关文章

【QT】:QT(介绍、下载安装、认识 QT Creator)

背景 &#x1f680; 在我们的互联网中的核心岗位主要有以下几种 开发&#xff08;程序员&#xff09;测试运维&#xff08;管理机器&#xff09;产品经理&#xff08;非技术岗位&#xff0c;提出需求&#xff09; 而我们这里主要关注的是开发方向&#xff0c;开发岗位又分很…

捷米特 EtherNet/IP 总线协议网关的具体内容介绍

关于EtherNET/IP的基本介绍 EtherNet/IP 中的 “Ethernet” 指以太网&#xff0c;是一种常见的局域网技术&#xff0c;用于在有限区域内实现多台设备之间的数据传输&#xff1b;“IP” 在此处指工业协议&#xff08;Industrial Protocol&#xff09;&#xff0c;而不是通常所说…

Ubuntu 20.04安装rsync 3.2.7

前言 Ubuntu 20.04的apt中不支持rsync 3.2.0的安装&#xff0c;因此需要手动编译安装&#xff0c;记录下过程 ~$ apt policy rsync rsync:已安装&#xff1a;(无)候选&#xff1a; 3.1.2-2.1ubuntu1.6版本列表&#xff1a;3.1.3-8ubuntu0.7 -1100 /var/lib/dpkg/status下载所…

Ubuntu22.04搭建LAMP环境(linux服务器学习笔记)

目录 引言&#xff1a; 一、系统更新 二、安装搭建Apache2 1.你可以通过以下命令安装它&#xff1a; 2.查看Apache2版本 3.查看Apache2运行状态 4.浏览器访问 三、安装搭建MySQL 1.安装MySQL 2.查看MySQL 版本 3.安全配置MySQL 3.1是否设置密码&#xff1f;(按y|Y表…

【PyQt5教程 二】Qt Designer 信号与槽的使用方法及PyQt5基本小部件说明

目录 一、信号与槽机制&#xff1a; 二、信号与槽使用方法&#xff1a; &#xff08;1&#xff09;使用Qt Designer 的信号与槽编辑器&#xff1a; &#xff08;2&#xff09;使用固定语法直接建立信号槽连接&#xff1a; 三、PyQt小部件及其触发信号&#xff1a; &#x…

23种设计模式之解释器模式

目录 1. 简介2. 代码2.1 Expression &#xff08;抽象表达式类&#xff09;2.2 TerminalExpression &#xff08;终结符表达式类&#xff09;2.3 OrExpression &#xff08;非终结符表达式类&#xff09;2.4 AndExpression &#xff08;非终结符表达式类&#xff09;2.5 Test &…

Vue3+Vite+ElementPlus 构建 笔记

1、Node.js 查看 node -v 2、如果你的网络环境不佳&#xff0c;推荐使用 换源 npm config set registry https://registry.npmmirror.com 3、Vue 3 编码规范 使用 1.编码语言&#xff1a;TypeScript 2.代码风格&#xff1a;组合式API 3.简写形式&#xff1a;setup语法糖 4…

QT 中 QString 转换为 Unicode 和 ASCII 的方法

目录 ​编辑 前言 一、QString转换成 Unicode编码 二、QString转换成ASCII编码 三、Unicode编码转换成QString汉字 四、ASCII编码转成QString 五、注意事项 六、总结 前言 在 Qt 开发中&#xff0c;经常会遇到需要将QString中的字符转换为特定编码格式的需求。本文将介…