电商项目后端框架springbootmybatisplus(代码片段)

一只呆桃酱 一只呆桃酱     2023-03-09     668

关键词:

后端框架基础

1.代码自动生成工具 mybatis-plus

(1)首先需要添加依赖文件
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.2</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.25</version>
        </dependency>

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.2</version>
        </dependency>

        <!-- 代码自动生成工具 mybatis-plus -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.4.1</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <!-- spring-boot-devtools -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <!-- json object对象支持 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.76</version>
        </dependency>

        <!-- 代码生成器中使用freemarker -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
            <version>2.2.2.RELEASE</version>
        </dependency>

(2)设置application.yml文件中数据库信息并且创建数据库及表
server:
  port: 8080

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/market?useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false&serverTimezone=Asia/Shanghai
    username: root
    password: 123456
    druid:
      validation-query: SELECT 1 FROM DUAL
      initial-size: 10 #初始化时建立物理连接的个数。
      min-idle: 10 #最小连接池数量
      max-active: 200 #最大连接池数量
      min-evictable-idle-time-millis: 300000 #连接保持空闲而不被驱逐的最小时间
      test-on-borrow: false #申请连接时执行validationQuery检测连接是否有效
      test-while-idle: true #申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,
      #执行validationQuery检测连接是否有效。
      time-between-eviction-runs-millis: 30000 #1) Destroy线程会检测连接的间隔时间,
      #如果连接空闲时间大于等于minEvictableIdleTimeMillis则关闭物理连接。
      #2) testWhileIdle的判断依据
      pool-prepared-statements: true #是否缓存preparedStatement,也就是PSCache
      max-open-prepared-statements: 100 #要启用PSCache,必须配置大于0,当大于0时,
      #poolPreparedStatements自动触发修改为true

mybatis-plus:
  type-aliases-package: com.example.entity
  global-config:
    db-config:
      logic-delete-field: deleted
      logic-delete-value: 1
      logic-not-delete-value: 0
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    map-underscore-to-camel-case: true
    cache-enabled: false
    jdbc-type-for-null: 'null'
(3)自动生成代码文档代码

注意更改相应父类位置以及数据库信息、表信息

package com.imooc.mall.common;

import com.baomidou.mybatisplus.annotation.DbType;
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.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

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

// 演示例子,执行 main 方法控制台输入模块表名回车自动生成对应项目目录中
public class CodeGenerator 

    /**
     * <p>
     * 读取控制台内容
     * </p>
     */
    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 + "/src/main/java");
        gc.setAuthor("pommy");
        gc.setOpen(false);
        // gc.setSwagger2(true); 实体属性 Swagger2 注解
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/mall?useUnicode=true&useSSL=false&characterEncoding=utf8");
        // dsc.setSchemaName("public");
        dsc.setDbType(DbType.MYSQL);
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("123456");
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        //pc.setModuleName(scanner("模块名"));
        pc.setParent("com.imooc.mall");
        mpg.setPackageInfo(pc);

        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() 
            @Override
            public void initMap() 
                // to do nothing
            
        ;

        // 如果模板引擎是 freemarker
        String templatePath = "/templates/mapper.xml.ftl";
        // 如果模板引擎是 velocity
         //String templatePath = "/templates/mapper.xml.vm";

        // 自定义输出配置
        List<FileOutConfig> focList = new ArrayList<>();
        // 自定义配置会被优先输出
        focList.add(new FileOutConfig(templatePath) 
            @Override
            public String outputFile(TableInfo tableInfo) 
                // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
                return projectPath + "/src/main/resources/mapper/" + pc.getModuleName()
                        + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            
        );
        /*
        cfg.setFileCreate(new IFileCreate() 
            @Override
            public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) 
                // 判断自定义文件夹是否需要创建
                checkDir("调用默认方法创建的目录,自定义目录用");
                if (fileType == FileType.MAPPER) 
                    // 已经生成 mapper 文件判断存在,不想重新生成返回 false
                    return !new File(filePath).exists();
                
                // 允许生成模板文件
                return true;
            
        );
        */
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);

        // 配置模板
        TemplateConfig templateConfig = new TemplateConfig();

        // 配置自定义输出模板
        //指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
        // templateConfig.setEntity("templates/entity2.java");
        // templateConfig.setService();
        // templateConfig.setController();

        templateConfig.setXml(null);
        mpg.setTemplate(templateConfig);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        //strategy.setSuperEntityClass("你自己的父类实体,没有就不用设置!");
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        strategy.setLogicDeleteFieldName("deleted");  //设置逻辑删除字段名
        // 公共父类
        //strategy.setSuperControllerClass("你自己的父类控制器,没有就不用设置!");
        // 写于父类中的公共字段
        //strategy.setSuperEntityColumns("id");
        strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
        strategy.setControllerMappingHyphenStyle(true);
        strategy.setTablePrefix("imooc_mall_");

        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    


