从多个条件中选择一个
choose(when,otherwise): 选择,类似于java中的switch语句
编写接口方法:Mapper接口
参数:除了id之外的所有数据
结果:void
编写sql语句:sql映射文件
执行方法,测试
此时注意:在添加数据的时候如果之前有数据库表中的数据删除过,那么所添加的信息的主键id将会继承之之前被删除的数据的主键的id序号并且继续自增;
解决方法:在图形化工具中,或者是控制台重新定义当前所要添加的主键序列号
-- 修改id自增排序
alter table tb_brand auto_increment = 4; -- 此时的主键id序列就被改为了4,即下一条数据的主键id为4
public void dataAdd() {
int status = 0;
String brand_name = "8848钛金手机";
String address = "北京";
int ordered = 20;
// 封装参数
Brand brand = new Brand();
brand.setStates(status);
brand.setName(brand_name);
brand.setAddress(address);
brand.setOrdered(ordered);
InputStream is = null;
try {
is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory sf = sqlSessionFactoryBuilder.build(is);
SqlSession sqlSession = sf.openSession(true);
// 获取mapper接口
BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
// 执行sql语句
mapper.add(brand);
// List result = mapper.selectByCondition(status, brand_name, address);
// 返回执行结果
// 释放资源
sqlSession.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
============================================================
Process finished with exit code 0
执行结果为:
public void dataAdd() {
int status = 0;
String brand_name = "8848钛金手机";
String address = "北京";
int ordered = 20;
// 封装参数
Brand brand = new Brand();
brand.setStates(status);
brand.setName(brand_name);
brand.setAddress(address);
brand.setOrdered(ordered);
InputStream is = null;
try {
is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory sf = sqlSessionFactoryBuilder.build(is);
SqlSession sqlSession = sf.openSession(true);
// 获取mapper接口
BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
// 执行sql语句
mapper.add(brand);
// List result = mapper.selectByCondition(status, brand_name, address);
// 返回执行结果-->得到id的值
Integer id = brand.getId();
// 将得到的id输出再控制面板
System.out.println("执行的对象的id为:"+id);
// 释放资源
sqlSession.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
=======================================================
执行的对象的id为:6
Process finished with exit code 0
在mapper接口文件中定义update方法:
在xml文件中映射接口文件:
在test文件中编写java代码实现修改数据的sql:
// 修改数据库中的数据
public void update() {
int status = 0;
String brand_name = "智能老人机";
String address = "北京";
int ordered = 100;
int id = 4;
// 封装参数
Brand brand = new Brand();
brand.setId(id);
brand.setStates(status);
brand.setName(brand_name);
brand.setAddress(address);
brand.setOrdered(ordered);
InputStream is = null;
try {
is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory sf = sqlSessionFactoryBuilder.build(is);
SqlSession sqlSession = sf.openSession(true);
// 获取mapper接口
BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
int result = mapper.update(brand);
// 控制台返回执行结果
System.out.println("结果为:"+result);
// 释放资源
sqlSession.close();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
============================================
结果为:1
Process finished with exit code 0