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

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

2022-05-10 06:17:27 後端開發

講義:


  • 動態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="https://www.cnblogs.com/Burning-youth/p/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="https://www.cnblogs.com/Burning-youth/p/${jdbc.driverClassName}"/>
                <property name="url" value="https://www.cnblogs.com/Burning-youth/p/${jdbc.url}"/>
                <property name="username" value="https://www.cnblogs.com/Burning-youth/p/${jdbc.username}"/>
                <property name="password" value="https://www.cnblogs.com/Burning-youth/p/${jdbc.password}"/>
            </dataSource>
        </environment>
    </environments>

    <!--注冊mapper.xml檔案-->
    <mappers>
        <!--優化mapper.xml檔案注冊-->

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

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

        <!--單個注冊-->
        <!--<mapper ></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));
    }
}

 

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

標籤:Java

上一篇:Day15

下一篇:PyScript:讓Python在HTML中運行

標籤雲
其他(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)

熱門瀏覽
  • 【C++】Microsoft C++、C 和匯編程式檔案

    ......

    uj5u.com 2020-09-10 00:57:23 more
  • 例外宣告

    相比于斷言適用于排除邏輯上不可能存在的狀態,例外通常是用于邏輯上可能發生的錯誤。 例外宣告 Item 1:當函式不可能拋出例外或不能接受拋出例外時,使用noexcept 理由 如果不打算拋出例外的話,程式就會認為無法處理這種錯誤,并且應當盡早終止,如此可以有效地阻止例外的傳播與擴散。 示例 //不可 ......

    uj5u.com 2020-09-10 00:57:27 more
  • Codeforces 1400E Clear the Multiset(貪心 + 分治)

    鏈接:https://codeforces.com/problemset/problem/1400/E 來源:Codeforces 思路:給你一個陣列,現在你可以進行兩種操作,操作1:將一段沒有 0 的區間進行減一的操作,操作2:將 i 位置上的元素歸零。最終問:將這個陣列的全部元素歸零后操作的最少 ......

    uj5u.com 2020-09-10 00:57:30 more
  • UVA11610 【Reverse Prime】

    本人看到此題沒有翻譯,就附帶了一個自己的翻譯版本 思考 這一題,它的第一個要求是找出所有 $7$ 位反向質數及其質因數的個數。 我們應該需要質數篩篩選1~$10^{7}$的所有數,這里就不慢慢介紹了。但是,重讀題,我們突然發現反向質數都是 $7$ 位,而將它反過來后的數字卻是 $6$ 位數,這就說明 ......

    uj5u.com 2020-09-10 00:57:36 more
  • 統計區間素數數量

    1 #pragma GCC optimize(2) 2 #include <bits/stdc++.h> 3 using namespace std; 4 bool isprime[1000000010]; 5 vector<int> prime; 6 inline int getlist(int ......

    uj5u.com 2020-09-10 00:57:47 more
  • C/C++編程筆記:C++中的 const 變數詳解,教你正確認識const用法

    1、C中的const 1、區域const變數存放在堆疊區中,會分配記憶體(也就是說可以通過地址間接修改變數的值)。測驗代碼如下: 運行結果: 2、全域const變數存放在只讀資料段(不能通過地址修改,會發生寫入錯誤), 默認為外部聯編,可以給其他源檔案使用(需要用extern關鍵字修飾) 運行結果: ......

    uj5u.com 2020-09-10 00:58:04 more
  • 【C++犯錯記錄】VS2019 MFC添加資源不懂如何修改資源宏ID

    1. 首先在資源視圖中,添加資源 2. 點擊新添加的資源,復制自動生成的ID 3. 在解決方案資源管理器中找到Resource.h檔案,編輯,使用整個專案搜索和替換的方式快速替換 宏宣告 4. Ctrl+Shift+F 全域搜索,點擊查找全部,然后逐個替換 5. 為什么使用搜索替換而不使用屬性視窗直 ......

    uj5u.com 2020-09-10 00:59:11 more
  • 【C++犯錯記錄】VS2019 MFC不懂的批量添加資源

    1. 打開資源頭檔案Resource.h,在其中預先定義好宏 ID(不清楚其實ID值應該設定多少,可以先新建一個相同的資源項,再在這個資源的ID值的基礎上遞增即可) 2. 在資源視圖中選中專案資源,按F7編輯資源檔案,按 ID 型別 相對路徑的形式添加 資源。(別忘了先把檔案拷貝到專案中的res檔案 ......

    uj5u.com 2020-09-10 01:00:19 more
  • C/C++編程筆記:關于C++的參考型別,專供新手入門使用

    今天要講的是C++中我最喜歡的一個用法——參考,也叫別名。 參考就是給一個變數名取一個變數名,方便我們間接地使用這個變數。我們可以給一個變數創建N個參考,這N + 1個變數共享了同一塊記憶體區域。(參考型別的變數會占用記憶體空間,占用的記憶體空間的大小和指標型別的大小是相同的。雖然參考是一個物件的別名,但 ......

    uj5u.com 2020-09-10 01:00:22 more
  • 【C/C++編程筆記】從頭開始學習C ++:初學者完整指南

    眾所周知,C ++的學習曲線陡峭,但是花時間學習這種語言將為您的職業帶來奇跡,并使您與其他開發人員區分開。您會更輕松地學習新語言,形成真正的解決問題的技能,并在編程的基礎上打下堅實的基礎。 C ++將幫助您養成良好的編程習慣(即清晰一致的編碼風格,在撰寫代碼時注釋代碼,并限制類內部的可見性),并且由 ......

    uj5u.com 2020-09-10 01:00:41 more
最新发布
  • Rust中的智能指標:Box<T> Rc<T> Arc<T> Cell<T> RefCell<T> Weak

    Rust中的智能指標是什么 智能指標(smart pointers)是一類資料結構,是擁有資料所有權和額外功能的指標。是指標的進一步發展 指標(pointer)是一個包含記憶體地址的變數的通用概念。這個地址參考,或 ” 指向”(points at)一些其 他資料 。參考以 & 符號為標志并借用了他們所 ......

    uj5u.com 2023-04-20 07:24:10 more
  • Java的值傳遞和參考傳遞

    值傳遞不會改變本身,參考傳遞(如果傳遞的值需要實體化到堆里)如果發生修改了會改變本身。 1.基本資料型別都是值傳遞 package com.example.basic; public class Test { public static void main(String[] args) { int ......

    uj5u.com 2023-04-20 07:24:04 more
  • [2]SpinalHDL教程——Scala簡單入門

    第一個 Scala 程式 shell里面輸入 $ scala scala> 1 + 1 res0: Int = 2 scala> println("Hello World!") Hello World! 檔案形式 object HelloWorld { /* 這是我的第一個 Scala 程式 * 以 ......

    uj5u.com 2023-04-20 07:23:58 more
  • 理解函式指標和回呼函式

    理解 函式指標 指向函式的指標。比如: 理解函式指標的偽代碼 void (*p)(int type, char *data); // 定義一個函式指標p void func(int type, char *data); // 宣告一個函式func p = func; // 將指標p指向函式func ......

    uj5u.com 2023-04-20 07:23:52 more
  • Django筆記二十五之資料庫函式之日期函式

    本文首發于公眾號:Hunter后端 原文鏈接:Django筆記二十五之資料庫函式之日期函式 日期函式主要介紹兩個大類,Extract() 和 Trunc() Extract() 函式作用是提取日期,比如我們可以提取一個日期欄位的年份,月份,日等資料 Trunc() 的作用則是截取,比如 2022-0 ......

    uj5u.com 2023-04-20 07:23:45 more
  • 一天吃透JVM面試八股文

    什么是JVM? JVM,全稱Java Virtual Machine(Java虛擬機),是通過在實際的計算機上仿真模擬各種計算機功能來實作的。由一套位元組碼指令集、一組暫存器、一個堆疊、一個垃圾回收堆和一個存盤方法域等組成。JVM屏蔽了與作業系統平臺相關的資訊,使得Java程式只需要生成在Java虛擬機 ......

    uj5u.com 2023-04-20 07:23:31 more
  • 使用Java接入小程式訂閱訊息!

    更新完微信服務號的模板訊息之后,我又趕緊把微信小程式的訂閱訊息給實作了!之前我一直以為微信小程式也是要企業才能申請,沒想到小程式個人就能申請。 訊息推送平臺🔥推送下發【郵件】【短信】【微信服務號】【微信小程式】【企業微信】【釘釘】等訊息型別。 https://gitee.com/zhongfuch ......

    uj5u.com 2023-04-20 07:22:59 more
  • java -- 緩沖流、轉換流、序列化流

    緩沖流 緩沖流, 也叫高效流, 按照資料型別分類: 位元組緩沖流:BufferedInputStream,BufferedOutputStream 字符緩沖流:BufferedReader,BufferedWriter 緩沖流的基本原理,是在創建流物件時,會創建一個內置的默認大小的緩沖區陣列,通過緩沖 ......

    uj5u.com 2023-04-20 07:22:49 more
  • Java-SpringBoot-Range請求頭設定實作視頻分段傳輸

    老實說,人太懶了,現在基本都不喜歡寫筆記了,但是網上有關Range請求頭的文章都太水了 下面是抄的一段StackOverflow的代碼...自己大修改過的,寫的注釋挺全的,應該直接看得懂,就不解釋了 寫的不好...只是希望能給視頻網站開發的新手一點點幫助吧. 業務場景:視頻分段傳輸、視頻多段傳輸(理 ......

    uj5u.com 2023-04-20 07:22:42 more
  • Windows 10開發教程_編程入門自學教程_菜鳥教程-免費教程分享

    教程簡介 Windows 10開發入門教程 - 從簡單的步驟了解Windows 10開發,從基本到高級概念,包括簡介,UWP,第一個應用程式,商店,XAML控制元件,資料系結,XAML性能,自適應設計,自適應UI,自適應代碼,檔案管理,SQLite資料庫,應用程式到應用程式通信,應用程式本地化,應用程式 ......

    uj5u.com 2023-04-20 07:22:35 more