首页 > 数据库 >具体的数据库操作

具体的数据库操作

时间:2023-04-15 18:24:58浏览次数:30  
标签:status String companyName 数据库 brand brandName 具体 操作 public

Brand类

package com.itheima.pojo;

/**
 * 品牌
 *
 * alt + 鼠标左键:整列编辑
 *
 * 在实体类中,基本数据类型建议使用其对应的包装类型
 */

public class Brand {
    // id 主键
    private Integer id;
    // 品牌名称
    private String brandName;
    // 企业名称
    private String companyName;
    // 排序字段
    private Integer ordered;
    // 描述信息
    private String description;
    // 状态:0:禁用  1:启用
    private Integer status;


    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getBrandName() {
        return brandName;
    }

    public void setBrandName(String brandName) {
        this.brandName = brandName;
    }

    public String getCompanyName() {
        return companyName;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

    public Integer getOrdered() {
        return ordered;
    }

    public void setOrdered(Integer ordered) {
        this.ordered = ordered;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    @Override
    public String toString() {
        return "Brand{" +
                "id=" + id +
                ", brandName='" + brandName + '\'' +
                ", companyName='" + companyName + '\'' +
                ", ordered=" + ordered +
                ", description='" + description + '\'' +
                ", status=" + status +
                '}';
    }
}

BrandMapper文件:一个接口用于执行

package com.itheima.mapper;


import com.itheima.pojo.Brand;
import org.apache.ibatis.annotations.Param;

import java.util.List;
import java.util.Map;

public interface BrandMapper {


    public List<Brand> selectAll();

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

    /*
    * 完成的是条件查询
    *   *参数接收
    *       1.散装参数:如果方法中有多个参数,需要使用@Param("SQL参数占位符名称")
    *       2.对象参数:对象的属性名称要和参数占位符名称一致
    *       3.map集合参数
    * */
    //List<Brand> selectByCondition(@Param("status") int status, @Param("companyName") String companyName,@Param("brandName") String brandName);





    //List<Brand> selectByCondition(Brand brand);

    List<Brand> selectByCondition(Map map);



}

对应的BrandMapper.xml文件,用于写具体的sql语句

<?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">
<!--
   namespace:名称空间
-->
<mapper namespace="com.itheima.mapper.BrandMapper">


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




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

    </select>

    <!--
        数据库的字段名称   和 实体类的属性名称不一样  , 则不能自动封装数据
        *起别名:对不一样的列名起别名 让别名和实体类名字相同
            *缺点 每次查询都要定义一次别名

        *sql片段

        *resultMap
    -->

    <!--<select id="selectAll" resultType="brand">
        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>
-->


    <!--
       *参数占位符:
         1.#{}:会将其替换为?,为了防止sql注入
         2.${}:拼sql。会存在sql注入问题
         3.使用时机:
               * 参数传递的时候:#{}
               * 表名或者列名不固定的情况下:${}

       *参数类型:parameterType 可以省略
       *特殊字符的处理:
           1.转义字符:
           2.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>-->

    <!--
        动态条件查询
            *if:条件判断
               * test:逻辑表达式
            *问题:
                *恒等式 加入条件 where 1 = 1 并在每个if逻辑表达式里面加上and
                *<where>标签 替换where关键字


    -->
    <select id="selectByCondition" resultMap="brandResultMap">
        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 != null and brandName != ''">
            and brand_name like #{brandName}
          </if>
        </where>
    </select>


</mapper>

执行具体的主要java test示例

MybatisTest.java

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();
    }


}

 

标签:status,String,companyName,数据库,brand,brandName,具体,操作,public
From: https://www.cnblogs.com/sxwgzx23/p/17321580.html

相关文章

  • Q:数据库方法的传播特性,外层方法的事务注解@Transactional默认会影响本方法么
    外层方法的事务注解默认会影响本方法么涉及知识:事务的传播特性实验前推测:目前了解内、外方法某个发生异常执行回滚是否影响另一个方法是由配置的哪个传播特性决定的。推测内方法出现异常要导致外方法的事务也要回滚,因为这个在现实场景最普遍。实验:描述:roleService.inse......
  • 01数据库环境搭建
    创建数据库1.打开navicat新建数据库,运行sql文件    ......
  • 一天吃透操作系统八股文
    操作系统的四个特性?并发:同一段时间内多个程序执行(与并行区分,并行指的是同一时刻有多个事件,多处理器系统可以使程序并行执行)共享:系统中的资源可以被内存中多个并发执行的进线程共同使用虚拟:通过分时复用(如分时系统)以及空分复用(如虚拟内存)技术把一个物理实体虚拟为多个异步:系统......
  • 项目连接讯飞语音接口的相关操作
    相关操作在讯飞官网找到了这样一个语音听写模件,然后自己尝试着调用了这个语音接口,幸运地,成功调用到了这个语音接口,但是这个调用仅仅是在Java里面实现了,并没有实现在web里面,后续还会继续完善这个调用的!具体步骤1、进入到讯飞平台官网:https://www.xfyun.cn/选择注册一个帐号之后......
  • Redis相关操作
    Redis相关文档一.Redis简单使用​ redis作为一款目前这个星球上性能最高的非关系型数据库之一.拥有每秒近十万次的读写能力.其实力只能用恐怖来形容.1.安装redisredis是我见过这个星球上最好安装的软件了.比起前面的那一坨.它简直了...直接把压缩包解压.然后配置一下......
  • delphi FastReport 从流(数据库)中加载和保存报表
    FastReport从流(数据库)中加载和保存报表属性和方法TfrxReport.LoadFromStreamprocedureLoadFromStream(Stream:TStream);从流中加载报表。参数Stream来源流。TfrxReport.SaveToStreamprocedureSaveToStream(Stream:TStream);将报表保存到流中。参数Stream来源......
  • 查看oracle数据库中的函数
    SQLPLUS下:查看建了哪些函数,注意,引号内大写selectobject_namefromuser_objectswhereobject_type='FUNCTION';查看函数内容,引号内为你要查询的函数名,也要大写selecttextfromuser_sourcewherename='函数名';PLSQLDeveloper下查询用户下的函数:SELECT*FROMdba_objects......
  • Delphi FDMemTable内存表用法及简单操作函数封装(转)
    在某些场景下当轻量级的应用需要在内存中缓存数量比较多且字段比较多的高频使用数据时。以前我都是采用Ini或直接使用sqlite数据库。JSON也试过基本无法或很难实现需要的功能,因为当涉及某一同类型对象多字段多列时不通过遍历基本无法直接取到或修改数据。这样就导致了效率的低下。......
  • 【数据结构】二叉树的基本操作与遍历(C语言)
     目录定义满二叉树 完全二叉树性质应用计算二叉树结点个数 计算叶子结点的个数第 k层结点的个数查找值为x的节点遍历前序遍历中序遍历后序遍历层序遍历判断是否为完全二叉树定义......
  • Linux-查看操作命令属于哪个软件包
    CentOS:利用yumprovides命令sar命令所属的软件包为sysstat-10.1.5-19.el7.x86_64[root@db01~13:02:13]#yumprovidessarLoadedplugins:fastestmirrorLoadingmirrorspeedsfromcachedhostfile*base:mirrors.aliyun.com*extras:mirrors.aliyun.com*updates:mir......