基于Eclipse的SSM框架配置文件

别等时光非礼了梦想. 提交于 2020-03-12 04:06:03

基于Eclipse的SSM框架配置文件

说明:该项目是基于Eclipse的SSM框架

1、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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.zhiyou</groupId>
  <artifactId>ssm-maven-crud</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <!-- 配置各种依赖 -->
  <dependencies>
  	
  <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-webmvc</artifactId>
	    <version>4.3.7.RELEASE</version>
	</dependency>
	
	<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-jdbc</artifactId>
	    <version>4.3.7.RELEASE</version>
	</dependency>
	<!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-aspects</artifactId>
	    <version>4.3.7.RELEASE</version>
	</dependency>
	
	<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
	<dependency>
	    <groupId>org.mybatis</groupId>
	    <artifactId>mybatis</artifactId>
	    <version>3.4.2</version>
	</dependency>
	
	<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
	<dependency>
	    <groupId>org.mybatis</groupId>
	    <artifactId>mybatis-spring</artifactId>
	    <version>1.3.1</version>
	</dependency>
	
	<!-- https://mvnrepository.com/artifact/c3p0/c3p0 -->
	<dependency>
	    <groupId>c3p0</groupId>
	    <artifactId>c3p0</artifactId>
	    <version>0.9.1.2</version>
	</dependency>
	
	<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
	<dependency>
	    <groupId>mysql</groupId>
	    <artifactId>mysql-connector-java</artifactId>
	    <version>8.0.13</version>
	</dependency>
	
	<!-- https://mvnrepository.com/artifact/jstl/jstl -->
	<dependency>
	    <groupId>jstl</groupId>
	    <artifactId>jstl</artifactId>
	    <version>1.2</version>
	</dependency>
	<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
	<dependency>
	    <groupId>javax.servlet</groupId>
	    <artifactId>javax.servlet-api</artifactId>
	    <version>3.0.1</version>
	    <scope>provided</scope>
	</dependency>
	
	<!-- https://mvnrepository.com/artifact/junit/junit -->
	<dependency>
	    <groupId>junit</groupId>
	    <artifactId>junit</artifactId>
	    <version>4.12</version>
	    <scope>test</scope>
	</dependency>
	
  </dependencies>
</project>

2、 在src / main / resources下,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"
	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.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

	<!-- spring的配置文件 -->
	<!-- spring 配置文件  主要配置和业务逻辑有关的 -->
	<context:property-placeholder location="classpath:dbconfig.properties"/>
	<!-- 数据源 -->
	<bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
	  <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
	  <property name="driverClass" value="${jdbc.driverClass}"></property>
	  <property name="user" value="${jdbc.user}"></property>
	  <property name="password" value="${jdbc.password}"></property>
	</bean>
	
	<!-- 扫描包 -->
	<!-- 只需要扫描service  和  dao -->
	<context:component-scan base-package="com.zhiyou">  
	 <!-- 不能扫描控制器 -->   
	<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> 
	</context:component-scan> 

	
	<!-- 配置和mybatis的整合 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	  <!-- 指定mybatis 全局配置文件的位置 -->
	  <property name="configLocation" value="classpath:mybatis-config.xml"></property>
	  <property name="dataSource" ref="pooledDataSource"></property>
	  <!-- 指定mybatis  mapper文件的位置 -->
	  <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
	</bean>

	<!-- 扫描接口 -->
	<!-- 找到接口后 通过动态代理  让框架实例化接口实现类 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		 <!-- 扫描所有的dao接口 -->
		 <property name="basePackage" value="com.zhiyou.dao"></property>
	</bean>


	<!-- 事务控制的配置 -->
	<!-- 用的是aop -->

	<!-- transactionManager  是事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
      <!-- 控制数据源 -->
      <property name="dataSource" ref="pooledDataSource"></property>
    </bean>
    <aop:config>
    <!-- 切入点表达式   只要满足这个表达式的  都是切入点  切入点是可以织入事务的-->
    <!-- 事务的织入一定是在service层进行的;而不是dao层 -->
    <!-- * com.zhiyou.service..*(..)  此包下所有子包,所有类,任意参数的所有方法 -->
        <aop:pointcut expression="execution(* com.zhiyou.service..*(..) )" id="txPoint"/>
        <!-- 配置事务 -->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
     </aop:config>
     <!-- 配置事务增强  事务如何切入 -->
     <tx:advice id="txAdvice">
     
        <tx:attributes>
        <!-- 所有service层的方法都是事务方法 -->
          <tx:method name="*"/>
          <!-- 以get开头的所有方法 -->
          <tx:method name="get*" read-only="true"/>
        </tx:attributes>
     </tx:advice>

</beans>

3、 在src / main / resources下,dbconfig.properties.xml

jdbc.jdbcUrl=jdbc:mysql://localhost:3306/ssm-crud
jdbc.driverClass=com.mysql.cj.jdbc.Driver
jdbc.user=root
jdbc.password=root

4、 在src / main / resources下,mybatis-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
 <configuration>
 
 </configuration>

5、 在WebContent / WEB-INF下,WEB-INF / dispatcherServlet-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"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		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-4.3.xsd">
	<context:component-scan base-package="com.zhiyou" use-default-filters="false">
	<!-- 因为是spring mvc 只需要扫描控制器 -->
	  <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>
	<!-- 配置视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	  <property name="prefix" value="/WEB-INF/views/"></property>
	  <property name="suffix" value=".jsp"></property>
	</bean>
	
<!-- 两个标准配置 -->
  <!-- 将springmvc 不能处理的请求交给tomcat  例如静态资源 -->
  <!-- 静态资源:js文件  css样式  图片... -->
  <!-- 静态资源是不需要经过DispatcherServlet的  -->
  <!-- 此注解的作用就是把这些静态资源交给tomcat处理 -->
  <mvc:default-servlet-handler/>
   <!-- 支持spring mvc 更高级的功能  例如 jsr303校验  映射动态请求  -->
   <mvc:annotation-driven/>

</beans>

6、 在WebContent / WEB-INF下, 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">

<!-- 配置spring容器 -->

<!-- needed for ContextLoaderListener -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<!-- 指定spring的配置文件的位置 -->
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>

	<!-- Bootstraps the root web application context before servlet initialization -->
	<!-- ContextLoaderListener 负责加载spring的容器 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
	
	<!-- 配置springmvc的 前端控制器 -->
	
	<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
	<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:springmvc.xml</param-value>
		</init-param> -->
		<load-on-startup>1</load-on-startup>
	</servlet>

	<!-- Map all requests to the DispatcherServlet for handling -->
	<servlet-mapping>
		<servlet-name>dispatcherServlet</servlet-name>
		<!-- 拦截所有请求 -->
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	
	<!-- 编码过滤器 -->
	<!-- <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> -->
	
	<!-- rest风格  编程 不是必须的-->
	<!-- <filter>
		<filter-name>hiddenHttpMethodFilter</filter-name>
		  <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
		</filter>
	<filter-mapping>
		<filter-name>hiddenHttpMethodFilter</filter-name>
		  <url-pattern>/*</url-pattern>
	</filter-mapping> -->
	
	
	
	
</web-app>

最后,页面的问题:

index.jsp 放到WebContent下

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
hello  ssm
<br/>
<a href="hello">hello</a>
</body>
</html>

WebContent / WEB-INF / views下放需要跳转的jsp页面

例如:success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
成功页面  success.jsp
</body>
</html>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!