01_购礼平台项目介绍_项目原型搭建_SpringCloud Netflix

1. 项目介绍 蓉礼购

Ø 1.gift项目基本介绍

Ø 2.gift项目技术架构(重点)

Ø 3.gift后端项目原型(重点)

Ø 4.gift GIT仓库搭建(重点)

用到的sql和模板代码等资源网盘连接
链接:https://pan.baidu.com/s/10oZmYKAz3i4W26HAAZWUOw?pwd=8848
提取码:8848

1.1. 项目背景

蓉礼购是以礼为核心,以产品、文化、思想为基础,以互联网应用为载体,构建一个用户自主、资源共享、全民创新的可以实现完全个性化的智能生活应用平台,是中国礼行业创意网络平台,是创意礼用户商家一体化生态圈建设者。

基于社会上,公司、社会团体、个人等逢年过节以及个人的一些重要节日(纪恋日、生日等),需要以礼品表示关怀、关爱、尊重、关心。所以需要进行购买礼物来赠送,但是现在该购买什么、在哪儿购买,很多人不清楚。所以xxx公司就发现了这个商机,委托我司给他们研发这个2b2c saas平台(2b2c-b端,c端)。这个项目包含礼品百科、礼品、用户、购物车、订单、支付、物流中心、售后、圈子、系统管理、权限、财务、组织机构等微服务中心。这个项目整体使用前后端分离架构,前端使用vue+elementui技术栈(后台管理),html+css+js实现(门户),小程序(用户端,商户端)、App(用户端,商户端),后端使用springcloud netflix/alibaba技术栈。

鲜花

海鲜

保健品

蛋糕

玩具

茶叶

其他

2b 2c ----》管理端(运营方) b端(入驻方)—企业端 c端( 用户端)

产品的角度

支持入驻: 2b2c

管理端(给平台的员工来用): vue+element (pc浏览器)

b端(入驻的企业使用的): vue+element(pc浏览器),手机端/小程序端

c端( 用户端) : pc浏览器,手机端/小程序端

不支持入驻: 2c

管理端:vue+element (pc浏览器)

C端(用户的):pc浏览器,手机端/小程序端

没有用户(管理系统):

b端(管理端)

==开发的角度=

前端 后端

==业务=

前台(做业务) 后台(管理)

2.项目基本介绍

2.1. SaaS平台设计

云计算(租用):

IAAS(基础设施即服务 (IaaS : Infrastructure as a Service)):云服务器,云数据库、oss(类似于fastdfs分布式的文件系统)-阿里云、华为云

PAAS(平台即服务(PaaS:Platform as a Service)):提供开发环境k8s ,桌面云-阿里云、华为云,开发云(CI,CD)

SAAS(软件即服务Software as a Service ):提供软件钉钉,processon-在线平面设计,金蝶、用友提供的软件,企业微信,华为ERP…

2.1.1. SaaS平台的认识-企业/个人直接租用别人软件

SaaS提供商为企业搭建信息化所需要的所有网络基础设施及软件、硬件运作平台,并负责所有前期的实施、后期的维护等一系列服务,企业无需购买软硬件、建设机房、招聘IT人员,即可通过互联网使用信息系统。就像打开自来水龙头就能用水一样,企业根据实际需要,从SaaS提供商租赁软件服务。

img

SaaS模式带来的问题:

² 软件面向的是多个客户企业的数据 , 数据量变大,比如达到上百万,千万数据:考虑做集群,分库,分表

² 不同客户企业的数据如何隔离

² 不同客户企业权限如何处理:需要考虑企业,套餐,角色,权限,资源

2.1.2. SaaS平台数据隔离
2.2.2.1独立数据库服务器

这是第一种方案,即一个租户一个数据库,这种方案的用户数据隔离级别最高,安全性最好,但成本较高。

² 优点
为不同的租户提供独立的数据库,有助于简化数据模型的扩展设计,满足不同租户的独特需求;如果出现故障,恢复数据比较简单。

