• Mybatis练习(按值单条件查询)


    Mybatis练习

    学习路线Mybatis快速入门->Mapper代理开发
    ->Mybatis练习(按值单条件查询)
    ->Mybatis练习(多条件查询)
    ->Mybatis练习(增加,删除,修改)

    安装MybatisX

    在这里插入图片描述
    在这里插入图片描述

    接下来我们就使用Mybatis完成品牌数据的增删改查操作。以下是我们要完成功能列表:

    • 查询
      • 查询所有数据
      • 查询详情
      • 条件查询
    • 添加
    • 修改
      • 修改全部字段
      • 修改动态字段
    • 删除
      • 删除一个
      • 批量删除

    创建数据库

    • 数据库表(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);
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
    • 创建Brand实体类

      com.study.pojo 包下创建 Brand 实体类。

    package com.study.pojo;
    
    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 +
                    '}';
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77

    编写接口方法

    com.study.mapper 包写创建名为 BrandMapper 的接口。并在该接口中定义 List selectAll() 方法。

    package com.study.mapper;
    
    import com.study.pojo.Brand;
    
    import java.util.List;
    
    public interface BrandMapper {
    
        /**
         * 查询所有
         */
        List<Brand> selectAll();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    编写BrandMapper.xml

    reources 下创建 com/study/mapper 目录结构,并在该目录下创建名为 BrandMapper.xml 的映射配置文件

    
    DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    
    <mapper namespace="com.study.mapper.BrandMapper">
        <resultMap id="brandResultMap" type="brand">
            
            <result column="brand_name" property="brandName"/>
            <result column="company_name" property="companyName"/>
        resultMap>
    
    
    
        <select id="selectAll" resultMap="brandResultMap">
            select *
            from tb_brand;
        select>
    mapper>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    编写logback.xml和mybatis-config.xml

    这里参考文章Mapper代理开发

    编写测试方法

    • 编写测试用例
      在这里插入图片描述
    package com.study.test;
    
    import com.study.mapper.BrandMapper;
    import com.study.pojo.Brand;
    import org.apache.ibatis.io.Resources;
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    import org.junit.Test;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.List;
    
    public class MyBatisTest {
        @Test
        public void testSelectAll() throws IOException {
            //1. 获取SqlSessionFactory
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    
            //2. 获取SqlSession对象
            SqlSession sqlSession = sqlSessionFactory.openSession();
    
            //3. 获取Mapper接口的代理对象
            BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
    
            //4. 执行方法
            List<Brand> brands = brandMapper.selectAll();
            System.out.println(brands);
    
            //5. 释放资源
            sqlSession.close();
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    运行结果讲解

    在这里插入图片描述

    根据id来查询

    • 编写接口方法:Mapper接口

      • 参数:id

        查看详情就是查询某一行数据,所以需要根据id进行查询。而id以后是由页面传递过来。

      • 结果:Brand

        根据id查询出来的数据只要一条,而将一条数据封装成一个Brand对象即可

    • 编写SQL语句:SQL映射文件

    • 执行方法、进行测试

    编写接口方法

    BrandMapper 接口中定义根据id查询数据的方法

    /**
      * 查看详情:根据Id查询
      */
    Brand selectById(int id);
    
    • 1
    • 2
    • 3
    • 4

    编写SQL语句

    BrandMapper.xml 映射配置文件中编写 statement,使用 resultMap 而不是使用 resultType

    <select id="selectById"  resultMap="brandResultMap">
        select *
        from tb_brand where id = #{id};
    select>
    
    • 1
    • 2
    • 3
    • 4

    注意:上述SQL中的 #{id}先这样写,一会我们再详细讲解

    编写测试方法

    test/java 下的 com.study.mapper 包下的 MybatisTest类中 定义测试方法

     @Test
    public void testSelectById() throws IOException {
        //接收参数,该id以后需要传递过来
        int id = 1;
    
        //1. 获取SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    
        //2. 获取SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
    
        //3. 获取Mapper接口的代理对象
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
    
        //4. 执行方法
        Brand brand = brandMapper.selectById(id);
        System.out.println(brand);
    
        //5. 释放资源
        sqlSession.close();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    运行结果
    在这里插入图片描述

    SQL语句中特殊字段处理

    我们开发的时候有可能会遇到某些特殊字符,导致报错的情况发生。
    在这里插入图片描述

    在这里插入图片描述

    在这里插入图片描述

  • 相关阅读:
    c++数组教程
    安全机制(security) - 加解密算法 - 对称加密 - 加解密模式
    Kubernetes常用命令
    Vue前端框架08 Vue框架简介、VueAPI风格、模板语法、事件处理、数组变化侦测
    vue3项目运行时解析文件的顺序
    解决sass问题:npm ERR! node-sass@9.0.0 postinstall: `node scripts/build.js`
    鸿蒙开发工程师面试-架构篇
    【创作活动】作为程序员的那些愚蠢瞬间
    警钟:SBP持有的MogaFX外汇储备暴跌9.56亿美元,达到79.6亿美元
    微信内测朋友圈内容转发功能;快手前副总裁侵占756万余元,一审获刑7年;​俄罗斯法院驳回苹果上诉,将继续进行反垄断调查|极客头条
  • 原文地址:https://blog.csdn.net/qq_51447496/article/details/128153059