当前位置: 首页 > news >正文

电商项目后端框架SpringBoot、MybatisPlus

后端框架基础

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: 8080spring:datasource:type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/market?useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false&serverTimezone=Asia/Shanghaiusername: rootpassword: 123456druid:validation-query: SELECT 1 FROM DUALinitial-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,也就是PSCachemax-open-prepared-statements: 100 #要启用PSCache,必须配置大于0,当大于0时,#poolPreparedStatements自动触发修改为truemybatis-plus:type-aliases-package: com.example.entityglobal-config:db-config:logic-delete-field: deletedlogic-delete-value: 1logic-not-delete-value: 0configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImplmap-underscore-to-camel-case: truecache-enabled: falsejdbc-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() {@Overridepublic void initMap() {// to do nothing}};// 如果模板引擎是 freemarkerString templatePath = "/templates/mapper.xml.ftl";// 如果模板引擎是 velocity//String templatePath = "/templates/mapper.xml.vm";// 自定义输出配置List<FileOutConfig> focList = new ArrayList<>();// 自定义配置会被优先输出focList.add(new FileOutConfig(templatePath) {@Overridepublic String outputFile(TableInfo tableInfo) {// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!return projectPath + "/src/main/resources/mapper/" + pc.getModuleName()+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;}});/*cfg.setFileCreate(new IFileCreate() {@Overridepublic boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) {// 判断自定义文件夹是否需要创建checkDir("调用默认方法创建的目录,自定义目录用");if (fileType == FileType.MAPPER) {// 已经生成 mapper 文件判断存在,不想重新生成返回 falsereturn !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 {@Beanpublic PaginationInterceptor paginationInterceptor(){return new PaginationInterceptor();}
}

具体使用的时候用Page即可

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

(1)首先在application.yml中添加以下内容
mybatis-plus:type-aliases-package: com.example.entityglobal-config:db-config:logic-delete-field: deletedlogic-delete-value: 1logic-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,即可成功

http://www.lryc.cn/news/28053.html

相关文章:

  • 2023年03月IDE流行度最新排名
  • 华为校招机试 - 数组取最小值(Java JS Python)
  • 20 客户端服务订阅的事件机制剖析
  • ThreadPoolExecutor中的addWorker方法
  • 9 有线网络的封装
  • Linux----网络基础(2)--应用层的序列化与反序列化--守护进程--0226
  • uipath实现滑动验证码登录
  • openai-chatGPT的API调用异常处理
  • css实现音乐播放器页面 · 笔记
  • buu [NPUCTF2020]这是什么觅 1
  • Restful API 设计规范
  • sigwaittest测试超标的调试过程
  • Python进阶-----面对对象4.0(面对对象三大特征之--继承)
  • 九龙证券|利好政策密集发布,机构扎堆看好的高增长公司曝光
  • stm32CubeIDE FMC 驱动LCD(8080)
  • Java 数据类型
  • Prometheus 监控云Mysql和自建Mysql(多实例)
  • Vue3中的h函数
  • 阿尔法开发板 IMX6ULL 说明
  • Altium Designer19 #学习笔记# | 基础应用技巧汇总
  • Python 元类编程实现一个简单的 ORM
  • 《C++ Primer Plus》第18章:探讨 C++ 新标准(7)
  • Redis学习(二):Redis安装测试
  • Vector - CAPL - 简介及数据结构
  • 20230304英语学习
  • 【基础算法】单链表的OJ练习(3) # 移除链表元素 # 相交链表 #
  • 【自用】SpringBoot项目通用类整理
  • 动态规划法(总述)多阶段决策最优化问题
  • MySQL跨服务器数据映射
  • 利用反射实现通过读取配置文件对类进行实例化-课后程序(JAVA基础案例教程-黑马程序员编著-第十二章-课后作业)