mapper.xml的基本写法

本文介绍了XML映射文件的基础写法,包括如何修改和删除代码块,以及进行查找操作。通过对XML配置的学习,可以更好地理解Java应用中的数据映射过程。

修改代码块

    <update id="editContent" parameterType="com.ilearning.ssm.pojo.LessionContent">
        update tb_lession_content
        <set>
            <if test="tb_le_con_content!=null">
                tb_le_con_content=#{tb_le_con_content},
            </if>
            <if test="tb_le_con_video!=null">
                tb_le_con_video=#{tb_le_con_video}
            </if>
        </set>
        where tb_le_con_content=#{tb_le_con_content}
    </update>

删除代码块

   <delete id="delContent" parameterType="com.ilearning.ssm.pojo.LessionContent">
        delete from tb_lession_content
        <where>
            <if test="tb_le_id!=0">
                and tb_le_id = #{tb_le_id}
            </if>
            <if test="tb_le_con_id!=0">
                and  tb_le_con_id = #{tb_le_con_id}
            </if>
            <if test="tb_le_con_content!=null and tb_le_con_content!=''"  >
                and tb_le_con_content = #{tb_le_con_content}
            </if>
        </where>
    </delete>

查找

      <select id="findTeacher" resultType="com.ilearning.ssm.pojo.Teacher" parameterType="com.ilearning.ssm.pojo.Teacher">
        select
            t1.tb_tid,t1.tb_tname,t1.tb_tgrade,t1.tb_tcourse,t1.tb_tdetail,t1.tb_tpic,t1.tb_tdata,t2.tb_tud
        from
            tb_teacher AS t1
        INNER JOIN
            tb_teacher_manager AS t2 on t1.tb_tid=t2.tb_tid
        <where>
            <if test="tb_tid!=null">
                and t1.tb_tid = #{tb_tid}
            </if>
            <if test="tb_tid!=null">
                and t1.tb_tid = #{tb_tid}
            </if>
        </where>
    </select>
### MyBatis-Plus 中 `mapper.xml` 文件的正确写法 在使用 MyBatis-Plus 进行项目开发时,虽然该框架提供了许多便捷的功能,但在某些复杂业务逻辑下仍然需要手 SQL 来满足需求。对于这些情况下的 `mapper.xml` 编有特定的方式。 #### 基本结构与命名空间声明 每一个 Mapper XML 需要有一个唯一的命名空间(namespace),它通常是对应的 DAO 接口全限定名: ```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"> <mapper namespace="com.example.dao.UserDao"> <!-- SQL 映射语句 --> </mapper> ``` 上述代码片段展示了如何定义一个基本的映射文件并指定其关联的 Java 接口[^1]。 #### CRUD 操作示例 当利用 MyBatis-Plus 提供的基础功能不足以覆盖所有场景时,则可以在 `mapper.xml` 中补充自定义查询方法。下面是一个简单的增删改查例子: ##### 插入记录 ```sql <insert id="insertSelective" parameterType="com.example.entity.User"> INSERT INTO user ( <trim prefix="(" suffix=")" suffixOverrides=","> <if test="name != null"> name,</if> <if test="age != null"> age,</if> </trim> <trim prefix="VALUES (" suffix=")"> <if test="name != null"> #{name},</if> <if test="age != null"> #{age},</if> </trim> ) </insert> ``` 此段落描述了动态字段插入语法,允许仅传递必要的参数给数据库表中对应列赋值。 ##### 更新记录 ```sql <update id="updateByPrimaryKeySelective" parameterType="com.example.entity.User"> UPDATE user <set> <if test="name != null">name = #{name},</if> <if test="age != null">age = #{age}</if> </set> WHERE id = #{id} </update> ``` 这里通过 `<set>` 和条件判断标签实现了部分更新操作,即只修改传入实体对象中有值的部分属性。 ##### 删除记录 ```sql <delete id="deleteById" parameterType="java.lang.Long"> DELETE FROM user WHERE id=#{id} </delete> ``` 这段删除命令非常直观,直接指定了主键作为唯一标识符来进行单条目移除动作。 ##### 查询记录 ```sql <select id="selectByCondition" resultType="com.example.entity.User"> SELECT * FROM user <where> <if test="name != null and name != '' "> AND name LIKE CONCAT('%',#{name},'%')</if> <if test="minAge != null">AND age >= #{minAge} </if> <if test="maxAge != null">AND age <= #{maxAge}</if> </where> </select> ``` 以上选择性检索语句能够依据不同条件组合灵活获取符合条件的结果集,并支持模糊匹配等功能特性。 #### 使用 IPage 实现分页查询 为了更好地处理大数据量的情况,可以借助于 MyBatis-Plus 自带的支持来完成高效分页读取工作: ```sql <select id="selectPageVo" resultType="com.example.vo.UserVO"> SELECT u.*, d.dept_name AS deptName FROM user u LEFT JOIN department d ON u.dept_id=d.id <where> <if test="queryParam.name!=null and queryParam.name!=''">and u.name like concat('%',#{queryParam.name},'%')</if> <if test="queryParam.age!=null">and u.age=#{queryParam.age}</if> </where> </select> ``` 配合上文提到过的 `IPage<T>` 类型,在调用此类 Select 方法前先构建好 Page 对象即可轻松获得带有总数统计信息以及当前页面数据列表在内的完整响应体[^4]。 #### 结合拦截器优化性能 除了常规的操作外,还可以引入诸如 `MybatisPlusInterceptor` 等组件进一步提升系统的稳定性和扩展能力。例如设置全局配置项、注册插件等措施均有助于改善整体表现效果[^3]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

努力的小豆子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值