【第十四弹】MyBatis

官网: mybatis – MyBatis 3 | 简介

①MyBatis快速入门

②Mapper代理开发

③MyBatis核心配置文件

④配置文件完成增删改

案例        完成品牌数据的增删改查操作

产品原型地址:

产品大牛 - 让产品工作更简单(Axure原型托管、Sketch标注)

tb_brand

-- 删除tb_brand表

drop table if exists tb_brand;

-- 创建tb_brand表

create table tb_brand

(

    -- id 主键

    id           int primary key auto_increment,

    -- 品牌名称

    brand_name   varchar(20),

    -- 企业名称

    company_name varchar(20),

    -- 排序字段

    ordered      int,

    -- 描述信息

    description  varchar(100),

    -- 状态:0:禁用  1:启用

    status       int

);

-- 添加数据

insert into tb_brand (brand_name, company_name, ordered, description, status)

values ('三只松鼠', '三只松鼠股份有限公司', 5, '好吃不上火', 0),

       ('华为', '华为技术有限公司', 100, '华为致力于把数字世界带入每个人、每个家庭、每个组织,构建万物互联的智能世界', 1),

       ('小米', '小米科技有限公司', 50, 'are you ok', 1);

SELECT * FROM tb_brand;

brand

package com.itheima.pojo;

/**

 * 品牌

 *

 * alt + 鼠标左键:整列编辑

 *

 * 在实体类中,基本数据类型建议使用其对应的包装类型

 */

public class Brand {

    // id 主键

    private Integer id;

    // 品牌名称

    private String brandName;

    // 企业名称

    private String companyName;

    // 排序字段

    private Integer ordered;

    // 描述信息

    private String description;

    // 状态:0:禁用  1:启用

    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 +

                '}';

    }

}

MybatisX

1.查询

1.1.查询所有数据

1.2.查看详情

1.3.条件查询

 散装参数方法:

局部细节:

整体:

缺点: 必须完全输入三个条件才能查询,过于死板

解决方法: 运用动态SQL

BrandMapper.xml

BrandMapper.xml

2.添加

BrandMapper.xml

MyBatisTest.java

或者在获取sqlSession对象时,传入参数true,则会自动提交事务,则不用手动提交(sqlSession.commit();)

BrandMapper.xml

3.修改

3.1.修改全部字段

BrandMapper.xml

缺点:不够灵活,只能全部修改,不能单一修改

3.2.修改动态字段

BrandMapper.xml

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

4.删除

4.1.删除一个

BrandMapper.xml

<delete id="deleteByid">

Delete from tb_brand where id=#{id};

</delete>

4.2.批量删除

MyBatis参数传递

⑤注解完成增删改查

UserMapper.java

所学内容均来自于黑马程序员

所学内容均来自于黑马程序员

所学内容均来自于黑马程序员

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值