广告
返回顶部
首页 > 资讯 > 数据库 >Mybatis的基础知识点
  • 894
分享到

Mybatis的基础知识点

2024-04-02 19:04:59 894人浏览 独家记忆
摘要

小编给大家分享一下mybatis的基础知识点,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!mybatismybatis-conf

小编给大家分享一下mybatis的基础知识点,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

mybatis

mybatis-config.xml详细配置(配置时要把多余的属性删除 不能有中文 否则报错!)

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "Http://mybatis.org/dtd/mybatis-3-config.dtd"><!--configuration核心配置  配置文件的根元素 --><configuration>
    <!-- 属性:定义配置外在化 -->
    <properties></properties>
    <!-- 设置:定义mybatis的一些全局性设置 -->
    <settings>
        <!-- 具体的参数名和参数值 -->
        <setting name="" value=""/>
    </settings>
    <!-- 类型名称:为一些类定义别名 -->
    <typeAliases>
        <!-- 实体类少 建议 第一种取别名方式-->
        <typeAlias type="包路径" alias="别名"></typeAlias>
        <!--实体类多 建议  第二种取别名方式
        默认情况下用这种方式 别名为类名 首字母最好小写
        -->
        <package name="包名"/>
    </typeAliases>
    <!-- 类型处理器:定义Java类型与数据库中的数据类型之间的转换关系 -->
    <typeHandlers></typeHandlers>
    <!-- 对象工厂 -->
    <objectFactory type=""></objectFactory>
    <!-- 插件:mybatis的插件,插件可以修改mybatis的内部运行规则 -->
    <plugins>
        <plugin interceptor=""></plugin>
    </plugins>
    <!-- 环境:配置mybatis的环境 -->
    <environments default="development">
        <!-- 环境变量:可以配置多个环境变量,比如使用多数据源时,就需要配置多个环境变量 -->
        <environment id="development">
            <!-- 事务管理器 -->
            <transactionManager type="JDBC"/>
            <!-- 数据源 配置连接我的数据库-->
            <dataSource type="POOLED">
                <property name="driver" value="com.Mysql.jdbc.Driver"/>
                <property name="url"
                          value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=GMT%2B8&amp;useSSL=true&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
                <property name="passWord" value="123"/>
                <property name="username" value="root"/>
            </dataSource>
        </environment>
    </environments>
    <!-- 数据库厂商标识 -->
    <databaseIdProvider type=""></databaseIdProvider>
    <!-- 映射器:指定映射文件或者映射类 -->
    <mappers>
        <mapper resource="com/kang/w/dao/impl/UserMapper.xml"></mapper>
    </mappers></configuration>

分页

减少数据访问量
limt实现分页
sql语句: select * from 表名 limt 0,5;

  • 0:数据开始的位置

  • 5:数据的长度

第一种:使用Mybatis
1接口

  List<User> getUserByLimit(Map<String, Object> map);

2mapeer.xml

   <select id="getUserByLimit" parameterType="map" resultType="user">
        select *
        from mybatis.user
        limit ${starIndex},${pageSize}    </select>

2-1结果集映射

<resultMap id="map" type="User">
        <result property="pwd" column="password"></result>
    </resultMap>

3测试

 @Test
    public void getUserByLimitTest() {
        SqlSession sqlSession = MyBatisUtils.getSqlSession ();
        UserMapper mapper = sqlSession.getMapper (UserMapper.class);
        HashMap hashMap = new HashMap<String, Object> ();
        hashMap.put ("starIndex", 1);
        hashMap.put ("pageSize", 2);
        List userByLimit = mapper.getUserByLimit (hashMap);
        for (Object o : userByLimit) {
            System.out.println (o);
        }

        sqlSession.close ();
    }

第二种:使用RowBounds方法
1.接口
List getUserList();
2.实现接口

<select id="getUserList" resultType="user">
        select *
        from mybatis.user    </select>

3.测试:

 @Test
    public void getUserByLimitRowBoundsTest() {
        SqlSession sqlSession = MyBatisUtils.getSqlSession ();
        RowBounds rowBounds = new RowBounds (0, 2);
        List<User> userList = sqlSession.selectList ("com.kuang.w.dao.UserMapper.getUserList", null, rowBounds);
        for (User user : userList) {
            System.out.println (user);
        }
        //关闭
        sqlSession.close ();
    }

