单条件查询
(1).编写接口方法Mapper接口 List selectAllById();
参数:三个
返回值结果为:List
(2).编写sql语句:sql映射文件
(3).执行方法,测试
(4).结果
1.编写接口方法Mapper接口 Brand selectAllById();
//2.查看详情,根据id来查找对象 where id = ?;
public Brand selectAllById(int id);
2.编写sql语句:sql映射文件
参数占位符
1.#{} 会将其转换成?,防止sql注入问题 和prepareStatement类似
2.${} 拼sql,会存在sql注入问题
3.使用时机:
*参数传递的时候:#{}
*表明或者列明不固定的情况下:${},但是也存在sql注入问题
特殊字符的处理 例如:select * from tb_brand where id<${id};是不可以的
1.转义字符 小于号的转义字符 < select * from tb_brand where id < ${id};
2.CDATA区 在中间加入大写的CD确定 就可以正常写入小于号了
parameterType="integer"是参数类型,可以省略
select * from tb_brand where id = ${id};
3.执行方法,测试
@Test
public void selectById()throws IOException {
//1.加载mybatis的核心配置文件,获取SQL session factory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);//把字符串传进来,转换成一个字节输入流
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取SQL session的对象用它来执行sql
SqlSession SqlSession = sqlSessionFactory.openSession();
//3.获取BrandMapper接口的代理对象
BrandMapper brandMapper = SqlSession.getMapper(BrandMapper.class);
//4.执行方法
//定义一个局部变量用来接受参数
int id = 1;
Brand brand = brandMapper.selectAllById(id);
System.out.println(brand);
//5.释放资源
SqlSession.close();
}
多条件查询
(1).编写接口方法Mapper接口 List selectAllById();
参数:三个
返回值结果为:List
(2).编写sql语句:sql映射文件
(3).执行方法,测试
(4).结果
1.编写接口方法Mapper接口
多条件查询
1.散装参数 :如果方法中有多个参数,需要使用@param(“sql占位符的名称”)
2.对象作为参数 :对象属性名称要和参数占位符名称一致
3.集合作为参数
(1).编写接口方法Mapper接口 List
参数:三个
返回值结果为:List
(2).编写sql语句:sql映射文件
(3).执行方法,测试
(4).结果
1.编写接口方法Mapper接口(1).编写接口方法Mapper接口 List
selectAllById();
参数:三个
返回值结果为:List
(2).编写sql语句:sql映射文件
(3).执行方法,测试(4).结果
多条件查询
1.散装参数 :如果方法中有多个参数,需要使用@param(“sql占位符的名称”)
2.对象作为参数 :对象属性名称要和参数占位符名称一致
3.集合作为参数
(1).散装参数 :如果方法中有多个参数
ListselectByCondition (@Param("status")int status, @Param("companyName")String companyName, @Param("brandName")String brandName);
(2).对象作为参数
ListselectByCondition(Brand brand);
(3).集合作为参数
List2.编写sql语句:sql映射文件selectByCondition(Map map);
多条件查询
1.散装参数 :如果方法中有多个参数,需要使用@param(“sql占位符的名称”)
2.对象参数
3.集合参数
(1).散装参数 :如果方法中有多个参数
(2).对象作为参数
(3).集合作为参数
3.执行方法,测试
多条件查询
1.散装参数 :如果方法中有多个参数,需要使用@param(“sql占位符的名称”)
2.对象参数
3.集合参数
(1).散装参数 :如果方法中有多个参数
//定义三个局部变量用来接受参数
int status = 1;
String companyName = "华为";
String brandName = "华为";
处理参数>>>模糊表达式要用百分号或者下划线的占位符对参数进行处理
companyName = "%" + companyName+ "%";
brandName = "%" + brandName+ "%";
@Test
public void selectByCondition()throws IOException {
//1.加载mybatis的核心配置文件,获取SQL session factory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);//把字符串传进来,转换成一个字节输入流
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取SQL session的对象用它来执行sql
SqlSession SqlSession = sqlSessionFactory.openSession();
//3.获取BrandMapper接口的代理对象
BrandMapper brandMapper = SqlSession.getMapper(BrandMapper.class);
//4.执行方法
//定义三个局部变量用来接受参数
int status = 1;
String companyName = "华为";
String brandName = "华为";
//处理参数>>>模糊表达式要用百分号或者下划线的占位符对参数进行处理
companyName = "%" + companyName+ "%";
brandName = "%" + brandName+ "%";
List brand = brandMapper.selectByCondition(status,companyName,brandName);
System.out.println(brand);
//5.释放资源
SqlSession.close();
}
(2).对象作为参数
//定义三个局部变量用来接受参数 int status = 1; String companyName = "华为"; String brandName = "华为"; //处理参数>>>模糊表达式要用百分号或者下划线的占位符对参数进行处理 companyName = "%" + companyName+ "%"; brandName = "%" + brandName+ "%"; //封装成对象 Brand brand = new Brand(); brand.setStatus(status); brand.setCompanyName(companyName); brand.setBrandName(brandName); Listbrands = brandMapper.selectByCondition(brand); System.out.println(brands);
@Test
public void selectByCondition()throws IOException {
//1.加载mybatis的核心配置文件,获取SQL session factory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);//把字符串传进来,转换成一个字节输入流
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取SQL session的对象用它来执行sql
SqlSession SqlSession = sqlSessionFactory.openSession();
//3.获取BrandMapper接口的代理对象
BrandMapper brandMapper = SqlSession.getMapper(BrandMapper.class);
//4.执行方法
//定义三个局部变量用来接受参数
int status = 1;
String companyName = "华为";
String brandName = "华为";
//处理参数>>>模糊表达式要用百分号或者下划线的占位符对参数进行处理
companyName = "%" + companyName+ "%";
brandName = "%" + brandName+ "%";
Brand brand = new Brand();
brand.setStatus(status);
brand.setCompanyName(companyName);
brand.setBrandName(brandName);
//List brands = brandMapper.selectByCondition(status,companyName,brandName);
List brands = brandMapper.selectByCondition(brand);
System.out.println(brands);
//5.释放资源
SqlSession.close();
}
(3).集合作为参数
//定义三个局部变量用来接受参数
int status = 1;
String companyName = "华为";
String brandName = "华为";
//处理参数>>>模糊表达式要用百分号或者下划线的占位符对参数进行处理
companyName = "%" + companyName+ "%";
brandName = "%" + brandName+ "%";
//封装集合
Map map = new HashMap();
map.put("status",status);
map.put("companyName",companyName);
map.put("brandName",brandName);
@Test
public void selectByCondition()throws IOException {
//1.加载mybatis的核心配置文件,获取SQL session factory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);//把字符串传进来,转换成一个字节输入流
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取SQL session的对象用它来执行sql
SqlSession SqlSession = sqlSessionFactory.openSession();
//3.获取BrandMapper接口的代理对象
BrandMapper brandMapper = SqlSession.getMapper(BrandMapper.class);
//4.执行方法
//定义三个局部变量用来接受参数
int status = 1;
String companyName = "华为";
String brandName = "华为";
//处理参数>>>模糊表达式要用百分号或者下划线的占位符对参数进行处理
companyName = "%" + companyName+ "%";
brandName = "%" + brandName+ "%";
//封装集合
Map map = new HashMap();
map.put("status",status);
map.put("companyName",companyName);
map.put("brandName",brandName);
//List brands = brandMapper.selectByCondition(status,companyName,brandName);
//List brands = brandMapper.selectByCondition(brand);
List brands = brandMapper.selectByCondition(map);
System.out.println(brands);
//5.释放资源
SqlSession.close();
}
4.执行结果
3.多条件查询
1.散装参数 :如果方法中有多个参数,需要使用@param(“sql占位符的名称”)
2.对象参数
3.集合参数
(1).散装参数 :如果方法中有多个参数
(2).对象作为参数
(3).集合作为参数
总结:
此方式有一个bug,就是用户不会根据你有多少变量而去填写多少变量,如果用户只想填写一个变量呢?只填写了两个呢?,所查结果只能是空,具体解决bug的方法,请看这篇文章
我的猴子的博客_CSDN博客-Java,JDBC,MyBatis领域博主https://blog.csdn.net/qq_51272114?spm=1000.2115.3001.5343



