今天是电商项目的第一天,首先老师给我们回顾了之前学的指示点:
dubbo(这个框架还没搞定):首先就是dubbo的六大作用:
架构的演变:单体应用:crm:独立的war包业务模块递增,业务拆分复杂(基本不可以拆分)、耦合度高、跨语言困难、项目部署机器资源浪费严重
Rpc:远程过程调用(跨虚拟机),框架:Thrift、Grpc、dubbo
基本名词:http:应用层协议。dubbo:跨域传输的一种特殊协议
SOA:面向服务开发:一切皆服务,服务拆分粒度细,分布式环境、多台机器服务
dubbo框架执行流程:角色:服务提供方、注册中心、消费者、监控中心、IOC容器
服务方流程:1.服务提供方启动、服务实例化 2.注册服务到注册中心:dubbo://ip:port服务名称?k=v&k=v&.....
消费方:1.消费者启动、订阅信息到本地 2.消费者发起远程服务调用(invoke)
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<modules>
<module>shop-common</module>
<module>shop-manager</module>
<module>shop-generator</module>
</modules>
<!-- 使用继承方式,实现复用,符合继承的都可以被使用 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
</parent>
<groupId>com.yjxxt</groupId>
<artifactId>shop</artifactId>
<version>1.0-SNAPSHOT</version>
<!--
集中定义依赖组件版本号,但不引入,
在子工程中用到声明的依赖时,可以不加依赖的版本号,
这样可以统一管理工程中用到的依赖版本
-->
<properties>
<!-- JDK 版本定义 -->
<java.version>1.8</java.version>
<!-- mybatis 依赖 -->
<mybatis.version>2.1.1</mybatis.version>
<!-- pagehelper 分页依赖 -->
<pagehelper.version>1.2.13</pagehelper.version>
<!-- mysql 数据库依赖 -->
<mysql.version>8.0.18</mysql.version>
<!-- druid 连接池依赖 -->
<druid.version>1.1.20</druid.version>
</properties>
<!-- 项目依赖管理 父项目只是声明依赖,子项目需要写明需要的依赖(可以省略版本信息) -->
<dependencyManagement>
<dependencies>
<!-- mybatis 依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!-- pagehelper 分页依赖 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>${pagehelper.version}</version>
</dependency>
<!-- mysql 数据库依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- druid 连接池依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
在父项目上直接反键,点击这个选择Maven的quickstart,还是1.8
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>shop</artifactId>
<groupId>com.yjxxt</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>shop-common</artifactId>
<name>shop-common</name>
<dependencies>
<!-- spring boot web 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- pagehelper 分页依赖 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
</dependency>
</dependencies>
<build>
</build>
</project>
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.yjxxt</groupId>
<artifactId>shop</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>shop-manager</artifactId>
<version>1.0-SNAPSHOT</version>
<name>shop-manager</name>
<dependencies>
<!-- shop common 依赖 -->
<dependency>
<groupId>com.yjxxt</groupId>
<artifactId>shop-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- spring boot freemarker 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<!-- mybatis 依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<!-- mysql 数据库依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- druid 连接池依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<!-- spring boot test 依赖 -->
<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>
</build>
</project>
server:
port: 9090 # 项目访问端口,默认 8080
servlet: # 项目访问路径,默认 /
context-path: /shop-manager
# Spring
spring:
# 数据源
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
username: root
password: root
# 指定 druid 连接池以及 druid 连接池配置
type: com.alibaba.druid.pool.DruidDataSource
druid:
initial-size: 1 # 初始连接数
max-active: 20 # 最大连接数
max-idle: 20 # 最大空闲
min-idle: 1 # 最小空闲
max-wait: 60000 # 最长等待时间
# freemarker 模板引擎
freemarker:
cache: false
charset: UTF-8
content-type: text/html;charset=UTF-8
enabled: true
suffix: .ftl
template-loader-path: classpath:/views/
# 配置模板里是否可以直接取request的属性 request是别名
request-context-attribute: request
# 配置将request和session中的键值添加到
# AbstractTemplateView类的renderMergedOutputModel方法中的model这个Map参数中
expose-request-attributes: true
expose-spring-macro-helpers: true
# 配置模板里是否可以直接取session的属性 true 是允许
expose-session-attributes: true
settings:
tag_syntax: auto_detect # 配置标签语法为自动,页面可以将 <> 改为 [],为了区别 html 标签
template_update_delay: 0 # 模板更新时间,单位秒
default_encoding: UTF-8 # 默认编码字符集
output_encoding: UTF-8 # 模板输出编码字符集
locale: zh_CN # 本地化配置
date_format: yyyy-MM-dd # 日期格式化
time_format: HH:mm:ss # 时间格式化
datetime_format: yyyy-MM-dd HH:mm:ss # 日期时间格式化
number_format: #.## # 数字格式化
boolean_format: true,false # boolean格式化
# ignore,debug,html_debug,rethrow
# 1.TemplateExceptionHandler.IGNORE_HANDLER简单地压制所有异常
# 它对处理异常没有任何作用,也不会重新抛出异常,页面可以正常渲染,后台抛异常
# 2.TemplateExceptionHandler.DEBUG_HANDLER打印堆栈信息和重新抛出异常。这是默认的异常控制器
# 3.TemplateExceptionHandler.HTML_DEBUG_HANDLER和DEBUG_HANDLER相同
# 但是可以格式化堆栈跟踪信息,HTML页面,建议使用它而不是DEBUG_HANDLER
# 4.TemplateExceptionHandler.RETHROW_HANDLER简单重新抛出所有异常而不会做其他的事情
# 5.使用自定义异常类实现TemplateExceptionHandler重写handleTemplateException方法
template_exception_handler: html_debug
# MyBatis
mybatis:
# 配置 MyBatis数据返回类型别名(默认别名是类名)
type-aliases-package: com.yjxxt.manager.pojo
# 配置 MyBatis Mapper 映射文件
mapper-locations: classpath:/mapper/*.xml
# Mybatis SQL 打印(方法接口所在的包,不是 Mapper.xml 所在的包)
logging:
level:
com.yjxxt.manager.mapper: debug
@Controller
public class PageController {
//公共页面跳转
@RequestMapping("/{page}")
public String page(@PathVariable String page){
System.out.println(page);
return page;
}
}
<!-- 设置项目根路径全局变量 -->
<#assign ctx=request.contextPath/>
<!DOCTYPE html>
<html>
<head>
<title>shop商城后台管理系统</title>
</head>
<body>
<h1>${ctx}欢迎使用shop商城后台管理系统</h1>
</body>
</html>
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>shop</artifactId>
<groupId>com.yjxxt</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>shop-generator</artifactId>
<name>shop-generator</name>
<dependencies>
<!-- mybatis 依赖(让生成的 pojo 和 mapper 不缺少注解相关类型) -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<!-- mybatis generator core 依赖 -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- mybatis generator plugin 依赖 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<configuration>
<verbose>true</verbose>
<!-- 是否覆盖 -->
<overwrite>true</overwrite>
<!-- 自动生成的配置 -->
<configurationFile>src/main/resources/mybatis-generator.xml</configurationFile>
</configuration>
<dependencies>
<!-- mysql 数据库依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- mybatis generator core 依赖 -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
<!-- 将项目打包至本地仓库并添加依赖 -->
<dependency>
<groupId>com.yjxxt</groupId>
<artifactId>shop-generator</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
package com.yjxxt.generator;
import org.mybatis.generator.api.CommentGenerator;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.config.PropertyRegistry;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Properties;
import java.util.Set;
import static org.mybatis.generator.internal.util.StringUtility.isTrue;
public class ShopCommentGenerator implements CommentGenerator {
/**
* properties属性,即配置在 commentGenerator 标签之内的 Property 标签
*/
private Properties properties;
/**
* properties配置文件
*/
private Properties systemPro;
/*
* 是否生成日期
*/
private boolean suppressDate;
/**
* 是否生成注释
*/
private boolean suppressAllComments;
/**
* 日期格式
*/
private String currentDateStr;
public ShopCommentGenerator() {
super();
properties = new Properties();
systemPro = System.getProperties();
suppressDate = false;
suppressAllComments = false;
currentDateStr = DateTimeFormatter.ofPattern("yyyy/MM/dd").format(LocalDateTime.now());
}
/**
* 此方法返回格式化的日期字符串以包含在Javadoc标记中和XML注释。
*
* @return
*/
protected String getDateString() {
String result = null;
if (!suppressDate) {
result = currentDateStr;
}
return result;
}
/**
* 从该配置中的任何属性添加此实例的属性CommentGenerator配置。
*
* @param properties
*/
@Override
public void addConfigurationProperties(Properties properties) {
this.properties.putAll(properties);
suppressDate = isTrue(properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_DATE));
suppressAllComments = isTrue(properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_ALL_COMMENTS));
}
/**
* 为字段添加注释
*
* @param field
* @param introspectedTable
* @param introspectedColumn
*/
@Override
public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
if (suppressAllComments)
return;
StringBuilder sb = new StringBuilder();
field.addJavaDocLine("/**");
sb.append(" * ");
sb.append(introspectedColumn.getRemarks());
field.addJavaDocLine(sb.toString().replace("\n", " "));
field.addJavaDocLine(" */");
}
/**
* Java 属性注释
*
* @param field
* @param introspectedTable
*/
@Override
public void addFieldComment(Field field, IntrospectedTable introspectedTable) {
if (suppressAllComments)
return;
StringBuilder sb = new StringBuilder();
field.addJavaDocLine("/**");
sb.append(" * ");
sb.append(introspectedTable.getFullyQualifiedTable());
field.addJavaDocLine(sb.toString().replace("\n", " "));
field.addJavaDocLine(" */");
}
/**
* 为模型类添加注释
*
* @param topLevelClass
* @param introspectedTable
*/
@Override
public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
if (suppressAllComments) {
return;
}
topLevelClass.addJavaDocLine("/**");
topLevelClass.addJavaDocLine("* ");
topLevelClass.addJavaDocLine(" * @since 1.0.0");
topLevelClass.addJavaDocLine(" */");
}
/**
* Java类的类注释
*
* @param innerClass
* @param introspectedTable
*/
@Override
public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) {
if (suppressAllComments) {
return;
}
// 获取表注释
String remarks = introspectedTable.getRemarks();
innerClass.addJavaDocLine("/**");
innerClass.addJavaDocLine("/* "+remarks);
innerClass.addJavaDocLine("* ");
innerClass.addJavaDocLine(" * @since 1.0.0");
innerClass.addJavaDocLine(" */");
}
/**
* 为类添加注释
*
* @param innerClass
* @param introspectedTable
* @param b
*/
@Override
public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable, boolean b) {
// 获取表注释
String remarks = introspectedTable.getRemarks();
innerClass.addJavaDocLine("/**");
innerClass.addJavaDocLine("/* "+remarks);
innerClass.addJavaDocLine("* ");
innerClass.addJavaDocLine(" * @since 1.0.0");
innerClass.addJavaDocLine(" */");
}
/**
* 为枚举添加注释
*
* @param innerEnum
* @param introspectedTable
*/
@Override
public void addEnumComment(InnerEnum innerEnum, IntrospectedTable introspectedTable) {
if (suppressAllComments)
return;
StringBuilder sb = new StringBuilder();
innerEnum.addJavaDocLine("/**");
sb.append(" * ");
sb.append(introspectedTable.getFullyQualifiedTable());
innerEnum.addJavaDocLine(sb.toString().replace("\n", " "));
innerEnum.addJavaDocLine(" */");
}
/**
* 给getter方法加注释
*
* @param method
* @param introspectedTable
* @param introspectedColumn
*/
@Override
public void addGetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
}
/**
* 给setter方法加注释
*
* @param method
* @param introspectedTable
* @param introspectedColumn
*/
@Override
public void addSetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
}
/**
* 普通方法的注释,这里主要是XXXMapper.java里面的接口方法的注释
*
* @param method
* @param introspectedTable
*/
@Override
public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) {
}
/**
* 给Java文件加注释,这个注释是在文件的顶部,也就是package上面。
*
* @param compilationUnit
*/
@Override
public void addJavaFileComment(CompilationUnit compilationUnit) {
}
/**
* Mybatis的Mapper.xml文件里面的注释
*
* @param xmlElement
*/
@Override
public void addComment(XmlElement xmlElement) {
}
/**
* 此方法为其添加了自定义javadoc标签。
*
* @param javaElement
* @param markAsDoNotDelete
*/
protected void addJavadocTag(JavaElement javaElement, boolean markAsDoNotDelete) {
}
/**
* 为调用此方法作为根元素的第一个子节点添加注释。
*
* @param xmlElement
*/
@Override
public void addRootComment(XmlElement xmlElement) {
}
@Override
public void addGeneralMethodAnnotation(Method method, IntrospectedTable introspectedTable, Set<FullyQualifiedJavaType> set) {
}
@Override
public void addGeneralMethodAnnotation(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn, Set<FullyQualifiedJavaType> set) {
}
@Override
public void addFieldAnnotation(Field field, IntrospectedTable introspectedTable, Set<FullyQualifiedJavaType> set) {
}
@Override
public void addFieldAnnotation(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn, Set<FullyQualifiedJavaType> set) {
}
@Override
public void addClassAnnotation(InnerClass innerClass, IntrospectedTable introspectedTable, Set<FullyQualifiedJavaType> set) {
}
}
<?xml version="1.0" encoding="ISO-8859-1"?>
<!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="MysqlTables" targetRuntime="MyBatis3">
<!-- 生成的Java文件的编码 -->
<property name="javaFileEncoding" value="UTF-8"/>
<!-- 增加Models ToStirng方法 -->
<plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>
<!-- 增加Models Serializable实现 -->
<plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>
<!-- 覆盖重复标签 -->
<plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin"/>
<!-- 分页插件 -->
<!-- 在example类中增 page 属性,并在mapper.xml的查询中加入page !=null 时的查询 -->
<!-- <plugin type="org.mybatis.generator.plugins.MySQLPagerPlugin" /> -->
<!-- 在example类中增 offset和limit属性,并在mapper.xml的查询中加入limit ${offset} , ${limit} 提供在offset和limit>0时的查询 -->
<!-- <plugin type="org.mybatis.generator.plugins.MySQLPaginationPlugin"></plugin> -->
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<!-- type指定生成注释使用的对象 -->
<commentGenerator type="com.yjxxt.generator.ShopCommentGenerator">
<property name="suppressAllComments" value="false"/>
</commentGenerator>
<!-- mysql数据库连接配置 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&tinyInt1isBit=false"
userId="root" password="root">
</jdbcConnection>
<!--
是否忽略BigDecimals 非必填项
自动生成Java对象的时候,会根据number类型的长度不同生成不同的数据类型
number长度 Java类型
1~4 Short
5~9 Integer
10~18 Long
18+ BigDecimal
-->
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 以下内容,需要改动 -->
<!-- java类生成的位置 -->
<javaModelGenerator targetPackage="com.yjxxt.manager.pojo" targetProject="D:\workspace\Maven\shop\shop-manager\src\main\java">
<!-- 在targetPackage的基础上,根据数据库的schema再生成一层package,最终生成的类放在这个package下,默认为false -->
<property name="enableSubPackages" value="true"/>
<!-- 从数据库返回的值去除前后空格 -->
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- *Mapper.xml配置文件生成的位置 -->
<sqlMapGenerator targetPackage="mapper" targetProject="D:\workspace\Maven\shop\shop-manager\src\main\resources">
<!-- 是否让schema作为包后缀 -->
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- java mapper接口生成的位置(interface) -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.yjxxt.manager.mapper" targetProject="D:\workspace\Maven\shop\shop-manager\src\main\java">
<!-- 是否让schema作为包后缀 -->
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--
指定数据库表
tableName数据库表名
domainObjectName生成的实体类名
是否需要mapper配置文件加入sql的where条件查询,需要将enableCountByExample等设为true,会生成一个对应domainObjectName的Example类
-->
<table tableName="t_goods_category" domainObjectName="GoodsCategory"
enableCountByExample="true" enableDeleteByExample="true"
enableSelectByExample="true" enableUpdateByExample="true">
<!-- 用于insert时,返回主键的编号 -->
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
</context>
</generatorConfiguration>