² 缺点
增多了数据库的安装数量,随之带来维护成本和购置成本的增加。
这种方案与传统的一个客户、一套数据、一套部署类似,差别只在于软件统一部署在运营商那里。如果面对的是银行、医院等需要非常高数据隔离级别的租户,可以选择这种模式,提高租用的定价。如果定价较低,产品走低价路线,这种方案一般对运营商来说是无法承受的。

img

不同的数据库服务器

2.2.2.2共享服务器,不同数据库

这是第二种方案,每个租户共享服务器,但是有自己单独数据库

² 优点
为安全性要求较高的租户提供了一定程度的逻辑数据隔离,并不是完全隔离;每个数据库可支持更多的租户数量。

² 缺点
如果出现故障,数据恢复比较困难,因为恢复数据库将牵涉到其他租户的数据;
如果需要跨租户统计数据,存在一定困难。

img

同一个数据库服务器,不同库

2.2.2.3共享数据库,共享数据架构,不同的机构使用不同的表

不同的机构使用不同的表,表使用后缀进行区分如:t_employee_ali , t_employee_tx

img

相同的数据库不同的表

2.2.2.4共享数据库,共享数据架构(表),使用外键区分数据

相同的数据库相同的表

这是第三种方案,即租户共享同一个Database、同一个Schema,共享表,但在表中增加tenant_id多租户的数据字段。这是共享程度最高、隔离级别最低的模式。

² 优点
三种方案比较,第三种方案的维护和购置成本最低,允许每个数据库支持的租户数量最多。

² 缺点:
隔离级别最低,安全性最低,需要在设计开发时加大对安全的开发量;
数据备份和恢复最困难,需要逐表逐条备份和还原。
如果希望以最少的服务器为最多的租户提供服务,并且租户接受牺牲隔离级别换取降低成本,这种方案最适合。

img

查询不同的租户的数据的时候,需要增加条件:where tenant_id = 登录的租户ID

2.1.3. SaaS平台权限设计

RBAC(Role-Based Access Control):基于角色的权限控制: 员工/用户 -> 角色 -> 权限 -> 资源(菜单)

这里在传统的RBAC的权限控制基础上增加了租户,套餐,功能包(非必须)三个角色,把功能包关联权限.。租户购买套餐,套餐关联了对应的权限,租户享有的所有功能限制于购买的套餐的功能。租户下的员工亦是如此。

套餐:

img

² 套餐和权限的关系:套餐表 N ---- N 权限表

² 租户和套餐的关系:租 户 N ---- N 套 餐

² 租户和租户部门: 租 户 1 ---- N 部门

² 租户和租户的员工:租 户 1 ---- N 员 工

² 租户 N — N 套餐 N — N 权限 1 — 1 资源(菜单等)

租户入驻后,租户管理员就拥有他们套餐里面的所有权限

² 租户 1 — N 员工 N — N 角色N — N 权限 1 — 1 资源

​ 租户管理员创建对应的角色,并关联权限,授予相关员工

=================

² 平台管理员:创建权限列表 ,创建功能包,分配权限,创建套餐,分配功能包

N 员工 N — N 角色N — N 权限 1 — 1 资源

² 租户超管 租户入驻

租户购买套餐:购买套餐,支付,租户和套餐建立关系

创建员工 , 创建角色分配权限, 把角色分配给员工

² 租户员工

租户 1 — N 员工 N — N 角色N — N 权限 1 — 1 资源

² 用户: 有的功能需要登录才能够访问

2.1.4. SaaS平台相关表设计

img

系统管理员:

方案1:为运营公司,创建一个大大租户,包含了整个系统功能,系统超管、普通管理员当做店铺管理员/店员来处理。

​ 方案2(采纳):所有的地方店铺id为null

店铺管理员: 入驻成功默认创建一个角色店铺管理员,把套餐所有权限赋予它,再把这个角色赋予店铺管理员。

店员:店铺管理员创建角色,分配角色给店员

