作者简介:练习时长两年半的Java up主
个人主页:程序员老茶
ps:点赞是免费的,却可以让写博客的作者开兴好久好久
系列专栏:Java全栈,计算机系列(火速更新中)
格言:种一棵树最好的时间是十年前,其次是现在
动动小手,点个关注不迷路,感谢宝子们一键三连
Spring推出的一个Spring框架的脚手架。
不是一个新的框架,而是搭建Spring相关框架的平台。
它省去了Spring、SpringMVC项目繁琐的配置过程,让开发Web项目变得更加简单。
本质还是Spring+SpingMVC,可以搭配其他的ORM框架,如Mybatis,MybatisPlus,JPA,Hibernate等。
特点
官网模板Spring Initializr
会自动下载压缩包,解压后使用idea打开即可
创建后的目录结构
在SpringBoot的启动类XXXApplication所在的包中,创建子包,新建类
启动项目时,运行类中的main方法即可,默认项目名为localhost:8080
@Controller
public class FirstController {
@RequestMapping("/hello")
public String hello(){
System.out.println("xxxxxx");
return "Hello SpringBoot!";
}
}
如果这时启动项目后访问,是404页面,因为当前方法返回的字符串表示一个静态页面的名称,即static目录下的页面名。
@RequestMapping("/hello")
public String hello(){
System.out.println("xxxxxx");
return "welcome.html";
}
如果是返回一个.html页,且该页面位于static目录下,访问/hello时,就会跳转到对应页面
@RequestMapping("/hello")
@ResponseBody
public String hello(){
System.out.println("xxxxxx");
return "Hello SpringBoot!";
}
如果添加了@ResponseBody注解,访问该方法时,浏览器就会得到返回的内容
在application.properties文件中
# 修改默认端口号
server.port=8088
# 修改项目上下文路径
server.servlet.context-path=/first
项目在开发工程中,可以不需要每次改动代码后重启,等待一段时间后自动更新编译运行
添加依赖
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<scope>runtimescope>
<optional>trueoptional>
dependency>
设置
用于简化实体类中模板代码的工具
添加依赖
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
IDEA2020.2之后的版本会内置Lombok插件,无需安装。
旧版本需要安装
Lombok常用注解 | 作用 |
---|---|
@AllArgsConstructor | 生成全参构造方法 |
@Data | 以下注解之和 |
@Setter | 生成set方法 |
@Getter | 生成get方法 |
@NoArgsConstructor | 生成无参构造方法 |
@ToString | 生成所有参数的toString()方法 |
@EqualsAndHashCode | 生成所有参数的equlas()和hashCode()方法 |
在创建项目的同时可以选择第二步的依赖
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<scope>runtimescope>
<optional>trueoptional>
dependency>
<dependency>
<groupId>com.mysqlgroupId>
<artifactId>mysql-connector-jartifactId>
<scope>runtimescope>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.3.0version>
dependency>
在application.properties中添加
配置数据库连接信息
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/gamedb?serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root
配置Mybatis
在resouces目录下创建一个mapper目录用于保存mybatis的sql映射文件
#mapper映射文件目录
mybatis.mapper-locations=classpath:mapper/*.xml
#开启驼峰命名映射
mybatis.configuration.map-underscore-to-camel-case=true
#开启sql日志
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
mybatis的sql映射文件模板
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="某个dao层接口的全限定名">
mapper>
表
实体类
package com.hqyj.firstspringboot.entity;
import lombok.Data;
@Data
public class Hero {
private Integer id;
private String name;
private String position;
private String sex;
private Integer price;
private String shelfDate;
}
dao
package com.hqyj.firstspringboot.dao;
import com.hqyj.firstspringboot.entity.Hero;
import org.springframework.stereotype.Repository;
import java.util.List;
/*
* dao层接口
* */
@Repository
public interface HeroDao {
List<Hero> queryAll();
}
service
package com.hqyj.firstspringboot.service;
import com.hqyj.firstspringboot.dao.HeroDao;
import com.hqyj.firstspringboot.entity.Hero;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class HeroService {
@Autowired
private HeroDao heroDao;
public List<Hero> queryAll(){
return heroDao.queryAll();
}
}
controller
package com.hqyj.firstspringboot.controller;
import com.hqyj.firstspringboot.entity.Hero;
import com.hqyj.firstspringboot.dao.HeroDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@Controller
public class FirstController {
@Autowired
private HeroDao heroDao;
@RequestMapping("/queryAll")
@ResponseBody
public List<Hero> queryAll(){
return heroDao.queryAll();
}
}
package com.hqyj.firstspringboot;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.hqyj.firstspringboot.dao")
public class FirstSpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(FirstSpringBootApplication.class, args);
}
}
启动项目,根据设置的项目端口号、上下文路径、controller的映射访问对应的控制器
好好学习,天天向上。
往期专栏 |
---|
Java全栈开发 |
数据结构与算法 |
计算机组成原理 |
操作系统 |
数据库系统 |
物联网控制原理与技术 |