_Mybati条件查询
-
需求:
-
为了避免后面的@Pram注解出现不必要的bug,初学时还是将实体类对象Brand.java中的变量名都与数据库表中的列名保持一致 -
// 条件查询 public void conditionSelect() { int status = 0; String brand_name = "字节"; String address = "北京"; // 模糊查询参数操作 brand_name = "%" + brand_name + "%"; address = "%" + address + "%"; 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语句 List<Brand> result = mapper.selectByCondition(status, brand_name, address); // 返回执行结果 System.out.println("执行结果为:"+result); // 释放资源 sqlSession.close(); } catch (IOException e) { e.printStackTrace(); }finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } ====================================================================== 执行结果为:[Brand{id=4, brand_name='字节跳动', ordered=20, address='北京', states=0}] Process finished with exit code 0
_查询,多条件,动态查询
-
SQL语句会随着用户的输入或者外部条件的变化而变化,我们称之为动态SQL
-
<select id =" *** " resultMap = " *** "> select * from tb_brand <where> <if test = "abc != null and abc != '' "> abc like #{abc} </if> </where> </select>
-
如果不是动态的条件查询,加入用户所输入的信息有缺失,则程序将无法查询,比如:
查询到的结果为: -
但是如果用动态的条件查询的话:
-
注意:此时的查询是将需要查询的参数封装到一个list集合中的,具体的test问价的java代码如下所示:
-
package com.atguigudb.mybatis.test; import com.atguigudb.mybatis.mapper.BrandMapper; import com.atguigudb.mybatis.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.util.List; public class MyBatisBrand_Test { @Test // // public void testMyBatis() { // 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语句 // List<Brand> brands = mapper.selectAll(); // // // 返回结果在控制台输出 // System.out.println("result:" + brands); // // // 释放资源 // sqlSession.close(); // // // } catch (IOException e) { // e.printStackTrace(); // }finally { // try { // is.close(); // } catch (IOException e) { // e.printStackTrace(); // } // } // // // } /* @Test public void selectOne() { int id = 1; 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语句 Brand selectByid = mapper.selectByid(id); // 返回结果在控制台输出 System.out.println("您所查询的为:" + selectByid); // 释放资源 sqlSession.close(); } catch (IOException e) { e.printStackTrace(); }finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } */ // 条件查询 public void conditionSelect() { int status = 0; String brand_name = "字节"; String address = "北京"; // 模糊查询参数操作 brand_name = "%" + brand_name + "%"; address = "%" + address + "%"; // 封装参数 Brand brand = new Brand(); brand.setStates(status); // brand.setName(brand_name); brand.setAddress(address); 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语句 List<Brand> result = mapper.selectByCondition(brand); // List<Brand> result = mapper.selectByCondition(status, brand_name, address); // 返回执行结果 System.out.println("执行结果为:"+result); // 释放资源 sqlSession.close(); } catch (IOException e) { e.printStackTrace(); }finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } } =========================================== 执行结果为:[Brand{id=4, brand_name='字节跳动', ordered=20, address='北京', states=0}] Process finished with exit code 0
-
-
因为要封装封装查询参数,所以接口文件中也需要修改,具体如图所示,该方法条件查询的第二种方法: