实验一 MyBatis框架实验

一、实验环境

Windows10、IDEA2023.1.2、mybatis 3.5.6、DataGr

二、实验目的与要求

1、掌握 MyBatis 开发环境的搭建;

2、熟悉 MyBatis 的开发步骤;

3、掌握 MyBatis 基本对象、配置文件和映射文件的使用;

4、掌握 MyBatis 动态 SQL 开发。

三、实验步骤与结果(重点,应包含详细步骤、核心代码、结果截图等)

1、创建商品表和对应的实体类,包括 id(主键)、品类、名称、品牌、价格等字段;

在DataGrip中创建表:

USE mybatis;

CREATE TABLE goods(

    id int primary key auto_increment,

    sort varchar(25) not null,

    name varchar(20) not null ,

    brand varchar(20) not null ,

    price float not null

);


插入数据:

INSERT INTO goods VALUES (null,'数码产品','宏碁非凡S3X','宏碁',4600),

                         (null,'数码产品','联想R7000','联想',8600),

                         (null,'数码产品','联想R9000','联想',9600),

                         (null,'数码产品','联想小新','联想',4300);

创建对应的实体类:

package com.yin.pojo;


/**

 * Author: Rhett Butler

 * Project:MyBatisDemo1

 * Date:2023/12/9  15:07

 */

public class goods {

    private int id;

    private String sort;

    private String name;

    private String brand;

    private float price;


    @Override

    public String toString() {

        return "goods{" +

                "id=" + id +

                ", sort='" + sort + '\'' +

                ", name='" + name + '\'' +

                ", brand='" + brand + '\'' +

                ", price=" + price +

                '}';

    }


    public int getId() {

        return id;

    }


    public void setId(int id) {

        this.id = id;

    }


    public String getSort() {

        return sort;

    }


    public void setSort(String sort) {

        this.sort = sort;

    }


    public String getName() {

        return name;

    }


    public void setName(String name) {

        this.name = name;

    }


    public String getBrand() {

        return brand;

    }


    public void setBrand(String brand) {

        this.brand = brand;

    }


    public float getPrice() {

        return price;

    }


    public void setPrice(float price) {

        this.price = price;

    }

}

 

2、实现添加商品功能

<insert id="insertGoods"

            parameterType="goods">

        insert into goods values (null,#{sort},#{name},#{brand},#{price})

    </insert>

 

 

3、实现通过 id 删除指定商品功能;

   

 <delete id="deleteById"

            parameterType="integer">

        delete from goods where id=#{id}

    </delete>

 

4、实现通过 id 修改指定商品功能,注意当只需要修改某些商品属性时(如价格),用户不需要提供其它商品属性信息;

<update id="updateByProperty"

            parameterType="goods">

        update goods

        <trim prefix="set" suffixOverrides=",">

            <if test="sort !=null and sort !=''">

                sort =#{sort}

            </if>

            <if test="name !=null and name !=''">

                name =#{name}

            </if>

            <if test="brand !=null and brand !=''">

                brand =#{brand}

            </if>

            <if test="price !=null and price !=''">

                price =#{price}

            </if>

        </trim>

        where id=#{id}

    </update>

 

用户不需要提供其它商品属性信息即可修改任意属性。

5、实现对商品的查询,具体要求有:

(1)根据 id 查找某个商品;    

    <select id="selectById"
            parameterType="integer"
            resultType="goods">
        select *
        from goods
        where id = #{id}
    </select>

(2)查找某个品类下的所有商品;

    

<select id="selectGoods"

            resultType="goods">

        select * from goods where 1=1

        <choose>

            <when test="sort !=null and sort !=''">

                and sort =#{sort}

            </when>

            <otherwise>

                and number is not null

            </otherwise>

        </choose>

    </select>

(3)根据名称进行模糊查找;

    <select id="selectByName"

            resultType="goods">

        select * from goods where 1=1

        <if test="name !=null and name !=''">

            and name like concat('%',#{name},'%')

        </if>

    </select>

(4)根据品牌和价格进行联合查找(品牌和价格信息都可以提供或不提供);

    

<select id="selectByUnite"

            resultType="goods">

        select * from goods where 1=1

        <choose>

            <when test="brand !=null and brand !=''">

                and brand =#{brand}

                <if test="price !=null and price !=''">

                    and price =#{price}

                </if>

            </when>

            <when test="test= price !=null and price !=''">

                and price =#{price}

            </when>

            <otherwise>

                and number is not null

            </otherwise>

        </choose>

    </select>

(5)查找 id 小于 5 的所有商品。

    

<select id="selectByIdArea"

            resultType="goods">

        select * from goods where id in

        <foreach collection="list" separator="," close=")" open="(" item="id">

            #{id}

        </foreach>

    </select>

四、讨论、心得遇到的问题与解决办法等)

在使用多个关键词进行搜索的时候,感觉when和if好像都可以达到最终目的,在这时需要对问题进行细化思考,来决定最终使用什么来实现相应功能。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值