第三种:使用Mybatis的分页插件 pageHeIper
Mybatis的基础知识点

sql 多对一处理

数据库 :Mybatis的基础知识点
pojo
数据库中teacher-table表 对应实体类 Teacher

package com.kuang.w.pojo;

import lombok.Data;


@Data
public class Teacher {
    private int tId;
    private String tName;

}

数据库中user表 对应 实体类Student

package com.kuang.w.pojo;import lombok.Data;@Datapublic class Student {
    private int id;
    private int tid;
    private String name;
    private String password;
    private Teacher teacher;}

1.接口

   List<Student> getStudentList();

2.xml配置实现接口

  <!-- 多对一查询
    1 子查询 mysql 通过一个表里是数据   与另一个表的一个数据相的情况下 查询另一个的数据 一起显示
  -->
    <select id="getStudentList" resultMap="studentTeacher">
        select *
        from mybatis.user;    </select>
    <resultMap id="studentTeacher" type="Student">
        <!--  复杂属性 对象用 :association 集合用:collection-->
        <!--column 数据库中的字段 property 实体类中的属性-->
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="password" column="password"/>
        <!--javaType	一个 Java 类的全限定名
        ,或一个类型别名(关于内置的类型别名,可以参考上面的表格)。
        如果你映射到一个 JavaBean,MyBatis 通常可以推断类型。
        然而,如果你映射到的是 HashMap,
        那么你应该明确地指定 javaType 来保证行为与期望的相一致。-->
        <association property="teacher" column="tid" javaType="Teacher" select="getTeacher"></association>
    </resultMap>
    <select id="getTeacher" resultType="Teacher">
        select *
        from mybatis.teacher_table
        where tid = #{id};    </select>
 <!--2 多表联查-->
    <select id="getStudentList" resultMap="StudentList">
        select u.id       uid,
               u.name     uname,
               u.password upassword,
               u.tid      utid,
               t.tname
        from mybatis.user u,
             mybatis.teacher_table t
        where t.tid = u.tid;    </select>
     <!-- 映射-->
    <resultMap id="StudentList" type="Student">
        <result column="uid" property="id"/>
        <result column="utid" property="tid"/>
        <result column="uname" property="name"/>
        <result column="upassword" property="password"/>
        <association property="teacher" javaType="Teacher">
            <result property="tName" column="tname"></result>
        </association>
    </resultMap>

mybatis-config.xm配置

<?xml version="1.0" encoding="UTF8" ?><!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration>
    <properties resource="db.properties"/>
    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>
    <typeAliases>
        <typeAlias type="com.kuang.w.pojo.Teacher" alias="teacher"/>
        <typeAlias type="com.kuang.w.pojo.Student" alias="student"/>
    </typeAliases>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="password" value="${password}"/>
                <property name="username" value="${username}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <!--   <mapper resource="com/kuang/w/dao/TeacherMapper.xml"></mapper>
           <mapper resource="com/kuang/w/dao/StudentMapper.xml"></mapper>-->
        <mapper class="com.kuang.w.dao.StudentMapper"></mapper>
        <mapper class="com.kuang.w.dao.TeacherMapper"></mapper>
    </mappers></configuration>

3 测试

 @Test
    public void getStudentListTest() {
        SqlSession sqlSession = MyBatisUtils.getSqlSession ();
        StudentMapper mapper = sqlSession.getMapper (StudentMapper.class);


        List<Student> studentList = mapper.getStudentList ();
        for (Student student : studentList) {
            System.out.println (student);
        }

        sqlSession.commit ();
        sqlSession.close ();
    }

sql 一对多处理

数据表结构 对应的实体类 不变

第一种方式: 多表联查
1接口

    List<Teacher> getTeacher(int tid);

2.1 xml实现接口

  <select id="getTeacher" resultMap="TeacherStudent">
        select t.tid, t.tname, u.id, u.name, u.password
        from mybatis.user u,
             mybatis.teacher_table t
        where t.tid = u.tid
          and t.tid = #{tid};    </select>

2.2映射配置

<resultMap id="TeacherStudent" type="Teacher">
        <result property="tName" column="tname"/>
        <result property="tId" column="tid"/>
        <!--  复杂属性 对象用 :association 集合用:collection-->
        <collection property="students" ofType="Student">
            <!--javaType 指定属性类型 一个 Java 类的全限定名-->
            <result column="id" property="id"></result>
            <result column="name" property="name"></result>
            <result column="password" property="password"></result>
            <result column="tid" property="tid"></result>
        </collection>
    </resultMap>

