美文网首页我的Spring MVC
第2章 项目设计和框架搭建

第2章 项目设计和框架搭建

作者: cuzz_ | 来源:发表于2018-04-22 23:14 被阅读82次

    系统功能模块划分

    学习目标

    • 明确各模块的职责
    • 了解各模块所涉及的对象

    前端展示系统

    image.png

    店家管理系统

    image.png

    超级管理员系统

    image.png

    实体类的设计与创建

    image.png

    区域

    image.png

    在src新建java文件,然后把他设置为根目录


    image.png

    新建一个com.imooc.o2o.entity的包
    新建一个Area类,创建5个成员变量

    package com.imooc.o2o.entity;
    
    import java.util.Date;
    
    public class Area {
        // ID
        private Integer areaId;
        // 名称
        private String areaName;
        // 权重
        private Integer priority;
        // 创建时间
        private Date createTime;
        // 更新时间
        private Date lastEditTime;
    
        public Integer getAreaId() {
            return areaId;
        }
    
        public void setAreaId(Integer areaId) {
            this.areaId = areaId;
        }
    
        public String getAreaName() {
            return areaName;
        }
    
        public void setAreaName(String areaName) {
            this.areaName = areaName;
        }
    
        public Integer getPriority() {
            return priority;
        }
    
        public void setPriority(Integer priority) {
            this.priority = priority;
        }
    
        public Date getCreateTime() {
            return createTime;
        }
    
        public void setCreateTime(Date createTime) {
            this.createTime = createTime;
        }
    
        public Date getLastEditTime() {
            return lastEditTime;
        }
    
        public void setLastEditTime(Date lastEditTime) {
            this.lastEditTime = lastEditTime;
        }
    }
    

    使用Navicat创建数据表tb_area数据表

    use o2o;
    create table `tb_area`(
      `area_id` int(2) NOT NULL AUTO_INCREMENT,
      `area_name` varchar(200) NOT NULL,
      `priority` int(2) NOT NULL DEFAULT '0',
      `create_time` datetime DEFAULT NULL,
      `last_edit_time` datetime DEFAULT NULL,
      primary key(`area_id`),
      unique key `UK_AREA`(`area_name`)
    ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
    

    使用InnoDB引擎,自增为1,设置默认编码格式为utf8

    用户

    image.png

    创建PersonInfo类

    package com.imooc.o2o.entity;
    
    import java.util.Date;
    
    public class PersonInfo {
        private Long userId;
        private String name;
        private String profileImg;
        private String email;
        private String gender;
        private Integer enableStatus;
        // 1表示顾客  2表示店家  3表示管理员
        private Integer userType;
        private Date createTime;
    
        public Long getUserId() {
            return userId;
        }
    
        public void setUserId(Long userId) {
            this.userId = userId;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getProfileImg() {
            return profileImg;
        }
    
        public void setProfileImg(String profileImg) {
            this.profileImg = profileImg;
        }
    
        public String getEmail() {
            return email;
        }
    
        public void setEmail(String email) {
            this.email = email;
        }
    
        public String getGender() {
            return gender;
        }
    
        public void setGender(String gender) {
            this.gender = gender;
        }
    
        public Integer getEnableStatus() {
            return enableStatus;
        }
    
        public void setEnableStatus(Integer enableStatus) {
            this.enableStatus = enableStatus;
        }
    
        public Integer getUserType() {
            return userType;
        }
    
        public void setUserType(Integer userType) {
            this.userType = userType;
        }
    
        public Date getCreateTime() {
            return createTime;
        }
    
        public void setCreateTime(Date createTime) {
            this.createTime = createTime;
        }
    
        public Date getLastEditTime() {
            return lastEditTime;
        }
    
        public void setLastEditTime(Date lastEditTime) {
            this.lastEditTime = lastEditTime;
        }
    
        private Date lastEditTime;
    }
    

    创建tb_person_info数据表

    use o2o;
    create table `tb_person_info`(
      `user_id` int(10) NOT NULL AUTO_INCREMENT,
      `name` varchar(32) DEFAULT NULL,
      `profile_img` varchar(1024) DEFAULT NULL,
      `email` varchar(1024) DEFAULT NULL,
      `gender` varchar(2) DEFAULT NULL,
      `enable_status` int(2) NOT NULL DEFAULT '0' COMMENT '0:禁止, 1:允许',
      `user_type` int(2) NOT NULL DEFAULT '1' COMMENT '1代表顾客, 2代表商家, 3代表超级管理员',
      `create_time` datetime DEFAULT NULL,
      `last_edit_time` datetime DEFAULT NULL,
      primary key (`user_id`)
     )ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
    

    账号

    image.png
    openId是微信号与本地账号绑定的唯一标识
    新建一个WechatAuth类,用于微信登入
    package com.imooc.o2o.entity;
    
    import java.util.Date;
    
    public class WechatAuth {
        private Long WechatAuthId;
        private String openId;
        private Date createtime;
        private PersonInfo personInfo;
    
        public PersonInfo getPersonInfo() {
            return personInfo;
        }
    
        public void setPersonInfo(PersonInfo personInfo) {
            this.personInfo = personInfo;
        }
    
        public Long getWechatAuthId() {
            return WechatAuthId;
        }
    
        public void setWechatAuthId(Long wechatAuthId) {
            WechatAuthId = wechatAuthId;
        }
    
        public String getOpenId() {
            return openId;
        }
    
        public void setOpenId(String openId) {
            this.openId = openId;
        }
    
        public Date getCreatetime() {
            return createtime;
        }
    
        public void setCreatetime(Date createtime) {
            this.createtime = createtime;
        }
    }
    

    新建一个本地账号

    package com.imooc.o2o.entity;
    
    import java.util.Date;
    
    public class LocalAuth {
        private Long localAuthId;
        private String username;
        private String password;
        private Date createTime;
        private Date lastEditTime;
        private PersonInfo personInfo;
    
        public Long getLocalAuthId() {
            return localAuthId;
        }
    
        public void setLocalAuthId(Long localAuthId) {
            this.localAuthId = localAuthId;
        }
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        public Date getCreateTime() {
            return createTime;
        }
    
        public void setCreateTime(Date createTime) {
            this.createTime = createTime;
        }
    
        public Date getLastEditTime() {
            return lastEditTime;
        }
    
        public void setLastEditTime(Date lastEditTime) {
            this.lastEditTime = lastEditTime;
        }
    
        public PersonInfo getPersonInfo() {
            return personInfo;
        }
    
        public void setPersonInfo(PersonInfo personInfo) {
            this.personInfo = personInfo;
        }
    }
    

    创建tb_wechat_auth数据表

    use o2o;
    create table `tb_wechat_auth`(
      `wechat_auth_id` int(10) NOT NULL AUTO_INCREMENT,
      `user_id` int(10) NOT NULL,
      `open_id` varchar(1024) NOT NULL,
      `create_time` datetime DEFAULT NULL,
      `last_edit_time` datetime DEFAULT NULL,
      primary key(`wechat_auth_id`),
      constraint `fk_wechatauth_profile` foreign key(`user_id`) references `tb_person_info`(`user_id`)
      )ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
    

    创建tb_local_auth数据表

    use o2o;
    create table`tb_local_auth`(
      `local_auth_id` int(10) NOT NULL AUTO_INCREMENT,
      `user_id` int(10) NOT NULL,
      `username` varchar(128) NOT NULL,
      `password` varchar(128) NOT NULL,
      `create_time` datetime DEFAULT NULL,
      `last_edit_time` datetime DEFAULT NULL,
      primary key(`local_auth_id`),
      unique key `uk_local_profile`(`username`),
      constraint `fk_localauth_profile` foreign key(`user_id`) references `tb_person_info`(`user_id`)
      )ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
    

    我们要个open_id增加一个唯一索引

    alter table tb_wechat_auth add unique index(open_id);
    

    添加索引能增加查询的性能

    头条

    image.png
    创建一个HeadLine
    package com.imooc.o2o.entity;
    
    import java.util.Date;
    
    public class HeadLine {
        private Long lineId;
        private String lineName;
        private String lineLink;
        private String lineImg;
        private Integer priority;
        // 0表示不可用 1表示可用
        private Integer enableStatus;
        private Date createTime;
        private Date lastEditTime;
    
        public Long getLineId() {
            return lineId;
        }
    
        public void setLineId(Long lineId) {
            this.lineId = lineId;
        }
    
        public String getLineName() {
            return lineName;
        }
    
        public void setLineName(String lineName) {
            this.lineName = lineName;
        }
    
        public String getLineLink() {
            return lineLink;
        }
    
        public void setLineLink(String lineLink) {
            this.lineLink = lineLink;
        }
    
        public String getLineImg() {
            return lineImg;
        }
    
        public void setLineImg(String lineImg) {
            this.lineImg = lineImg;
        }
    
        public Integer getPriority() {
            return priority;
        }
    
        public void setPriority(Integer priority) {
            this.priority = priority;
        }
    
        public Integer getEnableStatus() {
            return enableStatus;
        }
    
        public void setEnableStatus(Integer enableStatus) {
            this.enableStatus = enableStatus;
        }
    
        public Date getCreateTime() {
            return createTime;
        }
    
        public void setCreateTime(Date createTime) {
            this.createTime = createTime;
        }
    
        public Date getLastEditTime() {
            return lastEditTime;
        }
    
        public void setLastEditTime(Date lastEditTime) {
            this.lastEditTime = lastEditTime;
        }
    }
    

    创建一个tb_head_line数据表

    use o2o;
    create table `tb_head_line`(
      `line_id` int(100) NOT NULL AUTO_INCREMENT,
      `line_name` varchar(1000) DEFAULT NULL,
      `line_link` varchar(2000) NOT NULL,
      `priority` int(2) NOT NULL,
      `enable_status` int(2) NOT NULL DEFAULT '0',
      `create_time` datetime DEFAULT NULL,
      `last_edit_time` datetime DEFAULT NULL,
      primary key(`line_id`)
      )ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
    

    店铺类别

    image.png

    上级id来实现无限分级,当上级id为空时,为第1级

    package com.imooc.o2o.entity;
    
    import java.util.Date;
    
    public class shopCategory {
        private Long shopCategoryId;
        private String shopCategoryName;
        private String shopCategoryDesc;
        private String shopCategoryImg;
        private Integer priority;
        private Date createTime;
        private Date lastEditTime;
        private shopCategory parent;
    
        public Long getShopCategoryId() {
            return shopCategoryId;
        }
    
        public void setShopCategoryId(Long shopCategoryId) {
            this.shopCategoryId = shopCategoryId;
        }
    
        public String getShopCategoryName() {
            return shopCategoryName;
        }
    
        public void setShopCategoryName(String shopCategoryName) {
            this.shopCategoryName = shopCategoryName;
        }
    
        public String getShopCategoryDesc() {
            return shopCategoryDesc;
        }
    
        public void setShopCategoryDesc(String shopCategoryDesc) {
            this.shopCategoryDesc = shopCategoryDesc;
        }
    
        public String getShopCategoryImg() {
            return shopCategoryImg;
        }
    
        public void setShopCategoryImg(String shopCategoryImg) {
            this.shopCategoryImg = shopCategoryImg;
        }
    
        public Integer getPriority() {
            return priority;
        }
    
        public void setPriority(Integer priority) {
            this.priority = priority;
        }
    
        public Date getCreateTime() {
            return createTime;
        }
    
        public void setCreateTime(Date createTime) {
            this.createTime = createTime;
        }
    
        public Date getLastEditTime() {
            return lastEditTime;
        }
    
        public void setLastEditTime(Date lastEditTime) {
            this.lastEditTime = lastEditTime;
        }
    
        public shopCategory getParent() {
            return parent;
        }
    
        public void setParent(shopCategory parent) {
            this.parent = parent;
        }
    }
    

    创建tb_shop_category数据表

    use o2o;
    create table `tb_shop_category`(
      `shop_category_id` int(11) NOT NULL AUTO_INCREMENT,
      `shop_category_name` varchar(100) NOT NULL DEFAULT '',
      `shop_category_desc` varchar(1000) NOT NULL DEFAULT '',
      `shop_category_img` varchar(1000) DEFAULT NULL,
      `priority` int(2) NOT NULL DEFAULT '0',
      `create_time` datetime DEFAULT NULL,
      `last_edit_time` datetime DEFAULT NULL,
      `parent_id` int(11) DEFAULT NULL,
      primary key(`shop_category_id`),
      constraint `fk_shop_category_self` foreign key(`parent_id`) references `tb_shop_category`(`shop_category_id`)
      )ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
    

    店铺

    image.png
    新建Shop
    package com.imooc.o2o.entity;
    
    import java.util.Date;
    
    public class Shop {
    
        private Long shopId;
        private String shopName;
        private String shopDesc;
        private String shopAddr;
        private String phone;
        private String shopImg;
        private Integer priority;
        private Date createTime;
        private Date lastEditTime;
        // -1 不可用  0 审核中 1 可用
        private Integer enableStatus;
        private String advice;
        private Area area;
        private PersonInfo owner;
        private ShopCategory shopCategory;
    
        public Long getShopId() {
            return shopId;
        }
    
        public void setShopId(Long shopId) {
            this.shopId = shopId;
        }
    
        public String getShopName() {
            return shopName;
        }
    
        public void setShopName(String shopName) {
            this.shopName = shopName;
        }
    
        public String getShopDesc() {
            return shopDesc;
        }
    
        public void setShopDesc(String shopDesc) {
            this.shopDesc = shopDesc;
        }
    
        public String getShopAddr() {
            return shopAddr;
        }
    
        public void setShopAddr(String shopAddr) {
            this.shopAddr = shopAddr;
        }
    
        public String getPhone() {
            return phone;
        }
    
        public void setPhone(String phone) {
            this.phone = phone;
        }
    
        public String getShopImg() {
            return shopImg;
        }
    
        public void setShopImg(String shopImg) {
            this.shopImg = shopImg;
        }
    
        public Integer getPriority() {
            return priority;
        }
    
        public void setPriority(Integer priority) {
            this.priority = priority;
        }
    
        public Date getCreateTime() {
            return createTime;
        }
    
        public void setCreateTime(Date createTime) {
            this.createTime = createTime;
        }
    
        public Date getLastEditTime() {
            return lastEditTime;
        }
    
        public void setLastEditTime(Date lastEditTime) {
            this.lastEditTime = lastEditTime;
        }
    
        public Integer getEnableStatus() {
            return enableStatus;
        }
    
        public void setEnableStatus(Integer enableStatus) {
            this.enableStatus = enableStatus;
        }
    
        public String getAdvice() {
            return advice;
        }
    
        public void setAdvice(String advice) {
            this.advice = advice;
        }
    
        public Area getArea() {
            return area;
        }
    
        public void setArea(Area area) {
            this.area = area;
        }
    
        public PersonInfo getOwner() {
            return owner;
        }
    
        public void setOwner(PersonInfo owner) {
            this.owner = owner;
        }
    
        public ShopCategory getShopCategory() {
            return shopCategory;
        }
    
        public void setShopCategory(ShopCategory shopCategory) {
            this.shopCategory = shopCategory;
        }
    }
    

    创建tb_shop数据表

    use o2o;
    CREATE TABLE `tb_shop` (
      `shop_id` int(10) NOT NULL AUTO_INCREMENT,
      `owner_id` int(10) NOT NULL COMMENT '店铺创建人',
      `area_id` int(5) DEFAULT NULL,
      `shop_category_id` int(11) DEFAULT NULL,
      `parent_category_id` int(11) DEFAULT NULL,
      `shop_name` varchar(256)  NOT NULL,
      `shop_desc` varchar(1024)  DEFAULT NULL,
      `shop_addr` varchar(200)  DEFAULT NULL,
      `phone` varchar(128)  DEFAULT NULL,
      `shop_img` varchar(1024)  DEFAULT NULL,
      `longitude` double(16,12) DEFAULT NULL,
      `latitude` double(16,12) DEFAULT NULL,
      `priority` int(3) DEFAULT '0',
      `create_time` datetime DEFAULT NULL,
      `last_edit_time` datetime DEFAULT NULL,
      `enable_status` int(2) NOT NULL DEFAULT '0',
      `advice` varchar(255)  DEFAULT NULL,
      PRIMARY KEY (`shop_id`),
      KEY `fk_shop_profile` (`owner_id`),
      KEY `fk_shop_area` (`area_id`),
      KEY `fk_shop_shopcate` (`shop_category_id`),
      KEY `fk_shop_parentcate` (`parent_category_id`),
      CONSTRAINT `fk_shop_area` FOREIGN KEY (`area_id`) REFERENCES `tb_area` (`area_id`),
      CONSTRAINT `fk_shop_parentcate` FOREIGN KEY (`parent_category_id`) REFERENCES `tb_shop_category` (`shop_category_id`),
      CONSTRAINT `fk_shop_profile` FOREIGN KEY (`owner_id`) REFERENCES `tb_person_info` (`user_id`),
      CONSTRAINT `fk_shop_shopcate` FOREIGN KEY (`shop_category_id`) REFERENCES `tb_shop_category` (`shop_category_id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
    

    商品类别

    创建ProductCategory

    package com.imooc.o2o.entity;
    
    import java.util.Date;
    
    public class ProductCategory {
        private Long productCategoryId;
        private Long shopId;
        private String productCategoryName;
        private Integer priority;
        private Date createTime;
    
        public Long getProductCategoryId() {
            return productCategoryId;
        }
    
        public void setProductCategoryId(Long productCategoryId) {
            this.productCategoryId = productCategoryId;
        }
    
        public Long getShopId() {
            return shopId;
        }
    
        public void setShopId(Long shopId) {
            this.shopId = shopId;
        }
    
        public String getProductCategoryName() {
            return productCategoryName;
        }
    
        public void setProductCategoryName(String productCategoryName) {
            this.productCategoryName = productCategoryName;
        }
    
        public Integer getPriority() {
            return priority;
        }
    
        public void setPriority(Integer priority) {
            this.priority = priority;
        }
    
        public Date getCreateTime() {
            return createTime;
        }
    
        public void setCreateTime(Date createTime) {
            this.createTime = createTime;
        }
    }
    

    创建数据表tb_shop

    use o2o;
    CREATE TABLE `tb_product_category` (
      `product_category_id` int(11) NOT NULL AUTO_INCREMENT,
      `product_category_name` varchar(100) NOT NULL,
      `product_category_desc` varchar(500) DEFAULT NULL,
      `priority` int(2) DEFAULT '0',
      `create_time` datetime DEFAULT NULL,
      `last_edit_time` datetime DEFAULT NULL,
      `shop_id` int(20) NOT NULL DEFAULT '0',
      PRIMARY KEY (`product_category_id`),
      CONSTRAINT `fk_procate_shop` FOREIGN KEY (`shop_id`) REFERENCES `tb_shop` (`shop_id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
    

    商品

    image.png
    新建Product
    package com.imooc.o2o.entity;
    
    import java.util.Date;
    import java.util.List;
    
    public class Product {
        private Long productId;
        private String productName;
        private String productDesc;
        private String imgAddr;
        private String normalPrice;
        private String promotionPrice;
        private Integer priority;
        private Date createTime;
        private Date lastEdittime;
        // 0 下架 1 展示
        private Integer enableStatus;
        // 图片列表
        private List<ProductImg> productImgList;
        // 商品类别
        private ProductCategory productCategory;
        // 所属商店
        private Shop shop;
    
        public Long getProductId() {
            return productId;
        }
    
        public void setProductId(Long productId) {
            this.productId = productId;
        }
    
        public String getProductName() {
            return productName;
        }
    
        public void setProductName(String productName) {
            this.productName = productName;
        }
    
        public String getProductDesc() {
            return productDesc;
        }
    
        public void setProductDesc(String productDesc) {
            this.productDesc = productDesc;
        }
    
        public String getImgAddr() {
            return imgAddr;
        }
    
        public void setImgAddr(String imgAddr) {
            this.imgAddr = imgAddr;
        }
    
        public String getNormalPrice() {
            return normalPrice;
        }
    
        public void setNormalPrice(String normalPrice) {
            this.normalPrice = normalPrice;
        }
    
        public String getPromotionPrice() {
            return promotionPrice;
        }
    
        public void setPromotionPrice(String promotionPrice) {
            this.promotionPrice = promotionPrice;
        }
    
        public Integer getPriority() {
            return priority;
        }
    
        public void setPriority(Integer priority) {
            this.priority = priority;
        }
    
        public Date getCreateTime() {
            return createTime;
        }
    
        public void setCreateTime(Date createTime) {
            this.createTime = createTime;
        }
    
        public Date getLastEdittime() {
            return lastEdittime;
        }
    
        public void setLastEdittime(Date lastEdittime) {
            this.lastEdittime = lastEdittime;
        }
    
        public Integer getEnableStatus() {
            return enableStatus;
        }
    
        public void setEnableStatus(Integer enableStatus) {
            this.enableStatus = enableStatus;
        }
    
        public List<ProductImg> getProductImgList() {
            return productImgList;
        }
    
        public void setProductImgList(List<ProductImg> productImgList) {
            this.productImgList = productImgList;
        }
    
        public ProductCategory getProductCategory() {
            return productCategory;
        }
    
        public void setProductCategory(ProductCategory productCategory) {
            this.productCategory = productCategory;
        }
    
        public Shop getShop() {
            return shop;
        }
    
        public void setShop(Shop shop) {
            this.shop = shop;
        }
    }
    

    创建tb_product数据表

    use o2o;
    CREATE TABLE `tb_product` (
      `product_id` int(100) NOT NULL AUTO_INCREMENT,
      `product_name` varchar(100) NOT NULL,
      `product_desc` varchar(2000) DEFAULT NULL,
      `img_addr` varchar(2000) DEFAULT '',
      `normal_price` varchar(100) DEFAULT NULL,
      `promotion_price` varchar(100) DEFAULT NULL,
      `priority` int(2) NOT NULL DEFAULT '0',
      `create_time` datetime DEFAULT NULL,
      `last_edit_time` datetime DEFAULT NULL,
      `enable_status` int(2) NOT NULL DEFAULT '0',
      `point` int(10) DEFAULT NULL,
      `product_category_id` int(11) DEFAULT NULL,
      `shop_id` int(20) NOT NULL DEFAULT '0',
      PRIMARY KEY (`product_id`),
      KEY `fk_product_shop` (`shop_id`),
      KEY `fk_product_procate` (`product_category_id`),
      CONSTRAINT `fk_product_procate` FOREIGN KEY (`product_category_id`) REFERENCES `tb_product_category` (`product_category_id`),
      CONSTRAINT `fk_product_shop` FOREIGN KEY (`shop_id`) REFERENCES `tb_shop` (`shop_id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
    
    

    详情图片

    新建ProductImg

    package com.imooc.o2o.entity;
    
    import java.util.Date;
    
    public class ProductImg {
        private Long productImgId;
        private String imgAddr;
        private String imgDesc;
        private Integer priority;
        private Date createTime;
        private Long productId;
    
        public Long getProductImgId() {
            return productImgId;
        }
    
        public void setProductImgId(Long productImgId) {
            this.productImgId = productImgId;
        }
    
        public String getImgAddr() {
            return imgAddr;
        }
    
        public void setImgAddr(String imgAddr) {
            this.imgAddr = imgAddr;
        }
    
        public String getImgDesc() {
            return imgDesc;
        }
    
        public void setImgDesc(String imgDesc) {
            this.imgDesc = imgDesc;
        }
    
        public Integer getPriority() {
            return priority;
        }
    
        public void setPriority(Integer priority) {
            this.priority = priority;
        }
    
        public Date getCreateTime() {
            return createTime;
        }
    
        public void setCreateTime(Date createTime) {
            this.createTime = createTime;
        }
    
        public Long getProductId() {
            return productId;
        }
    
        public void setProductId(Long productId) {
            this.productId = productId;
        }
    }
    

    创建tb_product_img数据表

    use o2o;
    CREATE TABLE `tb_product_img`(
      `product_img_id` int(20) NOT NULL AUTO_INCREMENT,
      `img_addr` varchar(2000) NOT NULL,
      `img_desc` varchar(2000) NOT NULL,
      `priority` int(2) DEFAULT '0',
      `create_time` datetime DEFAULT NULL,
      `product_id` int(20) DEFAULT NULL,
      PRIMARY KEY(`product_img_id`),
      CONSTRAINT `fk_proimg_product` FOREIGN KEY(`product_id`) REFERENCES `tb_product`(`product_id`)
      )ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
    

    配置

    Maven配置

    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/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.imooc.demo</groupId>
        <artifactId>myo2o</artifactId>
        <packaging>war</packaging>
        <version>0.0.1-SNAPSHOT</version>
        <name>myo2o Maven Webapp</name>
        <url>http://maven.apache.org</url>
        <properties>
            <spring.version>4.3.7.RELEASE</spring.version>
        </properties>
    
    
        <dependencies>
            <!-- 单元测试 -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
            </dependency>
    
            <!-- 1.日志 -->
            <!-- 实现slf4j接口并整合 -->
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
                <version>1.2.1</version>
            </dependency>
    
            <!-- 2.数据库 -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.37</version>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>c3p0</groupId>
                <artifactId>c3p0</artifactId>
                <version>0.9.1.2</version>
            </dependency>
    
            <!-- DAO: MyBatis -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.4.2</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>1.3.1</version>
            </dependency>
    
            <!-- 3.Servlet web -->
            <dependency>
                <groupId>taglibs</groupId>
                <artifactId>standard</artifactId>
                <version>1.1.2</version>
            </dependency>
            <dependency>
                <groupId>jstl</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>2.8.7</version>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>3.1.0</version>
            </dependency>
    
            <!-- 4.Spring -->
            <!-- 1)Spring核心 -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-beans</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <!-- 2)Spring DAO层 -->
            <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>
            <!-- 3)Spring web -->
            <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>
            <!-- 4)Spring test -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>${spring.version}</version>
            </dependency>
    
            <!-- redis客户端:Jedis -->
            <dependency>
                <groupId>redis.clients</groupId>
                <artifactId>jedis</artifactId>
                <version>2.9.0</version>
            </dependency>
            <dependency>
                <groupId>com.dyuproject.protostuff</groupId>
                <artifactId>protostuff-core</artifactId>
                <version>1.0.12</version>
            </dependency>
            <dependency>
                <groupId>com.dyuproject.protostuff</groupId>
                <artifactId>protostuff-runtime</artifactId>
                <version>1.0.12</version>
            </dependency>
    
            <!-- Map工具类 -->
            <dependency>
                <groupId>commons-collections</groupId>
                <artifactId>commons-collections</artifactId>
                <version>3.2</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/net.coobird/thumbnailator -->
            <dependency>
                <groupId>net.coobird</groupId>
                <artifactId>thumbnailator</artifactId>
                <version>0.4.8</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/com.github.penggle/kaptcha -->
            <dependency>
                <groupId>com.github.penggle</groupId>
                <artifactId>kaptcha</artifactId>
                <version>2.3.2</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
            <dependency>
                <groupId>commons-fileupload</groupId>
                <artifactId>commons-fileupload</artifactId>
                <version>1.3.2</version>
            </dependency>
            <!-- wechat相关 -->
            <!-- https://mvnrepository.com/artifact/net.sf.json-lib/json-lib -->
            <dependency>
                <groupId>net.sf.json-lib</groupId>
                <artifactId>json-lib</artifactId>
                <version>2.4</version>
                <classifier>jdk15</classifier>
            </dependency>
            <!-- https://mvnrepository.com/artifact/com.thoughtworks.xstream/xstream -->
            <dependency>
                <groupId>com.thoughtworks.xstream</groupId>
                <artifactId>xstream</artifactId>
                <version>1.4.9</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/org.dom4j/dom4j -->
            <dependency>
                <groupId>org.dom4j</groupId>
                <artifactId>dom4j</artifactId>
                <version>2.0.0</version>
            </dependency>
            <!-- 二维码相关 -->
            <!-- https://mvnrepository.com/artifact/com.google.zxing/javase -->
            <dependency>
                <groupId>com.google.zxing</groupId>
                <artifactId>javase</artifactId>
                <version>3.3.0</version>
            </dependency>
    
    
        </dependencies>
        <build>
            <finalName>myo2o</finalName>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.6.0</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                        <encoding>UTF8</encoding>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
    

    数据库文件配置

    新建一个resources文件夹


    image.png

    创建jdbc.properties文件

    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/o2o?useUnicode=true&characterEncoding=utf8
    jdbc.username=root
    jdbc.password=123456
    

    mybatis配置

    <?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>
            <!-- 使用jdbc的getGeneratedKeys获取数据库自增主键值 -->
            <setting name="useGeneratedKeys" value="true" />
    
            <!-- 使用列别名替换列名 默认:true -->
            <setting name="useColumnLabel" value="true" />
    
            <!-- 开启驼峰命名转换:Table{create_time} -> Entity{createTime} -->
            <setting name="mapUnderscoreToCamelCase" value="true" />
            <!-- 打印查询语句 -->
            <setting name="logImpl" value="STDOUT_LOGGING" />
        </settings>
    </configuration>
    

    spring配置

    在resources下新建一个spring文件夹


    image.png
    • 新建spring-dao.xml文件,中包括了连接池的配置,mybatis的orm配置,以及事务处理
    <?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"
           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">
        <!-- 配置整合mybatis过程 -->
        <!-- 1.配置数据库相关参数properties的属性:${url} -->
        <context:property-placeholder location="classpath:jdbc.properties" />
    
        <!-- 2.数据库连接池 -->
        <bean id="dataSource" 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}" />
    
            <!-- c3p0连接池的私有属性 -->
            <property name="maxPoolSize" value="30" />
            <property name="minPoolSize" value="10" />
            <!-- 关闭连接后不自动commit -->
            <property name="autoCommitOnClose" value="false" />
            <!-- 获取连接超时时间 -->
            <property name="checkoutTimeout" value="10000" />
            <!-- 当获取连接失败重试次数 -->
            <property name="acquireRetryAttempts" value="2" />
        </bean>
    
        <!-- 3.配置SqlSessionFactory对象 -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <!-- 注入数据库连接池 -->
            <property name="dataSource" ref="dataSource" />
            <!-- 配置MyBaties全局配置文件:mybatis-config.xml -->
            <property name="configLocation" value="classpath:mybatis-config.xml" />
            <!-- 扫描entity包 使用别名 -->
            <property name="typeAliasesPackage" value="com.imooc.o2o.entity" />
            <!-- 扫描sql配置文件:mapper需要的xml文件 -->
            <property name="mapperLocations" value="classpath:mapper/*.xml" />
        </bean>
    
        <!-- 4.配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 -->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <!-- 注入sqlSessionFactory -->
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
            <!-- 给出需要扫描Dao接口包 -->
            <property name="basePackage" value="com.imooc.o2o.dao" />
        </bean>
    </beans>
    
    • 新建spring-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:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx"
        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">
        <!-- 扫描service包下所有使用注解的类型 -->
        <context:component-scan base-package="com.imooc.myo2o.service" />
    
        <!-- 配置事务管理器 -->
        <bean id="transactionManager"
            class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <!-- 注入数据库连接池 -->
            <property name="dataSource" ref="dataSource" />
        </bean>
    
        <!-- 配置基于注解的声明式事务 -->
        <tx:annotation-driven transaction-manager="transactionManager" />
    </beans>
    
    • 新建spring-web.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/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/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
        <!-- 配置SpringMVC -->
        <!-- 1.开启SpringMVC注解模式 -->
        <!-- 简化配置: (1)自动注册DefaultAnootationHandlerMapping,AnotationMethodHandlerAdapter 
            (2)提供一些列:数据绑定,数字和日期的format @NumberFormat, @DateTimeFormat, xml,json默认读写支持 -->
        <mvc:annotation-driven />
    
        <!-- 2.静态资源默认servlet配置 (1)加入对静态资源的处理:js,gif,png (2)允许使用"/"做整体映射 -->
        <mvc:resources mapping="/resources/**" location="/resources/" />
        <mvc:default-servlet-handler />
    
        <!-- 3.定义视图解析器 -->
        <bean id="viewResolver"
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/html/"></property>
            <property name="suffix" value=".html"></property>
        </bean>
        <!-- 文件上传解析器 -->
        <bean id="multipartResolver"
            class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
            <property name="defaultEncoding" value="utf-8"></property>
            <property name="maxUploadSize" value="10485760000"></property><!-- 最大上传文件大小 -->
            <property name="maxInMemorySize" value="10960"></property>
        </bean>
        <!-- 在spring-mvc.xml文件中加入这段配置后,spring返回给页面的都是utf-8编码了 -->
        <bean
            class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
            <property name="messageConverters">
                <list>
                    <bean
                        class="org.springframework.http.converter.StringHttpMessageConverter">
                        <property name="supportedMediaTypes">
                            <list>
                                <value>text/html;charset=UTF-8</value>
                            </list>
                        </property>
                    </bean>
                </list>
            </property>
        </bean>
        <!-- 4.扫描web相关的bean -->
        <context:component-scan base-package="com.imooc.myo2o.web" />
    
    </beans>
    

    整合配置

    web.xml文件中

    web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                          http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
             version="3.1" metadata-complete="true">
    
    
        <welcome-file-list>
            <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
        
        <servlet>
            <servlet-name>spring-dispatcher</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:spring/spring-*.xml</param-value>
            </init-param>
        </servlet>
        <servlet-mapping>
            <servlet-name>spring-dispatcher</servlet-name>
            <!-- 默认匹配所在的请求 -->
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    
    </web-app>
    

    测试

    测试dao

    向数据库中添加数据

    image.png
    在dao中新建AreaDao
    package com.imooc.o2o.dao;
    
    import com.imooc.o2o.entity.Area;
    
    import java.util.List;
    
    public interface AreaDao {
        List<Area> queryArea();
    }
    

    在mapper文件中新建AreaDao.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.imooc.o2o.dao.AreaDao">
        <select id="queryArea" resultType="com.imooc.o2o.entity.Area">
            SELECT
            area_id,
            area_name,
            priority,
            create_time,
            last_edit_time
            FROM
            tb_area
            ORDER BY
            priority DESC
        </select>
    </mapper>
    

    如图新建基类BaseTest

    package com.imooc.o2o;
    
    import org.junit.runner.RunWith;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    /**
     * 配置spring和junit整合,junit启动时加载springIOC容器 spring-test,junit
     */
    @RunWith(SpringJUnit4ClassRunner.class)
    // 告诉junit spring配置文件
    @ContextConfiguration({ "classpath:spring/spring-dao.xml",
            "classpath:spring/spring-service.xml" })
    public class BaseTest {
    
    }
    

    测试类

    package com.imooc.o2o.dao;
    
    import static org.junit.Assert.assertEquals;
    
    import java.util.List;
    
    import org.junit.FixMethodOrder;
    import org.junit.Test;
    import org.junit.runners.MethodSorters;
    import org.springframework.beans.factory.annotation.Autowired;
    
    import com.imooc.o2o.BaseTest;
    import com.imooc.o2o.entity.Area;
    
    @FixMethodOrder(MethodSorters.NAME_ASCENDING)
    public class AreaDaoTest extends BaseTest {
        @Autowired
        private AreaDao areaDao;
    
        @Test
        public void testBQueryArea() throws Exception {
            List<Area> areaList = areaDao.queryArea();
            assertEquals(2, areaList.size());
        }
    }
    

    测试成功

    image.png

    说明文件配置正确

    测试service

    image.png
    在service目录下新建AreaService接口
    package com.imooc.o2o.service;
    
    import com.imooc.o2o.entity.Area;
    
    import java.util.List;
    
    public interface AreaService {
        List<Area> getAreaList();
    }
    

    在service目录下新建impl目录,用于实现service接口的实现类

    新建AreaServiceImpl

    package com.imooc.o2o.service.impl;
    
    import com.imooc.o2o.dao.AreaDao;
    import com.imooc.o2o.entity.Area;
    import com.imooc.o2o.service.AreaService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import java.util.List;
    
    @Service
    public class AreaServiceImpl implements AreaService {
    
        @Autowired
        private AreaDao areaDao;
    
        @Override
        public List<Area> getAreaList() {
            return areaDao.queryArea();
        }
    }
    

    测试类

    package com.imooc.o2o.service;
    
    import com.imooc.o2o.BaseTest;
    import com.imooc.o2o.entity.Area;
    import org.junit.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    
    import java.util.List;
    
    import static org.junit.Assert.assertEquals;
    
    public class AreaServiceTest extends BaseTest {
        @Autowired
        private AreaService areaService;
        @Test
        public void testGetAreaList() {
            List<Area> areaList = areaService.getAreaList();
            assertEquals("东苑", areaList.get(0).getAreaName());
        }
    }
    

    测试成功


    image.png

    测试Controller

    我们从超级管理员的维度来编写我的controller
    新建web.superadmin包


    image.png
    package com.imooc.o2o.web.superadmin;
    
    import com.imooc.o2o.entity.Area;
    import com.imooc.o2o.service.AreaService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    @Controller
    @RequestMapping("superadmin")
    public class AreaController {
        @Autowired
        private AreaService areaService;
    
        @RequestMapping(value = "/listareas", method = RequestMethod.GET)
        // 转化为json对象
        @ResponseBody
        private Map<String, Object> listArea() {
            Map<String, Object> modelMap = new HashMap<String, Object>();
            List<Area> list = new ArrayList<Area>();
            try {
                list = areaService.getAreaList();
                modelMap.put("rows", list);
                modelMap.put("total", list.size());
            } catch (Exception e) {
                e.printStackTrace();
                modelMap.put("success", false);
                modelMap.put("errMsg", e.toString());
            }
            return modelMap;
        }
    }
    

    测试
    我使用google插件Postman发送请求
    路由使用下面3个组成
    http://localhost:8080/project2/superadmin/listareas

    image.png
    测试结果
    image.png

    SSM重点

    • SpringMVC:DispatcherServlet
      是整个MVC框架中最核心一块,主要拦截符合要求的请求,并把请求分发到不同的控制中,根据控制器处理后的结果生成相应的响应,发送到客户端
    • Spring:IOC和AOP
      IOC:依赖注入
      AOP:面向切面编程,实现通过动态代理
    • MyBatis:ORM
      对象关系映射(Object Relational Mapping),将程序中的对象自动持久化到数据库

    相关文章

      网友评论

        本文标题:第2章 项目设计和框架搭建

        本文链接:https://www.haomeiwen.com/subject/hobflftx.html