实现MVC只需要添加web依赖,添加controller启动服务就可以直接访问
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--排除不需要的依赖-->
<exclusions>
<!--<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>-->
</exclusions>
</dependency>
application.yml
# 配置文件优先级:properties > yml > yaml
# yml配置文件优先级:
# 1、resources/application1.yml
# 2、resources/config/application1.yml
# 3、jar包目录/application1.yml
# 4、jar包目录/config/application1.yml
# 注意空格,格式非常重要
spring:
application:
name: springboot_01_springmvc
# 配置使用环境
profiles:
active: dev
# 日志
logging:
level:
root: info
---
# 开发环境
spring:
profiles: dev
server:
port: 8081
# 多环境配置用“---”隔开
---
# 生产环境
spring:
profiles: prd
server:
port: 8082
访问数据库
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
配置连接
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
# mysql6.0以上使用cj版本
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai
username: root
password: root
mybatisplus框架提供基本查询
// Mapper接口
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
// 测试
@Autowired
private UserMapper userMapper;
@Test
public void getUserList(){
// 直接调接口方法
System.out.println(userMapper.selectList(null));
}
将SpringBoot项目打包
将打好的jar包发给前端(前端只需要安装jdk)
在jar包所在目录执行dos命令:java -jar xxx.jar,就可以启动项目
也可以在命令后面修改配置,例如:java -jar xxx.jar --server.port=8081,指定端口启动