Mybatis 学习记录


1、先放上mybatis官网地址:

  https://mybatis.org/mybatis-3/zh/index.html

 

2、mybatis源码和有关包下载地址(GitHub):

  https://github.com/mybatis/mybatis-3

 

源码项目结构:

介绍:

  MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 。2013年11月迁移到Github。iBATIS一词来源于“internet”和“abatis”的组合,是一个基于Java的持久层框架。iBATIS提供的持久层框架包括SQL Maps和Data Access Objects(DAOs)。  

3、第一个mybatis项目的搭建

  创建普通Maven项目在pom.xml文件下进行相关依赖引入

1)Maven项目构建下通过pom.xml文件,Mybatis的引入

此处引入版本为3.5.3:
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.5.3version>
        dependency>    

  2)其他包的依赖

    <dependencies>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.12version>
            <scope>testscope>
        dependency>
        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.5.3version>
        dependency>
        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.47version>
        dependency>
    dependencies>

  3)数据库配置文件的配置(官网即可找到)

  命名: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>
    <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/填自己的数据库名?useSSL=true&useUnicode=true&characterEncoding=UTF-8"/>
                
                <property name="username" value="填自己的,用户名"/>
                
                <property name="password" value="填自己的,密码"/>
            dataSource>
        environment>
    environments>
  <mappers>
    
    <mapper resource="com/demo/dao/UserMapper.xml"/>
    mappers>


configuration>

  4)代码编写:

  创建java包相关目录结构:

  • com.demo.dao  
  • com.demo.utils
  • com.demo.pojo

   utils目录下:

  文件名:MybatisUtils.java

package com.demo.utils;

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;

/**
 * @author June
 * @date 2022/1/6 17:35
 */
//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();
        }
    }
    public static SqlSession getSqlSession(){
        return sqlSessionFactory.openSession();
    }
}

   pojo目录下:

  User.java

package com.demo.pojo;

/**
 * @author June
 * @date 2022/1/6 17:43
 */
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;
    }
}

  dao目录下:

  UserDao.java

package com.demo.dao;

import com.demo.pojo.User;

import java.util.List;

/**
 * @author June
 * @date 2022/1/6 17:44
 */
public interface Userdao {
    List getUser();
}

  UserMapper.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">