首页 > 其他分享 >2024/11/1日工作总结

2024/11/1日工作总结

时间:2024-11-02 19:57:27浏览次数:4  
标签:11 总结 companyName brand 2024 sqlSession brandName status import

学习mybatis查看详情、动态多条件/单条件查询

点击查看代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.itheima.mapper.BrandMapper">

    <!--
        数据库表的字段名称 和 实体类的属性名称 不一样,不能自动封装数据
            *起别名
            *resultMap
    -->

    <!--
        id:唯一标识
        type:映射类型,支持别名
    -->
    <resultMap id="brandResultMap" type="brand">
        <!--
            id:主键映射
            result:一般字段

                column:表列名
                property:实体类属性名
        -->
        <result column="brand_name" property="brandName"/>
        <result column="company_name" property="companyName"/>
    </resultMap>

    <select id="selectAll" resultMap="brandResultMap">
        select *
        from tb_brand;
    </select>


    <!--<select id="selectAll" resultType="brand">
        select id,brand_name as brandName,company_name as companyName,ordered,description,status
        from tb_brand;
    </select>-->
    
    <!--<select id="selectAll" resultType="brand">
        select *
        from tb_brand;
    </select>-->
    
    

    <!--
        *参数占位符:
            1.#{}:替换为?;
                传递参数;
            2.${}:拼字符串,存在sql注入;
                表名、列名不固定;
        *特殊字符处理:
            1.转义字符;
            2.CDATA区:
                <![CDATA[
                    <
                ]]>
    -->
    <select id="selectById" resultMap="brandResultMap">
        select *
        from tb_brand where id = #{id};

    </select>


   <!-- <select id="selectByCondition" resultMap="brandResultMap">
        select *
        from tb_brand
        where status = #{status}
            and company_name like #{companyName}
            and brand_name like #{brandName};

    </select>-->

    <!--
        动态条件查询
    -->
    <select id="selectByCondition" resultMap="brandResultMap">
        select *
        from tb_brand
        /*where 1 = 1*/
        <where>
            <if test="status != null">
                and status = #{status}
            </if>
            <if test="companyName != null and companyName != '' ">
                and company_name like #{companyName}
            </if>
            <if test="brandName != null and brandName != '' ">
                and brand_name like #{brandName}
            </if>
        </where>
    </select>


    <select id="selectByConditionSingle" resultMap="brandResultMap">
        select *
        from tb_brand
        <where>
            <choose>
                <when test="status != null">
                    status = #{status}
                </when>
                <when test="companyName != null and companyName != '' ">
                    company_name like #{companyName}
                </when>
                <when test="brandName != null and brandName != '' ">
                    brand_name like #{brandName}
                </when>
                <!--<otherwise>
                    1 = 1
                </otherwise>-->

            </choose>
        </where>

    </select>


</mapper>
点击查看代码
package com.itheima.test;

import com.itheima.mapper.BrandMapper;
import com.itheima.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.HashMap;
import java.util.List;
import java.util.Map;

public class MyBatisTest {

    @Test
    public void testSelectAll() throws IOException {
        //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<Brand> brands = brandMapper.selectAll();
        System.out.println(brands);

        //5.释放
        sqlSession.close();


    }


    @Test
    public void testSelectById() throws IOException {
        int id = 1;

        //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.执行方法
        Brand brand = brandMapper.selectById(id);
        System.out.println(brand);

        //5.释放
        sqlSession.close();


    }


