我写博客,一方面是为了大家方便查看学习,一方面是为自己做一个笔记,方便自己以后复习和查找。
连接数据库的基本结构
数据库和表的信息
我的数据库名为(spring2),表名为(message)。
Spring Boot项目的基本结构
(注意:application.yml文件是创建时没有的,而且也不能在templates文件夹下创建,创建方法请看下一条)
创建application.yml文件
可以选择项目文件,右击打开New下的File
然后在File name内输入application.yml,点击Finish就可以创建成功了。
编写application.yml文件
创建成功后,我们将application.yml手动拉到templates文件夹下即可。
(注意:application.yml文件相当于连接数据库的纽带)
server:
port: 8080
servlet:
context-path:
spring:
datasource:
username: root
password: root
//url里面的spring2是我写的数据库名称,在使用时除了改变数据库名称,其他的不用改变
url: jdbc:mysql://localhost:3306/spring2?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
配置application.properties
application.properties是程序资源文件,或者叫配置文件,我一般把程序的配置信息放在里面
#spring.mvc.view.prefix=/templates
spring.mvc.view.suffix=.html
配置pom.xml文件
这是最基本的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 https://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.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>HelloWorld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>HelloWorld</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
我们需要将下面的代码添加到dependencies标签的中间
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!-- for JPA, use hibernate-entitymanager instead of hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- thymeleaf 模板 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
因为篇幅实在太长了,关于后续大家可以看下一篇:
用Java Spring Boot连接数据库实现增删改查(没有使用表连接)