(4)持久层注入

在主类上方添加:@MapperScan(“com.example.mapper”)

2.分页功能

需要添加MybatisConfiguration配置文件完成分页

@Configuration
@MapperScan("com.example.mapper")
public class MyBatisConfiguration 
    @Bean
    public PaginationInterceptor paginationInterceptor()
        return new PaginationInterceptor();
    

具体使用的时候用Page即可

3. 逻辑删除deleted属性位置(1为删除)

(1)首先在application.yml中添加以下内容
mybatis-plus:
  type-aliases-package: com.example.entity
  global-config:
    db-config:
      logic-delete-field: deleted
      logic-delete-value: 1
      logic-not-delete-value: 0
(2)然后在entity实体类中的deleted属性前加上@TableLogic,表示逻辑删除属性

4. 模拟支付----支付宝沙箱环境设置

(1)登录客支付宝户端账号,开启公钥

前端基础知识Vue

注意跨域访问时,即在浏览器中输入网址访问数据,需要用主机ip地址,而不是localhost,之后在controller上面添加解决跨域访问的注解@CrossOrigin

在CMD中用ipconfig去查看本地的ip地址,ip4:10.6.12.170

1. vue cli创建新项目

下载anxios时,项目有中文路径往往不成功,此时可以选择别的方式,在cmd中切换到该路径下然后进行npm install axios,即可成功

springboot+thymeleaf+layui

后端框架 后端框架SpringBootMybatis前端框架ThymeleafLayUIJQuery数据库MySQL开发必备技术熟悉SpringMVC框架熟悉SpringBoot框架熟悉Mybatis框架了解Thymeleaf模板了解LayUI框架熟悉JQuery熟悉MySQL 查看详情

springbootmybatis使用

MybatisMyBatis的前身是Apache社区的一个开源项目iBatis,于2010年更名为MyBatis。MyBatis是支持定制化SQL、存储过程以及高级映射的优秀的持久层框架,避免了几乎所有的JDBC代码和手动设置参数以及获取结果集,使得开发人员... 查看详情

springbootmybatis使用(代码片段)

MybatisMyBatis的前身是Apache社区的一个开源项目iBatis,于2010年更名为MyBatis。MyBatis是支持定制化SQL、存储过程以及高级映射的优秀的持久层框架,避免了几乎所有的JDBC代码和手动设置参数以及获取结果集,使得开发人员... 查看详情

2018高级系统架构,ssm大型分布式架构电商项目,高并发,微服务,缓存技术

课程内容 1.课程目标: 1.1了解电商行业特点以及理解电商的模式 1.2了解整体电商的架构特点 1.3能够运用Dubbox+SSM搭建分布式应用 1.4搭建工程框架,完成品牌列表后端代码 2.电商行业技术特点 2.1技术新&... 查看详情

springbootmybatis后台框架平台集成代码生成器shiro权限

