移动商城第一篇【搭建项目环境+数据模型】

时间:2022-05-06
本文章向大家介绍移动商城第一篇【搭建项目环境+数据模型】,主要内容包括前言、搭建Oracle数据库环境、使用Maven搭建环境、编写pom、将原型页面拷贝到项目中、页面框架解析、Mybatis和Spring环境搭建、编写Mybatis配置文件、创建beans.xml文件、添加log4j、Dao层、编写Dao实现 ##、编写Service、Service实现、测试Service、Controller层配置、分析商品的数据模型、商品类目、类目与品牌、类目与商家、商品、商品标签图片和大字段表、商品属性、类目与属性、参数、商品与参数与属性关系、库存与价格值、总结、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

前言

本次该项目使用的技术如下:

这里写图片描述

搭建Oracle数据库环境

本次我们用Oracle作为我们的服务器,我们一般开发并不是把数据表放在我们练习的scott用户下的

需要我们自己创建用户、创建默认的表空间

使用超级管理员账号登陆Oracle

这里写图片描述

 sys/change_on_install as sysdba

创建表空间

create tablespace zhongfucheng
logging   
datafile 'D:appozcoradatazhongfuchengzhongfucheng.dbf'   
size 32m   
autoextend on   
next 32m maxsize 2048m   
extent management local;  

这里写图片描述

创建表空间并指定用户

create user zhongfucheng identified by zhong default tablespace zhongfucheng   

单单创建了用户是不能够使用的,我们还需要为它授权

grant connect,resource,dba to zhongfucheng;  

我们来查看一下当前用户默认的表空间

select username,default_tablespace from user_users;

这里写图片描述

导入相关的数据:

@D:/ECPS_GUIZHOU2.sql
@D:/data.sql

使用Maven搭建环境

因为Maven有两大特点:

  1. 依赖管理
  2. 项目构建

依赖管理非常好用,我们就不用拷贝对应的jar包,直接从别的项目依赖过来就行了

我们本次的项目分为前台和后台

值得注意的是:

  • 前台是给用户访问的
  • 后台是给管理员访问的

基于这么两个特性,那么前台系统和后台系统应该是分开的【当前台系统访问量奔溃的时候,后台系统不会受到影响

由于我们前台和后台系统的某些逻辑是一致的【都要查询商品..根据商品id获取具体商品信息等等】,因此我们需要把相同的逻辑抽取出来,多添加一个核心模块Core

从前边也已经说了,我们如果使用到图片上传的话,也应该把该逻辑抽取出来,因为我们在备份的时候,是备份该WEB项目的数据。如果把图片放在同一个WEB项目下,那么就非常难处理图片了。

因此,我们也需要把文件单独放在文件服务器上

到目前为止的分析,我们现在有4个独立的模块了….

  • 文件服务器
  • Core
  • 后台系统
  • 前台系统

当我们把项目发布出去的时候,我们需要把每个模块都要构建

因此,我们再用一个parent模块把它们4个管理起来!做到一次构建的效果!

这里写图片描述

这里写图片描述

idea下构建Maven项目

创建父模块,在父模块下创建4个子模块。在Idea下直接创建Model就行了

这里写图片描述

最终在Idea创建出来的目录结构是这样子的:

这里写图片描述

重置Maven的设置,使用自带的Maven

这里写图片描述

这里写图片描述

编写pom

父项目将其他4个子项目管理起来

这里写图片描述

portal和console对core依赖:

这里写图片描述


将原型页面拷贝到项目中

首先,我在Idea创建Maven项目的时候是没有用任何的模版的。因此,需要我们自己创建WEB功能。

http://blog.csdn.net/hon_3y/article/details/76942530

这里写图片描述

访问portal客户端的原型页面没有问题

这里写图片描述

访问console页面的原型页面的时候出现了以下的错误:

这里写图片描述

关注是不是没有使用webapp来生成maven项目的模版

  • 使用webapp模版来生成Maven项目才行。不然找不到类。

页面框架解析

他的JSP是非常简单的,我们来看一下:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/ecps/console/common/taglibs.jsp"%>
<head>
<title>商品管理</title>
<meta name="heading" content="商品管理"/>
<meta name="menu" content="ItemMgmtMenu"/>
</head>
<body id="main">

<div class="frameL"><div class="menu icon">
   <jsp:include page="/ecps/console/common/itemmenu.jsp"/>
