ssm-商城项目搭建
程序员文章站
2022-04-25 07:49:58
...
目录
拿客户模块为例
1.总pom文件
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.qytx</groupId>
<artifactId>architecturel</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>architecturelweb</module>
<module>common</module>
<module>customermgr</module>
<module>goodsmgr</module>
<module>goodsmgrweb</module>
<module>cartmgr</module>
<module>ordermgr</module>
<module>storemgr</module>
</modules>
<dependencies>
<!-- mybatis分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.4</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<!-- cglib的jar 做动态代理 -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.1</version>
<exclusions>
<exclusion>
<artifactId>asm</artifactId>
<groupId>org.ow2.asm</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>3.3.1</version>
</dependency>
<!-- springMvc相关 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<!-- aspectj相关的jar -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.7.4</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.7.4</version>
</dependency>
<!-- 连接池相关的jar -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.29</version>
</dependency>
<!-- mybatis相关的jar -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.1</version>
</dependency>
<!-- spring相关 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<!-- 日志处理 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
<exclusions>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.13</version>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- mysql的jar -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.28</version>
</dependency>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- web 的jstl的包 -->
<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>
</dependencies>
</project>
customer的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>
<parent>
<groupId>com.qytx</groupId>
<artifactId>architecturel</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>customermgr</artifactId>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>com.qytx</groupId>
<artifactId>common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.tld</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.java</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.8.v20150217</version>
<configuration>
<stopKey>shutdown</stopKey>
<stopPort>9966</stopPort>
<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
<port>9999</port>
<maxIdleTime>60000</maxIdleTime>
</connector>
<scanIntervalSeconds>2</scanIntervalSeconds>
<requestLog implementation="org.eclipse.jetty.server.NCSARequestLog">
<filename>target/access-yyyy_mm_dd.log</filename>
<filenameDateFormat>yyyy_MM_dd</filenameDateFormat>
<logDateFormat>yyyy-MM-dd HH:mm:ss</logDateFormat>
<logTimeZone>GMT+8:00</logTimeZone>
<append>true</append>
<logServer>true</logServer>
<retainDays>120</retainDays>
<logCookies>true</logCookies>
</requestLog>
<webApp>
<contextPath>/customer</contextPath>
</webApp>
</configuration>
</plugin>
</plugins>
</build>
</project>
3,配置文件
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/arch1?useUnicode=true&characterEncodeing=UTF-8
jdbc.username=root
jdbc.password=root
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:c="http://www.springframework.org/schema/c"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:ehcache="http://www.springmodules.org/schema/ehcache"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springmodules.org/schema/ehcache http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd">
<!-- 引入属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 扫描包Service实现类 -->
<context:component-scan base-package="com.qytx.architecturel.customermgr.service.imp"></context:component-scan>
<!-- 配置数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" 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="initialSize" value="1" />
<property name="minIdle" value="1" />
<property name="maxActive" value="10" />
</bean>
<!-- myBatis文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
<!-- 3. Mybatis自动扫描加载Sql映射文件/接口:MapperScannerConfigurer sqlSessionFactory
basePackage:指定sql映射文件/接口所在的包(自动扫描) -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.qytx.architecturel.customermgr.dao"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
<!-- 4.事务管理:DataSourceTransactionManager dataSource 引用上面定义好的数据源 -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:annotation-driven transaction-manager="txManager" />
</beans>
mybatis-config.xml (个人感觉mybatis配置文件就是起个别名,指定mapper文件用的,还有用到分页插件、配置拦截器,改变sql)
<?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">
<!--通过这个配置文件,完成mybatis与数据库的连接 -->
<configuration>
<!-- 设置类的别名 -->
<typeAliases>
<!-- 根据包取别名,把包下面的所有类都按类名来取别名 -->
<!-- 这用可以简化代码量 -->
<typeAlias type="com.qytx.architecturel.customermgr.vo.CustomerModel" alias="CM"/>
<typeAlias type="com.qytx.architecturel.customermgr.vo.CustomerQueryModel" alias="CQM"/>
</typeAliases>
<!-- 配置分页插件 -->
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库-->
<property name="dialect" value="mysql"/>
</plugin>
</plugins>
<mappers>
<mapper resource="com/qytx/architecturel/customermgr/dao/mapper/CustomerDaoMapper.xml" />
</mappers>
</configuration>
spring-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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:ehcache="http://www.springmodules.org/schema/ehcache"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springmodules.org/schema/ehcache http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd">
<!-- 扫描controller层 -->
<context:component-scan base-package="com.qytx.architecturel.customermgr.action"/>
<!-- 启用默认配置 -->
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" /><!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑 -->
</bean>
</beans>
CustomerDaoMapper.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.qytx.architecturel.customermgr.dao.CustomerDao">
<insert id="create" parameterType="CM">
insert into tb_customer(customerId,pwd,showName,trueName,registerTime)
values(#{customerId},#{pwd},#{showName},#{trueName},#{registerTime})
</insert>
<!-- 增加学生(有参) -->
<update id="update" parameterType="CM">
update tb_customer set customerId=#{customerId},pwd=#{pwd},showName=#{showName},trueName=#{trueName},registerTime=#{registerTime}
where uuid=#{uuid}
</update>
<!-- 删除学生 -->
<select id="delete" parameterType="int">
delete from tb_customer where uuid=#{uuid}
</select>
<!-- 无条件分页查询学生 -->
<select id="getByUuid" parameterType="int" resultType="CM">
select * from tb_customer where uuid=#{uuid}
</select>
<!-- 有条件分页查询学生 -->
<select id="getByCondition" parameterType="CQM" resultType="CM">
select * from tb_customer
<where>
<if test="uuid!=null && uuid>0">
and uuid = #{uuid}
</if>
<if test="customerId!=null">
and customerId like concat('%',#{customerId} ,'%')
</if>
<if test="showName!=null ">
and showName like concat('%',#{showName} ,'%')
</if>
</where>
</select>
</mapper>
4。dao层
在公共包写的。其他类只需继承就可以
BaseDao
package com.qytx.common.dao;
import java.util.List;
public interface BaseDao<E,QE> {
public void create(E cm);
public void update(E cm);
public void delete(int uuid);
public E getByUuid(int uuid);
public List<E> getByCondition(QE qe);
}
CustomerDao
package com.qytx.architecturel.customermgr.dao;
import org.springframework.stereotype.Repository;
import com.qytx.architecturel.customermgr.vo.CustomerModel;
import com.qytx.architecturel.customermgr.vo.CustomerQueryModel;
import com.qytx.common.dao.BaseDao;
@Repository
public interface CustomerDao extends BaseDao<CustomerModel, CustomerQueryModel>{
}
5,service层
BaseService
package com.qytx.common.service;
import com.github.pagehelper.PageInfo;
public interface BaseService<E, QE> {
public void create(E cm);
public void update(E cm);
public void delete(int uuid);
public E getByUuid(int uuid);
public PageInfo<E> getByCondition(QE qe);
}
BaseServiceImpl
这个需要特别说明一下,这里的实现serviceimp需要他的继承类来提供dao,因为basedao是不可能在spring中的。这点一定要记住,还有,注解是写在实现类上的。
package com.qytx.common.service;
import com.github.pagehelper.PageInfo;
import com.qytx.common.dao.BaseDao;
public class BaseServiceImpl<E, QE> implements BaseService<E, QE>{
private BaseDao<E, QE> baseDao;
public void setBaseDao(BaseDao<E, QE> baseDao) {
this.baseDao = baseDao;
}
@Override
public void create(E cm) {
// TODO Auto-generated method stub
baseDao.create(cm);
}
@Override
public void update(E cm) {
// TODO Auto-generated method stub
baseDao.update(cm);
}
@Override
public void delete(int uuid) {
// TODO Auto-generated method stub
baseDao.delete(uuid);
}
@Override
public E getByUuid(int uuid) {
// TODO Auto-generated method stub
return baseDao.getByUuid(uuid);
}
@Override
public PageInfo<E> getByCondition(QE qe) {
// TODO Auto-generated method stub
return new PageInfo<E>(baseDao.getByCondition(qe));
}
}
CustomerService
package com.qytx.architecturel.customermgr.service;
import com.qytx.architecturel.customermgr.vo.CustomerModel;
import com.qytx.architecturel.customermgr.vo.CustomerQueryModel;
import com.qytx.common.service.BaseService;
public interface CustomerService extends BaseService<CustomerModel, CustomerQueryModel>{
}
CustomerServiceImpl
package com.qytx.architecturel.customermgr.service.imp;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.qytx.architecturel.customermgr.dao.CustomerDao;
import com.qytx.architecturel.customermgr.service.CustomerService;
import com.qytx.architecturel.customermgr.vo.CustomerModel;
import com.qytx.architecturel.customermgr.vo.CustomerQueryModel;
import com.qytx.common.service.BaseServiceImpl;
@Service(value="customerService")
public class CustomerServiceImpl extends BaseServiceImpl<CustomerModel, CustomerQueryModel> implements CustomerService{
private CustomerDao customerDao=null;
@Resource
public void setCustomerDao(CustomerDao customerDao) {
this.customerDao = customerDao;
super.setBaseDao(customerDao);
}
}
6.实体类
CustomerModel
package com.qytx.architecturel.customermgr.vo;
import java.io.Serializable;
public class CustomerModel implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private Integer uuid;
private String customerId;
private String pwd;
private String showName;
private String trueName;
private String registerTime;
public Integer getUuid() {
return uuid;
}
public void setUuid(Integer uuid) {
this.uuid = uuid;
}
public String getCustomerId() {
return customerId;
}
public void setCustomerId(String customerId) {
this.customerId = customerId;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getShowName() {
return showName;
}
public void setShowName(String showName) {
this.showName = showName;
}
public String getTrueName() {
return trueName;
}
public void setTrueName(String trueName) {
this.trueName = trueName;
}
public String getRegisterTime() {
return registerTime;
}
public void setRegisterTime(String registerTime) {
this.registerTime = registerTime;
}
@Override
public String toString() {
return "CustomerModel [uuid=" + uuid + ", customerId=" + customerId
+ ", pwd=" + pwd + ", showName=" + showName + ", trueName="
+ trueName + ", registerTime=" + registerTime + "]";
}
public CustomerModel(Integer uuid, String customerId, String pwd,
String showName, String trueName, String registerTime) {
super();
this.uuid = uuid;
this.customerId = customerId;
this.pwd = pwd;
this.showName = showName;
this.trueName = trueName;
this.registerTime = registerTime;
}
public CustomerModel() {
super();
// TODO Auto-generated constructor stub
}
}
CustomerQueryModel 专为customer提供的查询类
package com.qytx.architecturel.customermgr.vo;
import java.io.Serializable;
import java.util.List;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
public class CustomerQueryModel extends CustomerModel{
private PageHelper pageHelper=new PageHelper();
public PageHelper getPageHelper() {
return pageHelper;
}
public void setPageHelper(PageHelper pageHelper) {
this.pageHelper = pageHelper;
}
}
上一篇: TextView图文混排
下一篇: POJ2309 BST【二叉搜索树】