• mybatis


    MyBatis简介

    mybatis是一款优秀的持久层框架

    1.2 持久层

    数据持久化
    ~持久化就是将程序的数据在持久状态和瞬时状态转化的过程
    ~内存:断电即失
    ~数据库(jdbc),io文件持久化

    为什么需要持久化?

    有一些对象不能让其丢失
    ~内存太贵了

    1.3 持久层

    Dao层,Service层,Controller层
    ~完成持久化工作的代码块
    ~层界限十分明显

    1.4为什么需要Mybatis

    ~方便
    ~传统的jdbc代码太复杂。简化,框架。自动化
    ~帮助程序员将数据存入到数据库中
    ~不用mybatis也可以,更容易上手

    最重要的一点是,使用的人多!

    第一个Mybatis程序

    思路:搭建环境–>导入Mybatis–>编写代码–>测试!

    2.1 搭建数据库

    1、新建一个普通的maven项目
    2、删除src目录
    3、导入maven

    
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0modelVersion>
        
        <groupId>org.examplegroupId>
        <artifactId>untitled1artifactId>
        <version>1.0-SNAPSHOTversion>
    
        
        <dependencies>
            
            
            <dependency>
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
                <version>8.0.25version>
            dependency>
            
            <dependency>
                <groupId>org.mybatisgroupId>
                <artifactId>mybatisartifactId>
                <version>3.5.2version>
            dependency>
    
            <dependency>
                <groupId>junitgroupId>
                <artifactId>junitartifactId>
                <version>4.12version>
            dependency>
        dependencies>
    
    project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    2.2 创建一个模块

    ~编写mybatis核心配置文件

    
    DOCTYPE configuration
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-config.dtd">
    
    <configuration>
        <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/bjw?useSSL=true&useUnicode=true&characterEncoding=UTF-8"/>
                    <property name="username" value="root"/>
                    <property name="password" value="bjw123"/>
                dataSource>
            environment>
        environments>
    
    configuration>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    ~编写mybatis工具类

    package com.bjw.utils;
    
    import jdk.internal.loader.Resource;
    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 java.io.IOException;
    import java.io.InputStream;
    
    //sqlSessionFactory -->sqlSession
    public class MybatisUtils {
    
        private static SqlSessionFactory sqlSessionFactory;
    
        static{
            try {
                //使用mybatis第一步获取sqlSessionFactory对象
                String resource = "mybatis-config.xml";
                InputStream inputStream = Resources.getResourceAsStream(resource);
                SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            }catch (IOException e) {
                e.printStackTrace();
            }
        }
        //既然有了SqlSessionFactory,顾名思义,我们就可以从中获得SqlSession的实例了
        //SqlSession完全包含了面向数据库执行SQL命令所需的所有方法
        public static SqlSession getSqlSession() {
            return sqlSessionFactory.openSession();
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    2.3 编写代码

    ~实体类

    package com.bjw.pojo;
    
    //实体类
    public class User {
        private int id;
        private String name;
        private String pwd;
        public User() {
    
        }
        public User(int id, String name, String pwd) {
            this.id = id;
            this.name = name;
            this.pwd = pwd;
        }
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getPwd() {
            return pwd;
        }
    
        public void setPwd(String pwd) {
            this.pwd = pwd;
        }
    
        @Override
        public String toString() {
            return "User{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", pwd='" + pwd + '\'' +
                    '}';
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49

    ~Dao接口层

    package com.bjw.dao;
    
    import com.bjw.pojo.User;
    
    import java.util.List;
    
    public interface UserDao {
        List<User> getUserList();
    
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    ~接口实现类由原来的UserDaoImpl转变为一个Mapper配置文件

    
    DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    
    <mapper namespace="com.bjw.dao.UserDao">
    
        <select id="getUserList" >
            select * from bjw.user
        select>
    
    
    mapper>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    2.4 测试

    核心配置文件中注册mappers
    ~junit测试

     @Test
        public void test() {
            //第一步,获得SqlSession对象
            SqlSession sqlSession = MybatisUtils.getSqlSession();
    
            //方式一:getMapper
            UserDao userDao = sqlSession.getMapper(UserDao.class);
            List<User> userList = userDao.getUserList();
    
            for (User user : userList) {
                System.out.println(user);
            }
            //关闭SqlSession
            sqlSession.close();
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    可能会遇到的问题
    1、配置文件没有注册
    2、绑定接口错误
    3、方法名不对
    4、返回类型不对
    5、maven导出资源问题

    注意:报错包含utf8字眼,应该是xml文件中加入中文注释的原因,去掉中文注释即可。

    MyBatis执行流程剖析

    在这里插入图片描述

  • 相关阅读:
    IVX低代码平台——小程序微信红包的应用的做法
    使用Jmeter+ant进行接口自动化测试(数据驱动)
    [NOI2018]情报中心
    MissionPlanner编译过程
    C++多线程基础
    前端批量下载文件(干货)
    如何封装axios请求。统一基地址、加载遮罩层、响应参数优化
    Web前端开发技术课程大作业——南京旅游景点介绍网页代码html+css+javascript
    夯实算法-每日温度
    【OS】第一章 操作系统导论
  • 原文地址:https://blog.csdn.net/qq_41398235/article/details/126473332