</div></div>

<div class="frameR"><div class="content">

</div></div>
</body>

明显地,我们看见它用了menu icon这么一个类来修饰,如果我们使用过bootstrap、easyui这么一类的框架的话,那么我们对这些的修饰也就不陌生了!

我们点进去它的包含JSP页面中

<%@ page language="java" errorPage="/error.jsp" pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" %>
<%@ include file="taglibs.jsp"%>
<head>
<title>商品管理</title>

</head>
<h2><samp class="t03"></samp>商品管理</h2>
<ul class="ul">
<li><a href="${path}/shop/item/list.jsp"><samp class="t05"></samp>商品录入/上下架</a></li>
<li><a href="${path}/shop/item/listAudit.jsp?auditStatus=0&showStatus=1"><samp class="t05"></samp>商品审核</a></li>
<li><a href="${base}/activity/listOfferGroup.do?showStatus=1&labelStatus=4"><samp class="t05"></samp>营销案管理</a></li>
<li><a href="${base}/activity/listAudit.do?auditStatus=0&labelStatus=1"><samp class="t05"></samp>营销案审核</a></li>
<li><a href="${base}/activity/listoffer.do"><samp class="t05"></samp>营销案档次</a></li>
<li><a href="${path}/shop/item/listfeature.jsp"><samp class="t05"></samp>属性管理</a></li>
<li><a href="${path}/shop/item/listbrand.jsp"><samp class="t05"></samp>品牌管理</a></li>
</ul>

发现这正是我们的左边菜单栏的数据

这里写图片描述

那么上边横线的菜单栏从何而来的呢???

我们发现项目中有好几个配置文件:

这里写图片描述

菜单栏的配置文件,该配置文件在Tomcat启动的时候加载的

<?xml version="1.0" encoding="UTF-8"?>
<MenuConfig>
    <Displayers>
        <Displayer name="Velocity" type="net.sf.navigator.displayer.VelocityMenuDisplayer"/>
    </Displayers>
    <Menus>

        <Menu name="MainMenu" title="首页" page="/shop/mainMenu.jsp" />
        <Menu name="ItemMgmtMenu" title="商品管理" page="/shop/item/index.jsp" />

        <!--新增-->
        <Menu name="studentMenu" title="学生管理" page="/student/index.jsp" />
        <!-- 辽宁2期屏蔽该功能 -->
        <!-- <Menu name="SimCardMgmtMenu" title="号卡管理" page="/shop/simcard/index.jsp" /> -->
        <Menu name="OrderMgmtMenu" title="订单管理" page="/shop/order/index.jsp" />
        <Menu name="ValetOrderMgmtMenu" title="代客下单" page="/shop/valetorder/index.jsp" />
        <Menu name="EbPtlUserMgmtMenu" title="会员管理" page="/shop/ptlUser/index.jsp" />
        <Menu name="RelationShipMgmtMenu" title="合作伙伴管理" page="/shop/relational/index.jsp"  />
        <Menu name="PaymentMgmtMenu" title="支付管理" page="/shop/payment/index.jsp" />
        <Menu name="AdvertisementMenu" title="广告管理" page="/shop/advertisement/index.jsp" />
        <!-- 辽宁2期屏蔽该功能 -->
        <!-- <Menu name="PromotionMenu" title="促销活动" page="/shop/promotion/index.jsp" /> -->
        <Menu name="SystemSet" title="系统配置" page="/shop/contentset/index.jsp" />
        <Menu name="permission" title="权限管理" page="/shop/permission/index.jsp" />
        <Menu name="reportMenu" title="报表管理" page="/shop/report/index.jsp" />
        <Menu name="Logout" title="退出" page="/shop/logout" />

        <Menu name="PMS-CHANNEL" title="栏目管理" page="/pms/frame/channel/main.jsp" />
        <Menu name="PMS-CONTENT" title="内容管理" page="/pms/frame/content/main.jsp" />
        <Menu name="PMS-PUBLISH" title="静态发布" page="/pms/frame/generate/main.jsp" />
        <Menu name="PMS-TEMPLATE" title="模版管理" page="/pms/frame/template/main.jsp" />
        <Menu name="PMS-SYSTEM" title="PMS管理" page="/pms/frame/system/main.jsp" />

        <Menu name="SAMPLE-LOGIN" title="APP-LOGIN" page="/sample/login.jsp" />
        <Menu name="SAMPLE-INDEX" title="mainMenu.title" page="/sample/index.jsp" />

    </Menus>