3测试

 
    @Test
    public void getTeacherTest2() {
        SqlSession sqlSession = MyBatisUtils.getSqlSession ();
        TeacherMapper mapper = sqlSession.getMapper (TeacherMapper.class);
        List<Teacher> teacher = mapper.getTeacher (1);
        for (Teacher teacher1 : teacher) {
            System.out.println (teacher1);
        }

		//提交事务   架子  这里可以不要
        sqlSession.commit ();
        // 关闭
        sqlSession.close ();
    }

结果

com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 com.kuang.w.dao.myTest,getTeacherTest2
Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.PooledDataSource forcefully closed/removed all connections.PooledDataSource forcefully closed/removed all connections.PooledDataSource forcefully closed/removed all connections.PooledDataSource forcefully closed/removed all connections.Opening JDBC Connection
Created connection 164974746.Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@9d5509a]==>  Preparing: select t.tid, t.tname, u.id, u.name, u.password from mybatis.user u, mybatis.teacher_table t where t.tid = u.tid and t.tid = ?; ==> Parameters: 1(Integer)<==    Columns: tid, tname, id, name, password<==        Row: 1, 狂神, 1, 天王盖地虎, 111<==        Row: 1, 狂神, 2, 小波, 123<==        Row: 1, 狂神, 3, 雷神, 922<==        Row: 1, 狂神, 5, 马儿扎哈, 123<==      Total: 4Teacher(tId=1, tName=狂神, students=[Student(id=1, tid=1, name=天王盖地虎, password=111), Student(id=2, tid=1, name=小波, password=123), Student(id=3, tid=1, name=雷神, password=922), Student(id=5, tid=1, name=马儿扎哈, password=123)])Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@9d5509a]Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@9d5509a]Returned connection 164974746 to pool.Process finished with exit code 0

第二种方式: 子查询
1接口

    List<Teacher> getTeacher(int tid);

2 实现接口

 <!--第二种方式: 子查询-->
    <select id="getTeacher3" resultMap="TeacherStudent3">
        select *
        from mybatis.teacher_table
        where tid = #{tid};    </select>
    <resultMap id="TeacherStudent3" type="Teacher">
        <!--  复杂属性 对象用 :association 集合用:collection
        我们需要单独处理对象: association 集合: collection
        javaType=""指定属性的类型!
        集合中的泛型信息,我们使用ofType 获取
        -->
        <result column="tid" property="tId"/>
        <result column="tname" property="tName"/>
        <collection property="students" javaType="ArrayList" ofType="Student" select="getStudentByTeacherId"
                    column="tid">
        </collection>
    </resultMap>
    <select id="getStudentByTeacherId" resultType="Student">
        select *
        from mybatis.user
        where tid = #{tid};    </select>

3测试 同上

以上是“Mybatis的基础知识点”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注编程网数据库频道!

您可能感兴趣的文档:

--结束END--

本文标题: Mybatis的基础知识点