用户:对于用户而言不需要控制权限,只判断某些功能需要登陆才能操作。

以上单体项目

img

微服务架构:分库+去除数据字典

https://blog.csdn.net/yhp0607/article/details/113624408?spm=1001.2014.3001.5501

DDD:领域驱动设计,没有5年搞不懂

2.2. 功能模块

1568969942161

2.3. 项目原型

通过项目原型进一步了解项目的功能,包括:门户首页、课程搜索页、个人中心等 参考“项目原型”

管理端 : vue+vuejs+elementui.vue-admin-master

B 端 : 微信小程序(原生小程序,uni-app(vue)–一套代码达成多种小程序,虽然可以打app,不建议用)

C 端 :PC浏览器端(html+css),微信小程序(原生小程序,uni-app(vue))

,App(原生(andorid,ios)和React Native-混合式APP,一套代码可以打成android,ios)

\3. 项目技术架构

3.1. 技术架构

项目采用主流的前后端分离模式,前端分为后台管理前端(平台管理员,租户管理员(超管,员工)),门户网站前端(主页、列表页、详情页、购物车,订单确认页,支付页,个人中心等等)。

img

3.2. 技术栈

² 系统管理前端采用技术栈为

Node.js,Vue.js,Npm,WebPack,Vue Cli ,Element UI ,Fast Mock等等。

² 门户网站前端技术栈为

Html ,css,js ,jquery等等。

² 后端采用微服务架构技术栈为

微服务架构:按照功能拆分N多个服务,每个服务可以独立技术选型,独立开发,独立部署,独立运维.单个服务使用基于ssm的springboot,服务间通过spring cloud协调.技术包括:

MyBatis-Plus,SpringBoot ,SpringMvc,SpringCloud(Eureka/Nacos,Zuul/Gateway,Config/Nacos,Feign,Hystrix/Sentinel等等),Redis,Fastdfs/Aicloud OSS,ElasticSearch,rocketmq,Velocity; 运维方面:阿里云服务器,Docker,Jenkins,K8S(或者docker-compose)等等。

3.3. 项目部署图

img

3.4. 开发步骤

项目是基于前后端分离的架构进行开发,前后端分离架构总体上包括前端和服务端,通常是多人协作并行开发,开 发步骤如下:

\1. 接项目,立项-产品经理

\2. 需求分析(产品经理) : 需求文档,概要设计,功能原型图,梳理用户的需求,分析业务流程

\3. 项目经理组建团队开发: PM(项目经理),SE(架构师),TM=PM+SE,项目启动会,里程碑,开发人员培训(SE),技术架构与选型+项目原型搭建(SE) 开发文档

\4. 开发(开发工程师)

敏捷(快速适应需求)、迭代(最开始的时候,把功能分为多个阶段实现,每个阶段实现几个功能就ok,阶段就是迭代。后期又可以一个功能,一个bug就是一个迭代)

img

a) 接口定义,根据需求分析定义接口

b) 服务端和前端并行开发 ,依据接口进行服务端接口开发。 postman测试

c) 前端开发用户操作界面,并请求服务端接口完成业务处理。 EasyMock模拟数据

d) 前后端集成测试(联调测试) 最终前端调用服务端接口完成业务。

瀑布模型:全部需求都设计完成,再来开发。 敏捷:分段开发

\5. 测试人员迭代测试 开发人员改bug

\6. …进行过N次迭代

\7. 集成测试

\8. 内侧、公测

\9. 上线 运维人员

\10. 运维-留一两个人

\11. 开发其他项目

4. 后端项目微服务原型搭建

4.1. 后端结构搭建

gift-parent(pom)               //管理jar:SpringBoot;SpringCloud,一些公共的内容
    gift-support-parent(pom) 		     //springcloud微服务支持模块
        gift-config-service-10010(jar)
        gift-eureka-service-20010(jar)
        gift-zuul-service-30010(jar)
    代码生成器。。。。
