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>
相关推荐
我叫小白菜5 分钟前
【Java_EE】单例模式、阻塞队列、线程池、定时器
java·开发语言
Albert Edison37 分钟前
【最新版】IntelliJ IDEA 2025 创建 SpringBoot 项目
java·spring boot·intellij-idea
超级小忍1 小时前
JVM 中的垃圾回收算法及垃圾回收器详解
java·jvm
weixin_446122461 小时前
JAVA内存区域划分
java·开发语言·redis
勤奋的小王同学~2 小时前
(javaEE初阶)计算机是如何组成的:CPU基本工作流程 CPU介绍 CPU执行指令的流程 寄存器 程序 进程 进程控制块 线程 线程的执行
java·java-ee
TT哇2 小时前
JavaEE==网站开发
java·redis·java-ee
2401_826097622 小时前
JavaEE-Linux环境部署
java·linux·java-ee
缘来是庄2 小时前
设计模式之访问者模式
java·设计模式·访问者模式
Bug退退退1233 小时前
RabbitMQ 高级特性之死信队列
java·分布式·spring·rabbitmq
梵高的代码色盘3 小时前
后端树形结构
java