首页 > 其他分享 >5.26总结

5.26总结

时间:2023-05-26 21:45:29浏览次数:41  
标签:总结 sqlSessionFactory String brand SqlSession 5.26 sqlSession brandMapper

package com.itheima.test;
//测试用例

import com.itheima.mapper.BrandMapper;
import com.itheima.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.HashMap;
import java.util.List;
import java.util.Map;

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();

}



@Test
public void testSelectById() throws IOException {
    //接受参数
    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();

}


@Test
public void testSelectByCondition() 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);*/

    Map map = new HashMap();
    map.put("status" , status);
    map.put("companyName", companyName);
    map.put("brandName" , 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.执行方法
    //List<Brand> brands = brandMapper.selectByCondition(status, companyName, brandName);
    //List<Brand> brands = brandMapper.selectByCondition(brand);
    List<Brand> brands = brandMapper.selectByCondition(map);
    System.out.println(brands);

    //5.释放资源
    sqlSession.close();

}


@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);

    /*Map map = new HashMap();
    map.put("status" , status);
    map.put("companyName", companyName);
    map.put("brandName" , 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.执行方法
    //List<Brand> brands = brandMapper.selectByCondition(status, companyName, brandName);
    //List<Brand> brands = brandMapper.selectByCondition(brand);
    //List<Brand> brands = brandMapper.selectByCondition(map);
    List<Brand> brands = brandMapper.selectByConditionSingle(brand);
    System.out.println(brands);

    //5.释放资源
    sqlSession.close();
}


@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. 获取SqlSessionFactory
    String resource = "mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

    //2. 获取SqlSession对象
    SqlSession sqlSession = sqlSessionFactory.openSession();
    //SqlSession sqlSession = sqlSessionFactory.openSession(true);//提交事务是否自动

    //3. 获取Mapper接口的代理对象
    BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);

    //4. 执行方法

    brandMapper.add(brand);

    //提交事务,注意,没有自动提交,数据库不会更新
    sqlSession.commit();

    //5. 释放资源
    sqlSession.close();

}


@Test
public void testAdd2() 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. 获取SqlSessionFactory
    String resource = "mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

    //2. 获取SqlSession对象
    SqlSession sqlSession = sqlSessionFactory.openSession();
    //SqlSession sqlSession = sqlSessionFactory.openSession(true);

    //3. 获取Mapper接口的代理对象
    BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);

    //4. 执行方法

    brandMapper.add(brand);
    Integer id = brand.getId();
    System.out.println(id);

    //提交事务
    sqlSession.commit();

    //5. 释放资源
    sqlSession.close();

}


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


    //封装对象
    Brand brand = new Brand();
    brand.setStatus(status);
    brand.setCompanyName(companyName);
    brand.setBrandName(brandName);
    brand.setDescription(description);
    brand.setOrdered(ordered);
    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();
    //SqlSession sqlSession = sqlSessionFactory.openSession(true);

    //3. 获取Mapper接口的代理对象
    BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);

    //4. 执行方法


    int count = brandMapper.update(brand);
    System.out.println(count);
    //提交事务
    sqlSession.commit();

    //5. 释放资源
    sqlSession.close();

}


@Test
public void testDeleteById() throws IOException {
    //接收参数

    int id = 5;


    //1. 获取SqlSessionFactory
    String resource = "mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

    //2. 获取SqlSession对象
    SqlSession sqlSession = sqlSessionFactory.openSession();
    //SqlSession sqlSession = sqlSessionFactory.openSession(true);

    //3. 获取Mapper接口的代理对象
    BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);

    //4. 执行方法

    brandMapper.deleteById(id);

    //提交事务
    sqlSession.commit();

    //5. 释放资源
    sqlSession.close();

}


@Test
public void testDeleteByIds() throws IOException {
    //接收参数

    int[] ids = {5,7,8};


    //1. 获取SqlSessionFactory
    String resource = "mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

    //2. 获取SqlSession对象
    SqlSession sqlSession = sqlSessionFactory.openSession();
    //SqlSession sqlSession = sqlSessionFactory.openSession(true);

    //3. 获取Mapper接口的代理对象
    BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);

    //4. 执行方法

    brandMapper.deleteByIds(ids);

    //提交事务
    sqlSession.commit();

    //5. 释放资源
    sqlSession.close();

}

}

标签:总结,sqlSessionFactory,String,brand,SqlSession,5.26,sqlSession,brandMapper
From: https://www.cnblogs.com/XiMenXve/p/17435887.html

相关文章

  • 5.26 C++文件读写操作
    程序运行时产生的数据都属于临时数据,程序—旦运行结束都会被释放通过文件可以将数据持久化C++中对文件操作需要包含头文件<fstream>文件类型分为两种:1.文本文件:文件以文本的ASCII码形式存储在计算机中2.二进制文件:文件以文本的二进制形式存储在计算机中操作文件的三大类:ofst......
  • 2023.5.26每日总结
    packageservlets;importjava.io.IOException;importjava.util.ArrayList;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjava......
  • 2023.5.26——软件工程日报
    所花时间(包括上课):6h代码量(行):0行博客量(篇):1篇今天,上午学习,下午学习。我了解到的知识点:1.了解了一些数据库的知识;2.了解了一些python的知识;3.了解了一些英语知识;5.了解了一些Javaweb的知识;4.了解了一些数学建模的知识;6.了解了一些计算机网络的知识;......
  • 2023.5.26——软件工程站立会议(阶段二)
    站立会议内容:1.整个项目预期的任务量:目前已经花的时间:剩余的时间:2.任务看板照片: 3.团队照片: 4.产品状态:最新做好的功能:正在完成中5.燃尽图:......
  • 5.26打卡
    #include<bits/stdc++.h>usingnamespacestd;classExamInfo{public:ExamInfo(stringname,chargrade):name(name),mode(GRADE),grade(grade){}ExamInfo(stringname,boolpass):name(name),mode(PASS),pass(pass){}ExamInfo(strin......
  • 总结Vue3 的一些知识点:Vue3 项目打包
    Vue3项目打包打包Vue项目使用以下命令:cnpmrunbuild执行以上命令,输出结果如下:执行完成后,会在Vue项目下会生成一个dist目录,该目录一般包含index.html文件及static目录,static目录包含了静态文件js、css以及图片目录images(如果有图片的话)。如果直接双击打开......
  • 总结Vue3 的一些知识点:Vue3 计算属性
    Vue3计算属性计算属性关键词:computed。计算属性在处理一些复杂逻辑时是很有用的。可以看下以下反转字符串的例子:实例1<divid="app">{{message.split('').reverse().join('')}}</div>实例1中模板变的很复杂起来,也不容易看懂理解。接下来我们看看使用了计算属......
  • 5月23日总结
    什么是MAF和MEF?MEF和MEF微软官方介绍:https://learn.microsoft.com/zh-cn/dotnet/framework/mef/MEF是轻量化的插件框架,MAF是复杂的插件框架。因为MAF有进程隔离和程序域隔离可选。我需要插件进程隔离同时快速传递数据,最后选择了MAF。如果不需要真正的物理隔离还是建议使用......
  • 5月22日总结
    深入理解apply()方法apply(thisArg)apply(thisArg,argsArray)thisArg在func函数运行时使用的this值。请注意,this可能不是该方法看到的实际值:如果这个函数处于非严格模式下,则指定为null或undefined时会自动替换为指向全局对象,原始值会被包装。argsArray可选一个......
  • 5月26日总结
    ArcMap手动新建矢量要素的方式合集-GIS空间分析(7)1.地统计学的基本概念及公式详解04-242.单窗算法的地表温度反演:谷歌地球引擎GEE代码04-263.SPSS计算极值、平均值、中位数、方差、偏度、峰度、变异系数05-084.Python忽略NoData计算多张遥感影像的像元平均值:whitebox库......