springboot学习笔记(一)我的第一个springboot项目搭建

我的第一个springboot项目详细记录

(springboot学习笔记,内容仅供参考,欢迎提问)

一.项目搭建

1.开发环境:

Java10.0.2版本 、idea 旗舰版。

2.应用技术:

springboot、mybatis、thymeleaf(前端工具)、mysql(数据库)。

3.创建过程:

我按照常规的方式创建springboot项目方式,创建的项目始终卡在reading … pom.xml。所以我创建了一个普通的maven项目,然后将对应的依赖加入pom.xml文件中,自己构建springboot项目。

3.首先解决maven仓库下载依赖慢的问题:

在maven的setting配置文件中加入阿里云的镜像地址,这里我用的是idea自带的maven路径如下。
在这里插入图片描述
添加代码如下:

  <mirrors>
        <!-- mirror
         | Specifies a repository mirror site to use instead of a given repository. The repository that
         | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
         | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
         |
        <mirror>
          <id>mirrorId</id>
          <mirrorOf>repositoryId</mirrorOf>
          <name>Human Readable Name for this Mirror.</name>
          <url>http://my.repository.com/repo/path</url>
        </mirror>
         -->
 
        <mirror>
            <id>alimaven</id>
            <name>aliyun maven</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            <mirrorOf>central</mirrorOf>
        </mirror>
 
        <mirror>
            <id>uk</id>
            <mirrorOf>central</mirrorOf>
            <name>Human Readable Name for this Mirror.</name>
            <url>http://uk.maven.org/maven2/</url>
        </mirror>
 
        <mirror>
            <id>CN</id>
            <name>OSChina Central</name>
            <url>http://maven.oschina.net/content/groups/public/</url>
            <mirrorOf>central</mirrorOf>
        </mirror>
 
        <mirror>
            <id>nexus</id>
            <name>internal nexus repository</name>
            <!-- <url>http://192.168.1.100:8081/nexus/content/groups/public/</url>-->
            <url>http://repo.maven.apache.org/maven2</url>
            <mirrorOf>central</mirrorOf>
        </mirror>
  </mirrors>
4.添加依赖:

完整的pom.xml文件,不同的工具包版本之间存在对应关系,但是不是非常严格的关系。寻找工具包的名字可以到maven依赖查询网站

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>org.example</groupId>
    <artifactId>one</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <java.version>10.0</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.4.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- thymeleaf -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.13</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

    <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>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
5.文件结构:

注意:其中OneApplication是启动类,其他类应该放在其包的子包中,这样才能扫描到。
controller控制器包,entity实体类包,dao数据处理包,service服务类包。服务类包内创建一个实现服务类包,通过接口和实现类的方式提供服务。
启动类代码:

package com.two;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;


@SpringBootApplication
public class OneApplication {

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

}

在这里插入图片描述
resources是资源文件夹,static存储静态文件,templates存储html文件,mapper文件夹中存储mapper映射文件(如果你想使用mybatis)。application.properties是资源文件夹下的配置文件,用来配置springboot的相关参数。

6.springboot配置

JPA和mybatis类似,是两种不同的数据处理工具。这里jsp配置不用可以不写,jsp技术太老了。

## 编码
server.tomcat.uri-encoding=utf-8

## 项目根路径及端口
#server.servlet.context-path=/demo1
server.port=8080

## 数据源
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/record?serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=4568988636lhn

## JPA
spring.jpa.database=MySQL
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update

## JSP配置
#默认前缀后缀
#spring.mvc.view.prefix=/WEB-INF/jsp/
#spring.mvc.view.suffix=.jsp

## 模板引擎
#关闭引擎
#spring.thymeleaf.cache=false
#spring.thymeleaf.enabled=false

#配置地址
mybatis.typeAliasesPackage=com.two.entity
mybatis.mapperLocations=classpath:mapper/*.xml

## 打印SQL语句
logging.level.dao=debug
## 上传文件存储地址
filePath=E:/upload/
7.项目搭建总结

这里项目初步创建完成,可以运行启动类启动项目,但是访问http://localhost:8080/的时候可能会遇到问题,例如404 或者white error,我遇到这个问题是因为数据库没有配置导致报错,这里具体的解决办法可以参考其他博客。

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值