MyBatis简单项目(增删改查)-MyBatis的快速入门

0.目录

  • 查询

    • 查询所有数据

    • 查看详情

    • 条件查询

  • 添加

  • 修改

    • 修改全部字段

    • 修改动态字段

  • 删除

    • 单个删除

    • 批量删除

1.查询

   所有的查询分为三步:

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

      1.1,关注接口是否有参数.

      1.2,关注接口是否有返回值,是什么返回值类型.

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

    3,编写测试用例,测试接口.

  1.1查询所有数据

      接口方法:   List<Brand>  findAll ();

        返回值: List<Brand>

        参数:无

      SQL语句:

    <select id="findAll" resultMap="result1">
        select *
        from tb_brand;
    </select>

      其中 id="findAll"  代表 sql语句的唯一标识,与接口中的方法名称相同,一一对应;

      resultMap="result1" 代表 结果集映射  用于解决 Java代码中变量名和数据库中字段名不匹配的问题,

      数据库中字段不区分大小写,所以习惯于下划线命名,但是Java中习惯驼峰命名,因此会造成查询结果的不一致。

      使用结果集映射 的作用是可以预先设置映射字段,实现查询功能:

<resultMap id="result1" type="brand">
    <result column="brand_name" property="brandName"/>
    <result column="company_name" property="companyName"/>
</resultMap>

      测试用例:

    @Test
    //查询所有
    public  void test1() throws IOException {
        //1. 加载mybatis的核心配置文件,获取 SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //2. 获取SqlSession对象,用它来执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //3. 执行sql
        //获取mapper
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        //查询结果
        List<Brand> brands = brandMapper.findAll();
        //打印结果
        System.out.println(brands);
        //4. 释放资源
        sqlSession.close();
    }

  1.2多条件-动态条件查询

      接口方法:   

    List<Brand> selectByCondition(
            @Param("status") Integer status,
            @Param("companyName") String companyName,
            @Param("brandName") String brandName
            );        

        返回值: List<Brand>

        参数:Integer status,  String companyName,  String brandName

      SQL语句:

    <select id="selectByCondition" resultType="pojo.Brand" resultMap="result1">
                 select *
         from tb_brand
         <where>
             <if test="status != null">
                 and status = #{status}
             </if>
             <if test="companyName != null and companyName!= ''">
                 and company_name like #{companyName}
             </if>
             <if test="brandName != brandName!= ''">
                 and brand_name like #{brandName}
             </if>
         </where>
    </select>

      其中 <where>标签可以自动省略 SQL语句中的and 

         <if>标签可以判断test中条件是否满足,从而决定是否执行。

      测试用例:

@Test
    //按条件查询(多条件)
    public  void test3() throws IOException {
        //1. 加载mybatis的核心配置文件,获取 SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2. 获取SqlSession对象,用它来执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //3. 执行sql
//        List<User> users = sqlSession.selectList("pojo.mapper.UserMapper.selectAll");
        //获取mapper
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        Integer status = null;
        String CompanyName = "";
        String BrandName="";
//在这个地方要设置模糊查询
        CompanyName ="%"+CompanyName+"%";
        BrandName ="%"+BrandName+"%";
        List<Brand> brands = brandMapper.selectByCondition( status,CompanyName,BrandName);
        System.out.println(brands);

   1.3单条件-动态条件查询

      接口方法:   List<Brand> selectByConditionsingle(@Param("brand") Brand brand);

        返回值: List<Brand>

        参数:Brand brand

      SQL语句:

    <select id="selectByConditionsingle" resultType="pojo.Brand" resultMap="result1">
        select *
        from tb_brand
        <where>
            <choose>
                <when test="brand.status != null">
                     status = #{brand.status}
                </when>
                <when test="brand.companyName != null and brand.companyName!= ''">
                    company_name like #{brand.companyName}
                </when>
                <when test="brand.brandName != null and brand.brandName != ''">
                    brand_name like #{brand.brandName}
                </when>
            </choose>
        </where>
    </select>

      测试用例:

   1.2单条件-动态条件查询

      接口方法:   

        返回值: List<Brand>

        参数:

      SQL语句:

      测试用例:

 

 

        

   

posted @ 2021-12-10 09:19  MarcoXiang  阅读(120)  评论(0)    收藏  举报