spring boot + Vue + iView前后端分离架构(Mac版) -- (四)后端工程构建

spring boot + Vue + iView前后端分离架构(Mac版) – (四)后端工程构建

小景哥哥博客

一、创建spring boot工程

在我们的hep-admin-web同级下创建后端工程,idea-->file-->new-->module,选中Spring Initializr点击next,安装后续图片步骤操作即可。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

二、配置pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.huerpu.admin</groupId>
    <artifactId>hep-admin-web-core</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>hep-admin-web-core</name>
    <description>This is Huerpu's admin project.</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--通用 Mapper-->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>2.0.4</version>
        </dependency>

        <!--连接池-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.9</version>
        </dependency>
        <!--数据库驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <!--分页插件-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.4</version>
        </dependency>

        <!-- swagger插件 -->
        <dependency>
            <groupId>com.didispace</groupId>
            <artifactId>spring-boot-starter-swagger</artifactId>
            <version>1.2.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <!--<version>3.5.1</version>-->
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>${org.mapstruct.version}</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.6</version>
                <configuration>
                    <configurationFile>
                        ${basedir}/src/main/resources/generator/generatorConfig.xml
                    </configurationFile>
                    <overwrite>true</overwrite>
                    <verbose>true</verbose>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>8.0.19</version>
                    </dependency>
                    <dependency>
                        <groupId>tk.mybatis</groupId>
                        <artifactId>mapper</artifactId>
                        <version>4.0.0</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>

三、执行数据库脚本

创建数据库cs,之后执行数据库脚本。包括四门计算机课程表、用户表、权限表、菜单表、学校信息表。
在这里插入图片描述

