主頁 > 軟體設計 > 一篇文章帶你掌握主流資料庫框架——MyBatis

一篇文章帶你掌握主流資料庫框架——MyBatis

2022-09-12 06:58:20 軟體設計

一篇文章帶你掌握主流資料庫框架——MyBatis

MyBatis 是一款優秀的持久層框架,它支持自定義 SQL、存盤程序以及高級映射,

在之前的文章中我們學習了MYSQL和JDBC,但是這些東西遠遠不足以支持我們書寫JavaWeb相關的內容

在學習MyBatis前,請先學習Java,Mysql,JDBC,Maven內容

MyBatis學前須知

首先我們先簡單介紹一下MyBatis:

  • Mybats是一款優秀的持久層框架,用于簡化JDBC開發
  • MyBatis本是Apache的一個開源專案iBatis,2010年由apache software foundation 遷移到了google code,并改名為MyBatis
  • 官網:MyBatis中文網

我們再來介紹一下JavaEE概念:

  • JavaEE三層架構:表現層,業務層,持久層
  • 表現層:負責接收客戶端請求,向客戶端回應結果
  • 業務層:它負責業務邏輯處理
  • 持久層:負責將資料保存到資料庫的那一層代碼

最后我們了解一下框架:

  • 框架是一個半成品軟體,是一套可重用的,通用的軟體基礎代碼模型
  • 在框架的基礎上構建軟體撰寫更加有效,規范,通用,可擴展

MyBatis產生背景

我們在前面提到MyBatis的產生是為了簡化JDBC開發

那么我們就先來介紹一下JDBC的缺點:

我們通過一段代碼進行決議:

package com.itheima.jdbc;

import org.junit.Test;

import java.sql.*;
import java.util.Date;

    @Test
    public void testPreparedStatement2() throws  Exception {

        // 前置鏈接操作
        String url = "jdbc:mysql:///db1?useSSL=false&useServerPrepStmts=true";
        String username = "root";
        String password = "1234";
        Connection conn = DriverManager.getConnection(url, username, password);

        // 接收用戶輸入 用戶名和密碼
        String name = "zhangsan";
        String pwd = "' or '1' = '1";

        // 定義sql
        String sql = "select * from tb_user where username = ? and password = ?";

        // 獲取pstmt物件
        PreparedStatement pstmt = conn.prepareStatement(sql);


        Thread.sleep(10000);
        // 設定?的值
        pstmt.setString(1,name);
        pstmt.setString(2,pwd);
        ResultSet rs = null;
                // 執行sql
        rs = pstmt.executeQuery();


        // 設定?的值
        pstmt.setString(1,"aaa");
        pstmt.setString(2,"bbb");

        // 執行sql
        rs = pstmt.executeQuery();


        // 判斷登錄是否成功
        if(rs.next()){
            System.out.println("登錄成功~");
        }else{
            System.out.println("登錄失敗~");
        }

        //7. 釋放資源
        rs.close();
        pstmt.close();
        conn.close();
    }
}

在上述代碼中,我們可以注意到:

/*
硬編碼概念: 代碼展現形式固定,如果有所更改需要手動修改代碼
硬編碼動作:注冊驅動,獲得鏈接,SQL陳述句
*/

		// 前置鏈接操作
        String url = "jdbc:mysql:///db1?useSSL=false&useServerPrepStmts=true";
        String username = "root";
        String password = "1234";
        Connection conn = DriverManager.getConnection(url, username, password);

        // 接收用戶輸入 用戶名和密碼
        String name = "zhangsan";
        String pwd = "' or '1' = '1";

/*
操作繁瑣:大量代碼堆疊
操作繁瑣動作:手動設定引數,手動封裝結果
*/
    
		// 獲取pstmt物件
        PreparedStatement pstmt = conn.prepareStatement(sql);


        Thread.sleep(10000);
        // 設定?的值
        pstmt.setString(1,name);
        pstmt.setString(2,pwd);
        ResultSet rs = null;
                // 執行sql
        rs = pstmt.executeQuery();


        // 設定?的值
        pstmt.setString(1,"aaa");
        pstmt.setString(2,"bbb");

        // 執行sql
        rs = pstmt.executeQuery();

因而為了解決JDBC的這些缺點,MyBatis應運而生!

MyBatis解決思想:

  • 硬編碼 -> 組態檔
    • 注冊驅動,獲得連接:在mybatis-config.xml設定其連接池資訊
    • SQL陳述句:設定專門的Mapper介面和Mapper.xml設定其資訊
  • 操作繁瑣 - > 自動完成
    • 采用SqlSession物件的各類方法直接封裝

MyBatis免除了幾乎所有的JDBC代碼以及設定引數和獲得結果集的作業

MyBatis快速入門

我們將以一個案例進行MyBatis的快速入門(資源來自B站黑馬程式員老陳的JavaWeb課程)

我們以步驟形式逐步進行:

  1. 準備資料庫資料

  1. 創建模塊,匯入坐標(這里采用Maven創建專案,在專案自動生成的pom.xml中匯入模塊坐標即可)
<!--
我們使用mybatis,匯入mybatis坐標
我們使用mysql,匯入mysql坐標
我們需要進行單元測驗,匯入junit坐標
我們需要日志,查看錯誤資訊,匯入日志坐標(注意:需要匯入logback.xml包,可在網路查詢)
->

<dependencies>
        <!--mybatis 依賴-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.5</version>
        </dependency>

        <!--mysql 驅動-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.46</version>
        </dependency>

        <!--junit 單元測驗-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
            <scope>test</scope>
        </dependency>


        <!-- 添加slf4j日志api -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.20</version>
        </dependency>
        <!-- 添加logback-classic依賴 -->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
        </dependency>
        <!-- 添加logback-core依賴 -->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>1.2.3</version>
        </dependency>

    </dependencies>
  1. 撰寫MyBatis核心組態檔(替換連接資訊,解決硬編碼問題)
