想把昨天的ssm项目换成springboot项目,首先是用maven创建了一个新的项目,在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 http://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>2.1.10.RELEASE</version>
</parent>
<groupId>com.bjsxt</groupId>
<artifactId>02_springboot_mybatis</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.10.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!--配置SpringBoot的web启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--配置SpringBoot整合Mybatis的依赖:SpringBootMybatis启动器-->
<!--相当于Spriing+Mybatis的项目开发环境就搭建好了-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.48</version>
</dependency>
<!--配置Druid数据库连接池依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<!--配置Jsp的依赖-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<!--配置Thymeleaf启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--配置SpringBoot开发者工具-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!--配置pageHelper插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.12</version>
</dependency>
<!--配置单元测试启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
<!--配置SpringBoot的打包插件-->
<build>
<finalName>myWar</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>
导入springboot的依赖后:
在main-resources下创建application.yml
在yml里配置:
server:
port: 8888
# mybatis 配置
spring:
datasource:
url: jdbc:mysql://localhost:3306/keyi?useSSL=false&useUnicode=true&serverTimezone=GMT&characterEncoding=utf8
username: root
password: 724788
driver-class-name: com.mysql.jdbc.Driver
mvc:
view:
prefix: /WEB-INF/
suffix: .jsp
static-path-pattern: /**
resources:
static-locations: classpath:static/*
# 加载MyBatis的mapper.xml
mybatis:
mapper-locations: classpath:mybatis/*.xml
由于项目之前有beans.xml(spring容器的配置文件)里面配置了datasource,所以在yml里的没有配就会出错,可见猜测:在原先的beans.xml里配置的datasource是无效的,不被springboot识别?
并且,一在resources文件夹下创建application.yml就被识别了,出现了叶子图标,说明这个文件名应该是唯一的,可能自动被springboot识别是其配置文件。
在配置文件里添加了port、datasource、mybatis,把所有mapper.xml放在了resources下面的mybatis文件夹里,并配置了mapper-locations。(感觉是把原先beans.xml和mybatis.xml的配置全都集合在application的配置里了。)
值得注意的是,yml的文件里的各层级要注意好,一不小心就容易出错。
在main-java里建包,在包里创建MyApplication.java,在包里把原先的各层代码移动过来。其他的静态文件和jsp文件也移动过来了。
在springboot的启动类MyApplication上面添加了注解@MapperScan("com.keyi.mapper"),并在每一个mapper接口上都有加了@Mapper注解。
package com.keyi;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.keyi.mapper")
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class,args);
}
}
至此,可以成功地启动springboot了。
但是访问不到jsp文件。死磕了一天还是访问不到jsp文件。打算放弃了,明天开始学习thymeleaf,到时候把所有页面转换成thymeleaf吧。
Circular view path [main]: would dispatch back to the current handler URL [/main] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)
211/5000
循环视图路径[main]:将再次调度回当前处理程序URL [/main]。检查您的ViewResolver设置!(提示:这可能是一个未指定视图的结果,由于默认视图名生成。)
一直出现404或者500
换了转发方法,可以打开单独的jsp了(静态资源没有放行),但是不能打开整合的jsp(但是在url直接输入jsp的文件名还是访问不了)
package com.keyi.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Controller
public class PageController {
@RequestMapping("{uri}")
public ModelAndView main(@PathVariable String uri) {
ModelAndView modelAndView = new ModelAndView(uri);//设置对应JSP的模板文件
return modelAndView;
}
}
我怀疑是springboot的底层没有把所有的jsp放在ModelAndView里。
暂时跳过吧。
ps:在文档里看到:
其实我们的web项目在创建的时候就自动的在web目录下创建了密室,就是
WEB-INF文件夹。也就是说WEB-INF文件夹下的资源浏览器是无法直接访问
的,必须通过内部请求转发才能访问。
之后,把所有的jsp从WEB-INF里移动出来之后,就能根据文件名访问了,但是用视图解析器的方法就访问失败了,另外静态资源也没有放行。
需要复习的地方:
1.视图解析器
2.模板
3.restful格式
等等
文档一定要好好看!!!!认真仔细看!