首页 > 其他分享 >MyBatis增删改查之添加、修改、删除操作

MyBatis增删改查之添加、修改、删除操作

时间:2023-02-23 22:35:25浏览次数:52  
标签:int Brand brand 改查 增删 sqlSession brandName MyBatis id

一、添加操作
Mapper接口

void add(Brand brand);

SQL语句

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

测试

 @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);
        //brand.setBrandName(brandName);
        //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. 执行方法
        brandMapper.add(brand);
        //提交事务
        sqlSession.commit();
        //5. 释放资源
        sqlSession.close();
    }

二、修改操作

Mapper接口

int update(Brand brand);

SQL语句

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

测试

     @Test
    public void testUpdate() throws IOException {
    //接收参数
        int status = 0;
        String companyName = "波导手机";
        String brandName = "波导";
        String description= "手机中的战斗机";
        int ordered = 200;
        int id = 6;
    // 处理参数

    //封装对象
        Brand brand = new Brand();
        brand.setStatus(status);
       //brand.setCompanyName(companyName);
        //brand.setBrandName(brandName);
        //brand.setDescription(description);
        //brand.setOrdered(ordered);
        //brand.setBrandName(brandName);
        brand.setId(id);
        //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. 执行方法
        int count = brandMapper.update(brand);
        System.out.println(count);
        //5. 释放资源
        sqlSession.close();
    }

三、删除操作

单行删除

Mapper接口

     /*
    根据id删除
     */
    void deleteById(int id);

SQL语句

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

测试

    @Test
    public void testDelete() throws IOException {
    //接收参数
        int id = 5;
    // 处理参数

    //封装对象
        Brand brand = new Brand();
        //brand.setCompanyName(companyName);
        //brand.setBrandName(brandName);
        //brand.setDescription(description);
        //brand.setOrdered(ordered);
        //brand.setBrandName(brandName);
        brand.setId(id);
        //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. 执行方法
        brandMapper.deleteById(id);
        //提交事务
        sqlSession.commit();
        //5. 释放资源
        sqlSession.close();
    }

批量删除

Mapper接口

    /*
    批量删除
     */
    void deleteByIds(int[] ids);

SQL语句

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

测试

    @Test
    public void testDeleteIds() throws IOException {
        //接收参数
        int[] ids = {1,2,3};
        // 处理参数

        //封装对象
        Brand brand = new Brand();
        //brand.setCompanyName(companyName);
        //brand.setBrandName(brandName);
        //brand.setDescription(description);
        //brand.setOrdered(ordered);
        //brand.setBrandName(brandName);
        //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. 执行方法
        brandMapper.deleteByIds(ids);
        //提交事务
        sqlSession.commit();
        //5. 释放资源
        sqlSession.close();
    }

 

标签:int,Brand,brand,改查,增删,sqlSession,brandName,MyBatis,id
From: https://www.cnblogs.com/ysk0904/p/17149695.html

相关文章

  • MyBatis-Plus 代码生成
    MyBatis-Plus的代码生成功能十分人性化,即支持通过简单的配置实现,也可以通过自定义模板实现。这里列出项目中的常用配置供参考,其他配置可以参考官网:https://baomidou.com/......
  • mybatis-puls解决多数据源事务的问题
    直接上代码:pom:<!--JTA组件核心依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jt......
  • 三、MybatisPlus常用注解
    @TableName经过以上的测试,在使用MyBatis-Plus实现基本的CRUD时,我们并没有指定要操作的表,只是在Mapper接口继承BaseMapper时,设置了泛型User,而操作的表为user表。由此得出......
  • mybatis中修改的两种方式
    1、updateById:传入的实体就是修改后的值;传入的实体一般只有需要修改的属性有值,对于那些没有值的属性保持原值不变。    2、update ......
  • Mongodb-使用javaDriver 实现增删改查
    1.回顾  上篇已经学习了使用Mongodb_C#Driver实现增删改查2.重点这篇将学习怎样使用和学习Mongodb_javaDriver实现增删改查3.准备篇   在使用的时候请先准......
  • String集合拼多个or,模糊查询。mybatis-plus-构造器的写法
    List<String>list=newArrayList<>();QueryWrapper<Object>queryWrapper=newQueryWrapper<>();queryWrapper.and(CollUtil.isNotEmpty(list),qw->{list.fo......
  • ssm学习笔记23001-mybatis-config.xml配置详解
    mybatis-config.xml是mybatis的配置文件,具体的一些标签和属性如下:需要注意的是1、配置项是有顺序要求的2、简写的配置有要求:例如类型别名,如果要简写,需要省掉alias属性,m......
  • ssm学习笔记23001-spring+mybatis修改删除和查询
    spring+mybatis修改删除和查询1、在UserMapper接口类中,创建接口在UserMapper接口类中,创建接口packagecom.wjw.mybatis.mapper;importcom.wjw.mybatis.pojo.User;......
  • 整合mybatis-spring
    一.整合mybatis步骤:第一步:导入相关的jar包:junitmybatismysql数据库spring相关的aop植入mybatis-spring【新包,兼容mybatis和spring】<dependencies>......
  • Mybatis-Plus取消自动转驼峰拦截器
    背景项目使用Mybatis-Plus,开启了全局结果集字段转驼峰map-underscore-to-camel-case:true。开启之后如果需要返回下划线需要自定义resultMap现需要返回List<Map>,且返回......