<!--
創建mybatis-config.xml,
寫入下列資訊(MyBatis官網可查找)

<?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="https://www.cnblogs.com/qiuluoyuweiliang/p/${driver}"/>
        <property name="url" value="https://www.cnblogs.com/qiuluoyuweiliang/p/${url}"/>
        <property name="username" value="https://www.cnblogs.com/qiuluoyuweiliang/p/${username}"/>
        <property name="password" value="https://www.cnblogs.com/qiuluoyuweiliang/p/${password}"/>
      </dataSource>
    </environment>
  </environments>
  <mappers>
    <mapper resource="org/mybatis/example/BlogMapper.xml"/>
  </mappers>
</configuration>

然后我們需要對上述資訊進行修改,使其完成連接資料庫的問題
-->

<?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表示資料庫連接--->
      <dataSource type="POOLED">
          <!--
			我們需要修改下述資訊
			driver:固定com.mysql.jdbc.Driver
			url:jdbc:mysql:/// + 資料庫名稱 + ?useSSL=false
			username:資料庫賬號
			password:資料庫密碼
			-->
        <property name="driver" value="https://www.cnblogs.com/qiuluoyuweiliang/p/com.mysql.jdbc.Driver"/>
        <property name="url" value="https://www.cnblogs.com/qiuluoyuweiliang/p/jdbc:mysql:///mybatis?useSSL=false"/>
        <property name="username" value="https://www.cnblogs.com/qiuluoyuweiliang/p/root"/>
        <property name="password" value="https://www.cnblogs.com/qiuluoyuweiliang/p/123456"/>
      </dataSource>
    </environment>
  </environments>
    <!--這里mapper表示映射地址:我們所需要的Mapper.xml,在后續我們會在Mapper.xml中書寫SQL陳述句-->
  <mappers>
    <mapper resource="com/itheima/mapper/UserMapper.xml"/>
  </mappers>
</configuration>
  1. 創建SQL映射檔案(統一管理sql陳述句,解決硬編碼問題)
<!--
同樣自己創建Mapper.xml檔案,這里注意在前面加上前綴,如果你是Usr用戶的資料庫操作,命名為UserMapper.xml便于區分
匯入下述代碼(同樣,在Mybatis官網可以找到)

<?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">
<mapper namespace="org.mybatis.example.BlogMapper">
  <select id="selectBlog" resultType="Blog">
    select * from Blog where id = #{id}
  </select>
</mapper>

在創建完成后,不要忘記回到上一步,把mapper的resource地址改為該檔案所在地址

-->

<?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介面名稱一致保證互聯(這里暫時設定為test)
id:查找方法的唯一表示
resultType:回傳型別
在<select></select>中間書寫陳述句即可

后續會繼續介紹
-->
<mapper namespace="test">
  <select id="selectAll" resultType="User">
    select * from Blog where id = #{id}
  </select>
</mapper>
  1. 1 定義POJO類(與資料庫資訊完全相同的類)
// 注意:資料為private,書寫get和set方法以及toString方法

package com.itheima.pojo;


// alt + 滑鼠左鍵 整列編輯
public class User {

    private Integer id;
    private String username;
    private String password;
    private String gender;
    private String addr;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getAddr() {
        return addr;
    }

    public void setAddr(String addr) {
        this.addr = addr;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", gender='" + gender + '\'' +
                ", addr='" + addr + '\'' +
                '}';
    }
}
  1. 2 主代碼展示
// 創建主代碼

package com.itheima;


import com.itheima.pojo.User;
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;
import java.util.List;

/**
 * Mybatis 快速入門代碼
 */
public class MyBatisDemo {

    public static void main(String[] args) throws IOException {

        //1. 加載mybatis的核心組態檔,獲取 SqlSessionFactory(官網下載)
        
        /*
        下述為官網下載,我們只需要修改第一行的mybatis-config.xml地址即可
        String resource = "org/mybatis/example/mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        */
        
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2. 獲取SqlSession物件,用它來執行sql(sqlSessionFactory方法)
        SqlSession sqlSession = sqlSessionFactory.openSession();
        
        //3. 執行sql(這里的test.selectList是我們的UserMapper.xml中所設定的名稱空間.id)
        List<User> users = sqlSession.selectList("test.selectAll");
        System.out.println(users);
        
        //4. 釋放資源
        sqlSession.close();

    }
}

至此,我們已經了解了MyBatis的整體創建流程

在下面我們會以上述入門為標準,進行各種流程上的簡化開發

解決SQL陳述句警告問題(IDEA正式版)

我們在Mapper.xml中書寫sql陳述句時,可能會出現sql表名顯示紅色(報錯)現象

產生原因:IDEA和資料庫沒有建立連接,不識別表資訊

解決方法:在IDEA中配置MYSQL資料庫連接

解決優點:代碼不再報錯,顯示所有SQL陳述句以及表列的補全資訊

解決方法:

  1. 在Database中打開加號,逐步打開Data Source,MYSQL
  2. 打開頁面后,填寫USer,Password即可

Mapper代理開發

我們在入門代碼中創建了Mapper.xml,并在其中書寫代碼

我們在主專案的代碼中包含有以下這段:

List<User> users = sqlSession.selectList("test.selectAll");

但test.selectAll屬于硬編碼階段,且書寫方式麻煩

