一、搭建步骤
- 创建maven项目,创建webapp目录
- pom文件导入相应依赖坐标
- 创建包结构(Controller、Service、Dao、entity)
- 创建web配置文件
- 编写简单示例(Controller、Service、Dao、entity)
- 创建jsp页面
- 简单测试(访问页面+访问数据)
二、开始
- 使用Idea创建maven项目,并在项目添加web模块
- 创建artifact——项目资源组合体
- 创建spring模块
- 搭建tomcat服务器
- 选择artifacts作为发布
- 导入相关依赖
<?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>
<groupId>org.example</groupId>
<artifactId>springTest</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.18</version>
</dependency>
<!-- druid数据源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- spring上下文 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<!-- web应用开发核心类 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<!--mvc框架相关类 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<!-- servlet依赖的jar包start -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<!-- jsp依赖jar包 -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.2.1</version>
<scope>provided</scope>
</dependency>
<!-- 处理 JSON 格式数据的类库 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.0</version>
</dependency>
<!-- 文件上传 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.3</version>
</dependency>
<!-- jdbc模板 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<!-- 事务支持 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
</dependencies>
</project>
- 创建项目包结构,简单Student样例
- 创建spring和web配置文件
创建applicationContext.xml文件
创建spring配置文件
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<context:component-scan base-package="com"></context:component-scan>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
继续创建JDBC配置文件——jdbc.properties
jdbc.properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/competition?serverTimezone=UTC
jdbc.username=root
jdbc.password=123
此时,resources目录下应该有两个文件applicationContext.xml和jdbc.properties
配置web.xml文件(web/WEB-INF/web.xml)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!-- 全局上下文 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- Spring前端控制器 -->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<absolute-ordering></absolute-ordering>
<!-- 监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
创建spring-mvc.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- mvc驱动器 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 静态资源权限开放 -->
<mvc:default-servlet-handler/>
<mvc:resources location="/js/" mapping="/js/**"/>
<context:component-scan base-package="com.controller"/>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
- 编写简单java示例,获取student信息
Entity层实体类
package com.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
/**
* 学生实体类
*/
public class Student {
/**
* 学生学号
*/
private String id;
/**
* 学生姓名
*/
private String name;
/**
* 登录密码
*/
private String password;
/**
* 性别
*/
private String sex;
/**
* 班级编号
*/
private String classId;
/**
* 注册时间
*/
private String createTime;
/**
* 联系电话
*/
private String contactNumber;
/**
* 修改时间
*/
private String modifyTime;
/**
* 最后登录时间
*/
private String lastTime;
}
Controller层接口
package com.controller;
import com.entity.Student;
import com.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
@Controller
@RequestMapping("/student")
public class StudentController {
@Autowired
@Qualifier("studentService")
private StudentService studentService;
@RequestMapping("/getStudentList")
@ResponseBody
public List<Student> getStudentList(){
return studentService.getStudentList();
}
@RequestMapping("/hello")
public String hello(){
return "hello";
}
@RequestMapping("/getStudent2")
@ResponseBody
public String getStudent(){
return "mmm";
}
@RequestMapping("/getStudent")
public ModelAndView getStudent(ModelAndView modelAndView){
Student student = new Student();
student.setId("1111");
System.out.println(student);
modelAndView.addObject("student",student);
modelAndView.setViewName("hello");
return modelAndView;
}
@RequestMapping("/getStudent1")
public String getStudent1(Model model){
Student student = new Student();
student.setId("1111");
System.out.println(student);
model.addAttribute("student",student);
return "hello";
}
}
- 编写简单示例(后端查询学生信息)
创建Entity层实体类Student
package com.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
/**
* 学生实体类
*/
public class Student {
/**
* 学生学号
*/
private String id;
/**
* 学生姓名
*/
private String name;
/**
* 登录密码
*/
private String password;
/**
* 性别
*/
private String sex;
/**
* 班级编号
*/
private String classId;
/**
* 注册时间
*/
private String createTime;
/**
* 联系电话
*/
private String contactNumber;
/**
* 修改时间
*/
private String modifyTime;
/**
* 最后登录时间
*/
private String lastTime;
}
创建Controller层web接口StudentController
package com.controller;
import com.entity.Student;
import com.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
@Controller
@RequestMapping("/student")
public class StudentController {
@Autowired
private StudentService studentService;
//返回学生列表list
@RequestMapping("/getStudentList")
@ResponseBody
public List<Student> getStudentList(){
return studentService.getStudentList();
}
//返回hello.jsp页面
@RequestMapping("/hello")
public String hello(){
return "hello";
}
//返回hello.jsp,并且在hello.jsp能通过${student}能获取到student对象值
@RequestMapping("/getStudent")
public ModelAndView getStudent(ModelAndView modelAndView){
Student student = new Student();
student.setId("1111");
modelAndView.addObject("student",student);
modelAndView.setViewName("hello");
return modelAndView;
}
//返回hello.jsp,并且在hello.jsp能通过${student}能获取到student对象值
@RequestMapping("/getStudent1")
public String getStudent1(Model model){
Student student = new Student();
student.setId("1111");
System.out.println(student);
model.addAttribute("student",student);
return "hello";
}
}
创建Service层接口StudentService和类StudentServiceImpl
package com.service;
import com.entity.Student;
import java.util.List;
public interface StudentService {
List<Student> getStudentList();
}
package com.service.impl;
import com.dao.StudentDao;
import com.entity.Student;
import com.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentDao studentDao;
public List<Student> getStudentList() {
return studentDao.getStudentList();
}
}
创建Dao层接口StudentDao和类StudentDaoImpl
package com.dao;
import com.entity.Student;
import java.util.List;
public interface StudentDao {
List<Student> getStudentList();
}
package com.dao.impl;
import com.dao.StudentDao;
import com.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class StudentDaoImpl implements StudentDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public List<Student> getStudentList() {
String sql = "select * from student";
List<Student> students = jdbcTemplate.query(sql,new BeanPropertyRowMapper<Student>(Student.class));
return students;
}
}
此时项目后端结构是这样的:
6. 在web目录下创建js目录存放jsp页面,创建hello.jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>mmm</title>
<script src="${pageContext.request.contextPath}/js/jquery-3.6.0.min.js"></script>
<script>
function getStudent() {
$.ajax({
url:"/student/getStudentList",
success : function(result) {//返回数据根据结果进行相应的处理
console.log(result)
}
});
}
</script>
</head>
<body id="div">
<span>${student}</span>
<input type="button" value="获取学生列表" onclick="getStudent()">
</body>
</html>
- 简单测试
项目里我配置的tomcat端口是8081,默认都是8080,跟你tomcat配置来访问
点击获取学生列表,页面请求后端接口,页面控制台打印
此时简单的spring项目就搭建好啦,这边只用到了jdbcTemplate作为与数据库增删改的工具,后续会更新spring+springMVC+mabatis项目搭建