gift-basic-parent(pom)               //基础模块          
    gift-basic-common          //工具 : AjaxResult ,PageListBaseQuery//公共模块: 
gift-system-parent(pom)              //系统管理中心
    gift-system-client          //如果该服务需要被其它服务调用,写feign接口
    gift-system-common         //system的公共代码 :Query ,Domain
    gift-system-service-40010    //系统管理中心服务Controller,Service,Mapper
gift-gift-parent(pom)                //礼品中心
    gift-gift-common         //公共代码
    gift-gift-service-40020    //礼品中心服务
gift-gift-search-parent
    gift-gift-search-common           //feign客户端接口模块
    gift-gift-search-service-40030    //礼品搜索服务
gift-user-parent (pom)               //用户中心
    gift-user-common           //公共代码
    gift-user-service-40040//用户中心服务
gift-auth-parent (pom)               //用户中心
    gift-auth-common           //公共代码
    gift-auth-service-40050  //授权服务
。。。。。。。

4.2. 前端结构搭建

暂时不管,后面再来搭建

gift-websites-parent 微前端(pc)
gift-website-system 6001
     gift-website-protal 6002
     gift-website-user 6003
     gift-website-wiki 6004
     gift-website-active 6005

4.3. SpringCloud集成-自己用自己包 cn.ronghuanet.gift

4.3.1. 顶级父工程 gift-parent
 <?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>

    <groupId>cn.ronghuanet.gift</groupId>
    <artifactId>gift-parent</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
   

    <!--管理一些公共的属性-->
    <properties>
       <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <!--管理SpringBoot-->
    <parent>
        <groupId> org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
    </parent>

    <!--管理Spring Cloud-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>


    <!--公共的jar包-->
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/cn.hutool/hutool-all -->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.8.5</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.8</version>
            <scope>provided</scope>
        </dependency>

</dependencies>
</project>


4.3.2. 基础模块(basic)
gift-parent
   gift-basic-parent  基础模块parent
   	gift-basic-common 基础模块公共代码

\1) 基础工具模块(basic-common)

拷贝基础工具到该模块化中

4.3.3. Config Server
4.3.3.1 配置文件准备
\1) Gitee仓库创建

img

\2) 构造一个能够提示配置库-假springboot项目
<!--管理SpringBoot-->
<parent>
    <groupId> org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.5.RELEASE</version>
</parent>


<dependencies>
    <!--springboot支持-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
</dependencies>

img

\3) 准备配置

application-zuul-dev.yml

server:
  port: 20201111
spring:
  application:
    name: eureka-server

\4) 把配置库项目初始化到码云,并且删除本地,从码云进行导入。

让本地项目受git仓库管理

4.3.3.2 config service实现

1)创建项目-1010

2)导入jar

<!--springboot支持-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>

        <!--配置中心支持-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

3)yml配置

  server:
  port: 1010
spring:
  application:
    name: gift-config-server
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/yhpjsjjy2_admin/gift-config-20200918.git
username: yhpjsjjy2_admin
password: admin1234
search-paths: src/main/resources # 如果不是在根路径下面需要加搜索地址

 

4)入口

@SpringBootApplication

@EnableConfigServer //启用配置服务端

\1) 测试

http://localhost:1010/application-eureka/dev

configserver不需要,其他的都是客户端

本地搜索配置
server:
  port: 1010
spring:
  profiles:
    active: native #激活本地搜索配置
  application:
    name: gift-config-server
  cloud:
    config:
      server:
        native: #本地配置
          search-locations: file:D:\\software\\idea2017_workspace\\gift-config\\src\\main\\resources
#        git:
#          uri: https://gitee.com/yhpjsjjy2_admin/gift-config-20210507.git
#          username: yhpjsjjy2_admin
#          password: admin1234
#          search-paths: src/main/resources # 如果不是在根路径下面需

4.3.4. Eureka注册中心
\1) 创建项目
gift-parent
    gift-support-parent 					//springcloud微服务支持模块
		gift-eureka-10020
