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.sql.Connection;
import java.sql.PreparedStatement;
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);
//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.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 = 6;
//
没有合适的资源?快使用搜索试试~ 我知道了~
黑马MyBatis(配套源码)
共46个文件
xml:25个
class:8个
java:8个
需积分: 0 1 下载量 128 浏览量
2024-08-08
22:11:11
上传
评论
收藏 47KB ZIP 举报
温馨提示
http://t.csdnimg.cn/fAEpc
资源推荐
资源详情
资源评论
收起资源包目录
mybatis-demo.zip (46个子文件)
mybatis-demo
pom.xml 2KB
src
test
java
com
itheima
test
UserMapperTest.java 2KB
MyBatisTest.java 11KB
main
resources
logback.xml 870B
mybatis-config.xml 2KB
com
itheima
mapper
BrandMapper.xml 7KB
UserMapper.xml 717B
java
com
itheima
MyBatisDemo.java 1KB
mapper
UserMapper.java 2KB
BrandMapper.java 1KB
pojo
Brand.java 2KB
User.java 1KB
MyBatisDemo2.java 1KB
.idea
jarRepositories.xml 880B
dataSources
f8d210f4-a914-4aab-bd44-e81ebacf9fca
storage_v2
_src_
schema
information_schema.FNRwLQ.meta 76B
f8d210f4-a914-4aab-bd44-e81ebacf9fca.xml 29KB
dataSources.local.xml 1001B
libraries
Maven__org_hamcrest_hamcrest_core_1_3.xml 646B
Maven__junit_junit_4_13.xml 569B
Maven__ch_qos_logback_logback_core_1_2_3.xml 661B
Maven__org_mybatis_mybatis_3_5_5.xml 614B
Maven__ch_qos_logback_logback_classic_1_2_3.xml 682B
Maven__mysql_mysql_connector_java_5_1_46.xml 688B
Maven__org_slf4j_slf4j_api_1_7_20.xml 627B
workspace.xml 10KB
misc.xml 390B
dataSources.xml 542B
compiler.xml 649B
modules.xml 271B
.gitignore 0B
mybatisx
templates.xml 293B
target
classes
logback.xml 870B
mybatis-config.xml 2KB
com
itheima
mapper
BrandMapper.xml 7KB
BrandMapper.class 722B
UserMapper.xml 717B
UserMapper.class 663B
MyBatisDemo2.class 2KB
MyBatisDemo.class 2KB
pojo
User.class 2KB
Brand.class 2KB
test-classes
com
itheima
test
MyBatisTest.class 6KB
UserMapperTest.class 2KB
maven-status
maven-compiler-plugin
compile
default-compile
createdFiles.lst 191B
inputFiles.lst 425B
generated-test-sources
test-annotations
generated-sources
annotations
mybatis-demo.iml 1KB
共 46 条
- 1
资源评论
去薯条搞点码头
- 粉丝: 286
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- matlab:基于遗传算法的多无人机协同任务分配 - 基于遗传算法的多无人机协同任务分配 - 种群中的每一个个体代表一次完整的任
- 施耐德Control Expert v15.0SP1工具 授权 系列号 SN
- 配电网光伏储能双层优化配置模型(选址定容) matlab+matpower 参考文档:含高比例可再生能源配电网灵活资源双层优化
- Untitled-2.html
- 橙色的食品零售综合商城模板打包下载.zip
- 【kk梦空间专业开发】食品类静态html模板打包下载.zip
- windows 下 libssh2编译后的库
- deepinlinuxv23给ax88772b有线网卡装驱动
- 安卓聊天工具开发(核心部分教程,附源代码)
- 8.9.58 麦当劳小猫气泡一.apk
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功