因而產生了Mapper代理開發,同樣我們采用案例的形式逐步書寫:

  1. 定義與SQL映射檔案同名的Mapper介面,并將該介面與SQL映射檔案放置在同一目錄級別下(IDEA2022版已解決這個問題)

  1. 設定SQL映射檔案的namespace屬性為Mapper介面全限定名(介面與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.itheima.mapper.UserMapper">

    <!--statement-->
    <select id="selectAll" resultType="user">
        select *
        from tb_user;
    </select>
</mapper>
  1. 在Mapper介面中定義方法,方法名就是SQL映射檔案中sql陳述句的id,并保持引數型別和回傳值型別一致
package com.itheima.mapper;

import com.itheima.pojo.User;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

public interface UserMapper {

    List<User> selectAll();
    
}
  1. 主代碼展示:
package com.itheima;


import com.itheima.mapper.UserMapper;
import com.itheima.pojo.User;
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;
import java.util.List;

/**
 * Mybatis 代理開發
 */
public class MyBatisDemo2 {

    public static void main(String[] args) throws IOException {

        //1. 加載mybatis的核心組態檔,獲取 SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2. 獲取SqlSession物件,用它來執行sql
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //3.1 獲取UserMapper介面的代理物件
        //(采用sqlSession方法獲得介面類產生物件,呼叫物件的方法[這里方法來自xml],并根據Mapper介面設定的回傳引數)
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = userMapper.selectAll();

        System.out.println(users);
        //4. 釋放資源
        sqlSession.close();

    }
}

Mapper補充內容

在mybatis-config.xml檔案中我們設定過mapper內容:

    <mappers>
        <!--加載sql映射檔案-->
       <mapper resource="com/itheima/mapper/UserMapper.xml">
    </mappers>

但隨著sql映射檔案增多,單個書寫過于麻煩,所以開發出了package方法

    <mappers>
        <!--加載sql映射檔案-->
       <!-- <mapper resource="com/itheima/mapper/UserMapper.xml"/>-->

        <!--Mapper代理方式:通過包掃描的方法,將包中所對應的mapper.xml映射過來-->
        <package name="com.itheima.mapper"/>

    </mappers>

MyBatis核心組態檔

Mybatis核心組態檔就是我們之前寫入的mybatis-config.xml檔案

現在我們對MyBatis的相關內容進行分析:

  1. 標簽:
    • 型別別名可為 Java 型別設定一個縮寫名字,
    • 它僅用于 XML 配置,意在降低冗余的全限定類名書寫,
    • 我們可以在標簽內書寫package標簽,并設定檔案夾名稱,則后續內容中不需要添加該檔案夾名稱
  2. 標簽:
    • MyBatis 可以配置成適應多種環境,這種機制有助于將 SQL 映射應用于多種資料庫之中
    • 通過default設定資訊來更換資料庫,default的值為下述environment的id值
  3. Mapper,dataSource以及內部資訊均已介紹,這里不再介紹

下面給出代碼展示:

<?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>

    
    
    <typeAliases>
        <package name="com.itheima.pojo"/>
    </typeAliases>
    
    <!--
    environments:配置資料庫連接環境資訊,可以配置多個environment,通過default屬性切換不同的environment
    -->
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <!--資料庫連接資訊-->
                <property name="driver" value="https://www.cnblogs.com/qiuluoyuweiliang/p/com.mysql.jdbc.Driver"/>
                <property name="url" value="https://www.cnblogs.com/qiuluoyuweiliang/p/jdbc:mysql:///mybatis?useSSL=false"/>
                <property name="username" value="https://www.cnblogs.com/qiuluoyuweiliang/p/root"/>
                <property name="password" value="https://www.cnblogs.com/qiuluoyuweiliang/p/123456"/>
            </dataSource>
        </environment>

        <environment id="test">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <!--資料庫連接資訊-->
                <property name="driver" value="https://www.cnblogs.com/qiuluoyuweiliang/p/com.mysql.jdbc.Driver"/>
                <property name="url" value="https://www.cnblogs.com/qiuluoyuweiliang/p/jdbc:mysql:///mybatis?useSSL=false"/>
                <property name="username" value="https://www.cnblogs.com/qiuluoyuweiliang/p/root"/>
                <property name="password" value="https://www.cnblogs.com/qiuluoyuweiliang/p/1234"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <!--加載sql映射檔案-->
       <!-- <mapper resource="com/itheima/mapper/UserMapper.xml"/>-->

        <!--Mapper代理方式-->
        <package name="com.itheima.mapper"/>

    </mappers>


</configuration>

MyBatis進階內容(增刪改查)

在下述內容中,我們會通過一個案例進行MyBatis的增刪改查并且補充相應知識點

在開始前,我們同樣準備環境:

  1. 資料庫表tb_brand

  1. 物體類Brand
package com.itheima.pojo;

/**
 * 品牌
 *
 * alt + 滑鼠左鍵:整列編輯
 *
 * 在物體類中,基本資料型別建議使用其對應的包裝型別
 */

public class Brand {
    // id 主鍵
    private Integer id;
    // 品牌名稱
    private String brandName;
    // 企業名稱
    private String companyName;
    // 排序欄位
    private Integer ordered;
    // 描述資訊
    private String description;
    // 狀態:0:禁用  1:啟用
    private Integer status;


    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getBrandName() {
        return brandName;
    }

    public void setBrandName(String brandName) {
        this.brandName = brandName;
    }

    public String getCompanyName() {
        return companyName;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

    public Integer getOrdered() {
        return ordered;
    }

    public void setOrdered(Integer ordered) {
        this.ordered = ordered;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    @Override
    public String toString() {
        return "Brand{" +
                "id=" + id +
                ", brandName='" + brandName + '\'' +
                ", companyName='" + companyName + '\'' +
                ", ordered=" + ordered +
                ", description='" + description + '\'' +
                ", status=" + status +
                '}';
    }
}
  1. 測驗用例(在test檔案夾下的java檔案下創建test即可)

  1. 安裝MyBatisX插件(方便對應Mapper.xml和Mapper介面)

查詢資料

我們將會介紹三種資料查詢方法:

  • 查詢所有資料
  • 根據ID查詢單個資料
  • 根據條件查詢資料

接下來我們逐一講解:

查詢所有資料

查詢所有資料步驟:

  1. 撰寫介面方法:Mapper介面(引數:無 回傳型別:List)
package com.itheima.mapper;


import com.itheima.pojo.Brand;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

public interface BrandMapper {


    /**
     * 查詢所有
     */
    List<Brand> selectAll();
}
  1. 撰寫SQL陳述句(在xml中撰寫)
    <select id="selectAll" resultType="brand">
        select *
        from tb_brand;
    </select>
  1. 執行方式
    @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();

    }

但是這種方法中,我們會發現brandName和companyName并沒有顯示

主要原因:

  • brandName在MYSQL中定義為brand_name;companyName在MYSQL中定義為company_name

解決方法有兩種:

  • 給所有名稱不同的屬性添加別名,使其與MYSQL屬性同名
<mapper namespace="com.itheima.mapper.BrandMapper">
	<select id="selectAll" resultMap="brandResultMap">
        select id, brand_name as brandName, company_name as companyName, ordered, description, status
        from tb_brand;
    </select>
</mapper>
  • 在xml中添加一段ResultMap屬性(推薦!!!)
<mapper namespace="com.itheima.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>
    
   <select id="selectAll" resultMap="brand">
        select *
        from tb_brand;
    </select>   
</mapper>

單個查詢

單個查詢步驟:

  1. 撰寫介面方法:Mapper介面(引數:id 回傳型別:Brand)
    Brand selectById(int id);
  1. 撰寫SQL陳述句
    <select id="selectById" resultMap="brandResultMap">
        select *
        from tb_brand
        where id = #{id};
    </select>
  1. 執行方法,測驗
    @Test
    public void testSelectById() throws IOException {
        //接收引數
        int id = 1;

        //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. 執行方法
        Brand brand = brandMapper.selectById(id);
        System.out.println(brand);

        //5. 釋放資源
        sqlSession.close();

    }

知識點講解:

  • {}:會將其替換為 ? 放置SQL注入

  • ${}:拼接sql,會存在SQL注入問題
  • 使用時機:
    • 引數傳遞:#{}
    • 表名或列名不固定的情況下:${}

條件查詢

這里我們介紹多條件查詢:

  1. 撰寫介面方法:Mapper介面(引數:所有查詢條件 回傳結果:List)
    /**
     * 條件查詢
     *  * 引數接收
     *      1. 散裝引數:如果方法中有多個引數,需要使用@Param("SQL引數占位符名稱")
     *      2. 物件引數:物件的屬性名稱要和引數占位符名稱一致
     *      3. map集合引數
     *
     */

    List<Brand> selectByCondition(@Param("status") int status, @Param("companyName") String companyName, @Param("brandName") String brandName);


    List<Brand> selectByCondition(Brand brand);


    List<Brand> selectByCondition(Map map);
  1. 撰寫SQL陳述句:
    <!--
        條件查詢:
		這里#{}中的元素和Param所書寫的屬性相同;
		如果是Brand,則與Brand的屬性相同;
		如果是Map,則與Map集合的鍵相同即可
    -->
    <select id="selectByCondition" resultMap="brandResultMap">
         select *
         from tb_brand
         where status = #{status}
           and company_name like #{companyName}
           and brand_name like #{brandName}
     </select>
  1. 執行方法,測驗:
    @Test
    public void testSelectByCondition() throws IOException {
        //接收引數
        int status = 1;
        String companyName = "華為";
        String brandName = "華為";

        // 處理引數
        companyName = "%" + companyName + "%";
        brandName = "%" + brandName + "%";

        //封裝物件
       /* Brand brand = new Brand();
        brand.setStatus(status);
        brand.setCompanyName(companyName);
        brand.setBrandName(brandName);*/

        Map map = new HashMap();
        // map.put("status" , status);
        map.put("companyName", companyName);
        // map.put("brandName" , brandName);

        //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.selectByCondition(status, companyName, brandName);
//        List<Brand> brands = brandMapper.selectByCondition(brand);
        List<Brand> brands = brandMapper.selectByCondition(map);
        System.out.println(brands);

        //5. 釋放資源
        sqlSession.close();

    }

動態查詢

動態查詢概念:

  • 在所有的查詢條件中,用戶可能并不會全部查詢,可能只有挑選條件查詢
  • 這時如果以之前的代碼,會導致匯入值為null而導致查詢錯誤

動態SQL陳述句:

  • if陳述句
<!--
if標簽:整體框架
test:判斷條件
  <if test="title != null">
		sql陳述句內容
  </if>
-->

<select id="findActiveBlogWithTitleLike"
     resultType="Blog">
  SELECT * FROM BLOG
  WHERE state = ‘ACTIVE’
  <if test="title != null">
    AND title like #{title}
  </if>
</select>
  • where陳述句
    <!--
        動態條件查詢
            * if: 條件判斷
                * test:邏輯運算式
            * 問題:
                * 恒等式
                * <where> 替換 where 關鍵字
    -->

<!--這里的where會根據實際情況,自行添加and或者洗掉and-->
	<select id="selectByCondition" resultMap="brandResultMap">
        select *
        from tb_brand
        /* where 1 = 1*/
        <where>

            <if test="status != null">
                and status = #{status}
            </if>
            <if test="companyName != null and companyName != '' ">
                and company_name like #{companyName}
            </if>
            <if test="brandName != null and brandName != '' ">
                and brand_name like #{brandName}
            </if>
            
        </where>
  • choose陳述句
<!--

<choose> <!--相當于switch-->
<when test="status != null"> <!--相當于case-->

-->


<select id="findActiveBlogLike"
     resultType="Blog">
  SELECT * FROM BLOG WHERE state = ‘ACTIVE’
  <choose>
    <when test="title != null">
      AND title like #{title}
    </when>
    <when test="author != null and author.name != null">
      AND author_name like #{author.name}
    </when>
    <otherwise>
      AND featured = 1
    </otherwise>
  </choose>
</select>

我們根據兩個案例來解釋上述語法:

  • 多條件查詢陳述句:
  1. 撰寫介面方法:Mapper介面(引數:Brand 回傳型別:List)
List<Brand> selectByCondition(Brand brand);
  1. 撰寫SQL陳述句:
    <!--
        動態條件查詢
            * if: 條件判斷
                * test:邏輯運算式
            * 問題:
                * 恒等式
                * <where> 替換 where 關鍵字
    -->
    <select id="selectByCondition" resultMap="brandResultMap">
        select *
        from tb_brand
        /* where 1 = 1*/
        <!--這里使用where,防止無條件或者and位置錯誤-->
        <where>

            <if test="status != null">
                and status = #{status}
            </if>
            <if test="companyName != null and companyName != '' ">
                and company_name like #{companyName}
            </if>
            <if test="brandName != null and brandName != '' ">
                and brand_name like #{brandName}
            </if>
            
        </where>

    </select>
  1. 執行方法,測驗:
    @Test
    public void testSelectByCondition() throws IOException {
        //接收引數
        int status = 1;
        String companyName = "華為";
        String brandName = "華為";

        // 處理引數
        companyName = "%" + companyName + "%";
        brandName = "%" + brandName + "%";

        //封裝物件
        Brand brand = new Brand();
        brand.setStatus(status);
        brand.setCompanyName(companyName);
        brand.setBrandName(brandName);

        //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.selectByCondition(brand);
        System.out.println(brands);

        //5. 釋放資源
        sqlSession.close();

    }
  • 單條件動態查詢:
  1. 撰寫介面方法:Mapper介面(引數:Brand 回傳型別:List)
List<Brand> selectByConditionSingle(Brand brand);
  1. 撰寫SQL陳述句:
    <select id="selectByConditionSingle" resultMap="brandResultMap">
        select *
        from tb_brand
        <!--這里使用where防止無條件出現導致錯誤-->
        <where>
            <choose><!--相當于switch-->
                <when test="status != null"><!--相當于case-->
                    status = #{status}
                </when>
                <when test="companyName != null and companyName != '' "><!--相當于case-->
                    company_name like #{companyName}
                </when>
                <when test="brandName != null and brandName != ''"><!--相當于case-->
                    brand_name like #{brandName}
                </when>

            </choose>
        </where>
    </select>
  1. 執行方法,測驗:
    @Test
    public void testSelectByConditionSingle() throws IOException {
        //接收引數
        int status = 1;
        String companyName = "華為";
        String brandName = "華為";

        // 處理引數
        companyName = "%" + companyName + "%";
        brandName = "%" + brandName + "%";

        //封裝物件
        Brand brand = new Brand();
        //brand.setStatus(status);
        brand.setCompanyName(companyName);
        //brand.setBrandName(brandName);

        //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.selectByConditionSingle(brand);
        System.out.println(brands);

        //5. 釋放資源
        sqlSession.close();

    }

添加資料

在這小節我們會講到添加功能并附帶回傳主鍵:

添加功能

添加功能步驟:

  1. 撰寫介面方法:Mapper介面(引數:Brand 回傳型別:void )
void add(Brand brand);
  1. 撰寫MYSQL陳述句:
<!--

useGeneratedKeys:對于支持自動生成記錄主鍵的資料庫,如:MySQL,SQL Server,此時設定useGeneratedKeys引數值為true,在執行添加記錄之后可以獲取到資料庫自動生成的主鍵ID,

keyProperty:后面跟資料庫中自動增長的列名,這時該屬性值就會反饋在Java代碼中

-->
<insert id="add" useGeneratedKeys="true" keyProperty="id">
        insert into tb_brand (brand_name, company_name, ordered, description, status)
        values (#{brandName}, #{companyName}, #{ordered}, #{description}, #{status});
    </insert>
  1. 代碼除錯,測驗:
    @Test
    public void testAdd() throws IOException {
        //接收引數
        int status = 1;
        String companyName = "波導手機";
        String brandName = "波導";
        String description = "手機中的戰斗機";
        int ordered = 100;


        //封裝物件
        Brand brand = new Brand();
        brand.setStatus(status);
        brand.setCompanyName(companyName);
        brand.setBrandName(brandName);
        brand.setDescription(description);
        brand.setOrdered(ordered);

        //1. 獲取SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2. 獲取SqlSession物件
        SqlSession sqlSession = sqlSessionFactory.openSession();
        // 這里openSession的引數未設定時為手動提交資訊,設定true后為自動提交事務
        //SqlSession sqlSession = sqlSessionFactory.openSession(true);

        //3. 獲取Mapper介面的代理物件
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);

        //4. 執行方法(這里就可以對brand的id進行提取,并輸出)

        brandMapper.add(brand);
        Integer id = brand.getId();
        System.out.println(id);

        // 執行add后并未立刻執行,我們需要提交事務才能反饋到資料庫中
        sqlSession.commit();

        //5. 釋放資源
        sqlSession.close();

    }

修改資料

該小節中我們會介紹兩種修改方法:

  • 全欄位修改
  • 單欄位修改

我們會在下述內容中一一講解:

全欄位修改

  1. 撰寫介面方法:Mapper介面(引數:所有資料 回傳型別:int(修改行)或void)
int update(Brand brand);
  1. 撰寫MYSQL陳述句:
    <update id="update">
        update tb_brand
        set	brand_name = #{brandName},
            company_name = #{companyName},
            ordered = #{ordered},
            description = #{description},
            status = #{status}
        where id = #{id};
    </update>
  1. 執行方法,測驗:
    @Test
    public void testUpdate() throws IOException {
        //接收引數
        int status = 0;
        String companyName = "波導手機";
        String brandName = "波導";
        String description = "波導手機,手機中的戰斗機";
        int ordered = 200;
        int id = 6;


        //封裝物件
        Brand brand = new Brand();
        brand.setStatus(status);
        brand.setCompanyName(companyName);
        brand.setBrandName(brandName);
        brand.setDescription(description);
        brand.setOrdered(ordered);
        brand.setId(id);

        //1. 獲取SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2. 獲取SqlSession物件
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //SqlSession sqlSession = sqlSessionFactory.openSession(true);

        //3. 獲取Mapper介面的代理物件
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);

        //4. 執行方法
        int count = brandMapper.update(brand);
        System.out.println(count);
        //提交事務
        sqlSession.commit();

        //5. 釋放資源
        sqlSession.close();

    }

單欄位修改

  1. 撰寫介面方法:Mapper介面(引數:所有資料 回傳型別:int(修改行)或void)
int update(Brand brand);
  1. 撰寫MYSQL陳述句:
    <update id="update">
        update tb_brand
        <set>
            <if test="brandName != null and brandName != ''">
                brand_name = #{brandName},
            </if>
            <if test="companyName != null and companyName != ''">
                company_name = #{companyName},
            </if>
            <if test="ordered != null">
                ordered = #{ordered},
            </if>
            <if test="description != null and description != ''">
                description = #{description},
            </if>
            <if test="status != null">
                status = #{status}
            </if>
        </set>
        where id = #{id};
    </update>
  1. 執行方法,測驗:
    @Test
    public void testUpdate() throws IOException {
        //接收引數
        int status = 0;
        String companyName = "波導手機";
        String brandName = "波導";
        String description = "波導手機,手機中的戰斗機";
        int ordered = 200;
        int id = 6;


        //封裝物件
        Brand brand = new Brand();
        brand.setStatus(status);
//        brand.setCompanyName(companyName);
//        brand.setBrandName(brandName);
//        brand.setDescription(description);
//        brand.setOrdered(ordered);
        brand.setId(id);

        //1. 獲取SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2. 獲取SqlSession物件
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //SqlSession sqlSession = sqlSessionFactory.openSession(true);

        //3. 獲取Mapper介面的代理物件
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);

        //4. 執行方法


        int count = brandMapper.update(brand);
        System.out.println(count);
        //提交事務
        sqlSession.commit();

        //5. 釋放資源
        sqlSession.close();

    }

