手把手教你SSM搭建Easymall电商项目 (一)

本文主要介绍了使用SSM框架搭建Easymall电商项目第一天的内容,包括创建MavenWeb项目、更新pom.xml文件、配置Web.xml、Spring和Mybatis相关文件、数据库配置文件,复制前端JSP文件,写测试类,最后运行项目,第一天搭建成功,后续将写具体代码和需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

                               SSM搭建Easymall电商项目 (一)


                                 

目录

                               SSM搭建Easymall电商项目 (一)

                                 

                                  第一天搭建SSM框架(骨架)

一.创建MavenWeb项目

一.项目创建报错

   二.更新Maven项目的pom.xml文件

三,Web.xml配置文件

四Spring相关配置文件

           4.1Spring.xml

          4.2Spring_MVC.xml

          4.3Spring_Mybatis.xml

五.Mybatis相关配置文件

          5.1mybatis-config.xml

          5.2OrderMapper.xml(没有子文件可能会报异常)

六数据库配置文件

七.前端JSP文件复制到项目

八.写一个测试类测试项目是否能跑的通

          8.1写一个简单测试类IndexController

九运行项目

          9.1启动tomcat

          9.2控制台信息

          9.3浏览器信息

二.第一天配置项目,搭建成功,第二天写具体代码和需求


                                  第一天搭建SSM框架(骨架)

一.创建MavenWeb项目

一.项目创建报错

   二.更新Maven项目的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>cn.easymall.com</groupId>
  <artifactId>cn.easymall.com</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <properties>
	<!-- spring的版本号 -->
	<tedu.haha>18</tedu.haha>
	<spring.version>4.3.7.RELEASE</spring.version>
	<!-- mybatis的版本号 -->
	<mybatis.version>3.4.5</mybatis.version>
	<!-- log日志管理 -->
	<slf4j.version>1.7.12</slf4j.version>
	<log4j.version>1.2.17</log4j.version>
	<pagehelper.version>3.4.2</pagehelper.version>
	<jsqlparser.version>0.9.1</jsqlparser.version>
  </properties>
   <dependencies>
  	<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
			<version>3.3.2</version>
   </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
	  <groupId>org.springframework</groupId>
	  <artifactId>spring-context</artifactId>
	  <version>${spring.version}</version>
	</dependency>
	<!-- spring mvc -->
  	<dependency>
	  <groupId>org.springframework</groupId>
	  <artifactId>spring-web</artifactId>
	  <version>${spring.version}</version>
	</dependency>
	<dependency>
	  <groupId>org.springframework</groupId>
	  <artifactId>spring-webmvc</artifactId>
	  <version>${spring.version}</version>
	</dependency>
	<!-- spring mvc 辅助包 jackson -->
  	<!-- spring mvc的辅助依赖包   解析json -->
<dependency>
          <groupId>com.fasterxml.jackson.core</groupId>
          <artifactId>jackson-annotations</artifactId>
          <version>2.7.0</version>
      </dependency>
      <dependency>
          <groupId>com.fasterxml.jackson.core</groupId>
          <artifactId>jackson-core</artifactId>
          <version>2.7.5</version>
      </dependency>
      <dependency>
          <groupId>com.fasterxml.jackson.core</groupId>
          <artifactId>jackson-databind</artifactId>
          <version>2.7.0</version>
      </dependency>
      <!-- 第三方解析json -->
		<dependency>
          <groupId>com.alibaba</groupId>
          <artifactId>fastjson</artifactId>
          <version>1.2.47</version>
 </dependency>
	<!--servlet依赖 -->
	<dependency>
		<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
		<version>3.1.0</version>
		<scope>provided</scope>
	</dependency>
	<!-- Spring tx -->
  	<dependency>
	  <groupId>org.springframework</groupId>
	  <artifactId>spring-tx</artifactId>
	  <version>${spring.version}</version>
	</dependency>
  	<!-- Spring jdbc -->
  	<dependency>
	  <groupId>org.springframework</groupId>
	  <artifactId>spring-jdbc</artifactId>
	  <version>${spring.version}</version>
	</dependency>
	<!-- mybatis -->
  	<dependency>
	  <groupId>org.mybatis</groupId>
	  <artifactId>mybatis</artifactId>
	  <version>${mybatis.version}</version>
	</dependency>
  	
  	<!-- mybatis-spring -->
  	<dependency>
	  <groupId>org.mybatis</groupId>
	  <artifactId>mybatis-spring</artifactId>
	  <version>1.3.1</version>
	</dependency>
  	<!-- mysql 驱动 -->
  	<dependency>
	  <groupId>mysql</groupId>
	  <artifactId>mysql-connector-java</artifactId>
	  <version>5.0.8</version>
	</dependency>
	<!-- 阿里巴巴数据库连接池 -->
  	<dependency>
	  <groupId>com.alibaba</groupId>
	  <artifactId>druid</artifactId>
	  <version>1.0.14</version>
	</dependency>
	<!-- commons fileupload -->
    <dependency>
	  <groupId>commons-fileupload</groupId>
	  <artifactId>commons-fileupload</artifactId>
	  <version>1.3.2</version>
	</dependency>
	<!-- jstl -->
	<dependency>
	  <groupId>jstl</groupId>
	  <artifactId>jstl</artifactId>
	  <version>1.2</version>
	</dependency>
  </dependencies>
    <build>
    <finalName>1902-easymall-ssm</finalName>
  <plugins>
  	<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>
	<plugin>
		<!--配置当前工程的tomcat运行插件,内部启动web应用  -->
		<groupId>org.apache.tomcat.maven</groupId>
		<artifactId>tomcat7-maven-plugin</artifactId>
		<version>2.2</version>
		<!-- 需要配置信息,默认8080,默认项目访问路径是项目名称 -->
		<configuration>
			<port>80</port>
			<path>/</path>
			<uriEncoding>utf-8</uriEncoding>
		</configuration>
	</plugin>
  </plugins>
  </build>
