Spring Boot 笔记 005 环境搭建

1.1 创建数据库和表(略)

2.1 创建Maven工程

2.2 补齐resource文件夹和application.yml文件

2.3 porn.xml中引入web,mybatis,mysql等依赖

2.3.1 引入springboot parent

2.3.2 删除junit 依赖--不能删,删了会报错

2.3.3 引入spring web依赖

2.3.4 引入mybatis依赖

2.3.5 引入mysql依赖

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>

<!--  引入springboot父工程-->
  <parent>
    <artifactId>spring-boot-starter-parent</artifactId>
    <groupId>org.springframework.boot</groupId>
    <version>3.1.8</version>
  </parent>


  <groupId>com.geji</groupId>
  <artifactId>big-event</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>big-event</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

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

  <dependencies>

<!--    spring web依赖 -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
<!--    mybatis依赖-->
    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>3.0.0</version>
    </dependency>
<!--    mysql驱动依赖-->
    <dependency>
      <groupId>com.mysql</groupId>
      <artifactId>mysql-connector-j</artifactId>
    </dependency>


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

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

3.1 在application.yml中配置数据库信息

XML 复制代码
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/big_event
    username: root
    password: 1234

4.1 创建包结构

tips: 有情况下idea中包结构是.形式的没有铺开,可以按图示方式转换成普通的包结构显示

4.2 根据数据库表创建实体类

4.2.1 Article

java 复制代码
package com.geji.pojo;


import java.time.LocalDateTime;

public class Article {
    private Integer id;//主键ID
    private String title;//文章标题
    private String content;//文章内容
    private String coverImg;//封面图像
    private String state;//发布状态 已发布|草稿
    private Integer categoryId;//文章分类id
    private Integer createUser;//创建人ID
    private LocalDateTime createTime;//创建时间
    private LocalDateTime updateTime;//更新时间
}

4.2.2 Category

java 复制代码
package com.geji.pojo;

import java.time.LocalDateTime;

public class Category {
    private Integer id;//主键ID
    private String categoryName;//分类名称
    private String categoryAlias;//分类别名
    private Integer createUser;//创建人ID
    private LocalDateTime createTime;//创建时间
    private LocalDateTime updateTime;//更新时间
}

4.2.3 User

java 复制代码
package com.geji.pojo;



import java.time.LocalDateTime;

public class User {
    private Integer id;//主键ID
    private String username;//用户名
    private String password;//密码
    private String nickname;//昵称
    private String email;//邮箱
    private String userPic;//用户头像地址
    private LocalDateTime createTime;//创建时间
    private LocalDateTime updateTime;//更新时间
}

注意命名方式,实体类为驼峰,数据库为下划线

5.1 更改启动类名字,并编写相应代码

java 复制代码
package com.geji;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Hello world!
 *
 */
@SpringBootApplication
public class BigEventApplication
{
    public static void main( String[] args )
    {

        SpringApplication.run(BigEventApplication.class,args);

    }
}

6.1 启动,不报错则创建环境成功

tips:

7.1 以上实体类没有getter,setter等方法,可以使用lombok在编辑阶段自动生成

7.1.1 在依赖中引入lombok

XML 复制代码
<!--    lombok依赖-->
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
    </dependency>

7.1.2 在实体类上加入@Data注解

7.1.3 在maven中重新编译,然后在target文件夹中就会发现编译好的实体类中有getter,setter等方法了

相关推荐
Query*11 分钟前
Java 设计模式—— 责任链模式:从原理到 SpringBoot 最优实现
java·spring boot·责任链模式
朝九晚五ฺ11 分钟前
【Redis学习】持久化机制(RDB/AOF)
数据库·redis·学习
虾说羊14 分钟前
sql中连接方式
数据库·sql
liweiweili12614 分钟前
Django中处理多数据库场景
数据库·python·django
追逐时光者16 分钟前
程序员必备!5 款免费又好用的数据库管理工具推荐
数据库
爱因斯坦乐2 小时前
【vue】I18N国际化管理系统
前端·javascript·vue.js·笔记·前端框架
Ro Jace2 小时前
模式识别与机器学习课程笔记(4):线性判决函数
人工智能·笔记·机器学习
兮兮能吃能睡3 小时前
SQL中常见的英文术语及其含义
数据库·sql·oracle
Rousson3 小时前
硬件学习笔记--82 连接器的选用原则与流程
笔记·单片机·学习
Elastic 中国社区官方博客4 小时前
根据用户行为数据中的判断列表在 Elasticsearch 中训练 LTR 模型
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索