</MenuConfig>
    <listener>
        <listener-class>net.sf.navigator.menu.MenuContextListener</listener-class>
    </listener>

从另外的配置文件中,我们可以发现他使用了装饰器来修饰我们的JSP页面

<decorators defaultdir="/ecps/console/decorators">
    <excludes>
        <pattern>*/top10.jsp</pattern>

    </excludes> 
    <decorator name="default" page="default.jsp">
        <pattern>/*</pattern>
    </decorator>
</decorators>

找到默认的装饰器JSP页面,找到了对应的修饰菜单

<div class="header">
    <jsp:include page="/ecps/console/common/header.jsp"/>
    <c:set var="currentMenu" scope="request"><decorator:getProperty property="meta.menu"/></c:set>
    <jsp:include page="/ecps/console/common/menu.jsp"/>
</div>
<%@ page language="java" errorPage="/ecps/console/error.jsp" pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" %>
<%@ include file="taglibs.jsp"%>

<menu:useMenuDisplayer name="Velocity" config="cssHorizontalMenu.vm">
<div class="nav w">
    <ul id="nav_m" class="ul">
        <menu:displayMenu name="MainMenu"/>
        <menu:displayMenu name="ItemMgmtMenu"/>
        <menu:displayMenu name="OrderMgmtMenu"/>
        <menu:displayMenu name="ValetOrderMgmtMenu"/>
         <menu:displayMenu name="PaymentMgmtMenu"/>
        <menu:displayMenu name="AdvertisementMenu"/>
        <menu:displayMenu name="RelationShipMgmtMenu"/>
        <menu:displayMenu name="EbPtlUserMgmtMenu"/>
        <menu:displayMenu name="permission"/>
        <menu:displayMenu name="SystemSet"/>
        <menu:displayMenu name="reportMenu"/>
     </ul>
    <p></p>
</div>
</menu:useMenuDisplayer>

我们发现该页面包含了taglibs.jsp,taglibs中又有众多的标签引入

<%@ taglib uri="http://www.springframework.org/security/tags" prefix="security" %>
<%@ taglib uri="http://www.springmodules.org/tags/commons-validator" prefix="v" %>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@ taglib uri="http://struts-menu.sf.net/tag" prefix="display" %>
<%@ taglib uri="http://struts-menu.sf.net/tag-el" prefix="menu" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator"%>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page"%>

<c:set var="path" value="${pageContext.request.contextPath}"/>
<c:set var="system" value="/ecps/console"/>
<c:set var="PmsServerPath" value="http://10.1.248.131:8080/pmsV4/pms/main.do"/>
<c:set var="datePattern"><fmt:message key="date.format"/></c:set>

而sitemesh就是我们在web.xml配置的过滤器,它的功能就是用来渲染我们的页面的

<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator"%>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page"%>
    <filter>
        <filter-name>sitemesh</filter-name>
        <filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>sitemesh</filter-name>
        <url-pattern>*.do</url-pattern> 
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
    </filter-mapping>
    <filter-mapping>
        <filter-name>sitemesh</filter-name>
        <url-pattern>*.jsp</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
    </filter-mapping>

再次看回我们的index页面,我们可以发现有那么一段代码

<meta name="menu" content="ItemMgmtMenu"/>

content和上面的name是一致的

模拟增加新功能

根据上面我们所看到的,我们在原有的页面上增添新的功能,看看我们的分析是否有错误!

在菜单的配置文件中新增学生管理

        <!--新增-->
        <Menu name="studentMenu" title="学生管理" page="/student/index.jsp" />

随后在修饰器中新增学生管理菜单项

        <%--新增!--%>
        <menu:displayMenu name="studentMenu"/>

接着根据其他的index页面,编写我们学生管理的JSP页面

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/ecps/console/common/taglibs.jsp"%>
<head>
    <title>学生管理</title>
    <meta name="heading" content="学生管理"/>
    <meta name="menu" content="studentMenu"/>
</head>
<body id="main">
<div class="frameL"><div class="menu icon">
    <jsp:include page="/student/studentmenu.jsp"/>
</div></div>
<div class="frameR"><div class="content">

</div></div>
</body>

左侧菜单的JSP

<%@ page language="java" errorPage="/error.jsp" pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" %>
<%@ include file="../ecps/console/common/taglibs.jsp"%>
<head>
    <title>学生管理</title>

</head>
<h2><samp class="t03"></samp>学生管理</h2>
<ul class="ul">
    <li><a href="${path}/shop/item/list.jsp"><samp class="t05"></samp>查看成绩</a></li>
</ul>

效果:

这里写图片描述


Mybatis和Spring环境搭建

由于我们的所编写的dao层、service代码可能前台和后台都需要用到的,因此我们把环境搭建在core模块中

逆向工程

首先,我们要做的就是品牌管理,我们首先来看看原型界面的的品牌管理

这里写图片描述

涉及到的数据库表在我们的powerdesigner中就已经定义过了

这里写图片描述

我们来看一下对应数据库表的数据

这里写图片描述

添加逆向工程插件

    <build>
        <finalName>zhongfucheng</finalName>
        <plugins>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <configuration>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>
        </plugins>
    </build>

编写逆向工程文件

<?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>
    <!-- 制定mysql的驱动包的路径 千万别放中文路径下 -->
    <classPathEntry location="F:传智168期JAVA传智168期JAVA移动商城shoprepositoryshoprepositoryrepositorycomoracleojdbc1410.2.0.2.0ojdbc14-10.2.0.2.0.jar" />

    <!-- 配置数据源和生成的代码所存放的位置 -->
    <context id="context1">

        <commentGenerator>
            <!-- 是否取消注释 -->
            <property name="suppressAllComments" value="true" />
            <!-- 是否生成注释代时间戳 -->
            <property name="suppressDate" value="true" />
        </commentGenerator>
        <jdbcConnection driverClass="oracle.jdbc.OracleDriver"
            connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:ZHONGFUCHENG" userId="zhongfucheng"
            password="zhong" />
        <!-- 所生成的实体类的位置默认资源包src -->
        <javaModelGenerator targetPackage="com.rl.ecps.model" targetProject="src/main/java" />
        <!-- 所生成的sqlMap的影射文件的位置,默认资源包src -->
        <sqlMapGenerator targetPackage="com.rl.ecps.sqlMap" targetProject="src/main/java" />

        <!-- 为哪些表生成代码 tableName:表名 schema:不用填写 -->
        <table schema="" tableName="eb_brand" enableCountByExample="false"
            enableUpdateByExample="false" enableDeleteByExample="false"
            enableSelectByExample="false" selectByExampleQueryId="false"
        >
        </table>
    </context>
</generatorConfiguration>

生成的文件:

这里写图片描述

sqlMapping映射文件也算是一种资源文件把,因此我把它移到resources中了

这里写图片描述

编写Mybatis配置文件

sqlMapConfig.xml

<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <mappers>
        <mapper resource="sqlMap/EbBrandMapper.xml"/>
    </mappers>
</configuration>

创建beans.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:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="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.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--开启注解-->
    <context:component-scan base-package="com.rl.ecps"/>

    <!--配置连接池-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="oracle.jdbc.OracleDriver"></property>
        <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:ZHONGFUCHENG"></property>
        <property name="username" value="zhongfucheng"></property>
        <property name="password" value="zhong"></property>
    </bean>


    <!--配置sessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:sqlMapConfig.xml"></property>
    </bean>


    <!--配置事务管理器-->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--配置事务具体方法-->
    <tx:advice id="txAdive" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="save*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="add*" propagation="REQUIRED"/>
            <tx:method name="modify*" propagation="REQUIRED"/>
            <tx:method name="delete*" propagation="REQUIRED"/>
            <tx:method name="remove*" propagation="REQUIRED"/>
            <tx:method name="select*" read-only="true"/>
            <tx:method name="get*" read-only="true"/>
        </tx:attributes>
    </tx:advice>

    <!--配置切面-->
    <aop:config>
        <aop:advisor advice-ref="txAdive" pointcut="execution(* com.rl.ecps.service..*.*(..))"/>
    </aop:config>

</beans>

添加log4j

log4j.properties

log4j.rootLogger=DEBUG, Console
#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG

Dao层

编写Dao接口

interface EbBrandDao {


    void saveBrand(EbBrand brand);

    EbBrand getBrandById(Long brandId);

    void updateBrand(EbBrand brand);

    void deleteBrand(Long brandId);

    List<EbBrand> selectBrand();

    List<EbBrand> selectBrandByName(String brandName);
}

编写Dao实现 ##

/**
 * 继承SqlSessionDaoSupport能够得到sessionFactory的引用,非常方便!
 */