</project>

三,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" version="2.5">
  <display-name>SSM-easymall-1</display-name>
  
  <!-- 全局初始化数据,spring的监听器读取此配置文件 
	多个配置文件用分号分隔 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
		          classpath:spring/spring*.xml;    	          
		</param-value>
	</context-param>
	<!-- spring容器初始化的监听器,会读取全局初始化的数据(xml文件) -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!-- spring处理中文乱码问题 -->
	<filter>
		<filter-name>encodingFilter</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>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<!-- 配置SpringMVC -->
	<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:spring/spring_mvc.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>
	<welcome-file-list>
  
    <welcome-file>index.jsp</welcome-file>
    
  </welcome-file-list>
</web-app>

四Spring相关配置文件

           4.1Spring.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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:util="http://www.springframework.org/schema/util" 
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/util 
        http://www.springframework.org/schema/util/spring-util.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
	<!-- 使用spring自带的占位符替换功能,可以实现注解方式获取属性文件中的配置值 -->
	<bean
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<!-- 允许JVM参数覆盖 -->
		<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
		<!-- 忽略没有找到的资源文件 -->
		<property name="ignoreResourceNotFound" value="true" />
		<!-- 配置资源文件 -->
		<property name="locations">
			<list>
				<value>classpath:mysql.properties</value>
				<value>classpath:demo01.properties</value>
			</list>
		</property>
	</bean>
   <!-- 加载属性文件 此种方式加载属性文件是给spring的配置文件使用的 -->
  
   <!--扫描所有com.jt的包-->
   <context:component-scan base-package="com.jt"></context:component-scan>
 	
   
 
 
 </beans>

          4.2Spring_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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:util="http://www.springframework.org/schema/util" 
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/util 
        http://www.springframework.org/schema/util/spring-util.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
	<!-- 在restful模式下,添加静态资源 如果删除导致页面没有js效果-->
	<mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
	<mvc:resources location="/" mapping="/**"></mvc:resources>
	
	<!-- 扫描spring的组件 -->
	<context:component-scan base-package="com.jt.easymall.controller"></context:component-scan>


	<!-- 驱动 spring mvc的注解 @RequestMapping@GetMapping
	 @ResponseBody @ReuqestBody @RequestParam @PathVariable-->
	<mvc:annotation-driven></mvc:annotation-driven>
	
	<!-- spring mvc处理响应的jsp,视图解析的前缀后缀 -->
	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	<!-- spring mvc 文件上传 -->
	<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<!--能配置多少个property,可以查文档和查询源代码 -->
		<!--最大上传文件的大小 -->
		<property name="maxUploadSize" value="8388608"></property>
		<property name="resolveLazily" value="true"></property>
	</bean>	
