• (三)项目搭建


    Maven

    安装好maven之后(这里用的是3.6.1版本,下载地址https://archive.apache.org/dist/maven/maven-3/3.6.1/binaries/),修改conf目录下的settings.xml文件,修改以下内容。

    配置阿里云镜像加速

    <mirrors>
        <mirror>
            <id>nexus-aliyun</id>
            <mirrorOf>central</mirrorOf>
            <name>Nexus aliyun</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public</url>
        </mirror>
    </mirrors>
    

    配置jdk1.8编译项目

    <profiles>
        <profile>
            <id>jdk-1.8</id>
            <activation>
                <activeByDefault>true</activeByDefault>
                <jdk>1.8</jdk>
                </activation>
            <properties>
                <maven.compiler.source>1.8</maven.compiler.source>
                <maven.compiler.target>1.8</maven.compiler.target>
                <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
            </properties>
        </profile>
    </profiles>
    

    Idea安装插件

    idea安装lombok,mybatisX插件。

    VSCode安装插件

    Vscode 安装开发必备插件

    Vetur —— 语法高亮、智能感知、Emmet 等

    包含格式化功能, Alt+Shift+F (格式化全文),Ctrl+K Ctrl+F(格式化选中代码,两个 Ctrl需要同时按着)

    EsLint —— 语法纠错

    Auto Close Tag —— 自动闭合 HTML/XML 标签

    Auto Rename Tag —— 自动完成另一侧标签的同步修改

    JavaScript(ES6) code snippets — — ES6 语 法 智 能 提 示 以 及 快 速 输 入 , 除 js 外 还 支

    持.ts,.jsx,.tsx,.html,.vue,省去了配置其支持各种包含 js 代码文件的时间

    HTML CSS Support —— 让 html 标签上写 class 智能提示当前项目所支持的样式

    HTML Snippets —— html 快速自动补全

    Open in browser —— 浏览器快速打开

    Live Server —— 以内嵌服务器方式打开

    Chinese (Simplified) Language Pack for Visual Studio Code —— 中文语言包

    创建项目

    在码云新建仓库,仓库名gulimall,选择语言java,在.gitignore选中maven,许可证选Apache-2.0,开发模型选生成/开发模型,开发时在dev分支,发布时在master分支,创建。

    在IDEA中New–Project from version control–git–复制刚才项目的地址,如https://gitee.com/hanferm/gulimall.git

    然后New Module–Spring Initializer–com.atguigu.gulimall , Artifact填 gulimall-product。Next—选择web,springcloud routin里选中openFeign。

    依次创建出以下服务

    • 商品服务product
    • 存储服务ware
    • 订单服务order
    • 优惠券服务coupon
    • 用户服务member

    共同点:

    • 导入web和openFeign
    • group:com.atguigu.gulimall
    • Artifact:gulimall-XXX
    • 每一个服务,包名com.atguigu.gulimall.XXX{product/order/ware/coupon/member}
    • 模块名:gulimall-XXX

    然后右下角显示了springboot的service选项,选择他

    从某个项目粘贴个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>
    
    	<groupId>com.atguigu.gulimall</groupId>
    	<artifactId>gulimall</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
    	<name>gulimall</name>
    	<description>聚合服务</description>
    
    	<packaging>pom</packaging>
    
    	<modules>
    		<module>gulimall-coupon</module>
    		<module>gulimall-member</module>
    		<module>gulimall-order</module>
    		<module>gulimall-product</module>
    		<module>gulimall-ware</module>
    
    	</modules>
    
    
    </project>
    
    
    

    在maven窗口刷新,并点击+号,找到刚才的pom.xml添加进来,发现多了个root。这样比如运行root的clean命令,其他项目也一起clean了。

    创建数据库

    开启虚拟机,在docker中运行mysql。为了避免每次重启都要启动mysql,设置为开机自启动。

    sudo docker ps
    sudo docker ps -a
    # 这两个命令的差别就是后者会显示  【已创建但没有启动的容器】
    
    # 我们接下来设置我们要用的容器每次都是自动启动
    sudo docker update redis --restart=always
    sudo docker update mysql --restart=always
    # 如果不配置上面的内容的话,我们也可以选择手动启动
    sudo docker start mysql
    sudo docker start redis
    # 如果要进入已启动的容器
    sudo docker exec -it mysql /bin/bash
    
    

    为每个服务创建一个数据库,去sqlyog直接我们的操作,在左侧root上右键建立数据库:字符集选utf8mb4,他能兼容utf8且能解决一些乱码的问题。分别建立了下面数据库。

    gulimall-oms
    gulimall-pms
    gulimall-sms
    gulimall-ums
    gulimall-wms
    
    

    人人开源

    在码云上搜索人人开源,我们使用renren-fast,renren-fast-vue项目。

    https://gitee.com/renrenio

    git clone https://gitee.com/renrenio/renren-fast.git
    
    git clone https://gitee.com/renrenio/renren-fast-vue.git
    123
    

    下载到了桌面,我们把renren-fast移动到我们的项目文件夹(删掉.git文件),而renren-vue是用VSCode打开的(后面再弄)

    在项目里的pom.xml添加一个

    <modules>
        <module>gulimall-coupon</module>
        <module>gulimall-member</module>
        <module>gulimall-order</module>
        <module>gulimall-product</module>
        <module>gulimall-ware</module>
    
        <module>renren-fast</module>
    </modules>
    123456789
    

    然后打开renren-fast/db/mysql.sql,复制全部,在sqlyog中创建库gulimall-admin,粘贴刚才的内容执行。

    然后修改项目里renren-fast中的application.yml,修改application-dev.yml中的数库库的url,通常把localhost修改为192.168.56.10即可。

    url: jdbc:mysql://192.168.56.10:3306/gulimall-admin?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
    username: root
    password: root
    123
    

    然后执行java下的RenrenApplication

    浏览器输入http://localhost:8080/renren-fast/ 得到{“msg”:“invalid token”,“code”:401}就代表无误

    人人vue

    用VSCode打开renren-fast-vue

    安装node:http://nodejs.cn/download/ 选择windows下载。下载完安装。

    NPM是随同NodeJS一起安装的包管理工具,如JavaScript-NPM,java-Maven。

    命令行输入node -v 检查配置好了,配置npm的镜像仓库地址,再执

    node -v
    npm config set registry http://registry.npm.taobao.org/
    12
    

    然后取VScode的终端中输入 npm install,会报错,解决方法如下:

    [使用npm install安装前端项目依赖时报错]()

    逆向工程生成项目代码

    逆向工程搭建

    git clone https://gitee.com/renrenio/renren-generator.git
    1
    

    下载到桌面后,同样把里面的.git文件删除,然后移动到我们IDEA项目目录中,同样配置好pom.xml

    <modules>
    		<module>gulimall-coupon</module>
    		<module>gulimall-member</module>
    		<module>gulimall-order</module>
    		<module>gulimall-product</module>
    		<module>gulimall-ware</module>
    		<module>renren-fast</module>
    		<module>renren-generator</module>
    	</modules>
    123456789
    

    在maven中刷新一下,让项目名变粗体,稍等下面进度条完成。

    修改application.yml

    url: jdbc:mysql://192.168.56.10:3306/gulimall-pms?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
    username: root
    password: root
    123
    

    然后修改generator.properties(这里乱码的百度IDEA设置properties编码)

    # 主目录
    mainPath=com.atguigu
    #包名
    package=com.atguigu.gulimall
    #模块名
    moduleName=product
    #作者
    author=hh
    #email
    email=55333@qq.com
    #表前缀(类名不会包含表前缀) # 我们的pms数据库中的表的前缀都pms
    # 如果写了表前缀,每一张表对于的javaBean就不会添加前缀了
    tablePrefix=pms_
    12345678910111213
    

    运行RenrenApplication。如果启动不成功,修改application中是port为801。访问http://localhost:801/

    在网页上下方点击每页显示50个(pms库中的表),以让全部都显示,然后点击全部,点击生成代码。下载了压缩包

    解压压缩包,把main放到gulimall-product的同级目录下。

    然后在项目上右击(在项目上右击很重要)new modules— maven—然后在name上输入gulimall-common。

    在pom.xml中也自动添加了<module>gulimall-common</module>

    在common项目的pom.xml中添加

    <!-- mybatisPLUS-->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.3.2</version>
    </dependency>
    <!--简化实体类,用@Data代替getset方法-->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.8</version>
    </dependency>
    <!-- httpcomponent包https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpcore</artifactId>
        <version>4.4.13</version>
    </dependency>
    <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>2.6</version>
    </dependency>
    1234567891011121314151617181920212223
    

    我们把每个微服务里公共的类和依赖放到common里。

    tips: shift+F6修改项目名

    然后在product项目中的pom.xml中加入下面内容

    <dependency>
        <groupId>com.atguigu.gulimall</groupId>
        <artifactId>gulimall-common</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
    12345
    

    复制

    • renren-fast----utils包下的Query和PageUtils、R、Constant复制到common项目的java/com.atguigu.common.utils下

    • 把@RequiresPermissions这些注解掉,因为是shiro的

    • 复制renren-fast中的xss包粘贴到common的com.atguigu.common目录下。

    • 还复制了exception文件夹,对应的位置关系自己观察一下就行

    • 注释掉product项目下类中的//import org.apache.shiro.authz.annotation.RequiresPermissions;,他是shiro的东西

    • 注释renren-generatorsrcmain esources emplate/Controller中所有的@RequiresPermissions。## import org.apache.shiro.authz.annotation.RequiresPermissions;

    总之什么报错就去fast里面找。重启逆向工程。重新在页面上得到压缩包。重新解压出来,不过只把里面的controller复制粘贴到product项目对应的目录就行。

    测试

    测试与整合商品服务里的mybatisplus

    https://mp.baomidou.com/guide/quick-start.html#配置

    在common的pom.xml中导入

    <!-- 数据库驱动 https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.17</version>
    </dependency>
    <!--tomcat里一般都带-->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>
    12345678910111213
    

    删掉common里xss/xssfiler和XssHttpServletRequestWrapper

    在product项目的resources目录下新建application.yml

    spring:
      datasource:
        username: root
        password: root
        url: jdbc:mysql://192.168.56.10:3306/gulimall-pms
        driver-class-name: com.mysql.jdbc.Driver
    
    # MapperScan
    # sql映射文件位置
    mybatis-plus:
      mapper-locations: classpath:/mapper/**/*.xml
      global-config:
        db-config:
          id-type: auto
    1234567891011121314
    

    classpath 和 classpath* 区别:
    classpath:只会到你的class路径中查找找文件;
    classpath*:不仅包含class路径,还包括jar文件中(class路径)进行查找

    classpath*的使用:当项目中有多个classpath路径,并同时加载多个classpath路径下(此种情况多数不会遇到)的文件,*就发挥了作用,如果不加*,则表示仅仅加载第一个classpath路径。

    然而执行后能通过,但是数据库中文显示乱码,所以我模仿逆向工程,把上面的配置url改为

    url: jdbc:mysql://192.168.56.10:3306/gulimall-pms?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
    1
    

    正常了。

    然后在主启动类上加上注解@MapperScan()

    @MapperScan("com.atguigu.gulimall.product.dao")
    @SpringBootApplication
    public class gulimallProductApplication {
    
    
        public static void main(String[] args) {
    
            SpringApplication.run(gulimallProductApplication.class, args);
        }
    
    }
    1234567891011
    

    然后去测试,先通过下面方法给数据库添加内容

    @SpringBootTest
    class gulimallProductApplicationTests {
        @Autowired
        BrandService brandService;
    
        @Test
        void contextLoads() {
            BrandEntity brandEntity = new BrandEntity();
            brandEntity.setDescript("哈哈1哈");
            brandEntity.setName("华为");
            brandService.save(brandEntity);
            System.out.println("保存成功");
        }
    }
    1234567891011121314
    

    在数据库中就能看到新增数据了

    @SpringBootTest
    class gulimallProductApplicationTests {
        @Autowired
        BrandService brandService;
    
        @Test
        void contextLoads() {
            BrandEntity brandEntity = new BrandEntity();
            brandEntity.setBrandId(1L);
            brandEntity.setDescript("修改");
            brandService.updateById(brandEntity);
        }
    }
    

    生成项目代码

    member

    重新使用代码生成器生成ums

    模仿上面修改下面两个配置

    代码生成器里:

        url: jdbc:mysql://192.168.56.10:3306/gulimall-ums?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
    1
    # 主目录
    mainPath=com.atguigu
    #包名
    package=com.atguigu.gulimall
    #模块名
    moduleName=member
    #作者
    author=hh
    #email
    email=55333@qq.com
    #表前缀(类名不会包含表前缀) # 我们的pms数据库中的表的前缀都pms
    # 如果写了表前缀,每一张表对于的javaBean就不会添加前缀了
    tablePrefix=ums_
    12345678910111213
    

    重启RenrenApplication.java,然后同样去浏览器获取压缩包解压到对应member项目目录

    member也导入依赖

    <dependency>
        <groupId>com.atguigu.gulimall</groupId>
        <artifactId>gulimall-common</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
    12345
    

    同样新建application.yml

    spring:
      datasource:
        username: root
        password: root
        url: jdbc:mysql://192.168.56.10:3306/gulimall-ums?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
        driver-class-name: com.mysql.cj.jdbc.Driver
    
    
    mybatis-plus:
      mapper-locations: classpath:/mapper/**/*.xml
      global-config:
        db-config:
          id-type: auto
          logic-delete-value: 1
          logic-not-delete-value: 0
    
    server:
      port: 8000
    123456789101112131415161718
    

    order端口是9000,product是10000,ware是11000。

    以后比如order系统要复制多份,他的端口计算9001、9002。。。

    重启web后,http://localhost:8000/member/growthchangehistory/list

    {"msg":"success","code":0,"page":{"totalCount":0,"pageSize":10,"totalPage":0,"currPage":1,"list
    

    其他的几个模块也类似这样操作即可。

  • 相关阅读:
    MVC小系列(六)【无刷新的验证码】
    MVC小系列(五)【在过滤器里引入重定向】
    EF 打造冲不掉的标签
    EF一次请求公用一个实例
    Android 学习资源[转]
    核心ASP.NET
    C#之任务,线程和同步
    史上最全面的正则表达式
    新年程序员福利(多图)
    C#之网络
  • 原文地址:https://www.cnblogs.com/six-hc/p/14327545.html
Copyright © 2020-2023  润新知