    @Test
    public void testSelectByCondition() throws IOException {
        int status = 1;
        String companyName = "华为";
        String brandName = "华为";

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

        /*Brand brand =new Brand();
        brand.setStatus(status);
        brand.setCompanyName(companyName);
        brand.setBrandName(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<Brand> brands = brandMapper.selectByCondition(status, companyName, brandName);
        //List<Brand> brands = brandMapper.selectByCondition(brand);
        List<Brand> brands = brandMapper.selectByCondition(map);
        System.out.println(brands);

        //5.释放
        sqlSession.close();


    }

    @Test
    public void testSelectByConditionSingle() throws IOException {
        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.获取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.selectByCondition(status, companyName, brandName);
        //List<Brand> brands = brandMapper.selectByCondition(brand);
        List<Brand> brands = brandMapper.selectByConditionSingle(brand);
        System.out.println(brands);

        //5.释放
        sqlSession.close();


    }
}

标签:11,总结,companyName,brand,2024,sqlSession,brandName,status,import
From: https://www.cnblogs.com/zhanglijian/p/18522394

相关文章

  • 2024-2025-1 20241316《计算机基础与程序设计》第六周学习总结
    2024-2025-120241316《计算机基础与程序设计》第六周学习总结作业信息这个作业属于哪个课程2024-2025-1-计算机基础与程序设计这个作业要求在哪里2024-2025-1计算机基础与程序设计第六周作业这个作业的目标计算机科学概论第7章《C语言程序设计》第5章作业正......
  • 2024秋软工实践 旺仔水饺队 原型设计和UML设计
    作业所属课程https://edu.cnblogs.com/campus/fzu/SE2024作业要求https://edu.cnblogs.com/campus/fzu/SE2024/homework/13297作业的目标实现项目的原型设计与概要设计团队名称旺仔水饺102201140黎曼102201138黄俊瑶102201127罗永辉102201130郑哲浩......
  • CSP 2024 游记
    CSP2024游记初赛发挥得还可以,J组92.5,S组90.5,大家的J组都比我高。day-1在正式比赛的时间去正式比赛的考场和座位做了一场模拟赛。发现楼下机房的Dev-C++的编译器竟然是重新配置过的,是winlibsGCC13.2.0,比我的电脑配置还好;听说vs-code也是配置好的,连NOI虚拟机......
  • 2024-2025-1 20241425《计算机基础与程序设计》第6周学习总结
    2024-2025-120241425《计算机基础与程序设计》第6周学习总结作业信息这个作业属于哪个课程2024-2025-1-计算机基础与程序设计这个作业要求在哪里https://edu.cnblogs.com/campus/besti/2024-2025-1-CFAP/homework/13276这个作业的目标计算机科学概论(第七版)第7章......
  • 11.2
     #include<cmath>#include<iostream>usingnamespacestd; classVector{private:    double*array=newdouble[4];    doublelength;public:    Vector(doublearr[4])    {        this->array[0]=arr[0];        this->a......
  • 24.11.2
    (填空题)软件复用的优点有()、()、()、()。(1)提高生产率(2)减少维护代价(3)提高互操作性(4)支持快速原型2. (填空题)依据复用的对象,软件复用分为()和()。(1)产品复用(2)过程复用3. (填空题)最常用的可复用设计是()和()。(1)架构模式(2)设计模式4. (填空题)框架方法包括:()......
  • HOOPS Publish SDK 2024.7.0
    通过使用HOOPSPublishSDK向您的工程应用程序添加交互式3DPDF、HTML和标准CAD格式导出(包括STEPAP242、JT10、IGES、STL和3MF),增强您的工程应用程序。用于创建丰富工程文档的3DCAD发布SDKHOOPSPublishSDK可帮助开发人员快速扩展其工程数据的范围,并具有导......
  • 关于k8s api-server端口范围和node节点范围冲突会导致集群不正常故障总结
    1.故障背景由于需要部署新环境,于是在阿里云新建一个ack集群,部署了业务,结果整晚上的存活探针告警,新集群接近30个业务pod,整晚上将近50多条存活探针告警,这个结果明显不正常。但是查看所有pod状态事件全部正常,阿里云托管的ack也没有事件异常,第一反应确实是集群某些参数不对......
  • NOIP2024模拟赛21
    省流:没过T1,玩了1h俄罗斯,不好评价。还好T3一个小时写完了平方暴力,还没菜到离谱,感觉这才是一个正常的分数。但是好像正解要不到1h?T2的dp优化是我弱项,做不出正常,spdarkle是真逆天。怎么一眼的怎么一眼的怎么一眼的怎么一眼的怎么一眼的怎么一眼的怎么一眼的。发现后面又......
  • 20222407 2024-2025-1 《网络与系统攻防技术》实验四实验报告
    (一)实践目标恶意代码文件类型标识、脱壳与字符串提取对提供的rada恶意代码样本,进行文件类型识别,脱壳与字符串提取,以获得rada恶意代码的编写作者,具体操作如下:o使用文件格式和类型识别工具,给出rada恶意代码样本的文件格式、运行平台和加壳工具;o使用超级巡警脱壳机等脱壳软件,对rad......