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>
相关推荐
JH30738 小时前
SpringBoot 优雅处理金额格式化:拦截器+自定义注解方案
java·spring boot·spring
工程师老罗9 小时前
YoloV1数据集格式转换,VOC XML→YOLOv1张量
xml·人工智能·yolo
Coder_Boy_9 小时前
技术让开发更轻松的底层矛盾
java·大数据·数据库·人工智能·深度学习
invicinble10 小时前
对tomcat的提供的功能与底层拓扑结构与实现机制的理解
java·tomcat
较真的菜鸟10 小时前
使用ASM和agent监控属性变化
java
黎雁·泠崖10 小时前
【魔法森林冒险】5/14 Allen类(三):任务进度与状态管理
java·开发语言
qq_124987075311 小时前
基于SSM的动物保护系统的设计与实现(源码+论文+部署+安装)
java·数据库·spring boot·毕业设计·ssm·计算机毕业设计
Coder_Boy_12 小时前
基于SpringAI的在线考试系统-考试系统开发流程案例
java·数据库·人工智能·spring boot·后端
Mr_sun.12 小时前
Day06——权限认证-项目集成
java
瑶山12 小时前
Spring Cloud微服务搭建四、集成RocketMQ消息队列
java·spring cloud·微服务·rocketmq·dashboard