栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

MyBatis配置文件完成增删改查

文章目录
  • MyBatis配置文件完成增删改查
    • 案例:完成品牌数据的增删改查操作
      • 要完成的功能清单:
      • 准备环境
    • MyBatis完成增删改查
      • 查询
        • 查询所有数据
        • 查看详情功能
        • 条件查询
          • 1.多条件查询
          • 2.单条件动态查询
      • 添加
      • MyBatis事务
      • 修改
        • 修改全部字段
        • 修改动态字段
      • 删除功能
        • 删除单个
        • 批量删除
    • 动态SQL

案例:完成品牌数据的增删改查操作 要完成的功能清单:
  1. 查询
    • 查询所有数据
    • 查看详情
    • 条件查询
  2. 添加
  3. 修改
    • 修改全部字段
    • 修改动态字段
  4. 删除
    • 删除一个
    • 批量删除
准备环境
  • 搭建Maven

  • 添加依赖

    
    
        4.0.0
    
        com.ityc
        maven-demo
        1.0-SNAPSHOT
    
        
        
            
                mysql
                mysql-connector-java
                5.1.32
            
    
            
            
                com.alibaba
                druid
                1.1.12
            
    
    
            
                junit
                junit
                4.13
                test
            
            
            
                org.mybatis
                mybatis
                3.5.5
            
    
    
            
                org.slf4j
                slf4j-api
                1.7.20
            
            
                ch.qos.logback
                logback-classic
                1.2.3
            
    
            
                ch.qos.logback
                logback-core
                1.2.3
            
    
        
    
    
  • 数据库表

-- 创建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 id,brand_name, company_name, ordered, description, `status` from tb_brand;
  • 实体类Brand
package com.ityc.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 +
                '}';
    }
}

  • 测试用例
  • 安装MyBatisX插件
    1. 打开Setting–>打开Plugins–>搜索MyBatisX,安装
    2. 作用:xml和接口方法相互跳转,根据接口方法生成statement
MyBatis完成增删改查 查询 查询所有数据
  1. 编写接口方法:Mapper接口

    • 参数:无

    • 结果:List

    package com.ityc.mapper;
    import com.ityc.pojo.Brand;
    import java.util.List;
    //接口
    public interface BrandMapper {
    
        public List selectALL();
    }
    
    
  2. 编写sql语句:SQL映射文件:

    
    
    
    
        
        
    
    
        
        
            
            
            
        
    
    
        
            select * from tb_brand where id = #{id};
    
    
  3. 执行方法:测试

     @Test
        public void testSelectById() throws IOException {
            int id =2;
    
            //1 获取 SqlSessionFaction对象
            //加载mybatis的核心配置文件,获取SqlSessionFactory
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    
            //获取SqlSession对象
            SqlSession sqlSession = sqlSessionFactory.openSession();
    
            //3. 获取mapper 接口的代理对象
            BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
    
            //4.执行方法
    
            Brand brand = mapper.selectById(id);
    
            System.out.println(brand);
    
            //5.释放资源
            sqlSession.close();
        }
    

总结:

  1. 参数占位符
    1. #{}    会替换为?,用来防止sql注入
    2. ${}    拼 sql ,会存在sql注入问题
    3.使用时机:
        *参数传递的时候:#{}
        *表明 列名不固定能使用 ${} 但是只要使用${}就一定存在sql注入问题
    
    *sql语句特殊字符的处理
     1. 转义字符
     2.CDATA区
    
