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>
相关推荐
麦兜*1 小时前
Spring Boot 企业级动态权限全栈深度解决方案,设计思路,代码分析
java·spring boot·后端·spring·spring cloud·性能优化·springcloud
ruan1145142 小时前
MySQL4种隔离级别
java·开发语言·mysql
Hellyc6 小时前
基于模板设计模式开发优惠券推送功能以及对过期优惠卷进行定时清理
java·数据库·设计模式·rocketmq
lifallen6 小时前
Paimon LSM Tree Compaction 策略
java·大数据·数据结构·数据库·算法·lsm-tree
hdsoft_huge6 小时前
SpringBoot 与 JPA 整合全解析:架构优势、应用场景、集成指南与最佳实践
java·spring boot·架构
百锦再7 小时前
详细解析 .NET 依赖注入的三种生命周期模式
java·开发语言·.net·di·注入·模式·依赖
程序员的世界你不懂7 小时前
基于Java+Maven+Testng+Selenium+Log4j+Allure+Jenkins搭建一个WebUI自动化框架(2)对框架加入业务逻辑层
java·selenium·maven
有没有没有重复的名字8 小时前
线程安全的单例模式与读者写者问题
java·开发语言·单例模式
程序员的世界你不懂10 小时前
基于Java+Maven+Testng+Selenium+Log4j+Allure+Jenkins搭建一个WebUI自动化框架(4)集成Allure报表
java·selenium·maven
isNotNullX10 小时前
数据中台架构解析:湖仓一体的实战设计
java·大数据·数据库·架构·spark