您的位置:首页 > 游戏 > 游戏 > 使用 SpringBoot 基础web开发的支持

使用 SpringBoot 基础web开发的支持

2024/11/17 18:32:26 来源:https://blog.csdn.net/whs_8792/article/details/142243333  浏览:    关键词:使用 SpringBoot 基础web开发的支持

首先导入项目相关的依赖:

pom.xml 文件: 导入相关项目依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.3.3</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.ty</groupId><artifactId>SpringBootWeb</artifactId><version>0.0.1-SNAPSHOT</version><packaging>war</packaging><name>SpringBootWeb</name><description>SpringBootWeb</description><url/><licenses><license/></licenses><developers><developer/></developers><scm><connection/><developerConnection/><tag/><url/></scm><properties><java.version>21</java.version></properties><dependencies><!--    导入jquery包    --><dependency><groupId>org.webjars</groupId><artifactId>jquery</artifactId><version>3.7.1</version></dependency><!--   SpringBoot热部署支持(开发阶段使用)  --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><optional>true</optional></dependency><!--   导入 jstl标签依赖库     -->
<!--        <dependency>-->
<!--            <groupId>javax.servlet</groupId>-->
<!--            <artifactId>javax.servlet-api</artifactId>-->
<!--            <version>3.1.0</version>-->
<!--        </dependency>--><dependency><groupId>jakarta.servlet</groupId><artifactId>jakarta.servlet-api</artifactId></dependency><!--   添加jsp引擎依赖   --><dependency><groupId>org.apache.tomcat.embed</groupId><artifactId>tomcat-embed-jasper</artifactId></dependency><!-- 导入 mybatis-plus 包 --><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-spring-boot3-starter</artifactId><version>3.5.7</version></dependency><!--    导入jpa包    --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><!-- 导入 junit 测试包      --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><scope>test</scope></dependency><!--        &lt;!&ndash;    导入mysql包    &ndash;&gt;-->
<!--        <dependency>-->
<!--            <groupId>mysql</groupId>-->
<!--            <artifactId>mysql-connector-java</artifactId>-->
<!--            <version>8.0.19</version>-->
<!--        </dependency>--><!--  导入 阿里巴巴的 Druid 数据源 --><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.2.22</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId><scope>provided</scope></dependency><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId><scope>provided</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins></build></project>

在启动类加bean 的扫描:

application.properties 配置文件:

spring.application.name=SpringBootWebspring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/crm?serverTimezone=GMT-8
spring.datasource.username=root
spring.datasource.password=whsspring.datasource.type=com.alibaba.druid.pool.DruidDataSourcespring.datasource.druid.initial-size=3
spring.datasource.druid.min-idle=3
spring.datasource.druid.max-active=10
spring.datasource.druid.max-wait=60000
spring.datasource.druid.stat-view-servlet.login-username=root
spring.datasource.druid.stat-view-servlet.login-password=whs
spring.datasource.druid.filter.stat.log-slow-sql=true
spring.datasource.druid.filter.stat.slow-sql-millis=2000# 打印sql
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl# 指定SpringMvc视图的前缀和后缀 , 使其支持使用Jsp文件作为视图
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp# 关闭Thymeleaf
spring.thymeleaf.cache=true
# 热部署配置
spring.devtools.restart.enabled=true  
spring.devtools.restart.additional-paths= src/main/java 
spring.devtools.restart.exclude=static/**,public/**

流程:

1. 在 mappr 层 继承BaseMapper

2. UserService 中定义自己另外需要的接口 

public interface UserService {public User login(String username, String password);public int addUser(User user);public int updateUser(User user);public int deleteUser(Long usrId);public User getUser(Long usrId);public List<User> findAllUsers();
}

3. UserServiceImpl 实现方法:

4. 编写  UserController 控制器类 :

5. 在浏览器进行访问

最后一个简单的 使用 SpringBoot 基础web开发的支持 就已经完成了。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com