秒杀项目之mybatis与微服务注册

目录

一、微服务项目集成MybatisPlus

创建自动生成代码子模块

创建商品服务子模块

二、微服务项目集成Freemarker

三、SpringBoot整合微服务&gateway&nginx

gateway

整合微服务之商品服务zmall-product

创建并配置网关gateway服务

安装配置Windows版nginx

安装配置SwitchHosts

请求链路测试


一、微服务项目集成MybatisPlus

创建自动生成代码子模块

1.基于maven方式创建子模块zmall-generator,用于结合mybatis-plus生成代码。

秒杀项目之mybatis与微服务注册_第1张图片

2. 在zmall-generator中引入mybatis-plus-generator依赖。该模块专用于mybatis-plus的代码生成,所以单独在此引入该依赖即可。



    com.baomidou
    mybatis-plus-generator
    3.4.0

秒杀项目之mybatis与微服务注册_第2张图片

3. 在src/main/resources下创建templates目录,并导入mybatis-generator生成代码模板页

秒杀项目之mybatis与微服务注册_第3张图片

4.在src/main/java下创建包com.zking.zmall,并导入generator下的CodeGenerator类用于代码生成

秒杀项目之mybatis与微服务注册_第4张图片

 CodeGenerator 

package com.zking.zmall.generator;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class CodeGenerator {

    //数据库连接参数
    public static String driver = "com.mysql.jdbc.Driver";
    public static String url = "jdbc:mysql://localhost:3306/zmall?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true";
    public static String username="root";
    public static String password="123456";
    //父级别包名称
    public static String parentPackage = "com.zking.zmall";
    //项目名设置(如果是SpringCloud项目则需要设置,其他为""即可)
    public static String projectName="/zmall-generator";
    //代码生成的目标路径
    public static String generateTo = "/src/main/java";
    //mapper.xml的生成路径
    public static String mapperXmlPath = "/src/main/resources/mapper";
    //控制器的公共基类,用于抽象控制器的公共方法,null值表示没有父类
    public static String baseControllerClassName ;
    //业务层的公共基类,用于抽象公共方法
    public static String baseServiceClassName ;
    //作者名
    public static String author = "zking";
    //模块名称,用于组成包名
    public static String modelName = "model";

    /**
     * 

* 读取控制台内容 *

*/ public static String scanner(String tip) { Scanner scanner = new Scanner(System.in); StringBuilder help = new StringBuilder(); help.append("请输入" + tip + ":"); System.out.println(help.toString()); if (scanner.hasNext()) { String ipt = scanner.next(); if (StringUtils.isNotBlank(ipt)) { return ipt; } } throw new MybatisPlusException("请输入正确的" + tip + "!"); } public static void main(String[] args) { // 代码生成器 AutoGenerator mpg = new AutoGenerator(); // 全局配置 GlobalConfig gc = new GlobalConfig(); //设置代码输出目录 String projectPath = System.getProperty("user.dir"); gc.setOutputDir(projectPath + projectName + generateTo); //作者 gc.setAuthor(author); //设置时间类型为Date gc.setDateType(DateType.TIME_PACK); gc.setOpen(false); //设置Mapper.xml的BaseColumnList gc.setBaseColumnList(true); //设置Mapper.xml的BaseResultMap gc.setBaseResultMap(true); // 设置实体属性 Swagger2 注解 // gc.setSwagger2(true); mpg.setGlobalConfig(gc); // 数据源配置 DataSourceConfig dsc = new DataSourceConfig(); dsc.setUrl(url); dsc.setDriverName(driver); dsc.setUsername(username); dsc.setPassword(password); mpg.setDataSource(dsc); // 包配置 PackageConfig pc = new PackageConfig(); //pc.setModuleName(scanner("模块名")); pc.setParent(parentPackage); //设置包名 pc.setEntity(modelName); mpg.setPackageInfo(pc); // 自定义配置 InjectionConfig cfg = new InjectionConfig() { @Override public void initMap() { // to do nothing } }; // 如果模板引擎是 freemarker String templatePath = "/templates/mybatis-generator/mapper2.xml.ftl"; // 如果模板引擎是 velocity // String templatePath = "/templates/mapper.xml.vm"; // 自定义输出配置 List focList = new ArrayList<>(); // 自定义配置会被优先输出 focList.add(new FileOutConfig(templatePath) { @Override public String outputFile(TableInfo tableInfo) { // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!! return projectPath + projectName + mapperXmlPath + pc.getModuleName() + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML; } }); cfg.setFileOutConfigList(focList); mpg.setCfg(cfg); // 配置模板 TemplateConfig templateConfig = new TemplateConfig(); // 配置自定义输出模板 //指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别 templateConfig.setMapper("templates/mybatis-generator/mapper2.java"); templateConfig.setEntity("templates/mybatis-generator/entity2.java"); templateConfig.setService("templates/mybatis-generator/service2.java"); templateConfig.setServiceImpl("templates/mybatis-generator/serviceImpl2.java"); templateConfig.setController("templates/mybatis-generator/controller2.java"); templateConfig.setXml(null); mpg.setTemplate(templateConfig); // 策略配置 StrategyConfig strategy = new StrategyConfig(); strategy.setNaming(NamingStrategy.underline_to_camel); strategy.setColumnNaming(NamingStrategy.underline_to_camel); strategy.setEntityLombokModel(true); strategy.setRestControllerStyle(true); strategy.setEntitySerialVersionUID(false); //设置controller的父类 if (baseControllerClassName!=null) strategy.setSuperControllerClass(baseControllerClassName); //设置服务类的父类 if (baseServiceClassName !=null ) strategy.setSuperServiceImplClass(baseServiceClassName); // 写于父类中的公共字段 //strategy.setSuperEntityColumns("id"); strategy.setInclude(scanner("表名,多个英文逗号分割").split(",")); strategy.setControllerMappingHyphenStyle(true); strategy.setTablePrefix("t_","zmall_"); mpg.setStrategy(strategy); mpg.setTemplateEngine(new FreemarkerTemplateEngine()); mpg.execute(); } }

5.修改CodeGenerator类基本生成参数,并生成代码

//数据库连接参数
public static String driver = "com.mysql.jdbc.Driver";
public static String url = "jdbc:mysql://localhost:3306/zmall?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true";
public static String username="root";
public static String password="123456";
//父级别包名称
public static String parentPackage = "com.zking.zmall";
//项目名设置(如果是SpringCloud项目则需要设置,其他为""即可)
public static String projectName="/zmall-generator";
//代码生成的目标路径
public static String generateTo = "/src/main/java";
//mapper.xml的生成路径
public static String mapperXmlPath = "/src/main/resources/mapper";
//控制器的公共基类,用于抽象控制器的公共方法,null值表示没有父类
public static String baseControllerClassName ;
//业务层的公共基类,用于抽象公共方法
public static String baseServiceClassName ;
//作者名
public static String author = "zking";
//模块名称,用于组成包名
public static String modelName = "model";

注意:

  • 修改数据库连接URL中的数据库名、数据库账号和密码;

  • 修改父级别包名称

  • 修改项目名,如果是SpringCloud项目则修改,不是则默认“”

先生成

zmall_product_category,zmall_product

 此时直接运行代码生成类会报错,因为还缺少mybatis-plus的依赖

秒杀项目之mybatis与微服务注册_第5张图片

 在自动生成代码子模块添加公共模块

秒杀项目之mybatis与微服务注册_第6张图片

 此时再运行秒杀项目之mybatis与微服务注册_第7张图片

 就可以生成了秒杀项目之mybatis与微服务注册_第8张图片

创建商品服务子模块

1.基于Spring Initializr方式创建商品服务模块zmall-product

秒杀项目之mybatis与微服务注册_第9张图片

 2.在主模块pom.xml中加入商品服务子模块zmall-product

修改zmall-product的pom.xml



    4.0.0
    
        com.zking.zmall
        zmall
        1.0-SNAPSHOT
    
    zmall-product
    
        
            com.zking.zmall
            zmall-common
            1.0-SNAPSHOT
        
    




    zmall-common
    zmall-user
    zmall-generator
    zmall-product

秒杀项目之mybatis与微服务注册_第10张图片

 3.配置商品服务子模块zmall-product的application.yml配置文件

server:
  port: 8020
spring:
  application:
    name: zmall-product
  datasource:
    #type连接池类型 DBCP,C3P0,Hikari,Druid,默认为Hikari
    type: com.zaxxer.hikari.HikariDataSource
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/zmall?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true
    username: root
    password: 123456
  freemarker:
    suffix: .html
    template-loader-path: classpath:/templates/
#mybatis-plus配置
mybatis-plus:
  #所对应的 XML 文件位置
  mapper-locations: classpath*:/mapper/*Mapper.xml
  #别名包扫描路径
  type-aliases-package: com.zking.zmall.model
  configuration:
    #驼峰命名规则
    map-underscore-to-camel-case: true
#日志配置
logging:
  level:
    com.zking.zmall.mapper: debug

4. 在商品服务子模块中启动类上添加

ZmallProductApplication 

package com.zking.zmall;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan({"com.zking.zmall.mapper"})
public class ZmallProductApplication {

    public static void main(String[] args) {
        SpringApplication.run(ZmallProductApplication.class, args);
    }

}

5.将公共子模块中生成的service层代码复制到商品服务子模块zmall-product中,并删除掉非商品相关的service接口及实现类

秒杀项目之mybatis与微服务注册_第11张图片

秒杀项目之mybatis与微服务注册_第12张图片

6.创建junit实现接口测试

zmall-common模块


       
            junit
            junit
            4.12
       

       
            org.springframework.boot
            spring-boot-starter-test
       

ProductServiceImplTest 

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class ProductServiceImplTest {

    @Autowired
    private IProductService productService;

    @Before
    public void setUp() throws Exception {
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void queryProduct() {
        List list = productService.list();
        list.forEach(System.out::println);
    }
}

秒杀项目之mybatis与微服务注册_第13张图片

 拿到所有的数据

秒杀项目之mybatis与微服务注册_第14张图片

二、微服务项目集成Freemarker

1.在公共模块zmall-common中引入freemarker依赖


    org.springframework.boot
    spring-boot-starter-freemarker

2.在商品子模块zmall-product中添加首页和商品详情页面及公共资源(js/css/images)

  • 将资料目录中的《易买网网页素材.rar》解压后,将其中Index.html、Product.html和js/css/images等等添加到项目的templates和static目录下,最好请将Index.html、Product.html页面首字母改成小写

  • 导入资料目录中的common目录到项目的templates目录下

  • 将页面中的头部申明修改成(支持H5风格)

  • 在页面中通过<#include>指令引入common目录中的head.html

3.创建ProductController定义请求方法

ProductController 

package com.zking.zmall.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zking.zmall.model.Product;
import com.zking.zmall.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class ProductController {

    @Autowired
    private IProductService productService;

    @RequestMapping("/index.html")
    public String index(Model model){
        //按照商品的销量降序排序获取销量排名Top5的商品
        List products = productService.list(new QueryWrapper()
                .orderByDesc("hot")
                .last("limit 5"));
        model.addAttribute("hots",products);
        return "index";
    }

    @RequestMapping("/product.html")
    public String detail(Model model,Integer pid){
        //根据商品ID查询商品详情信息
        Product product = productService.getById(pid);
        model.addAttribute("product",product);
        return "product";
    }
}

运行启动类

 秒杀项目之mybatis与微服务注册_第15张图片

4. 在index.html中绑定热门数据和product.html中绑定商品详情数据

index.html

秒杀项目之mybatis与微服务注册_第16张图片

 替换为

product.html

秒杀项目之mybatis与微服务注册_第17张图片

 替换为

Loading...

${(product.name)!}

“开业巨惠,北京专柜直供”,不光低价,“真”才靠谱!
本店价格:¥${(product.price)}
消费积分:28R
型号选择:
  • 30ml
  • 50ml
  • 100ml
颜色选择:
  • 红色
  • 白色
  • 黑色
分享

效果图:

秒杀项目之mybatis与微服务注册_第18张图片

秒杀项目之mybatis与微服务注册_第19张图片

三、SpringBoot整合微服务&gateway&nginx

gateway

秒杀项目之mybatis与微服务注册_第20张图片

 请求链路要求:客户端发送请求先经过nginx,再用nginx转至内部访问网关gateway,最后由网关服务的路由规则转发到微服务的内部服务。

整合微服务之商品服务zmall-product

在公共模块zmall-common中导入微服务相关依赖



    com.alibaba.cloud
    spring-cloud-starter-alibaba-nacos-discovery




    org.springframework.cloud
    spring-cloud-starter-openfeign




    com.alibaba.cloud
    spring-cloud-starter-alibaba-nacos-config

启动nacos(该快捷方式在之前的博客中有提到怎么设置的)

秒杀项目之mybatis与微服务注册_第21张图片

 访问

秒杀项目之mybatis与微服务注册_第22张图片

 配置商品服务模块zmall-product的application.yml文件

spring:
  application:
    name: zmall-product
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848

秒杀项目之mybatis与微服务注册_第23张图片

 修改启动类,向nacos进行注册

package com.zking.zmall;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient
@SpringBootApplication
@MapperScan({"com.zking.zmall.mapper"})
public class ZmallProductApplication {

    public static void main(String[] args) {
        SpringApplication.run(ZmallProductApplication.class, args);
    }

}

创建并配置网关gateway服务

1.基于Spring initializr方式创建网关模块zmall-gateway

并且将网关模块zmall-gateway和父模块进行关联

秒杀项目之mybatis与微服务注册_第24张图片

2.配置pom.xml添加nacos和gateway的依赖

4.0.0

        com.zking.zmall
        zmall
        1.0-SNAPSHOT
    
zmall-gateway

    
    
        org.springframework.cloud
        spring-cloud-starter-gateway
    

    
    
        com.alibaba.cloud
        spring-cloud-starter-alibaba-nacos-discovery	
    

    
        org.projectlombok
        lombok
    

    
        org.apache.commons
        commons-lang3
    

此时网关模块的pom依赖



    4.0.0
    
        com.zking.zmall
        zmall
        1.0-SNAPSHOT
    
    com.zking
    zmall-gateway

    
        
        
            org.springframework.cloud
            spring-cloud-starter-gateway
        

        
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-discovery
        

        
            org.projectlombok
            lombok
        

        
            org.apache.commons
            commons-lang3
        
    


3.修改启动类,向nacos进行注册

package com.zking.zmall;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient
@SpringBootApplication
public class ZmallGatewayApplication {

    public static void main(String[] args) {
        SpringApplication.run(ZmallGatewayApplication.class, args);
    }

}

4.配置application.yml设置gateway路由转发规则

server:
  port: 8000
spring:
  application:
    name: zmall-gateway
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
    gateway:
      routes:
        - id: product_route
          uri: lb://zmall-product # lb指的是从nacos中按照名称获取微服务,并遵循负载均衡策略
          predicates:
            - Path=/product-serv/**
          filters:
            - StripPrefix=1

 5.将易买网网页素材中的公共静态资源js/css/images复制到gateway网关服务中

这里请注意了,之前在商品服务模块zmall-product中已经配置了易买网的静态资源,为什么还要在gateway网关服务中再配置一次呢?这是因为当请求经过gateway网关服务后会进行断言条件匹配和条件路径截取等操作,从而导致gateway网关路由转发后静态资源失效404的问题,所以特此在gateway网关服务中也配置一次易买网网页素材中的公共静态资源js/css/images,确保能正常访问

解决方案:(使用nginx动静分离方式实现) 配置静态资源访问服务器,将各个微服务模块中的静态访问资源迁移到静态资源访问服务器中,然后通过http方式访问即可。

 在gateway网关服务中配置一次易买网网页素材中的公共静态资源js/css/images

秒杀项目之mybatis与微服务注册_第25张图片

  在gateway网关服务中配置一次易买网网页素材中的公共静态资源js/css/images

秒杀项目之mybatis与微服务注册_第26张图片

 秒杀项目之mybatis与微服务注册_第27张图片

 秒杀项目之mybatis与微服务注册_第28张图片

 

安装配置Windows版nginx

1.解压nginx-1.18.0.zip至任意目录

下载安装包

秒杀项目之mybatis与微服务注册_第29张图片

秒杀项目之mybatis与微服务注册_第30张图片

 秒杀项目之mybatis与微服务注册_第31张图片

双击 以上红框 就可以访问到nginx了

秒杀项目之mybatis与微服务注册_第32张图片

确保nginx是能用的 然后关掉进程 进行配置

秒杀项目之mybatis与微服务注册_第33张图片

 此时就访问不到了

秒杀项目之mybatis与微服务注册_第34张图片

 2.进入conf目录,并修改nginx.conf配置文件

server
{
    listen 80;
    server_name zmall.com;
    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    location / {
        proxy_pass http://127.0.0.1:8000/;
    }
}

秒杀项目之mybatis与微服务注册_第35张图片

安装配置SwitchHosts

1.直接双击exe文件即可安装SwitchHosts

秒杀项目之mybatis与微服务注册_第36张图片

 桌面就有这个

秒杀项目之mybatis与微服务注册_第37张图片

2. 打开SwitchHosts设置一级域名

出现这种情况

秒杀项目之mybatis与微服务注册_第38张图片

 就用管理员身份运行

秒杀项目之mybatis与微服务注册_第39张图片

 秒杀项目之mybatis与微服务注册_第40张图片

 再次启动nginx(最后运行nginx根目录下的nginx.exe启动nginx )

如果出现IIS7,那么cmd窗口中执行下列指令

秒杀项目之mybatis与微服务注册_第41张图片

net stop w3svc

请求链路测试

单独访问商品服务:http://localhost:8020/index.html

通过gateway访问:http://localhost:8000/product-serv/index.html

通过nginx访问:http://zmall.com/product-serv/index.html

 单独访问商品服务秒杀项目之mybatis与微服务注册_第42张图片

 通过gateway访问

秒杀项目之mybatis与微服务注册_第43张图片

 通过nginx访问

秒杀项目之mybatis与微服务注册_第44张图片

 

你可能感兴趣的:(mybatis,微服务,java)