免费开发资源下载请见如下地址
本人正在学习框架,利用一个星期的时间,自己用所学的知识做了这个小项目,仅供学习探讨,有什么可以改进的意见,可以联系我,多多指教,下面是我部分项目代码
1、SSM框架的搭建,以及相应的xml文件
mybatis的SqlMapConfig.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>
<!-- 第一步,不能放在后面,外部的properties属性引入外部配置文件 -->
<!-- <properties resource="config/db.properties">
</properties> -->
<!-- 第二步,不能颠倒次序,全局配置参数,日志配置文件在src下 -->
<settings>
<setting name="logImpl" value="LOG4J"/>
<!-- 自动映射,返回值只需要手动添加不一致的属性映射,其他都可以直接映射 -->
<setting name="autoMappingBehavior" value="PARTIAL"/>
<!-- 打开延迟加载 -->
<setting name="lazyLoadingEnabled" value="true"/>
<!-- 将积极加载改成消极加载(即按需加载) -->
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
<!-- 第三步,别名设置 -->
<typeAliases>
<!-- 批量扫描别名 -->
<package name="cn.com.sm.po"/>
</typeAliases>
<!--加载映射文件 -->
<mappers>
<!-- <mapper resource="sqlmap/UserMapper.xml" />
-->
<!-- url为加载的映射文件的全路径 -->
<!-- <mapper usr=""/> -->
<!-- 通过mapper接口加载映射文件
遵循一些规范:需要将mapper接口的类名和xxxMapper.xml文件名称保持一致,且在一个目录中
上述规范的前提是,使用的是mapper代理的方法
-->
<!-- <mapper class="mapper.SelcetMapper"/> -->
<!-- 批量的加载mapper,推荐使用
mapper接口的包名,mybatis自动扫描包下边的所有mapper接口进行加载
遵循一些规范:需要将mapper接口的类名和xxxMapper.xml文件名称保持一致,且在一个目录中
上述规范的前提是,使用的是mapper代理的方法
-->
<!-- <package name="cn.com.sm.mapper"/>
-->
</mappers>
</configuration>
spring框架相应的xml
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:mvc="http://www.springframework.org/schema/mvc"
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-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-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/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- 编写配置信息位置 -->
<!-- 扫描controller包 -->
<context:component-scan base-package="cn.com.sm.controller"/>
<mvc:annotation-driven validator="validator"></mvc:annotation-driven>
<!-- 视图解析器,默认InternalResourceViewResolver(根据模板名和位置解析视图)
XMLViewResolver(从xml配置文件解析视图)
ResourceBundleViewResolver(从properties资源集解析视图)
-->
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix" value="/WEB-INF/web/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
-->
<!-- 自定义参数类型绑定 -->
<!-- <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
-->
<!-- 自定义的转换器 -->
<!--<property name="converters">
<list> -->
<!-- 日期类型的转化 -->
<!-- <bean class="cn.com.sm.controller.converter.CustomDateConverter"/>
</list>
</property>
</bean>-->
<!-- 现今使用最多的配置手段,简洁方便,使用默认代替手动配置 -->
<!-- 校验器 -->
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="providerClass" value="org.hibernate.validator.HibernateValidator"/>
<property name="validationMessageSource" ref="messageSource"/>
</bean>
<!-- 校验错误信息配置文件 -->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<!-- 要在cofig中创建ProductValidationMesssages.properties用来配置检验错误信息 -->
<value>classpath:ProductValidationMessages</value>
</list>
</property>
<property name="fileEncodings" value="utf-8"/>
<property name="cacheSeconds" value="120"/>
</bean>
<!-- 在jsp的表单中传输编码为multipart/form-data 传输的数据是能够被request接收到 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--设置上传文件的大小 为5M -->
<property name="maxUploadSize">
<value>5242880</value>
</property>
</bean>
<!-- 全局异常处理器
只要实现HandlerExceptionResolver接口的都是全局异常处理器
-->
<!-- <bean class="cn.com.sm.exception.CustomExceptionResolver"/>
-->
<!-- 访问静态资源,解析静态资源 -->
<!--
<mvc:resources location="/js/" mapping="/js/**/" />
<mvc:resources location="/img/" mapping="/img/**/" />
<mvc:resources location="/css/" mapping="/css/**/" />
-->
</beans>
applicationContext-dao.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:mvc="http://www.springframework.org/schema/mvc"
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-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-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/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 数据源,使用DBCP -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxActive" value="10"/>
<property name="maxIdle" value="5"/>
</bean>
<!-- sqlSessinFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 加载Mybatis的配置文件 -->
<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"/>
<!-- 数据源 -->
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 原始Dao接口 -->
<!-- <bean id="userDao" class="cn.com.sm.dao.UserImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 指定扫描的包名,如果扫描多个包,包之间使用半角逗号隔开 -->
<property name="basePackage" value="cn.com.sm.mapper" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
</beans>
applicationContext-service.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:mvc="http://www.springframework.org/schema/mvc"
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-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-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/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- 用户管理的Service -->
<bean id="productServiceimp" class="cn.com.sm.service.imp.ProductServiceImp"/>
<bean id="userServiceimp" class="cn.com.sm.service.imp.UserServiceImp"/>
<bean id="shoppingServiceimp" class="cn.com.sm.service.imp.ShoppingServiceImp"/>
</beans>
applicationContext-transaction.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:mvc="http://www.springframework.org/schema/mvc"
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-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-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/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- 事务管理器
Mybatis操作数据库事务控制,Spring使用jdbc的事务控制
-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 数据源
dataSource在applicationContext-dao.xml中配置了
-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 事务通知 -->
<tx:advice id="txAdivce" transaction-manager="transactionManager">
<tx:attributes>
<!-- 传播行为 -->
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- AOP -->
<aop:config>
<aop:pointcut expression="execution(* cn.com.sm.service.imp.*.*(..))" id="txPointcut"/>
<aop:advisor advice-ref="txAdivce" pointcut-ref="txPointcut"/>
</aop:config>
</beans>
主要控制处理器的结构如下
数据库的配置,结构
product产品表结构如下
seach存储模糊查询的数据(像淘宝,京东一样的搜索商品的关键字)
shopping用于存储购物车信息
user存储用户的信息,包括登陆注册。
以为代码较多,不一一发送,如果想要源码的可以联系我,下面是项目的基本结构,前端使用bootstrap搭建的,这个网上资源很多可以自己去看下