首页 > 其他分享 >006. MyBatis基本使用---03MyBatis数据查询

006. MyBatis基本使用---03MyBatis数据查询

时间:2022-11-23 23:46:33浏览次数:45  
标签:xml goods void --- 006 parentId MyBatis Integer public

1.MyBatis数据查询步骤

1.1 创建实体类entity

1.2 创建mapper  xml文件来说明当前sql语句是什么并编写<select > sql语句

1.3 在mybatis-config.xml开启驼峰映射

1.4 在mybatis-config.xml新增mapper  xml的声明

1.5 SqlSession执行select语句

2.具体实现操作

2.1 创建entity包用来存放实体类

package com.imooc.mybatis.entity;

public class Category {
    private Integer categoryId;
    private String categoryName;
    private Integer parentId;
    private Integer categoryLevel;
    private Integer categoryOrder;

    public Integer getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(Integer categoryId) {
        this.categoryId = categoryId;
    }

    public String getCategoryName() {
        return categoryName;
    }

    public void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }

    public Integer getParentId() {
        return parentId;
    }

    public void setParentId(Integer parentId) {
        this.parentId = parentId;
    }

    public Integer getCategoryLevel() {
        return categoryLevel;
    }

    public void setCategoryLevel(Integer categoryLevel) {
        this.categoryLevel = categoryLevel;
    }

    public Integer getCategoryOrder() {
        return categoryOrder;
    }

    public void setCategoryOrder(Integer categoryOrder) {
        this.categoryOrder = categoryOrder;
    }
}

2.2 在resource目录下创建新的子目录mappers(映射器)存放xml文件,创建goods.xml,用来说明实体类和哪一个表对应

      resultType代表将sql语句返回的结果包装成对应的goods对象

<?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="goods">

  <select id="selectAll" resultType="com.imooc.mybatis.entity.Goods" >
    select * from t_goods order by goods_id desc limit 10
  </select>

</mapper>

2.3 在mybatis-config.xml开启驼峰命名转换 

    <settings>
        <!-- goods_id ==> goodsId 驼峰命名转换 -->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>

2.4 在mybatis-config.xml声明mapper的配置文件

<!--声明mapper的配置文件-->
    <mappers>
        <mapper resource="mappers/goods.xml"/>
    </mappers>

2.5 测试,查询常用方法selectOne() 、selectList()

 /**
     * select查询语句执行
     */
    @Test
    public void testSelectAll() throws Exception
    {
        SqlSession session = null;
        try
        {
            session = MyBatisUtils.openSession();
            List<Goods> list = session.selectList("goods.selectAll");//goods.selectAll    命名空间.(SQL语句的ID)
            for (Goods g : list)
            {
                System.out.println(g.getTitle());
            }
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            MyBatisUtils.closeSession(session);
        }
    }

 

标签:xml,goods,void,---,006,parentId,MyBatis,Integer,public
From: https://www.cnblogs.com/LLL0617/p/16920549.html

相关文章

  • React---编程式导航/声明式导航
    声明式导航通过NavLink来实现路由跳转的导航编程式导航通过js来实现路由的跳转import{Component}from"react";​exportdefaultclassNewsextendsCo......
  • Kubenertes Ingress-nginx 部署
    IngressIngress公开从集群外部到集群内服务的HTTP和HTTPS路由。流量路由由Ingress资源上定义的规则控制。Ingress用于实现用域名的方式访问k8s内部应用。管理对集......
  • windows--cmake与c++的使用教程(14)
    1概述本文基于前文环境本节目标:target_include_directories用法2作用target_include_directories的作用,用于给固定目标指定头文件搜索路径。moderncmake之......
  • 【SQL必知必会】-12月
    了解Mysql一般叫DBMSDataBaseManagementSystem数据库管理系统表table:一个结构化的文件。来存储数据。一个数据库中表名是唯一的。列column:存储表中的某一部......
  • java-IO流之字符流
    字符流由于字节流操作中文不是特别的方便,所以Java就提供字符流字符流=字节流+编码表中文的字节存储方式用字节流复制文本文件时,文本文件也会有中文,但是没有问......
  • “ZWNBSP”是“Zero Width No-Break SPace”的缩写,意思是“零宽度无分隔符空间”
    惊奇地发现前面有个字符串ZWNBSP  问题的出现原因:     该CSV文件的编码格式是带有UTF-8-BOM,它与我们常用的UTF-8编码格式不同;区别就是在有没有BOM。即......
  • HDLBits-Adder3问题
    知识点genvari;generatefor(i=0;i<3;i=i+1)begin:fadd_arrfaddfadd_inst(a[i],b[i],......
  • react-infinite-scroll-component
    react-infinite-scroll-component  Acomponenttomakeallyourinfinitescrollingwoesgoawaywithjust4.15kB! PullDowntoRefresh featureadded.An......
  • 2022年春秋杯春季-勇者山峰-部分WriteUp
    关注公众号看图弹钢琴得到flag2、Mercy-code<?phphighlight_file(__FILE__);if($_POST['cmd']){$cmd=$_POST['cmd'];if(';'===preg_replace('/[a-z_]+\((?......
  • Python - tesserocr
    目录安装安装教程:https://cuiqingcai.com/31102.html自己在安装过程中遇到的错误:RuntimeError:TesseractlibrarynotfoundinLIBPATH:[]可以直接使用whl文件来......