目录
1、需求
2、项目准备
pom.xml
SQL
jdbc.properties
log4j.properties
applicationContext.xml
spring-mvc.xml
web.xml
3、工作流程
4、难点
项目已经上传到gitee:https://gitee.com/xzl-it/my-projects
1、需求
SpringMVC项目练习:数据后台管理系统
结合MVC和三重架构,使用Spring和SpringMVC实现前后端交互的web应用。
项目页面实现如下:
2、项目准备
Spring环境搭建步骤
① 创建工程 (Project&Module)
② 导入静态页面 (这部分后面再细讲)
③ 导入需要坐标 (pom.xml)
④ 创建包结构 (controller、service、dao、domain、utils)
⑤ 执行对应的sql语句
⑥ 创建POJO类 (User.java和Role.java)
⑦ 创建配置文件 (applicationContext.xml、spring-mvc.xml、jdbc.properties、log4j.properties)
总体环境搭建完成后,目录结构如下:
下面是项目需要的配置文件:
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><groupId>org.example</groupId><artifactId>projectForSpringMVC</artifactId><version>1.0-SNAPSHOT</version><packaging>war</packaging><properties><maven.compiler.source>11</maven.compiler.source><maven.compiler.target>11</maven.compiler.target></properties><build><plugins><plugin><groupId>org.apache.tomcat.maven</groupId><artifactId>tomcat7-maven-plugin</artifactId><version>2.2</version><configuration><port>8080</port><!--访问端口号 --><uriEncoding>UTF-8</uriEncoding></configuration></plugin></plugins></build><dependencies><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.32</version></dependency><dependency><groupId>c3p0</groupId><artifactId>c3p0</artifactId><version>0.9.1.2</version></dependency><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><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><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>5.0.5.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.0.5.RELEASE</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.0.1</version><scope>provided</scope></dependency><dependency><groupId>javax.servlet.jsp</groupId><artifactId>javax.servlet.jsp-api</artifactId><version>2.2.1</version><scope>provided</scope></dependency><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-annotations</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><dependency><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId><version>1.2</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.7</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency><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><dependency><groupId>jstl</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency></dependencies>
</project>
SQL
/* 创建数据库 'SpringMVC' */
CREATE DATABASE IF NOT EXISTS `SpringMVC` DEFAULT CHARACTER SET utf8;/* 使用数据库 'SpringMVC' */
USE `SpringMVC`;/* 创建表 'sys_role' */
DROP TABLE IF EXISTS `sys_role`;
CREATE TABLE `sys_role` (`id` bigint(20) NOT NULL AUTO_INCREMENT,`roleName` varchar(50) DEFAULT NULL,`roleDescription` varchar(50) DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;/* 插入数据到表 'sys_role' */
INSERT INTO `sys_role`(`id`,`roleName`,`roleDesc`)
VALUES (1,'院长','负责全面工作'),(2,'研究员','课程研发工作'),(3,'讲师','授课工作'),(4,'助教','协助解决学生的问题');/* 创建表 'sys_user' */
DROP TABLE IF EXISTS `sys_user`;
CREATE TABLE `sys_user` (`id` bigint(20) NOT NULL AUTO_INCREMENT,`username` varchar(50) DEFAULT NULL,`email` varchar(50) DEFAULT NULL,`password` varchar(80) DEFAULT NULL,`phoneNum` varchar(20) DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;/* 插入数据到表 'sys_user' */
INSERT INTO `sys_user`(`id`,`username`,`email`,`password`,`phoneNum`)
VALUES (1,'zhangsan','zhangsan@itcast.cn','123','13888888888'),(2,'lisi','lisi@itcast.cn','123','13999999999'),(3,'wangwu','wangwu@itcast.cn','123','18599999999');/* 创建表 'sys_user_role' */
DROP TABLE IF EXISTS `sys_user_role`;
CREATE TABLE `sys_user_role` (`userId` bigint(20) NOT NULL,`roleId` bigint(20) NOT NULL,PRIMARY KEY (`userId`,`roleId`),KEY `roleId` (`roleId`),CONSTRAINT `sys_user_role_ibfk_1` FOREIGN KEY (`userId`) REFERENCES `sys_user` (`id`),CONSTRAINT `sys_user_role_ibfk_2` FOREIGN KEY (`roleId`) REFERENCES `sys_role` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;/* 插入数据到表 'sys_user_role' */
INSERT INTO `sys_user_role`(`userId`,`roleId`)
VALUES (1,1),(1,2),(2,2),(2,3);
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/springmvc
jdbc.username=root
jdbc.password=xzlXZGK680
log4j.properties
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=c:/mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n### set log levels - for more verbose logging change 'info' to 'debug' ###log4j.rootLogger=info, stdout
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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
"><!--非自定义对象,在这里配置,自定义对象我全部使用注解--><!--1、组件扫描--><context:component-scan base-package="com.xzl"><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan><!--2、加载jdbc.properties--><context:property-placeholder location="classpath:jdbc.properties"/><!--3、配置数据源对象--><bean id="dataSource_C3p0" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${jdbc.driver}"/><property name="jdbcUrl" value="${jdbc.url}"/><property name="user" value="${jdbc.username}"/><property name="password" value="${jdbc.password}"/></bean><bean id="dataSource_Druid" 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><!--4、配置JdbcTemplate对象--><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource_Druid"/></bean>
</beans>
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.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!--1、配置mvc注解驱动,自动加载json转换工具--><mvc:annotation-driven/><!--2、配置视图资源解析器--><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/pages/"/><property name="suffix" value=".jsp"/></bean><!--3、静态资源放行--><mvc:default-servlet-handler/><!--4、组件扫描--><context:component-scan base-package="com.xzl"><context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan></beans>
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"><!--解决乱码的过滤器--><filter><filter-name>CharacterEncodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param></filter><filter-mapping><filter-name>CharacterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><!--全局的初始化参数--><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><!--Spring的监听器--><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!--SpringMVC的前端控制器--><servlet><servlet-name>DispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring-mvc.xml</param-value></init-param><load-on-startup>2</load-on-startup></servlet><servlet-mapping><servlet-name>DispatcherServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping></web-app>
3、工作流程
这里以一个请求动作进行举例:
前端对后端服务器的请求,被前端控制器全部拦截,交由对应的控制器;
控制器调用业务逻辑层代码,封装模型和视图;
业务逻辑层调用数据访问层代码,封装需要的数据进行返回;
最后返回到控制器进行全部封装完成,相应到对应的视图资源进行返回前端。
总体过程如图:
4、难点
这个过程主要是多表查询,要分清楚主键约束,即删表的时候要先删用户表,再删关系表。
还有一个就是存储过程中,用户的id是由MySQL自动生成的,存储过程获取不到,这里使用PreparedStatementCreator。
在UserDaoImpl.java中:
@Override
public Long save(final User user) {// 创建PreparedStatementCreator,用于构建PreparedStatementPreparedStatementCreator creator = new PreparedStatementCreator() {@Overridepublic PreparedStatement createPreparedStatement(Connection connection) throws SQLException {// 使用原始jdbc完成PreparedStatement的组建PreparedStatement preparedStatement = connection.prepareStatement("insert into sys_user values(?,?,?,?,?)", PreparedStatement.RETURN_GENERATED_KEYS);preparedStatement.setObject(1, null); // 设置第一个占位符为NULL,通常用于表示主键自动生成preparedStatement.setString(2, user.getUsername()); // 设置第二个占位符为用户的用户名preparedStatement.setString(3, user.getEmail()); // 设置第三个占位符为用户的电子邮件地址preparedStatement.setString(4, user.getPassword()); // 设置第四个占位符为用户的密码preparedStatement.setString(5, user.getPhoneNum()); // 设置第五个占位符为用户的电话号码return preparedStatement;}};// 创建keyHolder,用于获取自动生成的主键GeneratedKeyHolder keyHolder = new GeneratedKeyHolder();// 使用jdbcTemplate执行SQL插入,并将生成的主键保存到keyHolder中jdbcTemplate.update(creator, keyHolder);// 获得生成的主键long userId = keyHolder.getKey().longValue();// 返回当前保存用户的id,该id是数据库自动生成的return userId;
}