【SSM框架】MyBatis筆記 --- 動態sql講義+實戰;map在動態sql中的使用;列名與類中成員變數名不同的兩種解決方案

来源:https://www.cnblogs.com/Burning-youth/archive/2022/05/10/16251902.html
-Advertisement-
Play Games

講義: 動態sql可以定義代碼片斷,可以進行邏輯判斷,可以進行迴圈處理(批量處理),使條件判斷更為簡單。 一、動態sql核心標簽: 1、<sql>:當多種類型的查詢語句的查詢欄位或者查詢條件相同時,可以將其定義為常量,方便調用。 2、<include>:用來引用<sql>定義的代碼片斷。 <!--定 ...


講義:


  • 動態sql可以定義代碼片斷,可以進行邏輯判斷,可以進行迴圈處理(批量處理),使條件判斷更為簡單。

一、動態sql核心標簽:

1、<sql>:當多種類型的查詢語句的查詢欄位或者查詢條件相同時,可以將其定義為常量,方便調用。

 

2、<include>:用來引用<sql>定義的代碼片斷。
   

<!--定義代碼片斷-->
<sql id="allColumns">
    id,username,birthday,sex,address
</sql>
<!--引用定義好的代碼片斷-->
<select id="getAll" resultType="users" >
    select <include refid="allColumns"></include>
    from users
</select>


3、<if>:進行條件判斷。

test 屬性:if 執行條件(條件判斷的取值可以是實體類的成員變數,可以是map的key,可以是@Param註解的名稱)。

 

4、<where>:

特性:標簽可以自動的將第一個條件前面的邏輯運算符 (or ,and) 去掉,比如 id 查詢條件前面是有“and”關鍵字的,但是在列印出來的 SQL 中卻沒有。

<select id="getByCondition" parameterType="users" resultType="users">
    select <include refid="allColumns"></include>
    from users
    <where>
        <if test="userName != null and userName != ''">
            and username like concat('%',#{userName},'%')
        </if>
        <if test="birthday != null">
            and birthday = #{birthday}
        </if>
        <if test="sex != null and sex != ''">
            and sex = #{sex}
        </if>
        <if test="address != null and address != ''">
            and address like concat('%',#{address},'%')
        </if>
    </where>
</select>


5、<set>:使用見下麵的慄子。切記,至少更新一列(負責拋出異常)。

需求:使用 if+set 標簽進行update操作時,哪個欄位中有值才去更新,如果某項為 null 則不進行更新,而是保持資料庫原值。
  

慄子:

<update id="updateBySet" parameterType="users">
    update users
    <set>
        <if test="userName != null and userName != ''">
            username = #{userName},
        </if>
        <if test="birthday != null">
            birthday = #{birthday},
        </if>
        <if test="sex != null and sex != ''">
            sex = #{sex},
        </if>
        <if test="address != null and address != ''">
            address =#{address} ,
        </if>
    </set>
    where id = #{id}
</update>


6、<foreach>:用來進行迴圈遍歷,完成迴圈條件查詢,批量刪除,批量增加,批量更新。

1)collection 屬性:用來指定入參的類型,如果是List集合,則為list,如果是Map集合,則為map,如果是數組,則為array。

2)item 屬性 :迴圈體中的具體對象。支持屬性的點路徑訪問,如 item.age,item.info.details;在list和數組中是其中的對象,在map中是value。

3)index 屬性 :在list和數組中,index是元素的序號,在map中,index是元素的key,該參數可不寫。

4)separator 屬性:多個值或對象或語句之間的分隔符。

5)open 屬性 :表示該語句以什麼開始。

6)close 屬性 :表示該語句以什麼結束。

註意:要使用批量更新,必須在jdbc.properties屬性文件中的url中添加&allowMultiQueries=true,才允許多行操作。

   
二、通過指定下標來進行傳參:

可以不使用對象的屬性名進行參數值綁定,使用下標值。 mybatis-3.3 版本和之前的版本使用#{0},#{1}方式, 從 mybatis3.4 開始使用#{arg0},#{arg1}的方式。

 

三、map在動態sql中的使用:

  • 如果入參超過一個以上,使用map封裝查詢條件,更有語義,查詢條件更明確。

