SSM框架(spring+springMVC+mybatis),是目前比较主流的Java EE企业级框架,适用于搭建各种大型的企业级应用系统。 该SSM整合开发项目,前后花了十几天完成,一路入坑不断,差点从入坑到放弃,最后总算成功完成了。面对各种bug,心态很重要,要有毅力,跟bug刚着刚着就解决了。下面是该项目的介绍,希望大家多多支持,如有不妥之处,望大家斧正。
功能具体实现见项目源代码
软件 | 版本 |
---|---|
JDK | 1.8 |
Tomcat | 8.5.55 |
开发工具 | Eclipse 4.6.3 |
数据库 | MySQL |
create table product(
productID int(8) auto_increment comment '商品ID' primary key,
productName varchar(20) comment '名称',
productBrand varchar(20) comment '品牌',
productModel varchar(20) comment '型号',
productPrice decimal(10,2) comment '价格',
productImage varchar(30) comment '图片',
productDes varchar(100) comment '商品描述'
);
create table message(
messageID int(8) auto_increment comment '留言ID' primary key,
messageContent varchar(300) comment '内容',
writeDate DateTime comment '发布时间',
count int comment '回复数' ,
productID int(8) comment '商品ID' ,
userID char(10) comment '留言人'
);
create table revert(
revertID int(8) auo_increment comment '回复ID' primary key,
revertContent varchar(300) comment '内容',
writeDate datetime comment '发布时间',
messageID int(8) comment '留言ID',
uesrID char(10) comment '留言人'
);
create table user(
userID char(10) comment '用户ID' primary key,
userName char(50) comment '用户名',
userPassword char(10) comment '密码',
userImage varchar(50) comment '头像',
userRole int(8) comment '用户角色'
);
登录界面(通过发送Ajax请求实现用户ID和密码的校验)
注册界面(对用户ID是否可以用、用户名是否为空、密码是否为空进行校验)
商品首页(点击左侧导航条可获取商品分类数据,通过搜索框模糊查询商品)
商品详情界面(用户可以查看商品的评论、发表评论,也可以通过搜索框查询商品)
评论详情界面(可以查看评论的所有回复,也可以回答评论)
个人中心界面(可以查看和删除评论、修改个人信息、回到首页、退出等)
管理员界面(对商品进行增删改查操作)
管理员修改、添加商品(使用bootstrap模态框完成)
<filter>
<filter-name>loginFilter</filter-name>
<filter-class>jmu.hkx.filter.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>loginFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--配置文件解析器,处理文件,将客户端上传的File文件,处理为MultipartFile-->
<bean id ="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置文件解析的编码,注意:一定要和页面的pageEncoding保持一致 -->
<property name="defaultEncoding" value="UTF-8"></property>
<!-- 设置最大上传文件大小 -->
<property name="maxUploadSize" value="999999999"></property>
</bean>
<!-- mybatis PageHelper插件 -->
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressAllComments" value="true"></property>
</commentGenerator>
<!-- 设置连接数据库的信息 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/ssm?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true&allowPublicKeyRetrieval=true"
userId="root"
password="******">
<property name="nullCatalogMeansCurrent" value="true"/>
</jdbcConnection>
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- javabean的生成策略 -->
<javaModelGenerator targetPackage="jmu.hkx.vo" targetProject="./src/main/java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 映射文件的生成策略 -->
<sqlMapGenerator targetPackage="mapper" targetProject="./src/main/resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- mapper接口的生成策略 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="jmu.hkx.dao" targetProject="./src/main/java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 设置要将数据库中的哪张表逆向生成哪一个javabean -->
<table tableName="user" domainObjectName="User"></table>
<table tableName="product" domainObjectName="Product"></table>
<table tableName="message" domainObjectName="Message"></table>
<table tableName="revert" domainObjectName="Revert"></table>
</context>
</generatorConfiguration>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>ssm</display-name>
<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>
<filter>
<filter-name>loginFilter</filter-name>
<filter-class>jmu.hkx.filter.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>loginFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<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>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>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<absolute-ordering/>
</web-app>
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:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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
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">
<!-- 扫描组件 -->
<context:component-scan base-package="jmu.hkx">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!-- 引入资源文件 -->
<context:property-placeholder location="classpath:jdbcConfig.properties" />
<!--spring整合MyBatis-->
<bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 管理mybatis操作数据库的会话对象sqlsession -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 设置mybatis核心配置文件的路径 -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!-- 设置数据源 -->
<property name="dataSource" ref="pooledDataSource"></property>
<!-- 设置映射文件路径 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
</bean>
<!-- 对应几个接口,就有几个动态代理实现类,并由spring容器管理 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="jmu.hkx.dao"></property>
</bean>
<!-- 声明事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="pooledDataSource"></property>
</bean>
<!-- 开启事务注解驱动 -->
<tx:annotation-driven />
<!-- 配置AOP增强-->
<aop:config>
<!-- 切入点表达式 -->
<aop:pointcut expression="execution(* jmu.hkx.service..*(..))" id="txPoint"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
</aop:config>
<tx:advice id="txAdvice">
<tx:attributes>
<!-- 所有切入方法都是事务方法 -->
<tx:method name="*"/>
<tx:method name="get*" read-only="true"/>
</tx:attributes>
</tx:advice>
</beans>
springmvc.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="jmu.hkx">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<!--这句不加事务不能回滚-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan>
<!-- 配置视图解析器 -->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--配置文件解析器,处理文件,将客户端上传的File文件,处理为MultipartFile-->
<bean id ="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置文件解析的编码,注意:一定要和页面的pageEncoding保持一致 -->
<property name="defaultEncoding" value="UTF-8"></property>
<!-- 设置最大上传文件大小 -->
<property name="maxUploadSize" value="999999999"></property>
</bean>
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
</beans>
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>
<settings>
<!-- 将下划线映射成驼峰,user_name映射为userName -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!-- 开启延迟加载 -->
<setting name="lazyLoadingEnabled" value="true"/>
<!-- 是否查询所有数据 -->
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
<typeAliases>
<package name="jmu.hkx.vo"/>
</typeAliases>
<!-- mybatis PageHelper插件 -->
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>
</configuration>
项目源代码
纸上得来终觉浅,觉知此事要躬行,从实践中发现问题,解决问题,最后提升自己。本项目也有很多功能没有实现,后续有待完善。希望大家多多支持!