洗掉功能

這節介紹洗掉功能的兩種形式:

  • 單個洗掉
  • 多個洗掉

我們將會在下面一一講解:

單個洗掉

  1. 撰寫介面方法:Mapper介面(引數:id 回傳型別:void)
void deleteById(int id);
  1. 撰寫MYSQL:
    <delete id="deleteById">
        delete from tb_brand where id = #{id};
    </delete>
  1. 代碼執行,測驗:
    @Test
    public void testDeleteById() throws IOException {
        //接收引數

        int id = 6;


        //1. 獲取SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2. 獲取SqlSession物件
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //SqlSession sqlSession = sqlSessionFactory.openSession(true);

        //3. 獲取Mapper介面的代理物件
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);

        //4. 執行方法

        brandMapper.deleteById(id);

        //提交事務
        sqlSession.commit();

        //5. 釋放資源
        sqlSession.close();

    }

多個洗掉

  1. 撰寫介面方法:Mapper介面(引數:int[] id 回傳型別:void)
void deleteByIds(int[] ids);
  1. 撰寫MYSQL代碼:
    <!--
        mybatis會將陣列引數,封裝為一個Map集合,
            * 默認:array = 陣列
            * 使用@Param注解改變map集合的默認key的名稱
    -->

    <!--
    foreach:類似于for強化陳述句
    collection:集合/陣列
    item:陣列中的單個值
    separator:分割符
    open:開始處添加符號
    close:結尾處添加符合
    -->

    <delete id="deleteByIds">
        delete from tb_brand where id
        in
        <!--這部分整合出來類似于:(id1,id2,id3....)-->
            <foreach collection="array" item="id" separator="," open="(" close=")">
                #{id}
            </foreach>
             ;
    </delete>
  1. 代碼運行,測驗:
    @Test
    public void testDeleteByIds() throws IOException {
        //接收引數

        int[] ids = {5,7,8};


        //1. 獲取SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2. 獲取SqlSession物件
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //SqlSession sqlSession = sqlSessionFactory.openSession(true);

        //3. 獲取Mapper介面的代理物件
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);

        //4. 執行方法

        brandMapper.deleteByIds(ids);

        //提交事務
        sqlSession.commit();

        //5. 釋放資源
        sqlSession.close();

    }