@Repository
public class EbBrandDaoImpl extends SqlSessionDaoSupport implements EbBrandDao {

    String nameSpace = "com.rl.ecps.sqlMap.EbBrandMapper.";

    public void saveBrand(EbBrand brand) {

        this.getSqlSession().insert(nameSpace + "insert", brand);
    }
    public EbBrand getBrandById(Long brandId) {
        return this.getSqlSession().selectOne(nameSpace + "selectByPrimaryKey", brandId);
    }
    public void updateBrand(EbBrand brand) {

        /**
         * updateByPrimaryKeySelective和updata的区别:
         *  一个是动态SQL,一个是静态SQL。这里就使用动态SQL比较好。因为是更新操作
         */
        this.getSqlSession().update(nameSpace + "updateByPrimaryKeySelective", brand);
    }
    public void deleteBrand(Long brandId) {

        this.getSqlSession().delete(nameSpace + "deleteByPrimaryKey", brandId);
    }
    public List<EbBrand> selectBrand() {
        return this.getSqlSession().selectList(nameSpace+"selectBrand");
    }
    public List<EbBrand> selectBrandByName(String brandName) {
        return this.getSqlSession().selectList(nameSpace+"selectBrandByName", brandName);
    }
}

mybatis插件只有最后两个方法没有帮我们自动生成,其他的SQL语句都自动生成了。

  <select id="selectBrand" resultMap="BaseResultMap">
    select * from eb_brand
  </select>

  <select id="selectBrandByName" resultMap="BaseResultMap" parameterType="string">
      select * from eb_brand t where t.brand_name = #{brandName}
  </select>

