一、基础搭建
1.新建一个工程(New->Project)
2.点击Next
3.点击Next,选择Spring Web
4.点击Next,可指定工程的存放路径,或者默认即可。
5.点击Finish后,搭建完成。
搭建完成后的项目pom.xml中会自动引入web启动依赖,如下图:
写一个controller类
由于springboot内嵌了tomcat,因此只需要启动这个类即可,端口默认8080
使用浏览器 访问测试
1.sayHello1方法
http://localhost:8080/hello/say1?name=xxx
1.sayHello2方法
http://localhost:8080/hello/say2/name=yyy
二、升级一下难度(连接数据库)…
SpringBoot+Mybatis注解+Mysql数据库
1.pom.xml配置文件中增加mysql驱动和mybatis依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.0</version>
</dependency>
2.创建表并添加测试数据
#mysql创建表
CREATE TABLE userTest (
id int(11) NOT null primary key auto_increment,
email varchar(255) NOT NULL,
password varchar(255) NOT NULL,
username varchar(255) NOT NULL
)
#mysql添加数据
INSERT INTO userTest(email,password,username) VALUES ('1@qq.com', '123456', '张三'),('2@qq.com', '234567', '李四'),('3@qq.com', '345678', '王五')
3.包格式(注意:springboot的启动类一定要和bean,controller等包–平级。)
4. yml配置文件中新增数据源的连接配置以及实体类pojo的指向路径。
## 数据源配置
spring:
datasource:
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
## Mybatis 配置
# 配置为 com.br.springboottest.bean 指向实体类包路径。
mybatis:
typeAliasesPackage: com.br.springboottest.bean
5.Dao层
6.Service实现类
7.Controller层
8.使用浏览器测试访问
http://localhost:8080/user/find