引數傳遞

MyBatis介面方法中可以接收各種各樣的引數,MyBatis底層對這些引數有著不同的封裝處理方法

我們在下面一一介紹:

多引數傳遞

在介面多引數傳遞中會把所有引數轉移為Map集合

會轉化為兩種Map集合:

  • arg集合,下標從0開始: arg[0]

  • Param集合,下標從1開始: Param[1]

我們在xml中也可以直接使用arg或param來直接表示(但不推薦,因為不能直觀得到資訊)

我們所推薦的做法是使用@Param注解:

  • 在引數前加上注解@Param("")

  • 注意:這里使用Param注解實際上是對Param集合的鍵進行修改,也就是說你的Param集合將不再能夠使用

單引數傳遞

常見的單引數傳遞分為六種:

  1. POJO型別: 直接使用, 屬性名 和 引數占位符 名稱一致即可

  2. Map集合: 直接使用,鍵名 和 引數占位符 名稱一致即可

  3. Collection: 封裝為Map集合

    • map.put("arg0",collection集合);
    • map.put("collection",collection集合);
  4. List: 封裝為Map集合

    • map.put("arg0",List集合);
    • map.put("collection",List集合);
    • map.put("list",List集合);
  5. Array:封裝為Map

    • map.put("arg0",陣列);
    • map.put("array",陣列);
  6. 其他型別:直接使用