\2) pom.xml
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>

      <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!--configclient端-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
\3) 配置文件

配置库里面:application-eureka-dev.yml

spring:
  application:
    name: gift-eureka
server:
  port: 2010
eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #单机配置

bootstrap.yml

spring:
  profiles:
    active: dev #默认启动是dev
  cloud:
    config:
      profile: ${spring.profiles.active} #环境 java -jar -d spring.profiles.active=test gift-eureak.jar
      name: application-eureka #gitee上面名称
      label: master #分支
      uri: http://127.0.0.1:10010 #配置服务器,相当于连上仓库曹

\4) 入口类
/*注册中心启动类*/
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication
{
    public static void main( String[] args )
    {
        SpringApplication.run(EurekaServerApplication.class) ;
    }
}
\5) 启动测试

访问:http://localhost:2010

4.3.5. Zuul 服务网关
1)创建项目
gift-parent
    gift-support-parent 					//springcloud微服务支持模块
		gift-zuul-server-3010

2)导入jar
<!-- springBoot支持 -->
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
      <!-- Eureka 客户端依赖 -->
      <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
      </dependency>
     
       <!-- zuul支持-->
      <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
      </dependency>
<!--configclient端-->
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-config</artifactId>
</dependency>

\3) 做配置

application-zuul-dev.yml

server:
  port: 3010
spring:
  application:
    name: gift-zuul #服务名
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:2010/eureka/
  instance:
    prefer-ip-address: true  #使用ip注册到Eureka
    instance-id: zuul-service:3010  #指定客户端实例的ID
