mybatis代码


/*
//根据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();
}
*/
posted @ 2023-04-10 17:25  YE-  阅读(22)  评论(0编辑  收藏  举报