springboot 模版集成方案(第二章)

embedded/2024/10/11 0:43:10/

springboot 模版集成方案

jsp 模版集成

​ 在SpringBoot框架中默认模板推荐使用Thymeleaf模板,但是也不能排除有些公司还是使用jsp 模版解析;

1. 引入jsp 集成的 jar 包

<!--c标签库-->
<dependency><groupId>jstl</groupId><artifactId>jstl</artifactId><version>1.2</version>
</dependency><!--让内嵌tomcat具有解析jsp功能-->
<dependency><groupId>org.apache.tomcat.embed</groupId><artifactId>tomcat-embed-jasper</artifactId>
</dependency>

2. 引入插件,引入后才能正常打包解析jsp页面

<build><plugins><!--引入springboot插件 可以正确打包  显示jsp--><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>

3. 配置试图解析器

spring:mvc:view:prefix: /suffix: .jsp

4. 编写测试controller

在项目目录下面新建一个 webapp 目录,在这个目录下面新建jsp 页面,因为我们已经丢弃了web.xml 所以只需要配置一下 webapp 为jsp 根目录

alt
java">@Controller
public class JspController {@RequestMapping("jsp")public String jsp() {return "index";}
}

5. 处理404 问题

如果我们直接访问http://localhost:8080/jsp 这个时候会直接报 404,其实这是idea 问题,如果我们打包后直接运行不会出现这个错误,现在有两种解决方案;

  • 方案1一,修改idea 配置,idea中指定工作目录启动
    在这里插入图片描述

  • 方案二,我们利用 插件启动

在这里插入图片描述

6. 设置jsp 热部署,无需重新启动

server:servlet:jsp:init-parameters:development: true

mybatis框架整合

我们引入mysql 与alibaba的 druid 连接池,创建用户表,最终实现查询和添加操作

1、引入依赖

      <dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.2.4</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.38</version></dependency><!--整合mybatis--><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.1.4</version></dependency>

2、 编写配置文件

  • 数据库连接信息
  • mapper 对应的xml 扫描信息
  # 数据库配置datasource:driver-class-name: com.mysql.jdbc.Drivertype: com.alibaba.druid.pool.DruidDataSourceurl: jdbc:mysql://localhost:3306/spring_boot_db?characterEncoding=UTF-8&SSL=false          #指定urlusername: rootpassword: 123456# mybatis 配置server:servlet:jsp:init-parameters:development: true
mybatis:mapper-locations: classpath:mybatis/mapper/*.xmltype-aliases-package: com.fahion.entity

3、数据库建表信息

create table t_user_inf_boot (id integer primary key auto_increment comment '主键',`name` varchar(64) not null default '' comment '姓名',`address` varchar(128) not null default '' comment '住址',`phone` varchar(11) not null default '' comment '手机号',creTime datetime default now() comment '创建时间',updTime datetime default now() comment '更新时间'
)

4、 mapper.xml 编写

<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.fashion.mapper.UserInfDao"><select id="getAll" resultType="com.fashion.entity.UserInfBean">select * from t_user_inf_boot</select><insert id="insert" useGeneratedKeys="true" keyProperty="id" parameterType="com.fashion.entity.UserInfBean">insert into t_user_inf_boot(name,address,phone)values (#{name},#{address},#{phone})</insert></mapper>

5、mapper 接口类

java">public interface UserInfDao {/***  获取所有* @return*/List<UserInfBean> getAll();/***  添加* @param bean*/void insert(UserInfBean bean);}

6、配置mapper 扫描信息

@MapperScan

java">@SpringBootApplication
@MapperScan({"com.fashion.mapper"})
public class SpringbootDay2Application {public static void main(String[] args) {SpringApplication.run(SpringbootDay2Application.class, args);}}

7、controller 测试

java">  @Autowiredprivate UserInfDao userInfDao;@RequestMapping("getAll")public List<UserInfBean> getAll() {return userInfDao.getAll();}@RequestMapping("insert")public UserInfBean getAll(UserInfBean bean) {if (bean.getPhone() == null) {bean.setPhone("138xxxx");}userInfDao.insert(bean);return bean;}

单元测试

开发过程中业务代码课程非常复杂频繁启动服务器测试,非常麻烦!这个时候使用本地测试就是一个很好的解决方案,springboot也提供了本地测试解决方案!

1、引入jar依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope>
</dependency>

2、 编写测试类

  • @SpringbootTest
    • 修饰范围:类上
    • 作用:用来启动本地spring环境,进去 ioc 容器注入
