前言
本博客仅做学习笔记,如有侵权,联系后即刻更改
科普:
SpringMVC项目创建
- 新建项目或者模型
- 选择maven方式
项目结构目录
- 在main文件夹下建java文件夹
- Java文件夹下建com.hnucm.springmvc
- 该文件夹下建MVC的三层架构: controller,dao,service
dao、service层每个类对应一个接口和实现类
- controller:收发
package com.hnucm.springmvc.controller;
import com.hnucm.springmvc.service.UserService;
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.servlet.ModelAndView;
@Controller
public class LoginController {
@Autowired
UserService userService;
@RequestMapping("/login")
public String login(Model model) {
// 加载Spring容器
// TomCat启动时 -> 自动加载web.xml
model.addAttribute("name", userService.findUserName());
return "login";
}
@RequestMapping("/register")
public String register() {
// 加载Spring容器
// TomCat启动时 -> 自动加载web.xml
return "login";
}
}
- dao:数据库相关操作
- service:处理数据
- 在resources文件夹下建spring-servlet.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 https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 开启Spring注解-->
<context:component-scan base-package="com.hnucm.springmvc"/>
<!-- 视图解析器,简化访问路径-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
- 在webapp文件夹
- WEB-INF文件夹下建jsp文件夹
将自己的jsp网页放在该文件下- WEB-INF文件夹下的web.xml内
SpringMVC的封装的servlet
<?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">
<!-- 加载spring配置文件-->
<!-- DispatcherServlet springmvc框架封装的 Servlet-->
<servlet>
<servlet-name>zjc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>zjc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
- 在pom.xml内引入依赖
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>SpringMvc</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>SpringMvc Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.22</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
</dependencies>
<build>
<finalName>SpringMvc</finalName>
</build>
</project>
数据库连接和操作
- 引入依赖包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.22</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
- 在spring-servlet.xml添加数据库配置
<!-- DriverManagerDataSource -> 打开关闭连接 处理异常-->
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
<!-- 数据库驱动-->
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<!-- 数据库连接url-->
<property name="url" value="jdbc:mysql://localhost:3306/class2?characterEncoding=utf8&serverTimezone=UTC"/>
<!-- 用户名和密码-->
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
- 创建实体类
package com.hnucm.springmvc.pojo;
public class Person {
private int id;
private int age;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
总结
小小励志
有些事你现在不做,一辈子都不会做了。
如果你想做一件事,全世界都会为你让路。
《搭车去柏林》