zuul:
  retryable: true #是否开启重试功能
  routes:
    ribbon:
      eager-load.enabled: true     # 饥饿加载
    myUser.serviceId: user-provider #服务名
    myUser.path: /myUser/** #哪个路径
    xxx.serviceId: order-provider #服务名
    xxx.path: /myOrder/** #哪个路径
  ignored-services: "*" #哪些服务不能通过服务名访问,*表示所有的服务
  prefix: "/services" #所有访问地址都加一个前缀
ribbon:
  MaxAutoRetries: 1 #对当前服务的重试次数
  MaxAutoRetriesNextServer: 1 #切换相同Server的次数
  OkToRetryOnAllOperations: false # 对所有的操作请求都进行重试,如post就不能重试,如果没做幂等处理,重试多次post会造成数据的多次添加或修改
  ConnectTimeout: 250 #请求连接的超时时间
  ReadTimeout: 3000 #请求处理的超时时间
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 15000 #如果配置ribbon的重试,hystrix的超时时间要大于ribbon的超时时间

spring:
 profiles:
   active: dev #默认启动是dev
 cloud:
   config:
     profile: ${spring.profiles.active} #环境 java -jar -d spring.profiles.active=test gift-eureak.jar
     name: application-zuul #gitee上面名称
     label: master #分支
     uri: http://127.0.0.1:10010 #配置服务器,相当于连上仓库曹

\4) 入口
//@EnableZuulProxy :开启zuul
@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
public class ZuulServerApplication1020 {
    public static void main(String[] args) {
        SpringApplication.run(ZuulServerApplication1020.class) ;
    }
}

5)启动测试

在Eureka中有Zuul就ok

\5. 创建Git仓库

5.1. 登录码云创建仓库

gift-parent-20220516

5.2. 本地克隆仓库

5.3. 拷贝项目到仓库目录

Copy,commit and push

img

5.4. 忽略不需要提交的内容

.gitignore

#忽略所有target目录

target/

5.5. 删除idea中没受git重新从服务器克隆

\6. 系统中心需求分析

img

1 系统配置 对整个系统做一些全局配置。 fastdfs地址,邮箱,短信 项目一启动就从数据库查询缓存到reids中,以后直接从redis查询 略过

2 部门 略过

2 员工管理 略过

3 租户类型: redis缓存 略过

4 租户:姚桑礼品店,… 要做,入驻,入驻审核,admin激活,购买套餐

5 套餐:关联权限

以上功能都需要操作数据库,操作数据库使用mybatis,有点弱,玩Mybatis-plus,所以接下来对Mybatis-plus进行入门学习,然后在集成到项目中。

7. Mybatis-plus入门

7.1. 引入

在真实项目开发中我们的服务模块,一般都要进行数据库操作,并且每个domain都有crud,需多次写重复代码。我们使用MybatisPlus,就不用写重复代码(封装好了基础的crud),并且还有模板的功能,可以一键生成domain,query,mapper接口,mapper.xml,service,controller,非常好用。

反向工程:设计好表,快速生成相关模块

代码生成器:通过表一键生成代码

快开

低代码

7.2. 简介

7.2.1. 是什么

MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

无侵入:只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑

损耗小:启动即会自动注入基本 CURD,性能基本无损耗,直接面向对象操作

强大的 CRUD 操作:内置通用 Mapper、通用 Service,仅仅通过少量配置即可实现单表大部分 CRUD 操作,更有强大的条件构造器,满足各类使用需求

支持 Lambda 形式调用:通过 Lambda 表达式,方便的编写各类查询条件,无需再担心字段写错

支持多种数据库:支持 MySQL、MariaDB、Oracle、DB2、H2、HSQL、SQLite、Postgre、SQLServer2005、SQLServer 等多种数据库

支持主键自动生成:支持多达 4 种主键策略(内含分布式唯一 ID 生成器 - Sequence),可自由配置,完美解决主键问题

支持 XML 热加载:Mapper 对应的 XML 支持热加载,对于简单的 CRUD 操作,甚至可以无 XML 启动

支持 ActiveRecord 模式:支持 ActiveRecord 形式调用,实体类只需继承 Model 类即可进行强大的 CRUD 操作

支持自定义全局通用操作:支持全局通用方法注入( Write once, use anywhere )

支持关键词自动转义:支持数据库关键词(order、key…)自动转义,还可自定义关键词

内置代码生成器:采用代码或者 Maven 插件可快速生成 Mapper 、 Model 、 Service 、 Controller 层代码,支持模板引擎,更有超多自定义配置等您来使用

内置分页插件:基于 MyBatis 物理分页,开发者无需关心具体操作,配置好插件之后,写分页等同于普通 List 查询

内置性能分析插件:可输出 Sql 语句以及其执行时间,建议开发测试时启用该功能,能快速揪出慢查询

内置全局拦截插件:提供全表 delete 、 update 操作智能分析阻断,也可自定义拦截规则,预防误操作

内置 Sql 注入剥离器:支持 Sql 注入剥离,有效预防 Sql 注入攻击

7.2.2. 结构(了解)

img

7.3. 运行项目搭建 - 我们的微服务 - 要生成代码的项目

7.3.1. 项目结构

img

在springboot中使用.

步骤分析:

​ 1创建springboot项目 略过

​ 2 导入mybatis-plus jar

​ 3 配置

7.3.2. 导入mybatis-plus jar

img

<artifactId>gift-dependencies-basic</artifactId>
    <dependencies>
        <!-- springBoot支持 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- springBoot支持 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>

        <!--平台基础代码-->
        <dependency>
            <groupId>cn.ronghuanet</groupId>
            <artifactId>gift-basic-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

<artifactId>gift-dependencies-core</artifactId>

    <dependencies>
        <dependency>
            <groupId>cn.ronghuanet</groupId>
            <artifactId>gift-dependencies-basic</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <!--mybatisplusjar-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>2.2.0</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
    </dependencies>

<artifactId>user-service-codegenstest</artifactId>

    <dependencies>
       <!-- 依赖gift的核心jar-->
        <dependency>
            <groupId>cn.ronghuanet</groupId>
            <artifactId>gift-dependencies-core</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

7.3.3. 配置

server:
  port: 8888
spring:
  application:
    name: user-service-test
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test
    username: root
    password: admin
mybatis-plus:
  type-aliases-package: cn.ronghuanet.gift.domain,cn.ronghuanet.gift.query

分页插件配置

package cn.ronghuanet.mybatisplus.mybatisplustest.config;

import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

//Spring boot方式
@EnableTransactionManagement
@Configuration
public class MybatisPlusConfig {

    /**
     * 分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}
7.3.4. 入口类
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("org.yn.gift.mapper")
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class,args);
    }
}

启动报错:springboot2.2.5,默认数据库驱动是8的,现在要缓存5,在gift-parent中配置一下

<!--管理一些公共的属性-->
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <mysql.version>5.1.47</mysql.version>
</properties>

7.3.5. 测试-生成代码后再做测试
import cn.ronghuanet.gift.domain.User;
import cn.ronghuanet.gift.service.IUserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = App.class)
public class UserServiceTest {
    @Autowired
    private IUserService userService;

    @Test
    public void testGetAll()throws Exception{

        System.out.println(userService.selectById(1L));
//        userService.insert(new User());
//        userService.updateById(new User());
//        userService.deleteById(1L);
        for (User user : userService.selectList(null)) {
            System.out.println(user);
        }
    }
}
7.4. 代码生成项目 - mybatisplus的代码生成器模块

可以重新开一个模块,也可以就在同一个模块里面。为了不影响运行的代码重新开新模块。

步骤分析:

1 创建项目

2 导入jar

3 配置

3 写代码测试

7.4.1. 创建项目并导入jar

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

      <!--模板引擎-->
      <dependency>
          <groupId>org.apache.velocity</groupId>
          <artifactId>velocity-engine-core</artifactId>
          <version>2.0</version>
      </dependency>

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

7.4.2. 配置

resource/mybatiesplus-config-服务名(英文).properties

#此处为本项目src所在路径(代码生成器输出路径),注意一定是当前项目所在的目录哟
OutputDir=F:\\workspace\\idea_workspace\\mybatisplustest\\src\\main\\java
#mapper.xml SQL映射文件目录
OutputDirXml=F:\\workspace\\idea_workspace\\mybatisplustest\\src\\main\\resources

OutputDirBase=F:\\workspace\\idea_workspace\\mybatisplustest\\src\\main\\java
#设置作者
author=yaohuaipeng
#自定义包路径
parent=cn.ronghuanet.mybatisplus.mybatisplustest

#数据库连接信息
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///gift_user
jdbc.user=root
jdbc.pwd=admin

7.4.3. 写代码测试

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.*;

/**
* Created by CDHong on 2018/4/6.
*/
public class GenteratorCode {