条件查询 1.多条件查询
  1. 编写接口方法Mapper接口

    参数:所有查询条件

    结果:list

    //1.散装的参数
    List selectByCondition(@Param("status") int status,@Param("companyName") String companyName,@Param("brandName") String brandName);
    //2.对象的参数
     List selectByCondition(Brand brand);
    // 3.Map集合参数
    List selectByCondition(Map map);
    
  2. 编写SQL语句:sql映射文件

     
            select *
            from tb_brand
            where
    #         
                
                  status = #{status}
                
                
                    company_name like #{companyName}
                
                
                    brand_name like #{brandName}
                
            //保底的otherwise,当用户一个都不选时执行
            	
                    1=1
                
    
            
    

    测试类

    @Test
        public void testSelectByConditionSingle() throws IOException {
            //定义局部变量,接收参数
            int status =1;
            String companyName = "华为";
            String brandName = "华为";
            //处理参数
            companyName ="%"+ companyName +"%";
            brandName ="%"+ brandName +"%";
    
            //封装对象
             Brand brand = new Brand();
             brand.setStatus(status);
             //brand.setCompanyName(companyName);
             //brand.setBrandName(brandName);
    
            //1 获取 SqlSessionFaction对象
            //加载mybatis的核心配置文件,获取SqlSessionFactory
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    
            //获取SqlSession对象
            SqlSession sqlSession = sqlSessionFactory.openSession();
    
            //3. 获取mapper 接口的代理对象
            BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
    
            //4.执行方法
    
            List brands = mapper.selectByConditionSingle(brand);
    
            System.out.println(brands);
    
            //5.释放资源
            sqlSession.close();
        }
    
  3. 添加

    步骤

    1. 编写接口方法
    • 参数:除了id以外的所有数据

    • 结果void

      //添加功能
          void add(Brand brand);
      
    1. 编写SQL语句:sql映射文件

        
            insert into tb_brand(brand_name, company_name, ordered, description, status)
              values(#{brandName},#{companyName},#{ordered},#{description},#{status});
        
      
    2. 执行方法,测试

    //添加功能
        @Test
        public void testAdd() throws IOException {
            int status =1;
            String companyName = "旺旺碎冰";
            String brandName ="旺旺";
            String description = "给你哦破";
            int ordered =100;
            //封装对象
            Brand brand = new Brand();
            brand.setStatus(status);
            brand.setCompanyName(companyName);
            brand.setBrandName(brandName);
            brand.setDescription(description);
            brand.setOrdered(ordered);
            //1 获取 SqlSessionFaction对象
            //加载mybatis的核心配置文件,获取SqlSessionFactory
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            //获取SqlSession对象
            SqlSession sqlSession = sqlSessionFactory.openSession();
    
            //3. 获取mapper 接口的代理对象
            BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
    
            //4.执行方法
            mapper.add(brand);
    
            //手动提交事务
            sqlSession.commit();
            //5.释放资源
            sqlSession.close();
        }
    
    
    MyBatis事务
    • openSession():开启事务,进行增删改操作后需要使用sqlSession.commit();手动提交事务

    • openSession(true):可以设置为自动提交事务(关闭事务)

    • 添加----主键返回

      在数据添加成功后,需要获取插入数据库数据的主键的值

      
      

      比如:添加订单和订单项

      1.添加订单

      2.添加订单项,订单项中需要设置所属订单的id

    修改 修改全部字段

    步骤:

    1.编写接口方法Mapper

    • 参数,所有数据
    • 结果:void
     //修改
        void update(Brand brand);
    

    2.编写sql语句:sql映射文件

        
            update tb_brand
            set brand_name= #{brandName},
                company_name = #{companyName},
                ordered = #{ordered},
                description = #{description},
                status = #{status}
            where id =#{id};
        
    

    3.执行方法,测试

    //修改功能
        @Test
        public void testUpdate() throws IOException {
            int status = 1;
            String companyName = "旺旺碎冰";
            String brandName ="哇哇哇哇哇哇哇哇哇哇哇";
            String description = "给你哦破";
            int ordered =200;
            int id =5;
            //封装对象
            Brand brand = new Brand();
            brand.setId(id);
            brand.setStatus(status);
            brand.setCompanyName(companyName);
            brand.setBrandName(brandName);
            brand.setDescription(description);
            brand.setOrdered(ordered);
            //1 获取 SqlSessionFaction对象
            //加载mybatis的核心配置文件,获取SqlSessionFactory
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            //获取SqlSession对象
            SqlSession sqlSession = sqlSessionFactory.openSession();
    
            //3. 获取mapper 接口的代理对象
            BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
    
            //4.执行方法
            mapper.update(brand);
    
    
            //手动提交事务
            sqlSession.commit();
            //5.释放资源
            sqlSession.close();
        }
    
    修改动态字段

    因为上面的修改方法会修改所有字段,如果其他字段不填写就会被改为null,太过死板,所以就需要动态sql。

    步骤:

    1.编写接口方法Mapper

    • 参数,所有数据
    • 结果:void

    2.编写sql语句:sql映射文件

       
        
            update tb_brand
            
                
                    brand_name = #{brandName}
                
                
                    company_name = #{companyName}
                
                
                    ordered= #{ordered}
                
                
                    description = #{description}
                
                
                    status = #{status}
                
            
            where id = #{id};
        
    
    

    3.执行方法,测试

    删除功能 删除单个

    步骤:

    1.编写接口方法Mapper

    • 参数,id
    • 结果:void

    2.编写sql语句:sql映射文件

     
        
            delete from tb_brand where id = #{id};
        
    

    3.执行方法,测试

       //删除单个功能
        @Test
        public void testDeleteByid() throws IOException {
           int id= 5;
            //封装对象
            Brand brand = new Brand();
            brand.setId(id);
            //1 获取 SqlSessionFaction对象
            //加载mybatis的核心配置文件,获取SqlSessionFactory
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            //获取SqlSession对象
            SqlSession sqlSession = sqlSessionFactory.openSession();
    
            //3. 获取mapper 接口的代理对象
            BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
    
            //4.执行方法
            mapper.deleteByid(id);
    
            //手动提交事务
            sqlSession.commit();
            //5.释放资源
            sqlSession.close();
        }
    
    
    批量删除

    步骤:

    1.编写接口方法Mapper

    • 参数,id数组
    • 结果:void
      //批量删除
        //使用@Param 注解来改变Map集合的默认key的名称
        void deleteByids(@Param("ids") int[] ids);
    

    2.编写sql语句:sql映射文件

     
        
            delete from tb_brand
            where id
            in(
            
                #{id}
            
            );
        
    

    3.执行方法,测试

    //批量删除功能
        @Test
        public void testDeleteByids() throws IOException {
            int []ids ={6,7,8};
            //封装对象
            Brand brand = new Brand();
            //1 获取 SqlSessionFaction对象
            //加载mybatis的核心配置文件,获取SqlSessionFactory
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            //获取SqlSession对象
            SqlSession sqlSession = sqlSessionFactory.openSession();
    
            //3. 获取mapper 接口的代理对象
            BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
    
            //4.执行方法
            mapper.deleteByids(ids);
    
            //手动提交事务
            sqlSession.commit();
            //5.释放资源
            sqlSession.close();
        }
    
    
    动态SQL
    • SQL语句会随着用户的输入或者外部条件的变化而变化,我们称为动态SQL

    • 动态 SQL 是 MyBatis 的强大特性之一。如果你使用过 JDBC 或其它类似的框架,你应该能理解根据不同条件拼接 SQL 语句有多痛苦,例如拼接时要确保不能忘记添加必要的空格,还要注意去掉列表最后一个列名的逗号。利用动态 SQL,可以彻底摆脱这种痛苦。

    • MyBatis对动态sql有很强大的支撑

    • if:判断参数是否有值:使用test属性进行条件判断,存在问题第一个条件不需要逻辑运算符

    • choose(when,otherwise)

    • trim(where,set):where标签替换关键词where,帮我们解决if中存在的问题

    • foreach

    例:

    
    
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/820760.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号