MyBatis模糊查询不报错,但查不出数据

MyBatis模糊查询不报错,但查不出数据

1.在navicat验证sql语句

select *
from tb_brand
where status = 1
  and brand_name like "%华为%"
  and company_name like "%华为%"

能显示查询结果:


2.检查测试代码

/**
     * 多条件模糊查询
     * @throws IOException
     */
    @Test
    public void testSelectCondition() throws IOException {
        //接收参数
        int status=1;
        String brandName="华为";
        String companyName="华为";

        //处理参数
        brandName="%"+brandName+"%";
        companyName="%"+companyName+"%";

        //封装对象
        Brand brand=new Brand();
        brand.setStatus(status);
        brand.setBrandName(brandName);
        brand.setCompanyName(companyName);

        //构建SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //获取执行sql对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //获取mapper代理
        BrandMapper brandmapper = sqlSession.getMapper(BrandMapper.class);

//        List<Brand> brands = mapper.selectCondition(status,brandName,companyName);

        List<Brand> brands = brandmapper.selectCondition(brand);

        System.out.println(brands);

        sqlSession.close();

    }

3.检查映射文件
在这里插入图片描述
都没有问题,就是没有查出数据,于是修改了下mybatis-config.xml
在这里插入图片描述

<property name="url" value="jdbc:mysql:///mybatis?useSSL=false&amp;characterEncoding=utf8"/>

characterEncoding=utf8,指定字符的编码格式,就可以查出数据
在这里插入图片描述

  • 15
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值