1.新建项目:
一般的web程序最基础的就是添加web依赖,包括Spring和SpringMVC相关依赖,上面为Spring Boot版本
以下是项目刚创建时的文件结构:
默认的pom.xml有以下配置:
2.基础配置:
application.properties基础配置:
#所使用的端口
server.port=8080
#servlet上下文路径
server.servlet.context-path=/susu
JSP配置:
如果是要用到JSP文件的话,还需要以下配置加到依赖中:
<!--引入 Spring Boot 内嵌的 Tomcat 对 JSP 的解析包,不加解析不了 jsp 页面-->
<!--如果只是使用 JSP 页面,可以只添加该依赖-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<!--如果要使用 servlet 必须添加该以下两个依赖-->
<!-- servlet 依赖的 jar 包-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
</dependency>
<!--如果使用 JSTL 必须添加该依赖-->
<!--jstl 标签依赖的 jar 包 start-->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
SpringBoot 要求 jsp 文件必须编译到指定的 META-INF/resources 目录下才能访问,否则访问不到。其实官方已经更建议使用模板技术。
为此需要在build中加入以下配置:
<resources>
<resource>
<!--源文件位置-->
<directory>src/main/webapp</directory>
<!--指定编译到 META-INF/resources,该目录不能随便写-->
<targetPath>META-INF/resources</targetPath>
<!--指定要把哪些文件编译进去,**表示 webapp 目录及子目录,*.*表示所有文件-->
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
在 application.properties 文件配置 Spring MVC 的视图展示为jsp,这里相当于 Spring MVC 的配置:
#相当于SpringMVC的视图解析器配置
#表示src/main/webapp
Spring.mvc.view.prefix=/jsp/
Spring.mvc.view.suffix=.jsp
在main目录下创建webapp资源文件夹,最好吧META-INF也创建,若右键没有创建JSP选项则按下面的做法:
这样webapp中的JSP文件就能被访问到了
Mybatis配置 :
需要用到Mybatis需要添加以下依赖:
<!--Mybatis 依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<!--mysql connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.27</version>
</dependency>
在 application.properties 文件配置连接数据库相关内容:
#数据库相关
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/spring_boot
spring.datasource.username=root
spring.datasource.password=root
3.项目开发:
在Application主程序的下级目录下创建以下的包(必须是下级目录否则扫描不到Bean):
此时目录为这样:
在数据库创建表text,然后在entity中创建相对应的实体类Student:
@Component
public class Student {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
在dao中创建mybatis映射文件StudentMapper,使其生效有两种方法
使用@Mapper标签:
会自动为mapper接口找相应的mapper映射
@Mapper
public interface StudentMapper {
Student queryStudentById(int id);
}
再在resources下创建mapper包,在里面创建Student-Mapper.xml,如下配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.suboheng.demo.dao.StudentMapper">
<select id="queryStudentById" parameterType="int" resultType="com.suboheng.demo.entity.Student">
SELECT * FROM text WHERE id = #{id}
</select>
</mapper>
有几点需要注意:
-
namespace必须对应mapper接口的全路径
-
//<select>标签内id在mapper接口里必须要有对应的方法,parameterType必须为方法的传入参数
-
resultType在没有给实体类取别名的时候必须为全路径
在application.properties中配置对应查找路径,否则找不到mapper文件
#mapper查找路径
mybatis.mapper-locations=classpath:/mapper/*.xml
注:
classpath是系统默认资源路径,可以是resources/,也可以是之前的/webapp/,要自己添加classpath路径只需在pom.xml文件的build标签加入如下即可,如下是将java目录下的.xml作为资源
<resource>
<!--源文件位置-->
<directory>src/main/java</directory>
<!--指定要把哪些文件编译进去,**表示 java 目录及子目录,*.xml表示所有xml文件-->
<includes>
<include>**/*.xml</include>
</includes>
</resource>
在service包中创建StudentService接口和impl包内创建其实现类
public interface StudentService {
Student SelectStudentById(int id);
}
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
StudentMapper studentMapper;
@Override
public Student SelectStudentById(int id) {
return studentMapper.queryStudentById(id);
}
}
在controller包中创建StudentController类
@Controller
public class StudentController {
@Autowired
StudentService studentService;
//@ResponseBody标签使其返回返回值的String,不加这个
//标签则会去寻找相对应返回值的.jsp文件
/*这个程序的顺序是controller调用service的方法,service
* 再调用mapper接口的方法,接口映射到mapper配置文件调用相应方法的sql*/
@RequestMapping(value = "/student")
public @ResponseBody Object student(){
return studentService.SelectStudentById(1);
}
}
至此,简单的SSM+Spring Boot Demo配置完毕,完整目录结构如下:
4.运行看到成果:
输入如下url,可以看到index.jsp的欢迎界面,/susu是配置文件中配置的上下文路径
输入如下url,可以看到controller从数据库中读取到的数据,表示程序运行成功
另一种方法配置Mapper,不用@Mapper
注销掉dao中StudentMapper中的@Mapper标签,在DemoApplication.java中加入@MapperScan("com.suboheng.demo.dao"),即可自动扫描到dao目录下的mapper,注意,这样可能导致service中的@Autowired报红,但不是错误,不影响运行
@SpringBootApplication
@MapperScan("com.suboheng.demo.dao")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
--end