java文件框架-mapper.xml

mapper.xml:

1.固定框架

xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

2.命名空间

对应mapper接口的名称。

xml 复制代码
<mapper namespace="com.bai.dao.UserMapper">

3.结果映射

id:结果映射名称

type:对应实体类,整个查询结果的类型

javaType:对应的实体类的属性的java类型

property:数据库字段名(列名)

column:对象属性名

select:关联的查询语句对应的mapper中的方法名

一对一映射:

xml 复制代码
<resultMap id="UserResultMap" type="com.bai.pojo.User">
    <id property="user_id" column="userId" />
    <result property="user_name" column="userName" />
    <result property="user_password" column="userPassword" />
</resultMap>

一对多映射:

嵌套结果

使用嵌套结果的方式,可以在查询主实体对象的同时,通过嵌套的方式将关联实体对象的属性嵌套到主实体对象的属性中。这种方式的实现需要在Mapper.xml文件中定义一个SQL语句,使用嵌套的方式将关联实体对象的属性映射到主实体对象的属性中。在定义映射关系时,需要使用resultMap标签来定义主实体对象和关联实体对象的映射关系,使用collection标签来嵌套关联实体对象的属性。

xml 复制代码
    <resultMap id="articleWithComment" type="Article">
        <id property="id" column="id"/>
        <result property="title" column="title"/>
        <result property="content" column="content"/>
        <collection property="commentList" ofType="Comment">
            <id property="id" column="cid"/>
            <result property="content" column="bcontent"/>
            <result property="author" column="author"/>
            <result property="aId" column="a_id"/>
        </collection>
    </resultMap>
嵌套查询(分步查询)

通过userid去user表查user的信息,并且同时通过userid去查对应的things。

userMapper.xml:

xml 复制代码
    <resultMap id="getUser" type="com.bai.pojo.User">
        <id column="user_id" property="userId" />
        <result column="user_name" property="userName" />
        <collection property="thingsList"
                    select="com.bai.dao.ThingsMapper.selectThingsByUserId"
                    column="user_id"/>
    </resultMap>

查询语句:

xml 复制代码
  <select id="getUserById" resultType="User">
       select * from user where user_id = #{userId}
   </select>

ThingsMapper.xml:

xml 复制代码
<select id="getThingsByUserId"  resultType="Thing">
      select * from things where user_id = #{userId}
    </select>

此时的UserMapper:

java 复制代码
User getUserById(Integer userId);

ThingsMapper:

java 复制代码
List<Thing> getThingsByUserId(Integer userId);

4.sql查询结果列

通用查询结果列

xml 复制代码
    <sql id="User_List">
        user_id, user_name, user_password
    </sql>

5.增删改查

id:mapper中的方法名

parameterType:接收的参数(id)的参数类型

resultType:拿到的参数(User)的参数类型

ofType:当查询到的结果为应该集合时,集合中元素的类型

resultMap:映射map的id

xml 复制代码
<insert id="insertUser" parameterType="com.example.model.User">
    INSERT INTO user (name, age) VALUES (#{name}, #{age})
</insert>

xml 复制代码
<delete id="deleteUserById" parameterType="int">
    DELETE FROM user WHERE id = #{id}
</delete>

xml 复制代码
<!-- 普通  当不存在不能为空的值时 -->
<update id="updateUser" parameterType="com.example.model.User">
    UPDATE user SET name = #{name}, age = #{age} WHERE id = #{id}
</update>

<!-- 特殊  当存在不能为空的值时 -->
<update id="updateUser" parameterType="com.example.model.User">
    update user
    <set>
        <if test="userName!=null">name=#{userName},</if>
        <if test="userAge!=null">age=#{userAge},</if>
	</set>
    where id = #{id} 
</update>

<!--  生成动态sql更新语句: -->
<!-- <trim> 元素用于在生成动态SQL时对文本进行处理。在这里,prefix="set" 表示在生成结果前插入
     SET 关键字,suffixOverrides="," 表示删除末尾多余的逗号(如果有)-->
<update id="updateUser" parameterType="com.example.model.User">
    update user
    <trim prefix="set" suffixOverrides=",">
        <if test="userName!=null">name=#{userName},</if>
        <if test="userAge!=null">age=#{userAge},</if>
    </trim>
    where id = #{id} 
</update>

xml 复制代码
<select id="getUserById" resultMap="UserResultMap">
    SELECT * FROM user WHERE id = #{id}
</select>
相关推荐
码路飞3 小时前
GPT-5.3 Instant 终于学会好好说话了,顺手对比了下同天发布的 Gemini 3.1 Flash-Lite
java·javascript
SimonKing4 小时前
OpenCode AI编程助手如何添加Skills,优化项目!
java·后端·程序员
Seven975 小时前
剑指offer-80、⼆叉树中和为某⼀值的路径(二)
java
怒放吧德德17 小时前
Netty 4.2 入门指南:从概念到第一个程序
java·后端·netty
雨中飘荡的记忆18 小时前
大流量下库存扣减的数据库瓶颈:Redis分片缓存解决方案
java·redis·后端
心之语歌21 小时前
基于注解+拦截器的API动态路由实现方案
java·后端
华仔啊1 天前
Stream 代码越写越难看?JDFrame 让 Java 逻辑回归优雅
java·后端
ray_liang1 天前
用六边形架构与整洁架构对比是伪命题?
java·架构
Ray Liang1 天前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
Java水解1 天前
Java 中间件:Dubbo 服务降级(Mock 机制)
java·后端