JavaWeb:MyBatis(6)查看详情、条件查询

查看详情

1.编写接口方法: Mapper接口

参数: id

结果:Brand

Brand selectById(int id);
 /*
   * 查看详情
   * */
   Brand selectById(int id);

2.编写SQL语句: SQL映射文件

<select id="selectByld" parameterType="int"resultType="brand">
    select * from tb_brand where id = #(id};
</select>
 <!--
        参数占位符:
            1.#{}:会将其替换为?,为了防止sql注入
            2.${}:拼sql,会存在sql注入问题
            3.使用时机:
                参数传递的时候:#{}
                表名或者列名不固定的情况下:${}会存在sql注入问题
        参数类型:parameterType:可以省略
        特殊字符的处理:
            1.转义字符:
            2.CDATA区:

    -->
    <select id="selectById" parameterType="int" resultMap="brandResultMap">
        select * from tb_brand where id = #{id};
    </select>

3.执行方法,测试

@Test
    public void testSelectById()throws Exception{

        //接收id
        int id = 1;
        //1.加载mybatis的核心配置文件,获取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.执行方法
        Brand brand = brandMapper.selectById(id);
        System.out.println(brand);

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

条件查询

多条件的查询

1.编写接口方法: Mapper接口

参数:所有查询条件

结果:List<Brand>

/*
   * 条件查询
   * 参数接收:
   *     1.散装参数:有多个参数,需要使用@Param(“SQL参数占位符名称”)注解
   *     2.对象参数:对象的属性名称要和参数占位符名称一致
   *     3.map集合的参数
   */

   List<Brand> selectByCondition1(@Param("status") int status,@Param("companyName") String companyName,@Param("brandName") String brandName);
   List<Brand> selectByCondition2(Brand brand);
   List<Brand> selectByCondition3(Map map);

2.编写SQL语句: SQL映射文件

<!--
        条件查询
    -->
    <select id="selectByCondition1" resultMap="brandResultMap">
        select *
        from tb_brand
        where
        status = #{status} and company_name like #{companyName} and brand_name like #{brandName}
    </select>
    <select id="selectByCondition2" resultMap="brandResultMap">
        select *
        from tb_brand
        where
        status = #{status} and company_name like #{companyName} and brand_name like #{brandName}
    </select>
    <select id="selectByCondition3" resultMap="brandResultMap">
        select *
        from tb_brand
        where
        status = #{status} and company_name like #{companyName} and brand_name like #{brandName}
    </select>

3.执行方法,测试

@Test
    public void testSelectByCondition1()throws Exception{

        //接收id
        int status = 1;
        String companyName = "华为";
        String brandName = "华为";

        //处理

        companyName = "%"+companyName+"%";
        brandName = "%"+brandName+"%";

        //1.加载mybatis的核心配置文件,获取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<Brand> brands = brandMapper.selectByCondition1(status, companyName, brandName);
        System.out.println(brands);

        //5.释放资源
        sqlSession.close();
    }
    @Test
    public void testSelectByCondition2()throws Exception{

        //接收id
        int status = 1;
        String companyName = "华为";
        String brandName = "华为";

        //处理

        companyName = "%"+companyName+"%";
        brandName = "%"+brandName+"%";

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

        //1.加载mybatis的核心配置文件,获取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<Brand> brands = brandMapper.selectByCondition2(brand);
        System.out.println(brands);

        //5.释放资源
        sqlSession.close();
    }
    @Test
    public void testSelectByCondition3()throws Exception{

        //接收id
        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);

        //1.加载mybatis的核心配置文件,获取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<Brand> brands = brandMapper.selectByCondition3(map);
        System.out.println(brands);

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

注:若查询结果为空需要配置核心文件mybatis-config.xml文件中的

<property name="url" value="jdbc:mysql:///mybatis?characterEncoding=utf8&amp;useSSL=false"/>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值