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 根目录
@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.Driver
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://localhost:3306/spring_boot_db?characterEncoding=UTF-8&SSL=false #指定url
username: root
password: 123456
# mybatis 配置
server:
servlet:
jsp:
init-parameters:
development: true
mybatis:
mapper-locations: classpath:mybatis/mapper/*.xml
type-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 mapper
PUBLIC "-//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 接口类
public interface UserInfDao {
/**
* 获取所有
* @return
*/
List<UserInfBean> getAll();
/**
* 添加
* @param bean
*/
void insert(UserInfBean bean);
}
6、配置mapper 扫描信息
@MapperScan
@SpringBootApplication
@MapperScan({"com.fashion.mapper"})
public class SpringbootDay2Application {
public static void main(String[] args) {
SpringApplication.run(SpringbootDay2Application.class, args);
}
}
7、controller 测试
@Autowired
private 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 容器注入
@SpringBootTest
class SpringbootDay2ApplicationTests {
@Autowired
private UserInfDao userInfDao;
@Test
public 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 时候代表已经生效