本文链接: https://www.lsjlt.com/news/54840.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Mybatis的基础知识点
    小编给大家分享一下Mybatis的基础知识点,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!mybatismybatis-conf...
    99+
    2022-10-18
  • ssm框架---MyBatis基础知识(一)
    MyBatis特性         1)MyBatis是支持定制化SQL、存储过程以及高级映射的优秀持久层框架         2)MyBatis避免了几乎所有的JDBC代码和手动设置参数以及获取结果集         3)MyBatis可...
    99+
    2023-09-04
    mybatis java mysql
  • PythonOpencv中基础的知识点
    目录1.创建窗口2.保存图片3.采集视频4.鼠标控制5.TrackBar组件OpenCV 是一个流行的开源计算机视觉库,可用于不同的编程语言,例如 Python、C++ 和 Java...
    99+
    2022-11-11
  • Python基础语法(Python基础知识点)
    Python与Perl,C和Java语言等有许多相似之处。不过,也有语言之间有一些明确的区别。本章的目的是让你迅速学习Python的语法。 第一个Python程序: 交互模式编程: 调用解释器不经过脚本文件...
    99+
    2022-06-04
    基础 知识点 语法
  • spark的基础知识点整理
    这篇文章主要介绍“spark的基础知识点整理”,在日常操作中,相信很多人在spark的基础知识点整理问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”spark的基础知识点整理”的疑惑有所帮助!接下来,请跟着小编...
    99+
    2023-06-02
  • linux的基础知识点整理
    这篇文章主要介绍“linux的基础知识点整理”,在日常操作中,相信很多人在linux的基础知识点整理问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”linux的基础知识点整理”的疑惑有所帮助!接下来,请跟着小编...
    99+
    2023-06-13
  • Html5的基础知识点整理
    这篇文章主要介绍“Html5的基础知识点整理”,在日常操作中,相信很多人在Html5的基础知识点整理问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Html5的基础知识点整理”...
    99+
    2022-10-19
  • React的基础知识点整理
    这篇文章主要介绍“React的基础知识点整理”,在日常操作中,相信很多人在React的基础知识点整理问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”React的基础知识点整理”...
    99+
    2022-10-19
  • Vbs的优点与基础知识
    这篇文章主要介绍“Vbs的优点与基础知识”,在日常操作中,相信很多人在Vbs的优点与基础知识问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Vbs的优点与基础知识”的疑惑有所帮助!接下来,请跟着小编一起来学习吧...
    99+
    2023-06-08
  • CSS基础知识点整理
    本篇内容介绍了“CSS基础知识点整理”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!CSS基础  CSS选择器 ...
    99+
    2023-06-05
  • Python基础知识点总结
       学了一年多的Python,去年做了一段时间的爬虫项目,近来在做数据分析和机器学习的东西,抽空整理一下以前学的Python基础知识点,有借鉴与总结。具体知识点后续会分段展开深入。     1.到底什么是Python?你可以在回答中与...
    99+
    2023-01-31
    知识点 基础 Python
  • redis 基础知识点汇总
    本文涉及的内容参考下面的大纲,另外版本的问题一般都会指出来。 正文 1. 思维导图 简单了做了一个思维导图,详细内容往后看。 2. 详解 下面针对思维导图列出的大纲,展开说明。 2.1 常用的 5 种数据类型 Redis 是基于 C 语言...
    99+
    2014-10-29
    redis 基础知识点汇总
  • MySQL基础知识点汇总
    本文给大家汇总介绍了mysql的23个基础的知识点,这些都是学习mysql的必备知识,小伙伴们可以参考下。 1.什么是SQL语句 sql语言:结构化的查询语言。(Structured Query...
    99+
    2022-10-18
  • Oracle基础知识点总结
    这篇文章主要讲解了“Oracle基础知识点总结”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Oracle基础知识点总结”吧!首先上一张Oracle体系结构图...
    99+
    2022-10-18
  • Python基础知识点分析
    本篇内容介绍了“Python基础知识点分析”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!Python简介Python的历史1989年圣诞节:...
    99+
    2023-06-02
  • JavaScript基础知识点分析
    这篇“JavaScript基础知识点分析”除了程序员外大部分人都不太理解,今天小编为了让大家更加理解“JavaScript基础知识点分析”,给大家总结了以下内容,具有一定借鉴价值,内容详细步骤清晰,细节处理妥当,希望大家通过这篇文章有所收获...
    99+
    2023-06-06
  • Linux基础知识点大全
    这篇文章主要讲解了“Linux基础知识点大全”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Linux基础知识点大全”吧!一、 从认识操作系统开始1.1 操作系统简介我通过以下四点介绍什么操作...
    99+
    2023-06-16
  • JAVA基础知识点总结
    文章目录 前言一、JAVA简介二、基础语法面向对象StringIntegerObject异常IO序列化Java 泛型注解反射 前言 一、JAVA简介 Java 是一门面向对象的编程语言。 语言特点:面向对象,平台无关性,支持多...
    99+
    2023-08-18
    java jvm 开发语言
  • C#基础知识点记录
    目录 课程一、C#基础1.C#编译环境、基础语法2.Winform-后续未学完 课程二、Timothy C#底层讲解一、类成员0常量1字段2属性3索引器5方法5.1值参数(创建副本,方法内...
    99+
    2023-09-03
    c#
  • STM32基础知识点总结
    一、基础知识点  1、课程体系介绍 单片机概述+arm体系结构+STM32开发环境搭建 STM32-GPIO编程-点亮世界的那盏灯 STM32-USART串口应用+SPI+液晶屏 STM32-中断系统 STM32-时钟系统 ...
    99+
    2023-09-04
    stm32 嵌入式硬件 单片机
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作