SSM项目整合,适合新手

时间:2021-01-15 00:59:51

SSM项目开发环境搭建,适合初学SSM的入门人员阅读和使用。

一.环境的准备

1.JDK的安装 2.tomcat安装 3.maven的安装及settings.xml的配置 4.eclipse或者Idea的安装 5.mysql的安装 6.在eclipse中对jdk、tomcat、maven、编码(java和web)的配置

二.进行数据库的设计

1.利用PowerDesigner对数据库进行设计 2.将PD生成的SQL语句导入到数据库中 3.对数据库表进行数据的添加

三.前端页面的简单设计

login.html、index.html.、index.jsp等等

四.创建maven项目引入相关依赖

1.项目名:项目名称_ssm 2.在pom文件中引入相关依赖,项目所需依赖 3.利用节点对jar包版本进行统一管理

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">  
<modelVersion>4.0.0</modelVersion>
<groupId>cn.tedu</groupId>
<artifactId>tes_ssm</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.3.7.RELEASE</spring.version>
<jackson.version>2.8.1</jackson.version>
<mybatis.version>3.4.5</mybatis.version>
<mybatis.spring.version>1.3.1</mybatis.spring.version>
<mysql.version>5.0.8</mysql.version>
<druid.version>1.0.14</druid.version>
<fileupload.version>1.3.2</fileupload.version>
<poi.version>3.13</poi.version>
<shiro.version>1.2.3</shiro.version>
<aspectjweaver.version>1.8.7</aspectjweaver.version>
</properties>
<dependencies>
<!-- servlet 依赖 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<!-- spring context -->
<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 jdbc 为了添加spring的声明式事务 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring 的单元测试-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring mvc 辅助依赖包 jackson-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.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>${mybatis.spring.version}</version>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- alibaba datasource -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!-- 文件上传 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>${fileupload.version}</version>
</dependency>
<!-- 导出微软的文档 excel表格 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${poi.version}</version>
</dependency>
<!-- apache shiro -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-all</artifactId>
<version>${shiro.version}</version>
</dependency>
<!-- spring aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency> <groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectjweaver.version}</version>
</dependency>
</dependencies>
</project>

五.相关配置文件

web.xml、spring.xml、spring_mybatis.xml、springmvc.xml、mybatisconfig.xml

5.1.web.xml文件

web.xml文件加载的顺序: 全局初始化参数context-param-->2.监听器listener-->3.过滤器filter-->4.Servletservlet(springmvc)。

<?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</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>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:conf/spring.xml;
classpath:conf/spring_mybatis.xml;
classpath:conf/spring_shiro.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>

<!-- 配置shiro的过滤器 注意此处的filtername跟spring配置文件中的bean id名称一致 -->
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<!-- 将shiro的声明周期交给web容器管理 -->
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- spring mvc的入口 加载spring mvc 前端控制器 restful -->
<!-- restful模式,必须注意在spring_mvc.xml中配置,刨除静态资源 -->
<servlet>
<servlet-name>dispatcher_restful</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:conf/spring_mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher_restful</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

5.2.Springmvc.xml文件

静态资源的添加,扫描包及其注解,spring mvc文件的上传。

<?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模式下,添加静态资源 -->
<mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
<mvc:resources location="/" mapping="/**"></mvc:resources>

<!-- 对springmvc层的包进行扫描, -->
<context:component-scan base-package="cn.tedu.controller"></context:component-scan>


<!-- 扫描 spring mvc的相关注解 @RequestMapping @ResponseBody -->
<mvc:annotation-driven></mvc:annotation-driven>

<!-- spring mvc 文件上传 -->
<bean
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--能配置多少个property,可以查文档和查询源代码 -->
<!--最大上传文件的大小 -->
<property name="maxUploadSize" value="8388608"></property>
<property name="resolveLazily" value="true"></property>
</bean>
</beans>

5.3.Spring.xml文件

属性文件的加载,扫描Service包,util包

<?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的配置文件使用的 -->
<context:property-placeholder
location="classpath:conf/mysql.properties"/>
<!-- 加载属性文件,用于在属性文件内通过@Value注解注入java对象中 -->
<util:properties
location="classpath:conf/page.properties"></util:properties>

<!-- 扫描service包,实例化带有@Service注解 -->
<context:component-scan base-package="cn.tedu.service"></context:component-scan>
<!-- 扫描util包,实例化带有@Component注解 -->
<context:component-scan base-package="cn.tedu.util"></context:component-scan>
</beans>

5.4.Spring_mybatis.xml文件

数据库连接池的配置、mybatis配置读取配置文件、扫描xxxMapper对象,创建代理对象、spring事务管理、事务的注解扫描

<?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
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>5</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
class="org.mybatis.spring.SqlSessionFactoryBean"
p:dataSource-ref="alibabaDataSource"
p:configLocation="classpath:conf/configuration.xml"
p:mapperLocations="classpath:mapper/*.xml">
</bean>
<!-- 扫描所有XXXMapper的对象 创建代理对象,对象名是接口名字的第一个字母小写 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
p:basePackage="cn.tedu.dao"
p:sqlSessionFactoryBeanName="sqlSessionFactory">

</bean>

<!-- spring 事务管理开始 -->

<!-- Spring jdbc 的事务管理器 spring提供的切面-->
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="alibabaDataSource"/>
</bean>

<!-- 扫描事务有关的注解@Transactional -->
<tx:annotation-driven transaction-manager="txManager"/>

<!-- Spring事务管理结束 -->
</beans>

5.5.configuration.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>
<typeAliases>
<typeAlias type="cn.tedu.entity.User" alias="User"/>
<typeAlias type="cn.tedu.entity.Role" alias="Role"/>
<typeAlias type="cn.tedu.entity.UserRole" alias="UserRole"/>
<typeAlias type="cn.tedu.vo.Page" alias="Page"/>
<typeAlias type="cn.tedu.entity.Course" alias="Course"/>
</typeAliases>
</configuration>