SpringMVC4.3.9+Spring4.3.9+Hibernate5.2.11
1.项目目录结构:
2.lib目录:
antlr-2.7.7.jar
asm-3.3.jar
asm-commons-3.3.jar
asm-tree-3.3.jar
c3p0-0.9.5.2.jar
classmate-1.3.0.jar
commons-fileupload-1.3.2.jar
commons-io-2.2.jar
commons-lang3-3.2.jar
commons-logging-1.1.3.jar
dom4j-1.6.1.jar
druid-1.0.9.jar
freemarker-2.3.22.jar
hibernate-c3p0-5.2.11.Final.jar
hibernate-commons-annotations-5.0.1.Final.jar
hibernate-core-5.2.11.Final.jar
hibernate-jpa-2.1-api-1.0.0.Final.jar
jandex-2.0.3.Final.jar
javassist-3.20.0-GA.jar
jboss-logging-3.3.0.Final.jar
jboss-transaction-api_1.2_spec-1.0.1.Final.jar
jstl-1.2.jar
log4j-1.2.17.jar
log4j-api-2.3.jar
log4j-core-2.3.jar
mchange-commons-java-0.2.11.jar
mysql-connector-java-5.0.5.jar
ognl-3.0.19.jar
slf4j-api-1.5.8.jar
slf4j-log4j12-1.5.8.jar
spring-aop-4.3.9.RELEASE.jar
spring-aspects-4.3.9.RELEASE.jar
spring-beans-4.3.9.RELEASE.jar
spring-context-4.3.9.RELEASE.jar
spring-core-4.3.9.RELEASE.jar
spring-expression-4.3.9.RELEASE.jar
spring-jdbc-4.3.9.RELEASE.jar
spring-orm-4.3.9.RELEASE.jar
spring-tx-4.3.9.RELEASE.jar
spring-web-4.3.9.RELEASE.jar
spring-webmvc-4.3.9.RELEASE.jar
standard-1.1.2.jar
xwork-core-2.3.32.jar
3.数据库表信息:
4.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>SpringMVCSpringHibernate</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 加载spring容器 -->
<context-param>
<!-- spring 的配置文件加载 -->
<!-- classpath: 代表的是类的根路径,即:src目录 -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 解决post乱码 -->
<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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- springmvc的前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation, springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 拦截规则 -->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
5.index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
webcontent下的index.jsp
<a href="${pageContext.request.contextPath}/student/index">去安全首页</a>
</body>
</html>
6.jsp文件夹中的jsp文件:
6.1addStudent.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
addStudent.jsp
<br>
<form action="${pageContext.request.contextPath}/student/addStudent" method="post">
<input type="text" name="name"><br>
<input type="text" name="age"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
6.2AdminIndex.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
jsp文件夹下index.jsp!
<a href="${pageContext.request.contextPath}/student/addAll">添加</a>
<a href="${pageContext.request.contextPath}/student/getAll">显示</a>
</body>
</html>
6.3error.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
错误界面!
<br>
<a href="${pageContext.request.contextPath}/student/index">去安全首页</a>
</body>
</html>
6.4studentList.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
studentList.jsp
<br>
<table border="1">
<tr>
<td>id</td>
<td>name</td>
<td>age</td>
</tr>
<c:forEach items="${sessionScope.student}" var="stu">
<tr>
<td>${stu.id }</td>
<td>${stu.name }</td>
<td>${stu.age }</td>
</tr>
</c:forEach>
</table>
</body>
</html>
6.5success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
成功界面!
<br>
<a href="${pageContext.request.contextPath}/student/index">去安全首页</a>
</body>
</html>
7配置文件
7.1springmvc.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 只扫描base-package指定包下@Controller注解 -->
<context:component-scan base-package="com.wg.controller"></context:component-scan>
<!-- 注解驱动,可以将请求参数绑定到控制器参数 -->
<mvc:annotation-driven />
<!-- 前后缀 -->
<!-- 配置jsp视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
7.2spring-hibernate.xml(即spring.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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/p
http://www.springframework.org/schema/p/spring-p-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
<!-- 使用注解注入 -->
<!-- 组件扫描器 controller、service、dao 里面对象的注入到依赖扫描器-->
<context:component-scan base-package="com.wg.*"></context:component-scan>
<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 数据库连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="driverClassName" value="${jdbc.driver}" />
<property name="maxActive" value="10" />
<property name="minIdle" value="5" />
</bean>
<!-- 配置创建sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 引入数据库配置 -->
<property name="dataSource" ref="dataSource" />
<!-- 指定Hibernate核心配置文件 -->
<!-- <property name="configLocations" value="classpath:hibernate.cfg.xml"
/> -->
<!-- 如果不使用核心配置文件,可将Hibernate的核心配置文件内容放到spring中配置 -->
<!-- 第二部分、配置Hibernate -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop><!-- native:每次运行都将重新创建数据库表 -->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop>
</props>
</property>
<!-- 第三部分:实体类配置文件(必须的) -->
<!-- 包扫描的方式加载注解类(推荐) -->
<property name="packagesToScan">
<list>
<value>com.wg.entity</value>
</list>
</property>
</bean>
<!-- 开启属性上的注解 -->
<context:annotation-config />
<!-- ==========================配置事务 start=================================== -->
<!-- 1、配置事务管理器: org.springframework.orm.hibernate5.HibernateTransactionManager -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 2、开启事务注解 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- 3、在事务的具体使用方法的类上添加注解(一般在业务逻辑层【service】添加):@Transactional -->
<!--============================ 配置事务 end=========================================== -->
</beans>
8.配置引用文件
8.1db.properties
#数据库基本信息配置
jdbc.url=jdbc:mysql://127.0.0.1:3306/ssh?characterEncoding=utf8&useUnicode=true
jdbc.driver=com.mysql.jdbc.Driver
jdbc.username=root
jdbc.password=123456
8.2log4j.properties
#
log4j.rootLogger=DEBUG,A1
#
log4j.logger.org.mybatis = DEBUG
#
log4j.appender.A1=org.apache.log4j.ConsoleAppender
#
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
#
log4j.appender.A1.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c]-[%p] %m%n