1、入參是map:

因為當傳遞的數據有多個,不適合使用指定下標或指定名稱的方式來進行傳參,又加上參數不一定與對象的成員變數一致,考慮使用map集合來進行傳遞,map使用的是鍵值對的方式.當在sql語句中使用的時候#{鍵名},${鍵名},{ }的是鍵的名稱。

 

2、返回值是map:

返回值是map的適用場景,如果的數據不能使用對象來進行封裝,可能查詢的數據來自多張表中的某些列,這種情況下,使用map,但是map的返回方式破壞了對象的封裝,返回來的數據是一個一個單獨的數據, 彼此之間不相關,map使用表中的列名或別名作為鍵名(key)進行返回數據。

 

四、列名與類中成員變數名稱不一致:

解決方案一:

使用列的別名,別名與類中的成員變數名一樣,即可完成註入。

 

解決方案二:

使用<resultMap>標簽進行映射。

property 屬性:為成員變數名

column 屬性:為列的別名

 

一堆慄子:


一、module 目錄結構:

 

二、pom.xml:

<?xml version="1.0" encoding="UTF-8"?>

<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.0</modelVersion>

  <groupId>org.burning</groupId>
  <artifactId>mybatis_003_dynamicsql</artifactId>
  <version>1.0</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <!--添加mybatis依賴-->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.5.9</version>
    </dependency>

    <!--添加mysql依賴-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.27</version>
    </dependency>
      <dependency>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis</artifactId>
          <version>3.5.9</version>
          <scope>test</scope>
      </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.5.9</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.5.9</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>

  <build>
    <!--指定資源文件位置-->
    <resources>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.xml</include>
          <include>**/*.properties</include>
        </includes>
      </resource>

      <resource>
        <directory>src/main/resources</directory>
        <includes>
          <include>**/*.xml</include>
          <include>**/*.properties</include>
        </includes>
      </resource>
    </resources>
  </build>
</project>

 

三、jdbc.properties:

jdbc.driverClassName=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=888

 

四、SqlMapConfig.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>
    <!--讀取jdbc.properties屬性-->
    <properties resource="jdbc.properties"></properties>

    <!--設置日誌輸出-->
    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>

    <!--註冊實體類別名-->
    <typeAliases>
        <package name="org.burning.entity"/>
    </typeAliases>
    
    <!--配置環境變數-->
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driverClassName}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>
    </environments>

    <!--註冊mapper.xml文件-->
    <mappers>
        <!--優化mapper.xml文件註冊-->

        <!--絕對路徑註冊-->
        <!--<mapper url="/////"></mapper>-->

        <!--非動態代理方式下的註冊-->
        <!--<mapper resource="StudentMapper.xml"></mapper>-->

        <!--單個註冊-->
        <!--<mapper class="org.burning.mapper.UsersMapper"></mapper>-->

        <!--批量註冊-->
        <package name="org.burning.mapper"/>
   </mappers>
</configuration>

 

五、建表語句:

CREATE TABLE `student` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
  `email` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
  `age` int DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=24 DEFAULT CHARSET=utf8mb3 COLLATE=utf8_bin

CREATE TABLE `books` (
  `book_id` int NOT NULL AUTO_INCREMENT,
  `book_name` varchar(45) COLLATE utf8_bin NOT NULL,
  PRIMARY KEY (`book_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb3 COLLATE=utf8_bin

 

六、User.java:

package org.burning.entity;

import java.util.Date;

public class User {
    private Integer id;
    private String userName;
    private Date birthday;
    private String sex;
    private String address;

    public User() {
    }

    public User(Integer id, String userName, Date birthday, String sex, String address) {
        this.id = id;
        this.userName = userName;
        this.birthday = birthday;
        this.sex = sex;
        this.address = address;
    }

    public User(String userName, Date birthday, String sex, String address) {
        this.userName = userName;
        this.birthday = birthday;
        this.sex = sex;
        this.address = address;
    }

    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 Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", userName='" + userName + '\'' +
                ", birthday=" + birthday +
                ", sex='" + sex + '\'' +
                ", address='" + address + '\'' +
                '}';
    }
}

 

七、Book.java:

package org.burning.entity;

public class Book {
    private Integer id;
    private String name;

    public Book() {
    }

    public Book(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

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

    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

 

八、UsersMapper.java:

package org.burning.mapper;

import org.apache.ibatis.annotations.Param;
import org.burning.entity.User;

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

/**
 * 數據訪問層的介面,規定的資料庫中可進行的各種操作
 */
