SSM搭建博客系統(三):數據庫設計

1、設計思想

項目中計劃實現的功能主要包括如下幾點:php

  • 用戶註冊;
  • 用戶的登陸和退出;
  • 用戶帳號登陸和註冊時須要輸入驗證碼驗證;
  • 首頁展現及分頁,主要展現文章內容,可進行搜索,將搜索結果高亮顯示;
  • 首頁文章的點贊、踩和評論功能;
  • 我的主頁模塊,包括我的的基本信息,文章分類,發佈文章,管理文章以及熱門文章推薦等;
  • 書寫文章功能;
  • 文章管理功能,包括文章的查看、修改和刪除;
  • 我的信息修改功能;
  • 安全框架 spring-security 的整合,對不符合條件的用戶或者 URL 進行攔截;
  • 記錄用戶登陸信息,包括登陸的時間、IP 等;

2、表結構設計

建立數據表

/* Navicat MySQL Data Transfer Source Server : localhost_3306 Source Server Version : 50505 Source Host : localhost:3306 Source Database : blog Target Server Type : MYSQL Target Server Version : 50505 File Encoding : 65001 Date: 2019-03-04 22:08:43 */

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for comments
-- ----------------------------
DROP TABLE IF EXISTS `comments`;
CREATE TABLE `comments` (
  `com_id` bigint(20) NOT NULL,
  `con_id` bigint(20) DEFAULT NULL,
  `user_id` bigint(20) DEFAULT NULL,
  `com_text` text COLLATE utf8_bin,
  `com_time` datetime DEFAULT NULL,
  `com_subid` varchar(255) COLLATE utf8_bin DEFAULT NULL,
  `com_upvote` int(100) DEFAULT NULL,
  PRIMARY KEY (`com_id`),
  KEY `FK_COMMENTS_REFERENCE_USER_CON` (`con_id`),
  CONSTRAINT `FK_COMMENTS_REFERENCE_USER_CON` FOREIGN KEY (`con_id`) REFERENCES `user_content` (`con_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

-- ----------------------------
-- Table structure for resource
-- ----------------------------
DROP TABLE IF EXISTS `resource`;
CREATE TABLE `resource` (
  `res_id` bigint(20) NOT NULL,
  `res_name` varchar(255) COLLATE utf8_bin DEFAULT NULL,
  `res_url` varchar(255) COLLATE utf8_bin DEFAULT NULL,
  `res_enable` tinyint(1) DEFAULT NULL,
  `res_comment` varchar(100) COLLATE utf8_bin DEFAULT NULL,
  PRIMARY KEY (`res_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

-- ----------------------------
-- Table structure for role
-- ----------------------------
DROP TABLE IF EXISTS `role`;
CREATE TABLE `role` (
  `role_id` bigint(20) NOT NULL,
  `role_name` varchar(255) COLLATE utf8_bin DEFAULT NULL,
  `role_value` char(11) COLLATE utf8_bin DEFAULT NULL,
  `role_url` varchar(255) COLLATE utf8_bin DEFAULT NULL,
  `role_enab` tinyint(1) DEFAULT NULL,
  `role_note` varchar(255) COLLATE utf8_bin DEFAULT NULL,
  PRIMARY KEY (`role_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

-- ----------------------------
-- Table structure for role_resource
-- ----------------------------
DROP TABLE IF EXISTS `role_resource`;
CREATE TABLE `role_resource` (
  `rr_id` bigint(20) NOT NULL,
  `role_id` bigint(20) NOT NULL,
  `res_id` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`rr_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

-- ----------------------------
-- Table structure for role_user
-- ----------------------------
DROP TABLE IF EXISTS `role_user`;
CREATE TABLE `role_user` (
  `ru_id` bigint(20) NOT NULL,
  `user_id` bigint(20) DEFAULT NULL,
  `role_id` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`ru_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

-- ----------------------------
-- Table structure for upvote
-- ----------------------------
DROP TABLE IF EXISTS `upvote`;
CREATE TABLE `upvote` (
  `upu_id` bigint(20) NOT NULL,
  `user_id` bigint(20) DEFAULT NULL,
  `user_ip` varchar(255) COLLATE utf8_bin DEFAULT NULL,
  `upv_state` tinyint(1) DEFAULT NULL,
  `upv_time` date DEFAULT NULL,
  PRIMARY KEY (`upu_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `user_id` bigint(20) NOT NULL,
  `user_email` varchar(255) COLLATE utf8_bin DEFAULT NULL,
  `user_password` varchar(225) COLLATE utf8_bin DEFAULT NULL,
  `user_phone` char(11) COLLATE utf8_bin DEFAULT NULL,
  `user_nickname` varchar(225) COLLATE utf8_bin DEFAULT NULL,
  `user_state` tinyint(1) DEFAULT NULL,
  `user_img` varchar(255) COLLATE utf8_bin DEFAULT NULL,
  `user_enable` tinyint(1) DEFAULT NULL,
  PRIMARY KEY (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

-- ----------------------------
-- Table structure for user_content
-- ----------------------------
DROP TABLE IF EXISTS `user_content`;
CREATE TABLE `user_content` (
  `con_id` bigint(20) NOT NULL,
  `user_id` bigint(20) DEFAULT NULL,
  `con_title` varchar(255) COLLATE utf8_bin DEFAULT NULL,
  `con_cate` varchar(100) COLLATE utf8_bin DEFAULT NULL,
  `con_content` text COLLATE utf8_bin,
  `con_privacy` tinyint(1) DEFAULT NULL,
  `con_time` datetime DEFAULT NULL,
  `user_img` varchar(255) COLLATE utf8_bin DEFAULT NULL,
  `user_name` varchar(255) COLLATE utf8_bin DEFAULT NULL,
  PRIMARY KEY (`con_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

-- ----------------------------
-- Table structure for user_info
-- ----------------------------
DROP TABLE IF EXISTS `user_info`;
CREATE TABLE `user_info` (
  `ui_id` bigint(20) NOT NULL,
  `user_id` bigint(20) DEFAULT NULL,
  `user_name` varchar(255) COLLATE utf8_bin DEFAULT NULL,
  `user_sex` tinyint(1) DEFAULT NULL,
  `user_brithday` date DEFAULT NULL,
  `user_hobby` varchar(255) COLLATE utf8_bin DEFAULT NULL,
  `user_address` varchar(255) COLLATE utf8_bin DEFAULT NULL,
  PRIMARY KEY (`ui_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
複製代碼

3、反向生成實體類

1. 準備好生成工具包

2. 配置生成文件

generatorConfig.xmljava

<?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-connector文件路徑 -->  
    <classPathEntry location="mysql-connector-java-5.1.25-bin.jar"/>    
    <context id="DB2Tables" targetRuntime="MyBatis3">    
        <commentGenerator>    
            <property name="suppressDate" value="true"/>    

            <property name="suppressAllComments" value="true"/>    
        </commentGenerator>    
        <!-- 連接配置 輸入你的數據庫名及密碼 -->  
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/blog" userId="root" password="root">    
        </jdbcConnection>    
        <javaTypeResolver>    
            <property name="forceBigDecimals" value="false"/>    
        </javaTypeResolver>    
        <!-- 生成實體類的路徑,wang.dreamland.www.entity 這個路徑能夠自動生成,可是必須有src這個路徑-->  
        <javaModelGenerator targetPackage="com.blog.entity" targetProject="src">    
            <property name="enableSubPackages" value="true"/>    
            <property name="trimStrings" value="true"/>    
        </javaModelGenerator>    
       <!-- 生成映射的路徑,這個路徑能夠自動生成,可是必須有src這個路徑-->  
       <sqlMapGenerator targetPackage="com.blog.mapping" targetProject="src">    
            <property name="enableSubPackages" value="true"/>    
        </sqlMapGenerator>    
           <!-- 生成接口的路徑,這個路徑能夠自動生成,可是必須有src這個路徑-->  
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.blog.dao" targetProject="src">    
            <property name="enableSubPackages" value="true"/>    
        </javaClientGenerator>    

        <!-- 表名、實體類名稱 -->
        <table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="user_info" domainObjectName="UserInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="resource" domainObjectName="Resource" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="user_content" domainObjectName="UserContent" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="comments" domainObjectName="Comments" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="login_log" domainObjectName="LoginLog" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="role" domainObjectName="Role" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="role_resource" domainObjectName="RoleResource" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="role_user" domainObjectName="RoleUser" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="upvote" domainObjectName="Upvote" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
    </context>    
</generatorConfiguration>  
複製代碼

3. 在該文件中打開cmd,輸入生成命令

java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite
複製代碼

4. 將生成結果拷貝到項目文件夾中

相關文章
相關標籤/搜索