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>
相关推荐
客卿12318 分钟前
滑动窗口--模板
java·算法
zjjsctcdl41 分钟前
java与mysql连接 使用mysql-connector-java连接msql
java·开发语言·mysql
Moe4881 小时前
WebSocket :从浏览器 API 到 Spring 握手、Handler 与前端客户端
java·后端·架构
顶点多余1 小时前
线程互斥+线程同步+生产消费模型
java·linux·开发语言·c++
⑩-1 小时前
Java基础+集合框架-八股文
java·开发语言
福运常在1 小时前
股票数据API(19)次新股池数据
java·python·maven
Zaki_gd1 小时前
Cortex-M7 D-Cache 与 DMA 缓存一致性说明
java·spring·缓存
多看书少吃饭1 小时前
Vue3 + Java + Python 打造企业级大模型知识库(含 SSE 流式对话完整源码)
java·python·状态模式
Arthas2171 小时前
Java大厂面试:从Spring到微服务的全面技术考察
java·jvm·spring·微服务·面试·并发
mifengxing1 小时前
力扣HOT100——(1)两数之和
java·数据结构·算法·leetcode·hot100