/*
 Navicat Premium Data Transfer

 Source Server         : Localhost
 Source Server Type    : MySQL
 Source Server Version : 80011
 Source Host           : localhost:3306
 Source Schema         : cs

 Target Server Type    : MySQL
 Target Server Version : 80011
 File Encoding         : 65001

 Date: 02/04/2020 23:55:31
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for hep_cn
-- ----------------------------
DROP TABLE IF EXISTS `hep_cn`;
CREATE TABLE `hep_cn` (
  `cn_id` int(11) NOT NULL,
  PRIMARY KEY (`cn_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='计算机网络(computer networking)课程';

-- ----------------------------
-- Table structure for hep_co
-- ----------------------------
DROP TABLE IF EXISTS `hep_co`;
CREATE TABLE `hep_co` (
  `co_id` int(11) NOT NULL,
  PRIMARY KEY (`co_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='计算机组成原理(Computer Organization and Design)课程';

-- ----------------------------
-- Table structure for hep_ds
-- ----------------------------
DROP TABLE IF EXISTS `hep_ds`;
CREATE TABLE `hep_ds` (
  `ds_id` int(11) NOT NULL,
  PRIMARY KEY (`ds_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='数据结构(data structure)课程';

-- ----------------------------
-- Table structure for hep_menu
-- ----------------------------
DROP TABLE IF EXISTS `hep_menu`;
CREATE TABLE `hep_menu` (
  `menu_id` int(11) NOT NULL,
  `menu_name` varchar(255) DEFAULT NULL,
  `menu_url` varchar(255) DEFAULT NULL,
  `menu_order` varchar(255) DEFAULT NULL,
  `menu_role` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`menu_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='菜单表';

-- ----------------------------
-- Table structure for hep_os
-- ----------------------------
DROP TABLE IF EXISTS `hep_os`;
CREATE TABLE `hep_os` (
  `os_id` int(11) NOT NULL,
  PRIMARY KEY (`os_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='操作系统(operating system)课程';

-- ----------------------------
-- Table structure for hep_role
-- ----------------------------
DROP TABLE IF EXISTS `hep_role`;
CREATE TABLE `hep_role` (
  `role_id` int(11) NOT NULL,
  PRIMARY KEY (`role_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='用户权限表';

-- ----------------------------
-- Table structure for hep_university
-- ----------------------------
DROP TABLE IF EXISTS `hep_university`;
CREATE TABLE `hep_university` (
  `university_id` int(11) NOT NULL COMMENT '主键',
  `university_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '学校名称',
  `university_college` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '学院名称',
  `university_exam_basic` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '计算机考研公共基础课',
  `university_exam_cs` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '计算机考研专业课',
  `university_exam_interview` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '计算机考研复试内容',
  `university_major_direction` varchar(1000) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '计算机主修研究方向',
  `university_coefficient_difficulty` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '考研难度系数',
  `university_academic_professional` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '学硕还是专硕',
  `university_study_period` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '研究生学习年限(2年、3年还是2.5年)',
  PRIMARY KEY (`university_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='学校信息';

-- ----------------------------
-- Table structure for hep_user
-- ----------------------------
DROP TABLE IF EXISTS `hep_user`;
CREATE TABLE `hep_user` (
  `user_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
  `user_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '用户名',
  `user_nickname` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '用户昵称',
  `user_email` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '邮箱',
  `user_mobile` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '手机号',
  `user_password` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '密码',
  `user_address` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '地址',
  `user_assign_date` datetime DEFAULT NULL COMMENT '注册时间',
  PRIMARY KEY (`user_id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='用户表';

SET FOREIGN_KEY_CHECKS = 1;

四、使用mybatis插件快速生成代码

src-->main-->java创建包com.huerpu.admin.web.core,在该目录下创建controllerdaoentityserviceutil工程结构目录。在src-->main-->resources-->generator下创建配置文件generatorConfig.xml,其内容如下:

<?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>
    <context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
        <property name="beginningDelimiter" value="`"/>
        <property name="endingDelimiter" value="`"/>
        <plugin type="tk.mybatis.mapper.generator.MapperPlugin">
            <property name="mappers" value="tk.mybatis.mapper.common.Mapper"/>
            <!-- 是否区分大小写,默认值 false -->
            <property name="caseSensitive" value="true"/>
            <!-- 是否强制生成注解,默认 false,如果设置为 true,不管数据库名和字段名是否一致,都会生成注解(包含 @Table 和 @Column) -->
            <property name="forceAnnotation" value="true"/>
        </plugin>
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/cs?characterEncoding=utf-8"
                        userId="root"
                        password="123456">
        </jdbcConnection>
        <javaModelGenerator targetPackage="com.huerpu.admin.web.core.entity" targetProject="src/main/java">
        </javaModelGenerator>
        <sqlMapGenerator targetPackage="mybatis/mapper" targetProject="src/main/resources" />
        <javaClientGenerator targetPackage="com.huerpu.admin.web.core.dao" targetProject="src/main/java"  type="XMLMAPPER"/>
        <table tableName="hep_cn" domainObjectName="Cn" mapperName="UserDao">
            <!-- 字段属性是否驼峰展示,true为驼峰展示 -->
            <property name="useActualColumnNames" value="true"></property>
            <generatedKey column="cnId" sqlStatement="JDBC"/>
        </table>
        <table tableName="hep_co" domainObjectName="Co" mapperName="CoDao">
            <!-- 字段属性是否驼峰展示,true为驼峰展示 -->
            <property name="useActualColumnNames" value="true"></property>
            <generatedKey column="coId" sqlStatement="JDBC"/>
        </table>
        <table tableName="hep_ds" domainObjectName="Ds" mapperName="DsDao">
            <!-- 字段属性是否驼峰展示,true为驼峰展示 -->
            <property name="useActualColumnNames" value="true"></property>
            <generatedKey column="dsId" sqlStatement="JDBC"/>
        </table>
        <table tableName="hep_menu" domainObjectName="Menu" mapperName="MenuDao">
            <!-- 字段属性是否驼峰展示,true为驼峰展示 -->
            <property name="useActualColumnNames" value="true"></property>
            <generatedKey column="menuId" sqlStatement="JDBC"/>
        </table>
        <table tableName="hep_os" domainObjectName="Os" mapperName="OsDao">
            <!-- 字段属性是否驼峰展示,true为驼峰展示 -->
            <property name="useActualColumnNames" value="true"></property>
            <generatedKey column="osId" sqlStatement="JDBC"/>
        </table>
        <table tableName="hep_role" domainObjectName="Role" mapperName="RoleDao">
            <!-- 字段属性是否驼峰展示,true为驼峰展示 -->
            <property name="useActualColumnNames" value="true"></property>
            <generatedKey column="roleId" sqlStatement="JDBC"/>
        </table>
        <table tableName="hep_university" domainObjectName="University" mapperName="UniversityDao">
            <!-- 字段属性是否驼峰展示,true为驼峰展示 -->
            <property name="useActualColumnNames" value="true"></property>
            <generatedKey column="universityId" sqlStatement="JDBC"/>
        </table>
        <table tableName="hep_user" domainObjectName="User" mapperName="UserDao">
            <!-- 字段属性是否驼峰展示,true为驼峰展示 -->
            <property name="useActualColumnNames" value="true"></property>
            <generatedKey column="userId" sqlStatement="JDBC"/>
        </table>
    </context>
</generatorConfiguration>

打开idea右侧的maven,找到mybatis-generator,双击运行。就会看到我们的entity、dao、mapper底下会生成文件,则说明插件已经帮助生成了我们想要的代码了。
在这里插入图片描述

五、配置mybatis

修改工程入口程序HepAdminWebCoreApplication如下:

package com.huerpu.admin.web.core;
import com.didispace.swagger.EnableSwagger2Doc;
import tk.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableSwagger2Doc
@MapperScan("com.huerpu.admin.web.core.dao")
public class HepAdminWebCoreApplication {
    public static void main(String[] args) {
        SpringApplication.run(HepAdminWebCoreApplication.class, args);
    }
}

src-->main-->resources下创建文件application.yml、application-dev.yml,二者内容分别如下:

application.yml

spring:
  profiles:
    active: dev

mybatis:
  mapper-locations: classpath:mybatis/mapper/*.xml

pagehelper:
  helperDialect: mysql
  reasonable: true

application-dev.yml

server:
  port: 80

# 配置日志信息
logging:
  level:
    root: INFO
    com.huerpu.admin.web.core: DEBUG

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/cs?characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
    username: root
    password: 123456
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      filters: stat
      maxActive: 20
      initialSize: 1
      maxWait: 60000
      minIdle: 1
      timeBetweenEvictionRunsMillis: 60000
      minEvictableIdleTimeMillis: 300000
      validationQuery: select 'x'
      testWhileIdle: true
      testOnBorrow: false
      testOnReturn: false
      poolPreparedStatements: true
      maxOpenPreparedStatements: 20

swagger:
  title: 上海沪尔浦考研股份有限公司
  description: 沪尔浦拥有最终解释权
  base-package: com.huerpu.admin.web.core.controller
  contact:
    name: 小景哥哥
    email: 1033885715@qq.com

六、User保存逻辑

接下来我们写一个保存User类的验证逻辑,包括UserController、UserService。

UserService:

package com.huerpu.admin.web.core.service;

import com.huerpu.admin.web.core.dao.UserDao;
import com.huerpu.admin.web.core.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {

    @Autowired
    private UserDao userDao;

    public void save(User user){
        userDao.insert(user);
    }

}

UserController:

package com.huerpu.admin.web.core.controller;

import com.huerpu.admin.web.core.entity.User;
import com.huerpu.admin.web.core.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "Find Hep user lists")
    @PostMapping("save")
    public void getUserLists(User user){
        userService.save(user);
    }
}

启动项目:
在这里插入图片描述
在这里插入图片描述

访问http://127.0.0.1/swagger-ui.html#!/user45controller/getUserListsUsingPOST地址,输入保存的参数,点击try it out,返回结果为200证明保存成功,成功之后去数据库核实数据即可。如果有数据,则说明我们的工程构建成功。
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

勤奋的凯尔森同学

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值