从简入深的使用SpringBoot搭建一个Web项目,同时也包括一些小的问题。第一篇博文是以较为简单的方式完成一个能够链接数据库的Springboot web程序。以前本身学习的时候看网上的教程老是感受有点太大,我只是想最简单的搭建一个能够运行的项目,以后再去深究细节。每一个教程的配置还都不同,让我不知所措,因此我就写了这篇博文来记录自我感受的较简单的步骤较少的方法去搭建一个springboot web项目,不常写文可能思路有点混乱。java
这里使用的IDE是IDEA,选择软件左上角File -> New -> Project来建立一个新的项目
mysql
选择Spring Initializr来初始化咱们的SpringBoot项目,选择JDK8以后点击Next,接下来的点击Next操做就不作说明了
linux
在以下界面咱们设置包名,项目类型选择Maven项目,语言选择Java,打包方式选择Jar,Java版本选择8
git
依赖这部分,咱们分别选择Web下的Spring Web,Template Engines下的Thymeleaf(为之后挖坑),SQL下的MyBatis Framework,固然这里也能够建立项目以后手动在POM文件中写入,不过这样更方便一些。
web
接下来选择项目的存放路径,便可完成项目的建立,建立完成项目打开项目以后IDEA右下角会弹出Maven projects need to be imported弹窗,选择Enable Auto-Import便可,这样Maven就能够本身下载依赖。spring
咱们打开新建的项目以后大体以下图所示。.idea文件夹下存放的是IDEA项目的配置文件,好比compiler.xml 配置了JDK版本,encodings.xml 配置了编码类型,该文件夹咱们不须要过多了解。.mvn文件夹下存放的是Maven的配置和相关Jar包。src文件夹是咱们主要编码的地方,src.main.java路径下是咱们编写java代码的地方,src.main.resources是咱们存放静态资源,页面以及配置文件的地方。test文件夹是咱们编写测试代码的地方。.gitignore文件配置了咱们使用git时忽略上传的文件。HELP.md是一个帮助文档。mvnw是一个linux脚本文件,可使咱们运行指定版本的Maven,mvnw.cmd则是相同功能的windows版本脚本文件,pom.xml是Maven项目的依赖配置文件。t1.iml是 intellij idea的工程配置文件,里面是当前t1 project的一些配置信息。
sql
咱们主要关注的仍是src文件夹下的文件,其他不重要的文件能够隐藏,选中t1 项目,而后点击这个文件夹右下角带三个小蓝色方块的图标
数据库
选择要隐藏的文件右键选择Excluded 而后这些文件夹就会变成橙黄色
windows
点击Apply回到原来的页面点击图片中右上角的小齿轮,点击取消Show Excluded Files,这样想要隐藏的文件就消失了
浏览器
首先咱们先来测试一下SpringBoot框架是否可以启动,建立TestController文件,目录结构以下
该类的代码以下
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController // 标注该类为Controller,SpringBoot会自动扫描该类 public class TestController { @RequestMapping("/test") // 处理请求路径为/test的请求 public String test(){ return "测试成功"; } }
注:本身敲代码的时候会自动导包,若是是复制上去的话可能不会自动导,须要手动处理。
接下来点击下图左侧主启动类的小箭头或者右上角的箭头均可以启动项目
而后观察控制台的输出,很天然的没有运行成功,出错了,咱们看一下错误提示
APPLICATION FAILED TO START
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
大致意思就是没有配置数据库驱动,咱们也没用到数据库啊?为啥要配置数据库驱动呢?还记得咱们以前选择依赖的时候选择了mybatis吗,缘由就出在这里,找到pom.xml文件注释掉mybatis依赖,重启项目。
<!-- <dependency>--> <!-- <groupId>org.mybatis.spring.boot</groupId>--> <!-- <artifactId>mybatis-spring-boot-starter</artifactId>--> <!-- <version>2.1.3</version>--> <!-- </dependency>-->
项目成功启动控制台输出以下
咱们能够看到程序启动在8080端口,在浏览器中输入请求地址便可看到咱们想要获得的测试成功字符串
咱们百度能够了解到mybatis是一个Java持久层框架,JDBC才是链接数据库用到的驱动,那为何咱们引入mybatis须要配置数据库驱动呢?
咱们从上面这张图片能够看到mybatis-spring-boot-starter依赖包含了jdbc依赖,因此引入了mybatis就至关于引入了jdbc,再加上SpringBoot的自动配置是根据是否引入类来进行自动配置的,天然的,引入了jdbc依赖就须要配置数据库驱动程序(选择数据库驱动天然是无法自动配置的),从以下的报错也能够得出一样结论。
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
咱们使用mysql数据库,首先建立一个数据库,我起的名字叫t1并建立了一张表Bear
表内字段以下
而后随便添加点数据,这样咱们数据库就准备好了。
再次回到代码这边,首先咱们是要配置数据库的链接信息,在application.properties
里作以下配置
# 数据库设置 ## 数据库驱动 spring.datasource.driver-class-name=com.mysql.jdbc.Driver # 数据库链接地址 spring.datasource.url=jdbc:mysql://localhost:3306/t1 # 数据库用户名 spring.datasource.username=root # 数据库密码 spring.datasource.password=root
其次是导入JDBC驱动,在pom.xml
中添加以下代码
<!-- mysql jdbc 驱动 https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency>
由于我安装的是5.7版本的mysql因此这里我选择的是5版本的jdbc,若是是8版本的mysql可能就须要选择8版本的驱动了,驱动程序能够在maven仓库找到,同时咱们要解开以前对mybatis依赖的注释。
接下来咱们要建立一个实体Bear,用来承接Bear表查询出来的数据,在t1目录下建立controller的同级目录entity,再在entity内建立java文件Bear.java,内容以下
package com.ljsh.t1.entity; public class Bear { private String name; private String type; private String weight; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getWeight() { return weight; } public void setWeight(String weight) { this.weight = weight; } public Bear(String name, String type, String weight) { this.name = name; this.type = type; this.weight = weight; } }
一个典型的pojo,下面的getter和setter方法能够在idea编码页面右键选择 Generate -> Getter and Setter 自动生成。
再以后是对mybaitis的配置与操做,建立controller目录同级目录mapper,在mapper目录中建立接口文件TestMapper.java,代码内容以下
package com.ljsh.t1.mapper; import com.ljsh.t1.entity.Bear; import java.util.List; public interface TestMapper { List<Bear> getAllBears(); //查询Bear表全部数据,做为List查出来 }
一样的在resources目录下也建立一个mapper文件夹,mapper文件夹里建立TestMapper.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="com.ljsh.t1.mapper.TestMapper"> <select id="getAllBears" resultType="com.ljsh.t1.entity.Bear"> select * from Bear </select> </mapper>
在T1Applicatin文件也就是主启动类中添加一个注解以下
package com.ljsh.t1; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.ljsh.t1.mapper") //扫描mapper接口类 public class T1Application { public static void main(String[] args) { SpringApplication.run(T1Application.class, args); } }
最后在application.properties
中增长一行
# 指向映射xml文件目录 mybatis.mapperLocations=classpath:mapper/*.xml
如今的目录结构以下
经过在主启动类上配置@MapperScan注解,让springboot扫描须要实现的Mapper接口文件。经过配置文件里xml地址的配置,让Mapper接口和Mapper的xml实现能够对应起来。
最后咱们在TestController里修改代码
package com.ljsh.t1.controller; import com.ljsh.t1.entity.Bear; import com.ljsh.t1.mapper.TestMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController // 标注该类为Controller,SpringBoot会自动扫描该类 public class TestController { @Autowired TestMapper testMapper; //自动注入 @RequestMapping("/test") // 处理请求路径为/test的请求 public Object test(){ List<Bear> bears = testMapper.getAllBears(); return bears; } }
从新启动项目访问http://localhost:8080/test咱们会收到从数据库查询出来的数据
这时候查看控制台可能会发现一些警告
Thu Jan 14 22:45:15 CST 2021 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
修改application.properties
中的数据库链接为以下便可消除警告
spring.datasource.url=jdbc:mysql://localhost:3306/t1?useSSL=false
原本的出发点是以最简单的方式搭建一个能跑起来的web项目,可是写完了感受仍是有点复杂,但有基础的话仍是很好理解的。以后应该还会根据这个demo更新 mvc / 配置 /mybatis 具体的一些细节,也是本身的一次复习,若是有时间的话。