使用IDEA搭建一个springboot的基础项目

一、基础搭建

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
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值