1. 程式人生 > 實用技巧 >Collection 集合內部原理

Collection 集合內部原理

1、程式碼結構

controller層

package com.mashibing.springboot04.controller;


import com.mashibing.springboot04.mapper.Account;
import com.mashibing.springboot04.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import java.util.List; @Controller public class MainController { @Autowired AccountService accountService; @RequestMapping("/list") @ResponseBody public Object list(){ List<Account> account = accountService.findAll();
return account; } }

Service層

package com.mashibing.springboot04.service;

import com.mashibing.springboot04.mapper.Account;
import com.mashibing.springboot04.mapper.AccountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import
java.util.List; @Service public class AccountService { @Autowired AccountMapper mapper; public List<Account> findAll(){ List<Account> all = mapper.findAll(); return all; } }

實體類層

package com.mashibing.springboot04.mapper;

public class Account {
    private int id;
    private String loginName;
    private String password;
    private String nickName;
    private int age;
    private String location;

    public int getId() {
        return id;
    }

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

    public String getLoginName() {
        return loginName;
    }

    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    public String getPassword() {
        return password;
    }

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

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }
}

mapper層

package com.mashibing.springboot04.mapper;


import org.apache.ibatis.annotations.Mapper;

import java.util.List;

//ibatis  mybatis mybatis-plus
@Mapper
public interface AccountMapper {

    List<Account> findAll();

}

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.mashibing.springboot04.mapper.AccountMapper">
    <!--pojo物件和表之間的欄位和屬性的對映關係-->
    <resultMap type="com.mashibing.springboot04.mapper.Account" id="BaseResultMap">
        <!--column表示資料庫表裡面的欄位名稱,property表示pojo實體類中的屬性-->
        <result column="login_name" property="loginName"/>
        <result column="password" property="password"/>
    </resultMap>

    <!--一個select實現-->
    <select id="findAll" resultMap="BaseResultMap">
        select * from account
    </select>
</mapper>

spring啟動類

package com.mashibing.springboot04;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan(value = "com.mashibing.springboot04.mapper")
public class Springboot04Application {

    public static void main(String[] args) {
        SpringApplication.run(Springboot04Application.class, args);
    }

}

application.properties

# 專案的啟動服務埠號
server.port=80
# 專案的起始路徑
server.servlet.context-path=/account

#spring.thymeleaf.prefix= classpath:/templates/
#spring.thymeleaf.mode=LEGACYHTML5  # 不進未關閉標籤檢查,需配合nekohtml使用
#spring.thymeleaf.cache=false

# 資料庫配置
spring.datasource.url=jdbc:mysql://localhost:3306/ssm?characterEncoding=utf8&useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456


#用來示例化mapper介面
mybatis.type-aliases-package=com.mashibing.springboot04.mapper
# 表對映到sql上的xml檔案在哪  sql語句
mybatis.mapper-locations=classpath://mybatis/mapper/*.xml

pom檔案

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mashibing</groupId>
    <artifactId>springboot04</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot04</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.3.0.RELEASE</spring-boot.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.0.RELEASE</version>
                <configuration>
                    <mainClass>com.mashibing.springboot04.Springboot04Application</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>

        <!-- 如果不新增此節點mapper.xml檔案都會被漏掉。 -->
        <resources>

            <resource>

                <directory>src/main/java</directory>

                <includes>

                    <include>*.properties</include>

                    <include>**/*.xml</include>

                </includes>

                <filtering>false</filtering>

            </resource>

        </resources>
    </build>

</project>

頁面展示: