idea ,SSM框架的搭建

idea 2019
jdk1.8
tomcat7
spring3.2.13
mybatis3.4.6

第一步创建webapp项目
1.建java包,resources包并设置为根目录和资源目录
第二部复制粘贴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>com.gaze</groupId>
  <artifactId>new_ssm</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <!-- 统一管理jar包版本 -->
  <properties>
    <spring.version>3.2.13.RELEASE</spring.version>
    <slf4j.version>1.6.6</slf4j.version>
    <log4j.version>1.2.17</log4j.version>
    <mysql.version>5.1.43</mysql.version>
    <mybatis.version>3.4.6</mybatis.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>


  <!-- 项目依赖jar包 -->
  <dependencies>
    <!-- 添加spring核心依赖 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.6.8</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-orm</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>${mysql.version}</version>
    </dependency>
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.1.9</version>
    </dependency>

    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.0</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>jstl</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>
    <!--
    <dependency>-->
    <!--<groupId>taglibs</groupId>-->
    <!--<artifactId>standard</artifactId>-->
    <!--<version>1.1.2</version>-->
    <!--</dependency>
    -->
    <!-- log start -->
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>${log4j.version}</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>${slf4j.version}</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>${slf4j.version}</version>
    </dependency>
    <!-- log end -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>${mybatis.version}</version>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.3.0</version>
    </dependency>

    <dependency>
      <groupId>commons-dbcp</groupId>
      <artifactId>commons-dbcp</artifactId>
      <version>1.4</version>
    </dependency>
    <!-- 添加fastjson -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.31</version>
    </dependency>

    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.3.3</version>
    </dependency>
    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.4</version>
    </dependency>
    <!-- JSR303校验相关 -->
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-validator</artifactId>
      <version>4.3.2.Final</version>
    </dependency>

    <dependency>
      <groupId>javax.validation</groupId>
      <artifactId>validation-api</artifactId>
      <version>1.0.0.GA</version>
    </dependency>
  </dependencies>
  <!-- 添加tomcat7插件 -->
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
        <configuration>
          <path>/</path> <!-- 项目访问路径 本例:localhost:8080, 如果配置的aa,则访问路径为localhost:9090/aa -->
          <port>8080</port>
          <uriEncoding>UTF-8</uriEncoding><!-- 非必需项 -->
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
          <encoding>UTF-8</encoding>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

第三步复复制applicationContext.xml,database.pproperties,mybatis-config.xml,spring-servlet.xml文件到resources下

1·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:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
	http://www.springframework.org/schema/aop 
	http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
	http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context-3.2.xsd
	http://www.springframework.org/schema/tx 
	http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
	
	<context:component-scan base-package="com.gaze.biz"></context:component-scan>
	<context:component-scan base-package="com.gaze.entity"></context:component-scan>
   
  <context:property-placeholder location="classpath:database.properties"/>
    <!-- 一、使用druid数据库连接池注册数据源 -->
  <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
     <!-- 基础配置 -->
     <property name="url" value="${jdbc.url}"></property>
     <property name="driverClassName" value="${jdbc.driver}"></property>
     <property name="username" value="${jdbc.user}"></property>
     <property name="password" value="${jdbc.password}"></property>

     <!-- 关键配置 -->
     <!-- 初始化时建立物理连接的个数。初始化发生在显示调用init方法,或者第一次getConnection时 --> 
     <property name="initialSize" value="3" /> 
     <!-- 最小连接池数量 --> 
     <property name="minIdle" value="2" /> 
     <!-- 最大连接池数量 --> 
     <property name="maxActive" value="15" />
     <!-- 配置获取连接等待超时的时间 --> 
     <property name="maxWait" value="10000" />

     <!-- 性能配置 -->
     <!-- 打开PSCache,并且指定每个连接上PSCache的大小 --> 
     <property name="poolPreparedStatements" value="true" /> 
     <property name="maxPoolPreparedStatementPerConnectionSize" value="20" />

     <!-- 其他配置 -->
     <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> 
     <property name="timeBetweenEvictionRunsMillis" value="60000" />
     <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> 
     <property name="minEvictableIdleTimeMillis" value="300000" />
     <!--   建议配置为true,不影响性能,并且保证安全性。申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,
               执行validationQuery检测连接是否有效。 -->
     <property name="testWhileIdle" value="true" />
     <!-- 这里建议配置为TRUE,防止取到的连接不可用 ,申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。--> 
     <property name="testOnBorrow" value="true" /> 
     <!-- 归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能 -->
     <property name="testOnReturn" value="false" />
     </bean>
 <!-- sqlsessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<property name="mapperLocations">
<list>
<value>classpath:com/gaze/dao/*.xml</value>
</list>
</property>
</bean>
<!--dao 扫描  创建dao实例,并自动放入spring容器,bean的名称,接口名首字母小写-->

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> 
<property name="basePackage" value="com.gaze.dao"></property>
</bean>
<!-- 声明事务管理 -->
<!-- 配置事务管理 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property> 
</bean>
<tx:annotation-driven></tx:annotation-driven>

 
</beans>

2.database.pproperties

jdbc.url=jdbc:mysql:///book?characterEncoding=UTF-8
jdbc.driver=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=root

3.mybatis-config

<?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>
 <properties resource="database.properties"></properties> 
	<typeAliases>
		<!--<typeAlias type="com.jkk.entity.User" alias="user"/> -->
		<package name="com.gaze.entity" />
	    <package name="com.gaze.dao" />
	</typeAliases>
</configuration>

4.springmvc-servlet

<?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: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-3.2.xsd
	http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context-3.2.xsd
	http://www.springframework.org/schema/mvc 
	http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
	<!-- 拦截器 -->
	<!-- <mvc:interceptors>
       <mvc:interceptor>
       <mvc:mapping path="/add"/>
       <bean class="com.gaze.web.interceptor.LoginInterceptor"/>
       </mvc:interceptor>
    </mvc:interceptors> -->
    <context:component-scan base-package="com.gaze.controller"></context:component-scan>
	<mvc:annotation-driven>
	<mvc:message-converters>
			<bean class="org.springframework.http.converter.StringHttpMessageConverter">
					<property name="supportedMediaTypes">
						<list>
							<value>application/json;charset=UTF-8</value>
						</list>
					</property>
				</bean>
		<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
	    			<property name="supportedMediaTypes">
	    				<list>
	    					<value>text/html;charset=UTF-8</value>
	    					<value>application/json</value>
	    				</list>
	    			</property>
	    			<property name="features">
	    				<list>
	    					<value>WriteDateUseDateFormat</value>
	    				</list>
	    			</property>
    			</bean>
		</mvc:message-converters>
	</mvc:annotation-driven>
	
	
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	<property name="prefix" value="/"></property>
	<property name="suffix" value=".jsp"></property>
	</bean>
     <mvc:resources location="css/" mapping="css/**"></mvc:resources>
     <mvc:resources location="images/" mapping="images/**"></mvc:resources>
     <mvc:resources location="js/" mapping="js/**"></mvc:resources>
     <mvc:resources location="upload/" mapping="upload/**"></mvc:resources>
     <mvc:resources location="plugins/" mapping="plugins/**"></mvc:resources>
     <mvc:resources location="img/" mapping="img/**"></mvc:resources>
     <mvc:resources location="jsp/" mapping="jsp/**"></mvc:resources>
     <mvc:resources location="common/" mapping="common/**"></mvc:resources>
     <mvc:resources location="video/" mapping="video/**"></mvc:resources>
     	<!-- 全局异常处理 -->
     <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
     <property name="exceptionMappings">
     <props>
     <prop key="java.lang.RuntimeException">error</prop>
     </props>
     </property>
     </bean>	
     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
     <property name="maxUploadSize" value="5000000"></property>
     <property name="defaultEncoding" value="UTF-8"></property>
     </bean>
	</beans>

5.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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>books</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 -->
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  
  <!-- 前端控制器/核心控制器 -->
  <servlet>
  	<servlet-name>springmvc</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:springmvc-servlet.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>
 <!-- 请求编码处理的过滤器 -->
 <filter>
 	<filter-name>encoding</filter-name>
 	<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
 	<init-param>
 		<param-name>forceEncoding</param-name>
 		<param-value>true</param-value>
 	</init-param>
 	<init-param>
 		<param-name>encoding</param-name>
 		<param-value>UTF-8</param-value>
 	</init-param>
 </filter>
 <filter-mapping>
 	<filter-name>encoding</filter-name>
 	<url-pattern>/*</url-pattern>
 </filter-mapping>
  
</web-app>

这就ok了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值