注解開發

我們先來介紹注解開發的格式:

  • 注解開發寫在Mapper介面的方法中
// 相當于節省了xml的一步,直接在介面中定義方法

@Select("select * from tb_user where id = #{id}")
List<Brand> selectAll();

當然,注解的方法也分為四種:

  • @Select
  • @Insert
  • @Update
  • @Delete

注解的優缺點:

  • 優點 : 注解完成簡單功能,方便快捷
  • 缺點 : 注解會導致Java代碼繁瑣,在介面中書寫大量Java和MYSQL代碼導致可讀性變差

使用注解來映射簡單陳述句會使代碼顯得更加簡潔,但對于稍微復雜一點的陳述句,Java 注解不僅力不從心,還會讓你本就復雜的 SQL 陳述句更加混亂不堪, 因此,如果你需要做一些很復雜的操作,最好用 XML 來映射陳述句,

選擇何種方式來配置映射,以及認為是否應該要統一映射陳述句定義的形式,完全取決于你和你的團隊, 換句話說,永遠不要拘泥于一種方式,你可以很輕松的在基于注解和 XML 的陳述句映射方式間自由移植和切換,

結束語

好的,關于MyBatis的內容就介紹到這里,希望能為你帶來幫助!

附錄

該文章屬于學習內容,具體參考B站黑馬程式員陳老師的JavaWeb課程