编写Service

Service接口

public interface EbBrandService {
    void saveBrand(EbBrand brand);

    EbBrand getBrandById(Long brandId);

    void updateBrand(EbBrand brand);

    void deleteBrand(Long brandId);

    List<EbBrand> selectBrand();

    List<EbBrand> selectBrandByName(String brandName);
}

Service实现

package com.rl.ecps.service.impl;

import com.rl.ecps.dao.EbBrandDao;
import com.rl.ecps.dao.impl.EbBrandDaoImpl;
import com.rl.ecps.model.EbBrand;
import com.rl.ecps.service.EbBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by ozc on 2017/8/26.
 */

@Service
public class EbBrandServiceImpl implements EbBrandService {


    @Autowired
    private EbBrandDao brandDao;

    public void saveBrand(EbBrand brand) {
        brandDao.saveBrand(brand);

    }

    public EbBrand getBrandById(Long brandId) {
        return brandDao.getBrandById(brandId);
    }

    public void updateBrand(EbBrand brand) {

        brandDao.updateBrand(brand);

    }
    public void deleteBrand(Long brandId) {

        brandDao.deleteBrand(brandId);

    }
    public List<EbBrand> selectBrand() {
        return brandDao.selectBrand();
    }
    public List<EbBrand> selectBrandByName(String brandName) {
        return brandDao.selectBrandByName(brandName);
    }

}

测试Service

在idea中,我们新建一个test目录

这里写图片描述

对着我们的serviceImpl使用快捷键ctrl+shift+t

这里写图片描述

idea就会帮我们自动把想要测试的方法把相关的目录创建出来了

这里写图片描述

我们在插入数据的时候,记得要改写SQL语句,因为默认是没有主键的。下面就是我们把在主键列上使用自动增长

<insert id="insert" parameterType="com.rl.ecps.model.EbBrand" >

    <selectKey keyProperty="brandId" order="BEFORE" resultType="long">
      select seqbrandid.nextval from dual
    </selectKey>


    insert into EB_BRAND (BRAND_ID, BRAND_NAME, BRAND_DESC, 
      IMGS, WEBSITE, BRAND_SORT
      )
    values (#{brandId,jdbcType=DECIMAL}, #{brandName,jdbcType=VARCHAR}, #{brandDesc,jdbcType=VARCHAR}, 
      #{imgs,jdbcType=VARCHAR}, #{website,jdbcType=VARCHAR}, #{brandSort,jdbcType=DECIMAL}
      )
  </insert>

插入成功

这里写图片描述

Controller层配置