</beans>

          4.3Spring_Mybatis.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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:util="http://www.springframework.org/schema/util" 
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/util 
        http://www.springframework.org/schema/util/spring-util.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

	<!-- 数据库连接池 commons-dbcp ,c3p0,proxool,阿里巴巴druid -->
	<bean id="alibabaDataSource"
	      class="com.alibaba.druid.pool.DruidDataSource"
	      init-method="init"
	      destroy-method="close">
	    <!-- 数据库连接的4项 -->
		<property name="driverClassName">
			<value>${jdbc_driverClass}</value>
		</property>
		<property name="url">
			<value>${jdbc_url}</value>
		</property>
		<property name="username">
			<value>${jdbc_userName}</value>
		</property>
		<property name="password">
			<value>${jdbc_userPassword}</value>
		</property>
		<!-- 连接池中的最大连接数 -->
		<property name="maxActive">
			<value>${jdbc_maxActive}</value>
		</property>
		<!-- 初始化的连接数 -->
		<property name="initialSize">
			<value>2</value>
		</property>
		<!-- 获取连接的最大等待时间 -->
		<property name="maxWait">
			<value>6000</value>
		</property>
		<!-- 连接池的最大空闲 -->
		<property name="maxIdle">
			<value>2</value>
		</property>
		<!-- 连接池的最小空闲 -->
		<property name="minIdle">
			<value>2</value>
		</property>
		<!-- 自动清除无用的连接 -->
		<property name="removeAbandoned">
			<value>true</value>
		</property>
		<!-- 自动清除无用的连接的等待时间 -->
		<property name="removeAbandonedTimeout">
			<value>180</value>
		</property>
		<!-- 连接属性 -->
		<property name="connectionProperties">
			<value>clientEncoding=UTF-8</value>
		</property>      
	</bean>
	<!-- 实例化MyBatis的SqlSessionFactoryBean对象-->
	<!--mybatis配置,读取配置文件(扫描配置文件)-->
    <bean id="sqlSessionFactory"
        class="org.mybatis.spring.SqlSessionFactoryBean"
		p:dataSource-ref="alibabaDataSource"
		p:mapperLocations="classpath:mapper/*.xml">
		<!-- mybatis-config.xml -->
		<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
		<!-- 定义当前工程的别名包 -->
		<property name="typeAliasesPackage" value="com.jt.easymall.pojo"/>
    	
    </bean>
    <!-- 扫描所有XXXMapper的对象 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
        p:basePackage="com.jt.easymall.mapper"
		p:sqlSessionFactoryBeanName="sqlSessionFactory">
    </bean>
    <!-- 定义事务管理器 使用@Transactional -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="alibabaDataSource" />
	</bean>

	<!-- 定义事务策略 -->
	<tx:annotation-driven transaction-manager="transactionManager" />
	
	
</beans>

五.Mybatis相关配置文件

          5.1mybatis-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>

	<settings>
		<!-- 开启驼峰自动映射 -->
		<setting name="mapUnderscoreToCamelCase" value="true" />
		<!-- 二级缓存的总开关  redis完成缓存的功能.不使用数据库自身的缓存-->
		<setting name="cacheEnabled" value="false" />
	</settings>
</configuration>

          5.2OrderMapper.xml(没有子文件可能会报异常)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.jt.easymall.mapper.OrderMapper">
	
</mapper> 






六数据库配置文件

jdbc_driverClass=com.mysql.jdbc.Driver
jdbc_url=jdbc:mysql://localhost:3306/easymalldb?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
jdbc_userName=root
jdbc_userPassword=123456
jdbc_maxActive=5

七.前端JSP文件复制到项目

ps:前端页面会有前端人员来写 我们只处理后端逻辑即可,当然感兴趣自己写也可以(我会把项目上传到优快云)

太多就不一一发了  需要去下载即可

八.写一个测试类测试项目是否能跑的通

          8.1写一个简单测试类IndexController

package com.jt.easymall.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class IndexController {
	
	//com.jt.easymall.interceptors.UserLoginInterceptor
	@RequestMapping("/")
	public String goIndex(){
		return "index";//WEB-INF/views/index.jsp
	}
	@RequestMapping("/page/{pageName}")
	public String goPage(@PathVariable String pageName){
		return pageName;
	}
	
}

九运行项目

          9.1启动tomcat

          9.2控制台信息

          9.3浏览器信息

 

二.第一天配置项目,搭建成功,第二天写具体代码和需求

                                                    项目已上传,数据库已上传

    

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值