這里附上鏈接:01-MyBatis簡介_嗶哩嗶哩_bilibili

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/506155.html

標籤:架構設計

上一篇:設計模式之(10)——橋接模式

下一篇:【設計模式】Java設計模式 - 裝飾者模式

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 面試突擊第一季,第二季,第三季

    第一季必考 https://www.bilibili.com/video/BV1FE411y79Y?from=search&seid=15921726601957489746 第二季分布式 https://www.bilibili.com/video/BV13f4y127ee/?spm_id_fro ......

    uj5u.com 2020-09-10 05:35:24 more
  • 第三單元作業總結

    1.前言 這應該是本學期最后一次寫作業總結了吧。總體來說,對作業的節奏也差不多掌握了,作業做起來的效率也更高了。雖然和之前的作業一樣,作業中都要用到新的知識,但是相比之前,更加懂得了如何利用工具以及資料。雖然之間卡過殼,但總體而言,這幾次作業還算完成的比較好。 2.作業程序總結 相比前兩個單元,此單 ......

    uj5u.com 2020-09-10 05:35:41 more
  • 北航OO(2020)第四單元博客作業暨課程總結博客

    北航OO(2020)第四單元博客作業暨課程總結博客 本單元作業的架構設計 在本單元中,由于UML圖具有比較清晰的樹形結構,因此我對其中需要進行查詢操作的元素進行了包裝,在樹的父節點中存盤所有孩子的參考。考慮到性能問題,我采用了快取機制,一次查詢后盡可能快取已經遍歷過的資訊,以減少遍歷次數。 本單元我 ......

    uj5u.com 2020-09-10 05:35:48 more
  • BUAA_OO_第四單元

    一、UML決議器設計 ? 先看下題目:第四單元實作一個基于JDK 8帶有效性檢查的UML(Unified Modeling Language)類圖,順序圖,狀態圖分析器 MyUmlInteraction,實際上我們要建立一個有向圖模型,UML中的物件(元素)可能與同級元素連接,也可與低級元素相連形成 ......

    uj5u.com 2020-09-10 05:35:54 more
  • 6.1邏輯運算子

    邏輯運算子 1. && 短路與 運算式1 && 運算式2 01.運算式1為true并且運算式2也為true 整體回傳為true 02.運算式1為false,將不會執行運算式2 整體回傳為false 03.只要有一個運算式為false 整體回傳為false 2. || 短路或 運算式1 || 運算式2 ......

    uj5u.com 2020-09-10 05:35:56 more
  • BUAAOO 第四單元 & 課程總結

    1. 第四單元:StarUml檔案決議 本單元采用了圖模型決議UML。 UML檔案可以抽象為圖、子圖、邊的邏輯結構。 在實作中,圖的節點包括類、介面、屬性,子圖包括狀態圖、順序圖等。 采用了三次遍歷UML元素的方法建圖,第一遍遍歷建點,第二、三次遍歷設定屬性、連邊,實作圖物件的初始化。這里借鑒了一些 ......

    uj5u.com 2020-09-10 05:36:06 more
  • 談談我對C# 多型的理解

    面向物件三要素:封裝、繼承、多型。 封裝和繼承,這兩個比較好理解,但要理解多型的話,可就稍微有點難度了。今天,我們就來講講多型的理解。 我們應該經常會看到面試題目:請談談對多型的理解。 其實呢,多型非常簡單,就一句話:呼叫同一種方法產生了不同的結果。 具體實作方式有三種。 一、多載 多載很簡單。 p ......

    uj5u.com 2020-09-10 05:36:09 more
  • Python 資料驅動工具:DDT

    背景 python 的unittest 沒有自帶資料驅動功能。 所以如果使用unittest,同時又想使用資料驅動,那么就可以使用DDT來完成。 DDT是 “Data-Driven Tests”的縮寫。 資料:http://ddt.readthedocs.io/en/latest/ 使用方法 dd. ......

    uj5u.com 2020-09-10 05:36:13 more
  • Python里面的xlrd模塊詳解

    那我就一下面積個問題對xlrd模塊進行學習一下: 1.什么是xlrd模塊? 2.為什么使用xlrd模塊? 3.怎樣使用xlrd模塊? 1.什么是xlrd模塊? ?python操作excel主要用到xlrd和xlwt這兩個庫,即xlrd是讀excel,xlwt是寫excel的庫。 今天就先來說一下xl ......

    uj5u.com 2020-09-10 05:36:28 more
  • 當我們創建HashMap時,底層到底做了什么?

    jdk1.7中的底層實作程序(底層基于陣列+鏈表) 在我們new HashMap()時,底層創建了默認長度為16的一維陣列Entry[ ] table。當我們呼叫map.put(key1,value1)方法向HashMap里添加資料的時候: 首先,呼叫key1所在類的hashCode()計算key1 ......

    uj5u.com 2020-09-10 05:36:38 more