  public static void main(String[] args) throws InterruptedException {
      //用来获取Mybatis-Plus.properties文件的配置信息
      ResourceBundle rb = ResourceBundle.getBundle("mybatitsPlus-config"); //不要加后缀
      AutoGenerator mpg = new AutoGenerator();
      // 全局配置
      GlobalConfig gc = new GlobalConfig();
      gc.setOutputDir(rb.getString("OutputDir"));
      gc.setFileOverride(true);
      gc.setActiveRecord(true);// 开启 activeRecord 模式
      gc.setEnableCache(false);// XML 二级缓存
      gc.setBaseResultMap(true);// XML ResultMap
      gc.setBaseColumnList(false);// XML columList
      gc.setAuthor(rb.getString("author"));
      mpg.setGlobalConfig(gc);
      // 数据源配置
      DataSourceConfig dsc = new DataSourceConfig();
      dsc.setDbType(DbType.MYSQL);
      dsc.setTypeConvert(new MySqlTypeConvert());
      dsc.setDriverName(rb.getString("jdbc.driver"));
      dsc.setUsername(rb.getString("jdbc.user"));
      dsc.setPassword(rb.getString("jdbc.pwd"));
      dsc.setUrl(rb.getString("jdbc.url"));
      mpg.setDataSource(dsc);
      // 策略配置
      StrategyConfig strategy = new StrategyConfig();
      strategy.setTablePrefix(new String[] { "t_" });// 此处可以修改为您的表前缀
      strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略
      strategy.setInclude(new String[]{"t_user"}); // 需要生成的表
      mpg.setStrategy(strategy);
      // 包配置
      PackageConfig pc = new PackageConfig();
      pc.setParent(rb.getString("parent"));
      pc.setController("controller");
      pc.setService("service");
      pc.setServiceImpl("service.impl");
      pc.setEntity("domain");
      pc.setMapper("mapper");
      mpg.setPackageInfo(pc);

      // 注入自定义配置,可以在 VM 中使用 cfg.abc 【可无】
      InjectionConfig cfg = new InjectionConfig() {
          @Override
          public void initMap() {
              Map<String, Object> map = new HashMap<String, Object>();
              map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-rb");
              this.setMap(map);
          }
      };

      List<FileOutConfig> focList = new ArrayList<FileOutConfig>();

      // 调整 domain 生成目录演示
      focList.add(new FileOutConfig("/templates/entity.java.vm") {
          @Override
          public String outputFile(TableInfo tableInfo) {
              return rb.getString("OutputDirBase")+ "/cn/ronghuanet/mybatisplus/domain/" + tableInfo.getEntityName() + ".java";
          }
      });
      //query
      focList.add(new FileOutConfig("/templates/query.java.vm") {
          @Override
          public String outputFile(TableInfo tableInfo) {
              return rb.getString("OutputDirBase")+ "/cn/ronghuanet/mybatisplus/query/" + tableInfo.getEntityName() + "Query.java";
          }
      });

      // 调整 xml 生成目录演示
      focList.add(new FileOutConfig("/templates/mapper.xml.vm") {
          @Override
          public String outputFile(TableInfo tableInfo) {
              return rb.getString("OutputDirXml")+ "/cn/ronghuanet/mybatisplus/mapper/" + tableInfo.getEntityName() + "Mapper.xml";
          }
      });
      cfg.setFileOutConfigList(focList);
      mpg.setCfg(cfg);

      // 自定义模板配置,可以 copy 源码 mybatis-plus/src/main/resources/templates 下面内容修改,
      // 放置自己项目的 src/main/resources/templates 目录下, 默认名称一下可以不配置,也可以自定义模板名称
      TemplateConfig tc = new TemplateConfig();
      tc.setService("/templates/service.java.vm");
      tc.setServiceImpl("/templates/serviceImpl.java.vm");
      tc.setEntity(null);
      tc.setMapper("/templates/mapper.java.vm");
      //tc.setController(null);
      tc.setXml(null);
      // 如上任何一个模块如果设置 空 OR Null 将不生成该模块。
      mpg.setTemplate(tc);

      // 执行生成
      mpg.execute();
  }

}

8.总结

8.1. 重点

² 项目原型架构图

² 项目原型架构搭建

² Mybatisplus集成

8.2. 难点

² 项目原型架构搭建

8.3. 如何掌握?

² 写代码前先把流程搞清楚,要做哪些事情要知道,记不住可以看我的课件

² 有些内容,比如配置文件,可以拷贝SpringCloud项目中的,也可以去官网拷贝

² 代码敲完,做一遍总结

8.4. 排错技巧(技巧)

1.根据错误日志来,看不懂就翻译,或者百度这个错误日志

2.详细检查你的代码,根据老师的步骤检查一遍

3.必要时使用Debug排错

4.一个问题超过20分钟搞不定,就可以在群里问同学问老师了

8.5. 面试题

\1. 你们的项目架构是怎么样的,你给我画出来

\2. 你在集成SpringCloud的时候遇到过什么问题

\9. 扩展知识或课外阅读推荐(可选)

9.1. 扩展知识

\1) maven默认配置

img

img

img

img

img

\2) idea dashbord

img

\3) idea2017 三级模块

img

9.2.

Session详解:http://www.cnblogs.com/lonelydreamer/p/6169469.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值