1.代码生成器:[正反双向](单表、主表、明细表、树形表,快速开发利器)+快速表单构建器freemaker模版技术,0个代码不用写,生成完整的一个模块,带页面、建表sql脚本、处理类、service等完整模块2.多数据源:(支持同时连接无数个... 查看详情

搭建项目学习框架(三,创建一个完整电商项目)(代码片段)

目录项目的构建思路格局创建的思路进行创建模块首先下实现父文件的配置文件,也就是pom.xml文件(xxx_parent_demo0319文件夹下)配置xxx.pojo(配置项目所有实体类)配置pom.xml文件(xxx.pojo)配置xxx.dao文... 查看详情

[vue项目实战]电商系统项目初始化(代码片段)

电商系统项目概述电商项目基本业务概述电商后台管理系统的功能电商后台管理系统的开发模式(前后端分离)电商后台管理系统的技术选型1.前端项目技术栈2.后端技术栈前端项目初始化123通过`vueui`来初始化项目4.配置element... 查看详情

springbootmybatis整合

新建项目在上一篇.第二步:创建表和相应的实体类实体类:user.javapackagecom.qtt.im.entity;importjava.io.Serializable;publicclassUserimplementsSerializable{privateLongid;privateStringloginName;privateStringpass;privateStringmobile 查看详情

项目实战-电商(网上书城)

电商两种模式:多元化电商(淘宝、京东),垂直电商(只卖某一类东西)2.图书商城项目简介  -》主要功能:图书信息、评论、购买  -》进行数据库搭建      商品分类表,商品信息表,管理员表  -》搭建开发... 查看详情

springbootmybatis多数据源配置

在springboot项目中配置多个数据源的情形在开发中经常会遇见,本文以SpringBoot+MyBatis的方式实现mysql+Postgresql双数据源项目搭建,具体详细代码请参考:https://gitee.com/senn-wen/my...一、依赖配置在pom.xml文件中引入postgresql和mysql的驱动... 查看详情

使用mybatis+flying-0.9.4的电商后端

代码地址如下:http://www.demodashi.com/demo/12779.htmlmybatis.flying-阳春(Sunny-Spring)项目介绍请见flying-doc.limeng32.com,国内代码托管网站请见gitee.com/limeng32/mybatis.flying,我们为开发最好的mybatis插件而努力。flying是一个可以极大增加mybatis... 查看详情

校园跑腿校园脱单代理帮忙拿快递的微信小程序基于springbootmybatis-plusmysql实现(代码片段)

一、文件夹说明代码下载地址:校园跑腿、校园脱单、代理、帮忙拿快递的微信小程序server后端项目project:项目CBD:校园跑腿服务(校园CBD中心)server-app:小程序apiserver-pc:小程序后台管理service-cgs-base-service:项目mapp... 查看详情

前端框架与后端框架

...后端框架的主要区别和相似之处是什么?我可以在一个Web项目中同时使用它们吗?或者它们会发生冲突吗?(从未见过同时使用前端和后端框架的项目)。如果一个项目只需要一个 查看详情

分布式电商项目(02)--后台管理系统ssm框架整合(代码片段)

前言:上一篇博客讲了此次分布式电商项目后台管理系统的工程的搭建,这一篇就讲一下SSM框架的整合1.整合思路下面说到的配置文件都需要放到manager-web工程下,因为此工程为war工程,而其它的工程都只是一个jar包,具体如下... 查看详情

php电商小程序的项目经验怎么写

php电商小程序的项目经验怎么写?用做简历Codeigniter是一个老牌的php框架,零配置,文档极其丰富,国内的流利程度上看github的start数,让人觉得不可思议,是国外流行比较流行吧。它没有ORM,没有模板引擎,用它,只是因为够... 查看详情

djangob2c电商功能模块

======B2C电商网站======前后端分离,前端采用Vue框架,后端采用DRF框架,接口采用RESTAPI---》 不分离:视图中调用模板,生成html字符串返回   分离:视图中构造json数据返回。 优点:前后端耦合度较低,便于维护。开发RESTAPI接... 查看详情

springbootmybatis多数据源解决方案

  在我们的项目中不免会遇到需要在一个项目中使用多个数据源的问题,像我在得到一个任务将用户的聊天记录进行迁移的时候,就是用到了三个数据源,当时使用的AOP的编程方式根据访问的方法的不同进行动态的切换数据源... 查看详情

谈谈电商项目相关

第一:项目总体技术介绍 第二:项目介绍-maven私服以及项目框架搭建第三:后台登录和CMS实现_sso实现_购物车_搜索等第四:下单_支付秒杀 查看详情