最新发布
  • 【中介者設計模式詳解】C/Java/JS/Go/Python/TS不同語言實作

    * 中介者模式是一種行為型設計模式,它可以用來減少類之間的直接依賴關系,
    * 將物件之間的通信封裝到一個中介者物件中,從而使得各個物件之間的關系更加松散。
    * 在中介者模式中,物件之間不再直接相互互動,而是通過中介者來中轉訊息。 ......

    uj5u.com 2023-04-20 08:20:47 more
  • 露天煤礦現場調研和交流案例分享

    他們集團的資訊化公司及研究院在一個礦區正在做智能礦山的統一平臺的 試點,專案投資大概1億,包括了礦山的各方面的內容,顯示得我們這次交流有點多余。他們2年前開始做智能礦山的規劃,有很多煤礦行業專家的加持,他們的描述是非常完美,但是去年底應該上線的平臺,現在還沒有看到影子。他們確實有很多場景需求,但是被... ......

    uj5u.com 2023-04-20 08:20:25 more
  • 《社區人員管理》實戰案例設計&個人案例分享

    設計是一個讓人夢想成真程序,開始編碼、測驗、除錯之前進行需求分析和架構設計,才能保證關鍵方面都做正確 ......

    uj5u.com 2023-04-20 08:20:17 more
  • 軟體架構生態化-多角色交付的探索實踐

    作為一個技術架構師,不僅僅要緊跟行業技術趨勢,還要結合研發團隊現狀及痛點,探索新的交付方案。在日常中,你是否遇到如下問題 “ 業務需求排期長研發是瓶頸;非研發角色感受不到研發技改提效的變化;引入ISV 團隊又擔心質量和安全,培訓周期長“等等,基于此我們探索了一種新的技術體系及交付方案來解決如上問題。 ......

    uj5u.com 2023-04-20 08:20:10 more
  • 【中介者設計模式詳解】C/Java/JS/Go/Python/TS不同語言實作

    * 中介者模式是一種行為型設計模式,它可以用來減少類之間的直接依賴關系,
    * 將物件之間的通信封裝到一個中介者物件中,從而使得各個物件之間的關系更加松散。
    * 在中介者模式中,物件之間不再直接相互互動,而是通過中介者來中轉訊息。 ......

    uj5u.com 2023-04-20 08:19:44 more
  • 露天煤礦現場調研和交流案例分享

    他們集團的資訊化公司及研究院在一個礦區正在做智能礦山的統一平臺的 試點,專案投資大概1億,包括了礦山的各方面的內容,顯示得我們這次交流有點多余。他們2年前開始做智能礦山的規劃,有很多煤礦行業專家的加持,他們的描述是非常完美,但是去年底應該上線的平臺,現在還沒有看到影子。他們確實有很多場景需求,但是被... ......

    uj5u.com 2023-04-20 08:19:07 more
  • 《社區人員管理》實戰案例設計&個人案例分享

    設計是一個讓人夢想成真程序,開始編碼、測驗、除錯之前進行需求分析和架構設計,才能保證關鍵方面都做正確 ......

    uj5u.com 2023-04-20 08:18:57 more
  • 軟體架構生態化-多角色交付的探索實踐

    作為一個技術架構師,不僅僅要緊跟行業技術趨勢,還要結合研發團隊現狀及痛點,探索新的交付方案。在日常中,你是否遇到如下問題 “ 業務需求排期長研發是瓶頸;非研發角色感受不到研發技改提效的變化;引入ISV 團隊又擔心質量和安全,培訓周期長“等等,基于此我們探索了一種新的技術體系及交付方案來解決如上問題。 ......

    uj5u.com 2023-04-20 08:18:49 more
  • 05單件模式

    #經典的單件模式 public class Singleton { private static Singleton uniqueInstance; //一個靜態變數持有Singleton類的唯一實體。 // 其他有用的實體變數寫在這里 //構造器宣告為私有,只有Singleton可以實體化這個類! ......

    uj5u.com 2023-04-19 08:42:51 more
  • 【架構與設計】常見微服務分層架構的區別和落地實踐

    軟體工程的方方面面都遵循一個最基本的道理:沒有銀彈,架構分層模型更是如此,每一種都有各自優缺點,所以請根據不同的業務場景,并遵循簡單、可演進這兩個重要的架構原則選擇合適的架構分層模型即可。 ......

    uj5u.com 2023-04-19 08:42:41 more