编写SpringMVC的配置文件

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.0.xsd 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">

    <context:component-scan base-package="com.rl.ecps.controller"/>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/shop/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="1024000"></property>
    </bean>

</beans>

分析商品的数据模型

本篇主要是分析我们的项目中的数据模型…一般地,我们都是用powerdesigner来设计我们的数据库模型表…

首先我们要看懂powerdesigner箭头的意义:

表之间的关系 一对多:箭头所指向的表是一的一端 多对一:箭头背向的方向的表是多的一端(除了一对一) 多对多:在数据库模型上,由三张表来组成,中间表体现的是其余两张表的关系是多对多,中间表只能有两个表的主键作为外键来组成,箭头由中介表来分别指向其余两张表。 一对一:箭头指向的方向是一的一端,背向的方向的表使用指向的方向的表的主键作为主键和外键就形成了一对一的关系。

品牌管理

我们前面已经写了品牌管理了,我们来看看数据库设计是怎么样的。

首先是我们的品牌表:主要定义了品牌基本信息字段

这里写图片描述

商品类目

该类目表主要对商品进行分类…

这里写图片描述

同时他还有自关联的连接线,那是什么意思呢??

该商品类目表拥有父类目的属性

这里写图片描述

也就是说,类目我们可以看成是树形结构的:

这里写图片描述

是可以根据当前类目查询出上一级类目的

这里写图片描述

类目与品牌

一个类目下是可以有多个品牌的,而一个品牌可以有多个类目..

这里写图片描述

我们可以举个例子来理解:

空调类目下:

  • 海尔
  • 格力

海尔:

  • 洗衣机
  • 空调

格力:

  • 洗衣机
  • 空调

对于海尔,格力这样的品牌,不仅仅是做空调,还可以做洗衣机。而空调和洗衣机又是不同的类目下的。

类目与商家

为了实现更好地管理,我们还有类目所对应的商家表

这里写图片描述

商品

我们的商品主要是手机和号卡

通过在类目表中预置的手机类目和号卡类目来区分

而一个品牌也对应着多个商品

  • 苹果
    • iphone5s
    • iphone6s

这里写图片描述

我们商品的存储事例数据如下:

这里写图片描述

除了与品牌和类目之间的关联关系,还存储着一些销售语、是否上架、等信息

这里写图片描述

商品标签图片和大字段表

我们的商品标签图片也是用另外一张数据库表来存储。它有着商品的全部属性

而当商品信息太多,写不完的时候就需要用到大字段表了。

这里写图片描述

商品属性

我们使用了一张专门管理商品属性的数据表来进行展示对应的商品属性

该数据库表可以设置该属性是否前台展示,以及属性是怎么录入的:

  • 树形
  • 文本框
  • 下拉框

这里写图片描述

数据如下所示:

这里写图片描述

类目与属性

对于属性而言也是分开的:

  • 要么是手机
  • 要么是手机子属性的

因此我们的属性与类目也是存在:一个类目对应多个属性的关系的

参数

我们可以发现在我们的商品属性表中,如果一个属性是可以多选的话,那么在属性值上是使用“,”逗号来进行分割的

这里写图片描述

而我们的参数表就是对其选中的值进行保存!

  • feature是用于展示我们的所有属性数据
  • para是用于存储用户选中的属性数据

这里写图片描述

商品与参数与属性关系

一个商品是有多个参数的,而一个属性也对应着多个参数。

这里写图片描述

库存与价格值

库存也是用一张表来存储的,对应地,不同类型的商品对应的不同的库存

我们可以简单理解成一下的情况:

这里写图片描述

总结

  • 我们的项目最好分成前台和后台两个模块,这样的话,即时前台模块访问量太大,我们后台的模块也不会受到影响
  • 图片服务器独立起来也是非常有必要的。
  • 为了做到一次的构建、还使用了一个parent模块把它们管理起来
  • 使用Idea时构建maven项目,最好就使用web-app模版来生成。
  • 对于页面的这些框架,我们主要看得懂它大概的流程就行了。没必要深入,因为每个项目可能用的都不一样。
  • 品牌管理其实就是CURD操作而已,从Idea中配置逆向工程的插件。生成出对应的POJO、映射文件,再编写DAO、Service、Controller即可
  • 到目前为止,我们仅仅是简单分析了数据库表的对应关系。是并没有深刻理解它为什么要设计成这样子的,当我们一边写代码的时候,再回顾它的设计。可能效果会更好。