栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

mybatis整体架构

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

mybatis整体架构

本文目录
  • mybatis架构
    • 一、mybatis架构图
    • 二、Mybatis入门程序代码实现
      • 1.导入相关依赖
      • 2.创建Mybatis的主配置文件
      • 3.POJO类
      • 4.创建一个mybatis工具类
      • 5.xml配置文件--SQL语句书写
      • 6.测试程序
      • 7.再次回到mybatis整体架构

mybatis架构

  本文将对mybatis的架构进行介绍

一、mybatis架构图

二、Mybatis入门程序代码实现

  为了更好的理解上面的mybatis的架构图,我们通过一个mybatis的入门程序来加深理解

1.导入相关依赖 2.创建Mybatis的主配置文件

  Mybatis主配置文件:SqlMappingConfig.xml




  

    
    
    
        
    

    
    
        
        
        
        
    


    
    
        
            
            
            
            
                
                
                
                
            
        

    


    
    
        
        
        
        

    


3.POJO类

  Customer类

package com.itlike.domain;

public class Customer {
    private  Integer cust_id;
    private String cust_name;
    private String cust_profession;
    private String cust_phone;
    private String cust_email;

    public Integer getCust_id() {
        return cust_id;
    }

    public void setCust_id(Integer cust_id) {
        this.cust_id = cust_id;
    }

    public String getCust_name() {
        return cust_name;
    }

    public void setCust_name(String cust_name) {
        this.cust_name = cust_name;
    }

    public String getCust_profession() {
        return cust_profession;
    }

    public void setCust_profession(String cust_profession) {
        this.cust_profession = cust_profession;
    }

    public String getCust_phone() {
        return cust_phone;
    }

    public void setCust_phone(String cust_phone) {
        this.cust_phone = cust_phone;
    }

    public String getEmail() {
        return cust_email;
    }

    public void setEmail(String email) {
        this.cust_email = email;
    }

    @Override
    public String toString() {
        return "Customer{" +
                "cust_id=" + cust_id +
                ", cust_name='" + cust_name + ''' +
                ", cust_profession='" + cust_profession + ''' +
                ", cust_phone='" + cust_phone + ''' +
                ", email='" + cust_email + ''' +
                '}';
    }
}
4.创建一个mybatis工具类

  由于每次使用mybatis对数据库进行操作都需要获取sqlSession对象,这将造成大量的代码冗余,所以我们把这些冗余的步骤写到工具类中,以后用到的时候直接调用工具类中的方法即可。

  MybatisUtils 工具类

package com.itlike.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;


public class MybatisUtils {
    public static final SqlSessionFactory sessionFactory ;
    //静态代码块 只执行一次
    static {
        //获取SqlSessionFactoryBuilder对象
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();

        //读取配置文件
        InputStream resourceAsStream=null;
        try {
            resourceAsStream = Resources.getResourceAsStream("SqlMappingConfig.xml");

        } catch (IOException e) {
            e.printStackTrace();
        }
        sessionFactory = sqlSessionFactoryBuilder.build(resourceAsStream);


    }
    public static SqlSession openSession(){
        return sessionFactory.openSession();
    }
}
5.xml配置文件–SQL语句书写

  Customer.xml
  里面写了增删查改的SQL语句





        
        
            
            SELECT * FROM customer WHERe cust_id=#{cust_id}
        
        
    
            
            SELECT * FROM customer WHERe cust_name LIKE #{name }
        
    
    
        insert into customer value (#{cust_id},#{cust_name},#{cust_profession},#{email},#{cust_phone})
    
    
    
    
        update  customer set cust_name=#{cust_name} where cust_id=#{cust_id}
    
    
    
        delete from customer where cust_id=#{cust_id}
    

6.测试程序

  单元测试代码

    //查询所有数据
    @Test
    public void test3() {
        SqlSession sqlSession = MybatisUtils.openSession();
        //遍历快捷方式 iter
        List queryAllCustomers = sqlSession.selectList("queryAllCustomers");
        for (Customer customer : queryAllCustomers) {
            System.out.println(customer);
        }
        sqlSession.close();
    }

  运行结果如图:

7.再次回到mybatis整体架构

  mybatis首先通过SqlSessionFactoryBuilder加载配置文件SqlMapConfig.xml 并给你一个工厂sessionFactory
  代码如下:

  static {
        //获取SqlSessionFactoryBuilder对象
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();

        //读取配置文件
        InputStream resourceAsStream=null;
        try {
            resourceAsStream = Resources.getResourceAsStream("SqlMappingConfig.xml");

        } catch (IOException e) {
            e.printStackTrace();
        }
        sessionFactory = sqlSessionFactoryBuilder.build(resourceAsStream);


    }

sessionFactory 可以给你一个sqlSession 你拿到这个sqlSession之后就可以针对数据库进行操作了。

   @Test
    public void test3() {
        SqlSession sqlSession = MybatisUtils.openSession();
        //遍历快捷方式 iter
        List queryAllCustomers = sqlSession.selectList("queryAllCustomers");
        for (Customer customer : queryAllCustomers) {
            System.out.println(customer);
        }
        sqlSession.close();
    }

你在上面代码里面调用方法,比如
List queryAllCustomers = sqlSession.selectList(“queryAllCustomers”);
里面会有专门的执行器来帮你执行这些方法,执行器会到对应的映射xml文件中去给你找对应的语句,找到后把对应语句的参数传给这个语句(如果这个语句需要参数的话)。这些步骤都完成之后再到数据库中去执行这个语句,执行完之后还有输出映射返回给你。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/955780.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号