public interface UsersMapper {
    //查詢用戶全部信息
    List<User> getAll();

    //按指定的條件進行多條件查詢
    List<User> selectByCondition(User user);

    //有選擇的更新
    int updateBySet(User user);

    //查詢多個指定id的用戶信息
    List<User> selectByIds(Integer[] arr);

    //批量刪除
    int deleteBatch(Integer[] arr);

    //批量增加
    int insertBatch(List<User> users);

    //查詢生日在兩個日期間的所有學生信息
    List<User> selectByTwoBirthday(Date begin,Date end);

    //入參是map
    List<User> selectByMap(Map map);

    //返回值是一行的map
    Map returnMap(Integer id);

    //返回多行的map
    List<Map> returnMaps();
}

 

九、UsersMapper.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">
<mapper namespace="org.burning.mapper.UsersMapper">
    <!--定義代碼片段-->
    <sql id="allcolumns">
        id,username,birthday,sex,address
    </sql>

    <!--查詢users中所有的學生信息-->
    <select id="getAll" resultType="user">
        select <include refid="allcolumns"></include>
        from users
    </select>

    <!--動態sql實現:
        根據多個欄位進行查詢操作(可以通過判斷user對象的實例變數是否“有意義”而進行sql的拼接)
        【無意義值指的是:比如userName是字元串類型,而它為null或者為空字元串,那它就是無意義的】
    -->
    <select id="selectByCondition" parameterType="User" resultType="User">
        select <include refid="allcolumns"></include>
        from users
        <where>
            <if test="userName != null and userName != ''">
                and username like concat('%',#{userName},'%')
            </if>
            <if test="birthday != null">
                and birthday = #{birthday}
            </if>
            <if test="sex != null and sex != ''">
                and sex = #{sex}
            </if>
            <if test="address != null and address != ''">
                and address like concat('%',#{address},'%')
            </if>
        </where>
    </select>

    <!--通過動態sql實現:
        根據入參user對象的實例變數是否有“意義”,而進行相應的更新處理
    -->
    <update id="updateBySet" parameterType="user">
        update users
        <set>
            <if test="userName != null and userName != ''">
                userName = #{userName},
            </if>
            <if test="birthday != null">
                birthday = #{birthday},
            </if>
            <if test="sex != null and sex != ''">
                sex = #{sex},
            </if>
            <if test="address != null and address != ''">
                address = #{address},
            </if>
        </set>
        where id=#{id}
    </update>

    <!--通過動態sql實現:(當入參是一個以上的時候,不需要寫parameterType)
        根據入參的id數組,來進行相應id多條信息查詢
    -->
    <select id="selectByIds" resultType="user">
        select <include refid="allcolumns"></include>
        from users
        where id in
            <foreach collection="array" item="id" separator="," open="(" close=")">
                #{id}
            </foreach>
    </select>

    <!--通過動態sql實現:
        批量刪除
    -->
    <delete id="deleteBatch">
        delete from users
        where id in
            <foreach collection="array" item="id" separator="," open="(" close=")">
                #{id}
            </foreach>
    </delete>

    <!--通過動態sql實現:
        批量增加
    -->
    <insert id="insertBatch">
        insert into users (username,birthday,sex,address)
        values
            <foreach collection="list" item="u" separator=",">
                (#{u.userName},#{u.birthday},#{u.sex},#{u.address})
            </foreach>
    </insert>

    <!--通過指定參數位置,來獲取入參值的慄子:
    -->
    <select id="selectByTwoBirthday" resultType="user">
        select <include refid="allcolumns"></include>
        from users
        where birthday between #{arg0} and #{arg1}
    </select>

    <!--通過入參為Map類型,來進行多個數據的傳遞
    -->
    <select id="selectByMap" resultType="user">
        select <include refid="allcolumns"></include>
        from users
        where birthday between #{birthdayBegin} and #{birthdayEnd}
    </select>

    <!--將查出來的數據封進Map里,欄位名(可以使用別名)就是key,列值就是value
    -->
    <select id="returnMap" parameterType="int" resultType="map">
        select id,username as name,address
        from users
        where id=#{id}
    </select>

    <!--將查出來的數據,封進Map里,同為一行的數據為一個Map
        最後返回一個裝著很多Map的List集合
    -->
    <select id="returnMaps" resultType="map">
        select username as name,address
        from users
    </select>
</mapper>

 

十、UserTest.java:

package org.burning;

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 org.burning.entity.Book;
import org.burning.entity.User;
import org.burning.mapper.UsersMapper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class UsersTest {
    SqlSession sqlSession;

    //動態代理對象
    UsersMapper usersMapper;

    //日期的格式化刷子
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    @Before
    public void openSqlSession() throws IOException {
        //讀取核心配置文件
        InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");

        //創建工廠對象
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);

        //取出sqlSession
        sqlSession = factory.openSession();

        //取出動態代理對象,完成介面方法的調用,實則是調用xml文件中相應的標簽的功能
        usersMapper = sqlSession.getMapper(UsersMapper.class);
    }

    @After
    public void closeSqlsession() {
        sqlSession.close();
    }

    @Test
    public void testGetAll() {
        List<User> users = usersMapper.getAll();

        users.forEach(user -> System.out.println(user));
    }

    @Test
    public void testSelectByCondition() throws ParseException {
        User u = new User();
        u.setSex("1");
        u.setUserName("小");
        u.setAddress("河");
        u.setBirthday(sdf.parse("1999-02-22"));
        List<User> users = usersMapper.selectByCondition(u);
        users.forEach(user -> System.out.println(user));
    }

    @Test
    public void testUpdateBySet() throws ParseException {
        User u = new User();
        u.setId(3);
        u.setUserName("小明的新名字");
        u.setBirthday(sdf.parse("1999-02-22"));
        int num = usersMapper.updateBySet(u);
        System.out.println(num);
        sqlSession.commit();
    }

    @Test
    public void testSelectByIds() {
        Integer[] array = {1,4,5};
        List<User> users = usersMapper.selectByIds(array);
        users.forEach(user -> System.out.println(user));
    }

    @Test
    public void testDeleteBatch() {
        Integer[] array = {11,12,13};
        int num = usersMapper.deleteBatch(array);
        System.out.println(num);
        sqlSession.commit();
    }

    @Test
    public void testBatch() throws ParseException {
        User user1 = new User("王1",sdf.parse("2020-01-01"),"2","大錘島分島A");
        User user2 = new User("王2",sdf.parse("2020-01-02"),"2","大錘島分島B");
        User user3 = new User("王3",sdf.parse("2020-01-03"),"2","大錘島分島C");
        List<User> users = new ArrayList<>();
        users.add(user1);
        users.add(user2);
        users.add(user3);
        int num = usersMapper.insertBatch(users);
        System.out.println(num);
        sqlSession.commit();
    }

    @Test
    public void testSelectByTwoBirthday() throws ParseException {
        List<User> users = usersMapper.selectByTwoBirthday(
                sdf.parse("1900-12-12"),
                sdf.parse("3000-01-01")
        );
        users.forEach(user -> System.out.println(user));
    }

    @Test
    public void testSelectByMap() throws ParseException {
        Map userMap = new HashMap();
        Date begin = sdf.parse("1900-12-12");
        Date end = sdf.parse("3000-01-01");
        userMap.put("birthdayBegin",begin);
        userMap.put("birthdayEnd",end);
        List<User> users = usersMapper.selectByMap(userMap);
        users.forEach(user -> System.out.println(user));
    }

    @Test
    public void testReturnMap() {
        Map map = usersMapper.returnMap(1);
        System.out.println(map);
    }

    @Test
    public void testReturnMaps() {
        List<Map> mapList = usersMapper.returnMaps();
        mapList.forEach(map -> System.out.println(map));
    }

}

 

十一、BooksMapper.java:

package org.burning.mapper;

import org.burning.entity.Book;

import java.util.List;

public interface BooksMapper {
    //查詢全部圖書(別名)
    List<Book> selectBooks();

    //查詢全部圖書(resultMap)
    List<Book> selectBooksPro();
}

 

十二、BooksMapper.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">
<mapper namespace="org.burning.mapper.BooksMapper">
    <!--使用resultMap手工完成映射-->
    <resultMap id="bookMap" type="book">
        <!--主鍵綁定-->
        <id property="id" column="book_id"></id>
        <!--非主鍵綁定-->
        <result property="name" column="book_name"></result>
    </resultMap>

    <!--通過起別名的方案解決欄位名和成員變數名不一致的問題-->
    <select id="selectBooks" resultType="book">
        select book_id id,book_name name
        from books
    </select>

    <!--通過resultMap方案解決欄位名和成員變數名不一致的問題-->
    <select id="selectBooksPro" resultMap="bookMap">
        select book_id,book_name
        from books
    </select>
</mapper>

 

十三、BookTest.java:

package org.burning;

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 org.burning.entity.Book;
import org.burning.mapper.BooksMapper;
import org.burning.mapper.UsersMapper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.security.spec.PSSParameterSpec;
import java.sql.PreparedStatement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class BooksTest {
    SqlSession sqlSession;

    //動態代理對象
    BooksMapper booksMapper;

    //日期的格式化刷子
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    @Before
    public void openSqlSession() throws IOException {
        //讀取核心配置文件
        InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");

        //創建工廠對象
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);

        //取出sqlSession
        sqlSession = factory.openSession();

        //取出動態代理對象,完成介面方法的調用,實則是調用xml文件中相應的標簽的功能
        booksMapper = sqlSession.getMapper(BooksMapper.class);
    }

    @After
    public void closeSqlsession() {
        sqlSession.close();
    }

    @Test
    public void testSelectBooks(){
        List<Book> bookList = booksMapper.selectBooks();
        bookList.forEach(book -> System.out.println(book));
    }

    @Test
    public void testSelectBooksPro(){
        List<Book> bookList = booksMapper.selectBooksPro();
        bookList.forEach(book -> System.out.println(book));
    }
}

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 只有在ajax才能找回一點點主場了,vue中的ajax一天整完,內容還行,主要是對axios的運用。 明天按理說要開始vuex了,這個從來都是只耳聞沒有眼見過,明天來看看看看是個什麼神奇的東西。 一. 解決開發環境ajax跨域問題 1.配置代理(方法一) 首先我們能發起ajax請求的,xhr原生不推 ...
  • 大家好,我是半夏👴,一個剛剛開始寫文的沙雕程式員.如果喜歡我的文章,可以關註➕ 點贊 👍 加我微信:frontendpicker,一起學習交流前端,成為更優秀的工程師~關註公眾號:搞前端的半夏,瞭解更多前端知識! 點我探索新世界! 原文鏈接 ==>http://sylblog.xin/archi ...
  • 詳解JavaScript中的事件迴圈機制!!! 一、簡單講解 這個大家應該或多或少都知道的 for (var i = 0; i < 10; i++) { setTimeout(() => { console.log(i); // 輸出10個10 }); } 解析:先執行for迴圈,迴圈疊加i,然後再 ...
  • 前端周刊:2022-6 期 前端開發 videojs-plugin-marker--videojs 進度條打點插件 基於 videojs-marker-plugin 項目二次開發,支持 marker 點更新和 marker 點擊事件;更換構建工具為 Vite。 @cutting-mat/direct ...
  • ####一、前言 #####因為最近練習Vue3項目的時候,發現Chrome瀏覽器的devtools插件不起作用了,這才想起當前安裝的devtools是5版本的,而Vue3項目需要6版本才支持。 ####二、安裝 #####1. 在Github上搜索devtools項目,直達車 #####2. 找到 ...
  • 認識jsJS是寄生語言,寄生在HTML中,與JAVA沒有關係JS可以寫在html文件中,以<script></script>標簽對出現在一些瀏覽器中,使用控制台可以監控到JS的工作<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...
  • 1.概念 1.1.知道的越少越好 迪米特法則,結合其含義又稱之為“最少知道原則”,即一個類作為一個調用方,應當對自己依賴的類(被調用的類)其中所處理的邏輯細節,知道的越少越好。對於被依賴的類(被調用的類)不管在使用上多麼的複雜,它都應儘量將處理邏輯封裝在它的內部,對調用方提供簡潔明瞭的公共方法即可, ...
  • 大家好,我是DD,已經是封閉在家的第51天了! 最近一直在更新Java新特性和IDEA Tips兩個專欄,其他方向內容的動態關註少了。昨天天晚上刷推的時候,瞄到了這個神奇的東西,覺得挺cool的,拿出來分享下: 相信你看到圖,不用我說,你也猜到是啥了吧?html里可以跑python代碼了! 看到好多 ...
一周排行
    -Advertisement-
    Play Games
  • Dapr Outbox 是1.12中的功能。 本文只介紹Dapr Outbox 執行流程,Dapr Outbox基本用法請閱讀官方文檔 。本文中appID=order-processor,topic=orders 本文前提知識:熟悉Dapr狀態管理、Dapr發佈訂閱和Outbox 模式。 Outbo ...
  • 引言 在前幾章我們深度講解了單元測試和集成測試的基礎知識,這一章我們來講解一下代碼覆蓋率,代碼覆蓋率是單元測試運行的度量值,覆蓋率通常以百分比表示,用於衡量代碼被測試覆蓋的程度,幫助開發人員評估測試用例的質量和代碼的健壯性。常見的覆蓋率包括語句覆蓋率(Line Coverage)、分支覆蓋率(Bra ...
  • 前言 本文介紹瞭如何使用S7.NET庫實現對西門子PLC DB塊數據的讀寫,記錄了使用電腦模擬,模擬PLC,自至完成測試的詳細流程,並重點介紹了在這個過程中的易錯點,供參考。 用到的軟體: 1.Windows環境下鏈路層網路訪問的行業標準工具(WinPcap_4_1_3.exe)下載鏈接:http ...
  • 從依賴倒置原則(Dependency Inversion Principle, DIP)到控制反轉(Inversion of Control, IoC)再到依賴註入(Dependency Injection, DI)的演進過程,我們可以理解為一種逐步抽象和解耦的設計思想。這種思想在C#等面向對象的編 ...
  • 關於Python中的私有屬性和私有方法 Python對於類的成員沒有嚴格的訪問控制限制,這與其他面相對對象語言有區別。關於私有屬性和私有方法,有如下要點: 1、通常我們約定,兩個下劃線開頭的屬性是私有的(private)。其他為公共的(public); 2、類內部可以訪問私有屬性(方法); 3、類外 ...
  • C++ 訪問說明符 訪問說明符是 C++ 中控制類成員(屬性和方法)可訪問性的關鍵字。它們用於封裝類數據並保護其免受意外修改或濫用。 三種訪問說明符: public:允許從類外部的任何地方訪問成員。 private:僅允許在類內部訪問成員。 protected:允許在類內部及其派生類中訪問成員。 示 ...
  • 寫這個隨筆說一下C++的static_cast和dynamic_cast用在子類與父類的指針轉換時的一些事宜。首先,【static_cast,dynamic_cast】【父類指針,子類指針】,兩兩一組,共有4種組合:用 static_cast 父類轉子類、用 static_cast 子類轉父類、使用 ...
  • /******************************************************************************************************** * * * 設計雙向鏈表的介面 * * * * Copyright (c) 2023-2 ...
  • 相信接觸過spring做開發的小伙伴們一定使用過@ComponentScan註解 @ComponentScan("com.wangm.lifecycle") public class AppConfig { } @ComponentScan指定basePackage,將包下的類按照一定規則註冊成Be ...
  • 操作系統 :CentOS 7.6_x64 opensips版本: 2.4.9 python版本:2.7.5 python作為腳本語言,使用起來很方便,查了下opensips的文檔,支持使用python腳本寫邏輯代碼。今天整理下CentOS7環境下opensips2.4.9的python模塊筆記及使用 ...