首页 > 其他分享 >mybatis代码

mybatis代码

时间:2023-04-10 17:33:06浏览次数:41  
标签:sqlSessionFactory resource String 代码 sqlSession brandMapper mybatis brand


/*
//根据id查询详情
public void selectById() 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();
}



条件查询:根据status进行等值查询,根据company_name和brand_name进行模糊查询
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();
}

//单条件动态查询
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. 获取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.selectByConditionSingle(brand);
System.out.println(brands);

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

/*

//添加数据
public void testAdd() throws IOException {
//接受参数
//现在是固定数据,以后会变成动态数据
int id = 6;
String brandName = "8848钛合金手机,尊贵";
String companyName = "8848";
String ordered = "100";
String description = "人机分离十米自动爆炸";
int status = 1;

//处理参数,定义一个关键字,将查询的关键字封装
Brand brand = new Brand();
brand.setId(id);
brand.setBrandName(brandName);
brand.setCompanyName(companyName);
brand.setOrdered(ordered);
brand.setDescription(description);
brand.setStatus(status);


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

*/
/* int ll = brand.getId();
System.out.println(ll);*//*

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


/*
//添加数据
public void testAdd() throws IOException {
//接受参数
//现在是固定数据,以后会变成动态数据
int id = 4;
String brandName = "刘紫锦";
String companyName = "喜欢";
String ordered = "叶照心";
String description = "!!!!!";
int status = 0;

//处理参数,定义一个关键字,将查询的关键字封装
Brand brand = new Brand();
brand.setId(id);
brand.setBrandName(brandName);
brand.setCompanyName(companyName);
brand.setOrdered(ordered);
//brand.setDescription(description);
brand.setStatus(status);


//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.update(brand);

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

/*
//删除数据
public void testDeleteById() throws IOException {
//接受参数
//现在是固定数据,以后会变成动态数据
int id = 1;

//处理参数,定义一个关键字,将查询的关键字封装
Brand brand = new Brand();
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.执行方法
brandMapper.deleteById(brand.getId());

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

/*public void testDeleteByIds() throws IOException {
//接受参数
//现在是固定数据,以后会变成动态数据
int[] ids = {4,5};

//处理参数,定义一个关键字,将查询的关键字封装
*//*Brand brand = new Brand();
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.执行方法
brandMapper.deleteByIds(ids);

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

/*
public void userselect() 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();
SqlSession sqlSession = sqlSessionFactory.openSession();

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

//4.执行方法
String username = "zhangsan";
String password = "123";

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

标签:sqlSessionFactory,resource,String,代码,sqlSession,brandMapper,mybatis,brand
From: https://www.cnblogs.com/yzx-sir/p/17303636.html

相关文章

  • MyBatisPlus——代码生成器
    代码生成器快速生成各项代码步骤创建Generator类,并创建main方法创建代码生成器AutoGeneratorautoGenerator=newAutoGenerator();连接要生成实体类的数据库DataSourceConfigdataSource=newDataSourceConfig();dataSource.setDriverName(......
  • MyBatisPlus——DML编程控制——乐观锁
    乐观锁业务并发现象带来的问题:秒杀最后一单用于中小型项目(2000请求以下)添加一个数据库字段,使每次请求修改数据时,这个字段就加一,当有多人同时请求时,这些人同时获取到的都是相同的该字段,但当有一人完成了秒杀后字段加一,其他同时获取到的该字段就不匹配了配置步骤数据库表中......
  • 低代码 系列 —— 中后台集成低代码预研
    其他章节请看:低代码系列中后台集成低代码预研背景笔者目前维护一个react中后台系统(以spug为例),每次来了新的需求都需要前端人员重新开发。前面我们已经对低代码有了一定的认识,如果能通过一个可视化的配置页面就能完成前端开发,将极大的提高前端(或后端)的效率。甚至能加快......
  • spring注解整合mybatis
    引入依赖<!--mybatis-spring与mybatis使用低版本适配--><!--https://mvnrepository.com/artifact/org.mybatis/mybatis-spring--><dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version&......
  • MybatisPlus——DML编程控制——增删改
    DML编程控制id生成策略控制不同的表应用不同的id生成策略日志:自增(1,2,3,4,......)购物订单:特殊规则(FQ23948AK3843)外卖单:关联地区日期等信息(1004202003143491)关系表:可省略id......注解控制@TableId类型:属性注解位置:模型类中用于表示主键的属性定义上方作用:设置......
  • Mybatis-Plus详解(一篇带入了解底层原理)
    一.MP简介我们知道,Mybatis属于一个半自动的ORM框架。之所以说Mybatis是一个半自动的ORM框架,原因是它还需要我们自己在注解或是映射文件中编写SQL语句,并没有实现完全的自动化。SQL语句的编写,虽然增加了项目和业务需求实现的灵活性,但对一些基本表的操作而言,无疑增加了数据库操作的......
  • 只有一个人说了真话!通解代码——python
    不管几个人说了这话,代码逻辑都是一样的,无非参数不同。例大老鼠发现家里的奶酪少了一大块,审问四只小老鼠ABCD,其实只有一只老鼠偷吃了奶酪。A说:我没吃。B说:是C吃的。C说:肯定是D吃的。D说:C在冤枉我。己知四只小老鼠中有一只说的是真话,三只说的是假话。到底是谁偷吃了......
  • OpenTiny 跨端、跨框架组件库升级TypeScript,10万行代码重获新生
    摘要:一份精心准备的《JS项目改造TS指南》文档供大家参考,顺便介绍TS基础知识和TS在Vue中的实践。本文分享自华为云社区《历史性的时刻!OpenTiny跨端、跨框架组件库正式升级TypeScript,10万行代码重获新生!》,作者:Kagol。根据TheSoftwareHouse发布的《2022前端开发市场状......
  • 解密prompt系列5. APE+SELF=自动化指令集构建代码实现
    上一章我们介绍了不同的指令微调方案,这一章我们介绍如何降低指令数据集的人工标注成本!这样每个人都可以构建自己的专属指令集,哈哈当然我也在造数据集进行时~介绍两种方案SELFInstruct和AutomaticPromptEngineer,前者是基于多样的种子指令,利用大模型的上下文和指令理解能力,......
  • 全网最详细中英文ChatGPT-GPT-4示例文档-食谱智能生成从0到1快速入门——官网推荐的48
    目录Introduce简介setting设置Prompt提示Sampleresponse回复样本APIrequest接口请求python接口请求示例node.js接口请求示例curl命令示例json格式示例其它资料下载ChatGPT是目前最先进的AI聊天机器人,它能够理解图片和文字,生成流畅和有趣的回答。如果你想跟上AI时代的潮流......