java">@SpringBootTest
class SpringbootDay2ApplicationTests {@Autowiredprivate UserInfDao userInfDao;@Testpublic void getOne() {UserInfBean userInfBean = userInfDao.getAll().stream().findFirst().orElseGet(UserInfBean::new);System.out.println(userInfBean);}}

热部署工具

当我们修改了后台代码,每次只能重新启动,这大大的浪费了我们的效率;springboot 也为我们提供了一种热部署的方案;

  • 导入所需jar
  • idea 配置
  • regiest 注册app 自动检测代码

1、导入jar 包

该包每次都需要导入才能使用

   <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><optional>true</optional></dependency>

2、设置idea 自动编译

## 1. 开启自动编译Preferences | Build, Execution, Deployment | Compiler -> 勾选上 Build project automatically 这个选项## 2. 开启允许在运行过程中修改文件
ctrl + alt + shift + / ---->选择1.Registry ---> 勾选 compiler.automake.allow.when.app.running 这个选项

idea设置
在这里插入图片描述

3、测试是否生效

当出现 restartedMain 时候代表已经生效

在这里插入图片描述


http://www.ppmy.cn/embedded/125628.html

相关文章

力扣10.9

3171. 找到按位或最接近 K 的子数组 给你一个数组 nums 和一个整数 k 。你需要找到 nums 的一个 子数组 &#xff0c;满足子数组中所有元素按位或运算 OR 的值与 k 的 绝对差 尽可能 小 。换言之&#xff0c;你需要选择一个子数组 nums[l..r] 满足 |k - (nums[l] OR nums[l 1…

PclSharp1.12.0库文件下载地址

C#Winfrom实现3D点云目标识别 使用PclSharp1.12.0库及PlcSharp可视化库&#xff0c;利用Winform框架开发点云算法处理应用程序&#xff0c;可适配激光雷达点云数据或者是3D相机拍摄扫描的点云数据&#xff0c;定位识别目标物体&#xff0c;得出抓取中心&#xff0c;通过数据通信…

jmeter学习(4)提取器

同线程组https://blog.csdn.net/vikeyyyy/article/details/80437530 不同线程组 在JMeter中&#xff0c;正则表达式提取的参数可以跨线程组使用。 通过使用Beanshell后置处理器和属性设置函数&#xff0c;可以将提取的参数设置为全局变量&#xff0c;从而在多个线程组之间共享…

ai智能电话机器人的核心技术有哪些?

ai智能电话机器人是一种高智能语音系统&#xff0c;它能够非常智能化的和用户进行畅通的交流&#xff0c;而不会存在任何的障碍问题&#xff0c;这个主要是由于它使用了很多的核心技术&#xff0c;我们一起来看看有哪些核心技术。 1.VAD 准确定位语音的开始点和结束点&#x…

模拟实现字符函数和字符串函数(一)

目录 一、模拟实现strlen 二、模拟实现strcpy 三、模拟实现strcmp 四、模拟实现strcat 五、模拟实现strstr 模拟实现strlen模拟实现strcpy模拟实现strcmp模拟实现strcat模拟实现strstr 一、模拟实现strlen strlen函数是用来求字符串长度的函数 #include <stdio.h>…

输出平方矩阵

题目&#xff1a; 输入一个正整数n&#xff0c;输出一个n阶的平方矩阵。 例如&#xff1a; 输入&#xff1a;5 输出&#xff1a; 1 4 9 16 25 4 9 16 25 1 9 16 25 1 4 16 25 1 4 9 25 1 4 9 16 解题思路&#xff1a; 本题我分别采用一维数组和二维数组来实现。 一…

抖店API接口系列(商品详情数据),Json数据格式参考

抖店API接口系列中的商品详情数据接口允许第三方应用通过编程方式访问抖音小店的商品数据。这些数据通常包括商品的基本信息、价格、库存、用户评价等&#xff0c;并且会以JSON数据格式返回。以下是一个抖店商品详情数据JSON格式的参考示例&#xff1a; { "status":…

【优选算法之BFS】No.16---多源BFS和BFS解决拓扑排序

文章目录 前言一、多源BFS示例&#xff1a;1.1 01 矩阵1.2 ⻜地的数量1.3 地图中的最⾼点1.4 地图分析 二、BFS解决拓扑排序&#xff1a;2.1 拓扑排序简介2.1.1 有向无环图(DAG图)2.1.2 AVO网&#xff1a;顶点活动图2.1.3 拓扑排序2.1.4 实现拓扑排序 2.2 BFS解决拓扑排序示例&…