MyBatis查询所有数据

查询

  1. 查询所有数据

    1. 编写接口方法:Mapper接口,参数:无,结果:List<Brand>

      1. 在com.uestc.mapper创建一个BrandMapper类

        public interface BrandMapper {
        ​
            /**
             * 查询所有
             */
            public List<Brand> selectAll();
        ​
        }

         

        2.(编写SQL语句:SQL映射文件)在resources下面的com.uestc.mapper创建一个BrandMapper.xml文件

        复制代码
        <?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.uestc.mapper.BrandMapper">
        
            <!--
                数据库表的字段名称,和 实体类的属性名称 不一样,则不能自动封装数据
                    * 起别名: 对不一样的列名起别名,让别名和实体类的属性名一样
                           *缺点:每次查询都要定义一次别名
                                *sql片段
                                    *缺点:不灵活
                    * resultMap
                        1.定义<resultMap>标签
                        2.在<select>标签中使用resultMap属性替换 resultType属性
            -->
        
            <!--
                id:唯一标识
                type:映射的类型,支持别名
            -->
        
            <resultMap id="brandResultMap" type="brand">
                <!--
                    id: 完成主键字段的映射
                        column:表的列名
                        property:实体类的属性名
                    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>
        
            <!--
                sql片段
            -->
        <!--    <sql id="brand_column">-->
        <!--        id,brand_name as brandName,company_name as companyName,ordered,description,status-->
        <!--    </sql>-->
        
        <!--    <select id="selectAll" resultType="brand">-->
        
        <!--        select-->
        <!--            <include refid="brand_column" />-->
        <!--        from tb_brand;-->
        
        <!--    </select>-->
        
        <!--    <select id="selectAll" resultType="brand">-->
        
        <!--        select *-->
        <!--        from tb_brand;-->
        
        <!--    </select>-->
        
        </mapper>
        复制代码

         

         

        3.(执行方法:测试)在test下面创建com.uestc.test.MyBatisTest的类

        复制代码
        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();
            }
        }
        复制代码

         

    实体类属性名和数据库表列名不一致,不能自动封装数据

    (1)起别名:在SQL语句中,对不一样的列名起别名,别名和实体类属性名一样。*可以定义<sql>片段,提升复用性

    (2)resultMap:定义<resultMap>完成不一致的属性名和列名的映射。

posted @   Resign~as  阅读(547)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示