栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JAVA Web学习笔记15 mybatis之查看详情 & 条件查询

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JAVA Web学习笔记15 mybatis之查看详情 & 条件查询

查看详情


1、编写接口方法
BrandMapper接口
这里需要注意的是Brand selectById(int id);这里的id并没有与BrandMapper.xml的select方法里的id相对应,select id="selectById"的含义是这段select对应的是BrandMapper接口里的selectById方法,而这里的#{id}则不同。这也是后面多条件查询需要关注的问题。

2、编写sql映射 BrandMapper.xml参数占位符:
1、# {}:会将其替换为?,为了放在sql注入
2、$ {}:拼入sql,会参在sql注入的问题
传参的时候一定要选择#
表名或列名不固定的情况下才使用$

参数类型:parameterType:可以省略,因为在brandmapper接口里面的抽象方法就已经定义的参数类型了

特殊字符处理:
有时候我们要选择的是id小于或者大于多少的,但是在xml的语法中<是开始、>是结束,所以直接这样使用会报错解决方案:
1、转义字符

2、CDATA区
tip:输入CD自动补齐CDATA区

条件查询 多条件查询

①散装参数:如果方法中有多个参数,需要使用@Param(“SQL参数占位符名称”)
1、编写接口方法

//    多条件查询
//散装参数
    List selectByCondition(@Param("status") int status,@Param("companyName") String companyName,@Param("brandName") String brandName);

2、编写BrandMapper.xml

3、执行test

@Test
    public void testSelectByCondition() throws IOException {
//        接收参数
        int status = 1;
        String companyName = "华为";
        String brandName = "华为";
//        为模糊匹配like处理参数
        companyName = "%" + companyName + "%";
        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 brands = brandMapper.selectByCondition(status,companyName,brandName);
        System.out.println(brands);

        //释放资源
        sqlSession.close();
    }

②封装对象作为参数
1、BrandMapper接口

2、BrandMapper.xml不改动

这里直接将接收到的brand对象的status,company、brandname属性给与对应的参数名上。

3、test修改

@Test
    public void testSelectByCondition() throws IOException {
//        接收参数
        int status = 1;
        String companyName = "华为";
        String brandName = "华为";
//        为模糊匹配like处理参数
        companyName = "%" + companyName + "%";
        brandName = "%" + brandName + "%";

        //封装对象
        Brand brand = new Brand();
        brand.setBrandName(brandName);
        brand.setCompanyName(companyName);
        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();
        //3、获取Mapper接口的代理对象
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        //4、执行方法
        List brands = brandMapper.selectByCondition(brand);
        System.out.println(brands);

        //释放资源
        sqlSession.close();
    }

将用户传来的参数封装为brand对象,再由brand对象传给sql语句,sql语句进行拆分后分别赋给同属性名的参数。

查询成功:

③map集合方法
1、brandMapper接口

List selectByCondition(Map map);

2、BrandMapper.xml的sql语句不改动

3、test修改
保证map中的键的名称和sql语句中需要的参数名称一一对应。

@Test
    public void testSelectByCondition() throws IOException {
//        接收参数
        int status = 1;
        String companyName = "华为";
        String brandName = "华为";
//        为模糊匹配like处理参数
        companyName = "%" + companyName + "%";
        brandName = "%" + 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 brands = brandMapper.selectByCondition(map);
        System.out.println(brands);

        //释放资源
        sqlSession.close();
    }

这个方法其实就和封装对象类似,不过这里是封装为了map对象。
总结:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/827525.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号