SpringBoot项目设置起始页面
时间: 2023-12-01 08:41:54 浏览: 167
可以通过在SpringBoot项目的静态资源路径(resources/static)下创建index.html文件来设置起始页面。SpringBoot项目在启动后,会自动去静态资源路径下查找index.html文件作为首页文件。如果需要设置其他名称的起始页面,可以在application.properties或application.yml文件中添加以下配置:
```yaml
spring.mvc.view.prefix=/templates/
spring.mvc.view.suffix=.html
spring.mvc.static-path-pattern=/static/**
```
其中,spring.mvc.view.prefix用于设置模板文件的路径前缀,spring.mvc.view.suffix用于设置模板文件的后缀名,spring.mvc.static-path-pattern用于设置静态资源的路径。例如,如果要将起始页面设置为home.html,可以在templates文件夹下创建home.html文件,并将spring.mvc.view.prefix设置为/templates/,spring.mvc.view.suffix设置为.html。如果需要将静态资源路径设置为statics文件夹,可以将spring.mvc.static-path-pattern设置为/statics/**。
相关问题
idea创建springboot项目2019
### 如何在 IntelliJ IDEA 2019 中创建 Spring Boot 项目
#### 准备工作
为了顺利创建 Spring Boot 项目,在开始之前需确认已安装并配置好了 Maven 和 JDK。这一步骤对于确保后续操作无误至关重要[^3]。
#### 创建新项目
打开 IntelliJ IDEA 后,选择 `File` -> `New` -> `Project...` 来启动新建项目的向导界面。此时应能看到多个选项可供选择;其中就包含了专门用于构建 Spring 应用程序的选择——即通过 **Spring Initializr** 进行初始化的方式[^4]。
#### 配置项目基本信息
当选择了基于 Spring Initializr 的方式之后,将会进入一个表单页面来填写关于要创建的新项目的各种属性,比如 GroupId, ArtifactId 等等。这些信息将帮助定义应用程序的身份以及其依赖关系管理文件 (pom.xml 或 build.gradle)[^1]。
#### 添加所需依赖项
继续前进到下一步,则可以挑选想要加入当前工程中的 Starter POMs(起始POM),它们代表了不同模块的功能集合,例如 Web 支持、数据访问层的支持等等。根据实际需求勾选相应的框即可完成设置[^2]。
#### 完成项目创建
最后点击 "Finish" 按钮结束整个流程,等待 IDE 自动下载必要的库并将所有东西组装在一起形成完整的开发环境。一旦这个过程完毕,就可以立即着手编写业务逻辑代码了。
```java
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
```
springboot项目使用mybatis分页查询
### 如何在 Spring Boot 中用 MyBatis 实现分页查询
#### 使用 MyBatis-Plus 的分页插件
为了有效管理大数据集并防止内存溢出,在 Spring Boot 项目中可以利用 MyBatis-Plus 提供的内置分页功能。这使得开发者能够轻松地对数据库记录进行分片检索,而无需手动编写复杂的 SQL 查询语句[^1]。
首先需要引入 `mybatis-plus-boot-starter` 和其他必要的依赖项到项目的构建文件(如 Maven 或 Gradle)。接着配置好数据源以及 MyBatis Plus 配置类之后就可以开始设置分页拦截器了:
```java
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Bean;
@Configuration
public class MyBatisConfig {
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
```
完成上述配置后,便可以在 Service 层定义带有分页参数的方法,并调用 Mapper 接口中的查询方法来获取分页后的结果集合。这里需要注意的是,Service 方法应当加上 `@Transactional` 注解以确保操作处于 Spring 管理下的事务环境中,从而避免不必要的 JDBC 连接问题[^2]。
下面是一个简单的例子展示怎样在一个 User 表上实施分页逻辑:
假设有一个名为 `UserMapper.xml` 文件用于映射用户实体对象与对应的 CRUD 操作,则可在其中声明如下形式的选择列表:
```xml
<select id="selectUsersByPage" resultType="com.example.demo.entity.User">
SELECT * FROM t_user WHERE deleted=0 ORDER BY create_time DESC LIMIT #{start},#{size};
</select>
```
而在 Java 代码层面则可以通过创建一个服务类来进行具体的业务处理:
```java
@Service
public class UserServiceImpl implements IUserService {
private final UserMapper userMapper;
public UserServiceImpl(UserMapper userMapper){
this.userMapper = userMapper;
}
@Override
@Transactional(rollbackFor = Exception.class)
public Page<User> getUsersByPage(int currentPage, int pageSize) throws ServiceException{
try {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
// 设置条件...
Page<User> page = new Page<>(currentPage,pageSize);
IPage<User> iPage = userMapper.selectPage(page,queryWrapper);
List<User> records = iPage.getRecords();
long totalElements = iPage.getTotal();
return new PageImpl<>(records, PageRequest.of(currentPage - 1, pageSize),totalElements );
} catch (Exception e) {
throw new ServiceException("Failed to get users by page.");
}
}
}
```
此段程序展示了如何基于传入的当前页面编号 (`currentPage`) 及每页大小(`pageSize`) 来计算起始位置,并最终返回封装好的 `Page<T>` 对象给前端应用以便进一步渲染表格或其他 UI 组件[^4]。
对于 RESTful API 开发者来说,还可以考虑直接暴露 JSON 数据作为响应体的一部分,这样客户端可以直接解析这些结构化信息而不必关心底层的具体实现细节[^5]。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044736.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)