Mybatis-配置文件完成增删改查

本文详细介绍了如何使用Mybatis配置文件进行增删改查操作,包括在Mapper接口配置文件中添加namespace,编写接口和SQL语句,以及参数占位符的使用。还特别提到了防止SQL注入的#()占位符和动态SQL拼接时使用的${},并讲解了在SQL语句中处理特殊字符的方法,如使用转义字符和CDATA。
摘要由CSDN通过智能技术生成

Mybatis-配置文件完成增删改查

  • 开发步骤

    在Mapper接口的配置文件中添加namespace

    <mapper namespace="...">
    
    • 编写接口方法
      • Mapper接口方法

        public interface BrandMapper {
            /*查询所有*/
            List<Brand selectAll();
            /*id查询*/
            Brand selectById(int id);
            /*条件查询*/
            List<Brand> selectByCondition(@Param("status") int status, @Param("companyName") String companyName, @Param("brandName") String brandName);
        
        
        }
        
      • pojo.Brand类

        package com.main.pojo;
        
        public class Brand {
            private Integer id;
            private String brandName;
            private String companyName;
            private Integer ordered;
            private String description;
            private Integer status;
        
            public Integer getId() {
                return id;
            }
        
            public void setId(Integer id) {
                this.id = id;
            }
        
            public String getBrandName() {
                return brandName;
            }
        
            public void setBrandName(String brandName) {
                this.brandName = brandName;
            }
        
            public String getCompanyName() {
                return companyName;
            }
        
            public void setCompanyName(String companyName) {
                this.companyName = companyName;
            }
        
            public Integer getOrdered() {
                return ordered;
            }
        
            public void setOrdered(Integer ordered) {
                this.ordered = ordered;
            }
        
            public String getDescription() {
                return description;
            }
        
            public void setDescription(String description) {
                this.description = description;
            }
        
            public Integer getStatus() {
                return status;
            }
        
            public void setStatus(Integer status) {
                this.status = status;
            }
        
            @Override
            public String toString() {
                return "Brand{" +
                        "id=" + id +
                        ", brandName='" + brandName + '\'' +
                        ", companyName='" + companyName + '\'' +
                        ", ordered=" + ordered +
                        ", description='" + description + '\'' +
                        ", status=" + status +
                        '}';
            }
        }
        
        
      • 编写SQL语句

        <?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.main.mapper.BrandMapper">
        
            <resultMap id="brandResultMap" type="brand">
                <result column="brand_name" property="brandName"/>
                <result column="company_name" property="companyName"/>
            </resultMap>
            <!--    添加信息-->
            <insert id="add" useGeneratedKeys="true" keyProperty="id">
                insert into tb_brand(brand_name, company_name, ordered, description, status)
                    value (#{brandName},#{companyName},#{ordered},#{description},#{status});
            </insert>
            <!-- 修改信息-->
            <update id="update">
                update tb_brand
                <set>
                    <if test="brandName != null and brandName !=''">
                        brand_name = #{brandName},
                    </if>
                    <if test="companyName != null and companyName !=''">
                        company_name = #{companyName},
                    </if>
                    <if test="ordered != null">
                        ordered = #{ordered},
                    </if>
                    <if test="description != null and description != ''">
                        description = #{description},
                    </if>
                    <if test="status != null">
                        status =#{status}
                    </if>
                </set>
                where id = #{id};
            </update>
            <!--    删除数据-->
            <delete id="deleteById">
                delete
                from tb_brand
                where id = #{id}
            </delete>
        
        <!--    批量删除-->
            <delete id="deleteByIds">
                delete
                from tb_brand
                where id in(
                <foreach collection="ids" item="id" separator="," open="(" close=")">
                    #{id}
                </foreach>
                    );
            </delete>
        
        
            <!--    查看所有数据-->
            <select id="selectAll" resultMap="brandResultMap">
                select *
                from tb_brand;
            </select>
            <!--    查看详情-->
            <select id="selectById" resultMap="brandResultMap">
                select *
                from tb_brand
                where id = #{id};
            </select>
            <!--    条件动态查询
                        * if:条件判断
                            * test:逻辑表达式
                        * 问题:如果第一个条件为空的情况,会造成where后面直接跟and
                            * <where>标签
            -->
            <select id="selectByCondition" resultMap="brandResultMap">
                select *
                from tb_brand
                <where>
        
                    <if test="status !=null">
                        and status = #{status}
                    </if>
                    <if test="companyName !=null and companyName != ''">
                        and company_name like #{companyName}
                    </if>
                    <if test="brandName !=null and brandName != ''">
                        and brand_name like #{brandName};
                    </if>
                </where>
        
            </select>
            <!--   单条件查询 -->
            <select id="selectByConditionSingle" resultMap="brandResultMap">
                select *
                from tb_brand
                <where>
        
                    <choose>
        
                        <when test="status !=null">
                            status=#{status}
                        </when>
                        <when test="companyName !=null and companyName != ''">
                            company_name=#{companyName}
                        </when>
                        <when test="brandName !=null and brandName != ''">
                            brand_name=#{brandName}
                        </when>
        
        
                    </choose>
                </where>
            </select>
        
        
        </mapper>
        
      • 测试

  • 小细节

    • 参数占位符
      • Mybatis里有两种参数占位符

        • #()

          #是将传入的值当成字符串形式

          很大程度上防止sql注入

        • $()

          $是将传入的数据直接显示生成sql语句

        • 使用场景

          • 参数传递,使用#{}
          • 对数据进行动态设置,用${}进行sql拼接
      • 例:id–>1

        <select id="selectById" resultMap="brandResultMap">
                select *
                from tb_brand
                where id = #{id};
            </select>  --id = '1'
        
        <select id="selectById" resultMap="brandResultMap">
                select *
                from tb_brand
                where id = ${id};
            </select>  --id = 1
        
    • parameterType:用于设置参数类型,该参数可以省略
    • SQL语句中特殊字符处理
      • 使用转义字符
      • <! [CDATA [内容]]> :输入的内容会被当初文本框
    • 添加数据到数据库是默认开启事务,需要手动提交事务
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小学五年级在读的蒟蒻

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

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

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

打赏作者

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

抵扣说明:

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

余额充值