MyBatis的CRUD

0-基础知识

        id:唯一标识

        type:映射的类型,支持别名

java里的命名规则是驼峰,而sql里面是下划线,如何对数据库表的字段起别名?

        数据库表的字段名称和实体类的属性名称 不一样,则不能自动封装数据,我们提供了三种解决方法。

1、起别名:对不一样的列名起别名,让别名和实体类的属性名一样。

 缺点:每次查询都要定义一次别名。

  <select id="selectAll" resultType="brand">
         select
              id, brand_name as brandName, company_name as companyName, ordered, description, status
         from tb_brand;
     </select>

2、 sql片段

缺点:不灵活。

    <sql id="brand_column">
         id, brand_name as brandName, company_name as companyName, ordered, description, status
     </sql>

     <select id="selectAll" resultType="brand">
         select
             <include refid="brand_column" />
         from tb_brand;
     </select>

 3、resultMap:

1. 定义<resultMap>标签。

 2. 在<select>标签中,使用resultMap属性替换 resultType属性。

    <resultMap id="brandResultMap" type="brand">
        <!--
            id:完成主键字段的映射
                column:表的列名
                property:实体类的属性名
            result:完成一般字段的映射
                column:表的列名
                property:实体类的属性名
        -->
        <result column="brand_name" property="brandName"/>
        <result column="company_name" property="companyName"/>
    </resultMap>

如何传入参数? 

    #{} 会将等待输入的文本替换成? 如同preparedStatement 防止sql注入。
    ${}在查询的时候,我们需要使用模糊查询like。这时候只能使用此种方法。

   参数类型:parameterType() 可以省略,因为传入什么就赋值什么,在接口中已经定义好了数据类型。


    <select id="selectById" parameterType="int" resultType="com.tsj.pojo.user">
        select *
        from tb_user
        where id = #{};
    </select>

</mapper>
        //随后通过代理对象调用接口中的方法,Mybatis会自动寻找到xml配置文件中相对应的sql语句并执行
        //List<brand> brands = brandMapper.selectAll();
        brand brand = brandMapper.selectByID(id);
        System.out.println(brand);

    特殊字符处理:小于号< 不可以直接食用
    1、转义字符&lt; 相当于小于号,字符比较少的时候使用
    2、CDATA区:字符比较多的时候使用,会将里面的一切都当作纯文本操作

    <select id="selectById" resultMap="brandResultMap">
        select *
        from tb_brand
        where id
         <![CDATA[
            <
         ]]>
         #{id};

    </select>

创建sql的执行对象

sqlSessionFactory.openSession(true/false) 

设置可以选择是否开启事务,true->自动提交 ;false->手动提交;

1-普通的查询->Mybatis的三种参数接收方式 

多条件查询

参数接收:

    <select id="selectByCondition" ResultMap="com.tsj.pojo.brand">
        select *
        from tb_brand
        where status = #{status}
        and company_name like #{company_name}
        and brand_name like #{brand_name}
    </select>

所有配置步骤,包括三种处理参数的方法。 

1、散装参数:如果方法中由多个参数,需要使用@param(sql语句占位符名称) 

@Interface
    List<brand> selectByCondition(@Param("status")int status,@Param("companyName") String company_name,@Param("brandName")String bramd_name);
@Test
List<brand> brands = brandMapper.selectByCondition(status, companyName, brandName);

  2、对象参数:对象属性名称要和参数占位符名称保持一致

@Interface 
List<brand> selectByCondition(brand brand);
@Test
List<com.tsj.pojo.brand> brands = brandMapper.selectByCondition(brand);

3、map集合参数:保证sql中参数名和map集合里面的健的名称对应的上,即可设置成功。

@Interface
List<brand> selectByCondition(Map map);
@Test
List<com.tsj.pojo.brand> brands = brandMapper.selectByCondition(map);

封装数据的过程

        //设置参数
        int status=1;
        String company_name="华为";
        String brand_name="华为";

        //处理参数,用作模糊处理
        company_name="%"+company_name+"%";
        brand_name ="%"+company_name+"%";


        //创建map集合
        Map map=new HashMap();
        map.put("status",status);
        map.put("company_name",company_name);
        map.put("brand_name",brand_name);

        //封装对象
        brand brand=new brand();
        brand.setStatus(status);
        brand.setCompany_name(company_name);
        brand.setBrand_name(brand_name);

2- 动态查询

有一些查询,我们并不要求所有的选项都被用户输入,一旦出现了数据为null的情况,无法从表中查询到有效的数据。

基于此情况,我们需要随着sql语句的变化进行修改,也就是switch判断。Mybatis提供了一套内置的标签,帮助我们进行判断。

<select id="selectByConditionSingle" resultMap="brandResultMap">
        select *
        from tb_brand
        <where>
            <choose><!--相当于switch-->
                <when test="status != null"><!--相当于case-->
                    status = #{status}
                </when>
                <when test="companyName != null and companyName != '' "><!--相当于case-->
                    company_name like #{companyName}
                </when>
                <when test="brandName != null and brandName != ''"><!--相当于case-->
                    brand_name like #{brandName}
                </when>

            </choose>
        </where>
    </select>

3-插入数据

    <insert id="add" useGeneratedKeys="true" keyProperty="id">
        insert into tb_brand (brand_name, company_name, ordered, description, status)
        values (#{brandName}, #{companyName}, #{ordered}, #{description}, #{status});
    </insert>

 返回主键

添加关联数据时。

在订单表和订单项中,他们之间是多对多的关系。即一个订单表和多个商品表对应商品中间表外键。

每当我们插入一个商品的时候, 我们需要将商品和中间表相关联。与此同时,我们需要将订单表也进行更新,更新时我们需要知道添加的是哪件商品,所以需要返回商品的id。

看不懂?以后再来修改。

 4-修改数据

在测试类中,我们仅仅对部分数据进行修改,其他保持不变,并对数据封装后提交给方法。这样会让不想修改的数据变成null。为了解决这个问题,我们可以使用<if></if>标签在配置文件中前进行筛选。

<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>

5-删除数据

删除一个数据

    <delete id="deleteById">
        delete from tb_brand where id = #{id};
    </delete>

批量删除

把选中的id值提交给代码,代码挨个去删除。我们会把待删除的id封装成数组,随后按照顺序一个个删除。

Mbatis中配置了遍历数组的标签<foreach>,我们随着遍历,随着将获取到的数据放到?处。

我们需要使用seperate将每个输入的?分隔开。

我们同样open=“” close=“”表示开始和结束的时候拼接什么。
Please Attention 

Mybatis会自动的将我们传递去的数组封装成一个map集合.

默认array=你传递的数组数据.

你可以使用@Param注解来改变map集合默认key的名称。

    <delete id="deleteByIds">
        delete from tb_brand where id
        in
            <foreach collection="array" item="id" separator="," open="(" close=")">
                #{id}
            </foreach>
             ;
    </delete>

6-Mybatis参数传递-Mybatis是如何封装底层数据的

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值