项目案例:商城Market(1)—— 数据表分析设计与框架搭建

一、项目的数据库与表的分析与设计

  • 使用 PowerDesigner 工具设计模型 PDM
  • 表结构.png
  • PowerDesigner结构.png
    • 设计完成后生成数据库的 .sql文件
    /*
     Navicat MySQL Data Transfer
     Source Server         : MySQL
     Source Server Type    : MySQL
     Source Server Version : 80018
     Source Host           : localhost:3307
     Source Schema         : supermarket
     Target Server Type    : MySQL
     Target Server Version : 80018
     File Encoding         : 65001
     Date: 24/03/2020 15:38:30
    */
    SET NAMES utf8mb4;
    SET FOREIGN_KEY_CHECKS = 0;
    -- ----------------------------
    -- Table structure for address
    -- ----------------------------
    DROP TABLE IF EXISTS `address`;
    CREATE TABLE `address`  (
      `did` int(11) NOT NULL AUTO_INCREMENT,
      `user_id` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
      `province` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
      `city` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
      `township` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
      `detial` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
      `types` int(11) NULL DEFAULT 1,
      PRIMARY KEY (`did`) USING BTREE,
      INDEX `FK_u_a`(`user_id`) USING BTREE,
      CONSTRAINT `FK_u_a` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`) ON DELETE RESTRICT ON UPDATE RESTRICT
    ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
    -- ----------------------------
    -- Table structure for cart
    -- ----------------------------
    DROP TABLE IF EXISTS `cart`;
    CREATE TABLE `cart`  (
      `cid` int(11) NOT NULL AUTO_INCREMENT,
      `user_id` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
      `fdid` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
      `count` int(11) NOT NULL,
      PRIMARY KEY (`cid`) USING BTREE,
      INDEX `FK_fg_c`(`fdid`) USING BTREE,
      INDEX `FK_u_c`(`user_id`) USING BTREE,
      CONSTRAINT `FK_fg_c` FOREIGN KEY (`fdid`) REFERENCES `fresh_goods` (`fdid`) ON DELETE RESTRICT ON UPDATE RESTRICT,
      CONSTRAINT `FK_u_c` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`) ON DELETE RESTRICT ON UPDATE RESTRICT
    ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
    -- ----------------------------
    -- Table structure for citys
    -- ----------------------------
    DROP TABLE IF EXISTS `citys`;
    CREATE TABLE `citys`  (
      `cid` int(11) NOT NULL AUTO_INCREMENT,
      `cname` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
      `pid` int(11) NOT NULL,
      PRIMARY KEY (`cid`) USING BTREE,
      INDEX `FK_p_c`(`pid`) USING BTREE,
      CONSTRAINT `FK_p_c` FOREIGN KEY (`pid`) REFERENCES `province` (`pid`) ON DELETE RESTRICT ON UPDATE RESTRICT
    ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
    -- ----------------------------
    -- Table structure for courier
    -- ----------------------------
    DROP TABLE IF EXISTS `courier`;
    CREATE TABLE `courier`  (
      `account` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
      `name` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
      `tel` varchar(11) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
      PRIMARY KEY (`account`) USING BTREE
    ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
    -- ----------------------------
    -- Table structure for emp_role
    -- ----------------------------
    DROP TABLE IF EXISTS `emp_role`;
    CREATE TABLE `emp_role`  (
      `eid` int(11) NOT NULL,
      `rid` int(11) NOT NULL,
      PRIMARY KEY (`eid`, `rid`) USING BTREE,
      INDEX `FK_r_er`(`rid`) USING BTREE,
      CONSTRAINT `FK_e_er` FOREIGN KEY (`eid`) REFERENCES `employee` (`eid`) ON DELETE RESTRICT ON UPDATE RESTRICT,
      CONSTRAINT `FK_r_er` FOREIGN KEY (`rid`) REFERENCES `roles` (`rid`) ON DELETE RESTRICT ON UPDATE RESTRICT
    ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
    -- ----------------------------
    -- Table structure for employee
    -- ----------------------------
    DROP TABLE IF EXISTS `employee`;
    CREATE TABLE `employee`  (
      `eid` int(11) NOT NULL AUTO_INCREMENT,
      `ename` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
      `password` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
      `tel` varchar(11) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
      PRIMARY KEY (`eid`) USING BTREE
    ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
    -- ----------------------------
    -- Table structure for evaluation
    -- ----------------------------
    DROP TABLE IF EXISTS `evaluation`;
    CREATE TABLE `evaluation`  (
      `eid` int(11) NOT NULL,
      `user_id` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
      `fdid` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
      `detial` text CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
      `grade` int(11) NOT NULL DEFAULT 5,
      `eva_date` datetime(0) NULL DEFAULT CURRENT_TIMESTAMP,
      PRIMARY KEY (`eid`) USING BTREE,
      INDEX `FK_fg_e`(`fdid`) USING BTREE,
      INDEX `FK_u_e`(`user_id`) USING BTREE,
      CONSTRAINT `FK_fg_e` FOREIGN KEY (`fdid`) REFERENCES `fresh_goods` (`fdid`) ON DELETE RESTRICT ON UPDATE RESTRICT,
      CONSTRAINT `FK_u_e` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`) ON DELETE RESTRICT ON UPDATE RESTRICT
    ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
    -- ----------------------------
    -- Table structure for fresh_goods
    -- ----------------------------
    DROP TABLE IF EXISTS `fresh_goods`;
    CREATE TABLE `fresh_goods`  (
      `fdid` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
      `good_name` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
      `gtid` int(11) NOT NULL,
      `img` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
      `price` double NOT NULL,
      `discount` int(11) NOT NULL DEFAULT 0,
      `preference` int(11) NOT NULL,
      `if_promotion` int(11) NOT NULL DEFAULT 0,
      `last_sales` int(11) NOT NULL,
      `current_sales` int(11) NOT NULL,
      `count_sales` int(11) NOT NULL,
      `summery` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
      `attr1` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
      `attr2` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
      PRIMARY KEY (`fdid`) USING BTREE,
      INDEX `FK_gt_fg`(`gtid`) USING BTREE,
      CONSTRAINT `FK_gt_fg` FOREIGN KEY (`gtid`) REFERENCES `good_type` (`gtid`) ON DELETE RESTRICT ON UPDATE RESTRICT
    ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
    -- ----------------------------
    -- Table structure for good_type
    -- ----------------------------
    DROP TABLE IF EXISTS `good_type`;
    CREATE TABLE `good_type`  (
      `gtid` int(11) NOT NULL AUTO_INCREMENT,
      `gtname` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
      PRIMARY KEY (`gtid`) USING BTREE
    ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
    -- ----------------------------
    -- Table structure for logistics
    -- ----------------------------
    DROP TABLE IF EXISTS `logistics`;
    CREATE TABLE `logistics`  (
      `lid` int(11) NOT NULL,
      `account` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
      `user_id` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
      `oid` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
      `pay_time` datetime(0) NOT NULL,
      `package_time` datetime(0) NULL DEFAULT NULL,
      `begin_time` datetime(0) NULL DEFAULT NULL,
      `end_time` datetime(0) NULL DEFAULT NULL,
      `status` int(11) NOT NULL DEFAULT 0,
      PRIMARY KEY (`lid`) USING BTREE,
      INDEX `FK_c_l`(`account`) USING BTREE,
      INDEX `FK_o_l`(`oid`) USING BTREE,
      INDEX `FK_u_l`(`user_id`) USING BTREE,
      CONSTRAINT `FK_c_l` FOREIGN KEY (`account`) REFERENCES `courier` (`account`) ON DELETE RESTRICT ON UPDATE RESTRICT,
      CONSTRAINT `FK_o_l` FOREIGN KEY (`oid`) REFERENCES `orders` (`oid`) ON DELETE RESTRICT ON UPDATE RESTRICT,
      CONSTRAINT `FK_u_l` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`) ON DELETE RESTRICT ON UPDATE RESTRICT
    ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
    -- ----------------------------
    -- Table structure for order_detial
    -- ----------------------------
    DROP TABLE IF EXISTS `order_detial`;
    CREATE TABLE `order_detial`  (
      `did` int(11) NOT NULL AUTO_INCREMENT,
      `oid` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
      `fdid` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
      `count` int(11) NOT NULL,
      PRIMARY KEY (`did`) USING BTREE,
      INDEX `FK_o_od`(`oid`) USING BTREE,
      CONSTRAINT `FK_o_od` FOREIGN KEY (`oid`) REFERENCES `orders` (`oid`) ON DELETE RESTRICT ON UPDATE RESTRICT
    ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
    -- ----------------------------
    -- Table structure for orders
    -- ----------------------------
    DROP TABLE IF EXISTS `orders`;
    CREATE TABLE `orders`  (
      `oid` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
      `user_id` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
      `order_time` datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP,
      `total_price` double NOT NULL,
      `order_status` int(11) NOT NULL DEFAULT 1,
      `pay_type` int(11) NOT NULL,
      PRIMARY KEY (`oid`) USING BTREE,
      INDEX `FK_u_o`(`user_id`) USING BTREE,
      CONSTRAINT `FK_u_o` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`) ON DELETE RESTRICT ON UPDATE RESTRICT
    ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
    -- ----------------------------
    -- Table structure for permissions
    -- ----------------------------
    DROP TABLE IF EXISTS `permissions`;
    CREATE TABLE `permissions`  (
      `pid` int(11) NOT NULL,
      `pname` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
      PRIMARY KEY (`pid`) USING BTREE
    ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
    -- ----------------------------
    -- Table structure for province
    -- ----------------------------
    DROP TABLE IF EXISTS `province`;
    CREATE TABLE `province`  (
      `pid` int(11) NOT NULL AUTO_INCREMENT,
      `pname` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
      PRIMARY KEY (`pid`) USING BTREE
    ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
    -- ----------------------------
    -- Table structure for role_perm
    -- ----------------------------
    DROP TABLE IF EXISTS `role_perm`;
    CREATE TABLE `role_perm`  (
      `rid` int(11) NOT NULL,
      `pid` int(11) NOT NULL,
      PRIMARY KEY (`rid`, `pid`) USING BTREE,
      INDEX `FK_p_rp`(`pid`) USING BTREE,
      CONSTRAINT `FK_r_rp` FOREIGN KEY (`rid`) REFERENCES `roles` (`rid`) ON DELETE RESTRICT ON UPDATE RESTRICT,
      CONSTRAINT `FK_p_rp` FOREIGN KEY (`pid`) REFERENCES `permissions` (`pid`) ON DELETE RESTRICT ON UPDATE RESTRICT
    ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
    -- ----------------------------
    -- Table structure for roles
    -- ----------------------------
    DROP TABLE IF EXISTS `roles`;
    CREATE TABLE `roles`  (
      `rid` int(11) NOT NULL,
      `rname` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
      `detial` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
      PRIMARY KEY (`rid`) USING BTREE
    ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
    -- ----------------------------
    -- Table structure for township
    -- ----------------------------
    DROP TABLE IF EXISTS `township`;
    CREATE TABLE `township`  (
      `tid` int(11) NOT NULL,
      `tname` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
      `cid` int(11) NOT NULL,
      PRIMARY KEY (`tid`) USING BTREE,
      INDEX `FK_c_t`(`cid`) USING BTREE,
      CONSTRAINT `FK_c_t` FOREIGN KEY (`cid`) REFERENCES `citys` (`cid`) ON DELETE RESTRICT ON UPDATE RESTRICT
    ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
    -- ----------------------------
    -- Table structure for users
    -- ----------------------------
    DROP TABLE IF EXISTS `users`;
    CREATE TABLE `users`  (
      `user_id` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
      `user_name` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
      `password` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
      `email` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
      `tel` varchar(11) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
      `level` int(11) NULL DEFAULT 1,
      `integral` int(11) NULL DEFAULT 0,
      `if_new` int(11) NULL DEFAULT 0,
      PRIMARY KEY (`user_id`) USING BTREE
    ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
    SET FOREIGN_KEY_CHECKS = 1;
    

二、创建项目并搭建框架

  • 打开 IDEA 创建一个Maven项目,并连接数据库与配置SSM框架
  • 使用EasyCode创建相应的controller、dao、entity、service 和 mapper 层
  • 将前端页面移植进项目内:链接:https://pan.baidu.com/s/1Qj-ZE8bW3a3vp2vYLaqwRA
    提取码:fb7v
  • 项目搭建完成
  • 测试项目
    • @RunWith(SpringJUnit4ClassRunner.class)
      @ContextConfiguration("classpath:spring-mybatis.xml")
      public class TestUserService {
          @Autowired
          private UsersService usersService;
          @Test
          public void testService(){
              Users s = usersService.queryById("100001");
              System.out.println(s);
          }
      }
      
    • 单元测试结果

      INFO [main] - {dataSource-1} inited
       DEBUG [main] - ==>  Preparing: select user_id, user_name, password, email, tel, level, integral, if_new from supermarket.users where user_id = ? 
       DEBUG [main] - ==> Parameters: 100001(String)
       TRACE [main] - <==    Columns: user_id, user_name, password, email, tel, level, integral, if_new
       TRACE [main] - <==        Row: 100001, AAA, 1236987, 123456@abc.com, 18888888888, 1, 0, 0
       DEBUG [main] - <==      Total: 1
      com.entity.Users@687ef2e0
        INFO [Thread-1] - {dataSource-1} closed
      
    • UsersController.java

      @RestController
      @RequestMapping("users")
      public class UsersController {
          @Resource
          private UsersService usersService;
          /**
           * 通过主键查询单条数据
           * @param id 主键
           * @return 单条数据
           */
          @GetMapping("selectOne/{id}")
          public Users selectOne(@PathVariable String id) {
              return this.usersService.queryById(id);
          }
          @GetMapping("/selectByPage")
          public List<Users> getUsersByPage(@RequestParam(value = "cp",defaultValue = "1") int currentPage ,
                                        @RequestParam(value = "ps",defaultValue = "5") int pageSize){
              currentPage = currentPage <= 1 ? 1 : currentPage;
              pageSize = pageSize <= 0 ? 10 : pageSize;
              List list = usersService.queryAllByLimit((currentPage - 1) * pageSize,pageSize);
              return list;
          }
      }
      
  • 打开浏览器输入:http://localhost:8081/users/selectByPage 测试是否成功
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,723评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,485评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,998评论 0 344
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,323评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,355评论 5 374
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,079评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,389评论 3 400
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,019评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,519评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,971评论 2 325
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,100评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,738评论 4 324
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,293评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,289评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,517评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,547评论 2 354
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,834评论 2 345

推荐阅读更多精彩内容