Java项目各框架环境配置

SpringBoot

  1. 使用Spring Initializr创建工程,勾选Spring Web
  2. 导入依赖
<dependency>
	<groupId>org.projectlombokgroupId>
	<artifactId>lombokartifactId>
	<version>1.18.20version>
dependency>
<dependency>
	<groupId>junitgroupId>
	<artifactId>junitartifactId>
	<scope>testscope>
dependency>
  1. application.properties配置文件改为application.yml
    • 配置端口:
server: 
	port: 8080

Mybatis-Plus

  1. 导入依赖
<dependency>
	<groupId>org.mybatis.spring.bootgroupId>
	<artifactId>mybatis-spring-boot-starterartifactId>
	<version>2.2.2version>
dependency>
<dependency>
	<groupId>com.baomidougroupId>
	<artifactId>mybatis-plus-boot-starterartifactId>
	<version>3.1.1version>
dependency>
  1. 编写配置文件application.yml
#下面这些内容是为了让MyBatis映射
mybatis:
  #指定Mybatis的Mapper文件
  mapper-locations=classpath: mappers/*xml
  #指定Mybatis的实体目录
  type-aliases-package: com.rainbow.entity

mybatis-plus:
  configuration:
    #在映射实体或者属性时,将数据库中表名和字段名中的下划线去掉,按照驼峰命名法映射.
    map-underscore-to-camel-case: true
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  global-config:
    db-config:
      id type: ASSIGN_ID
  1. 配置mapper、service、serviceImpl
    • mapper:创建接口继承BaseMapper;该接口用@Mapper注解
    • service:创建接口继承IService
    • serviceImpl:创建类实现service接口;继承ServiceImpl;用@Service注解该类

Mysql

  1. 导入依赖
<dependency>
	<groupId>mysqlgroupId>
	<artifactId>mysql-connector-javaartifactId>
	<version>5.1.47version>
dependency>

<dependency>
	<groupId>com.alibabagroupId>
	<artifactId>druidartifactId>
	<version>1.2.16version>
dependency>
  1. 配置文件application.yml
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/DatabaseName
    username: root
    password: root
    type: com.alibaba.druid.pool.DruidDataSource

Redis

  1. 需要在服务器上启动Redis服务
  2. 在Java中引入依赖
<dependency>
	<groupId>org.springframework.bootgroupId>
	<artifactId>spring-boot-starter-data-redisartifactId>
dependency>

<dependency>
	<groupId>org.apache.commonsgroupId>
	<artifactId>commons-pool2artifactId>
dependency>
  1. 配置文件application.yml中配置redis
spring:
  redis:
    host: 192.168.133.128
    port: 6379
    password: root
    lettuce:
      pool:
        max-active: 8
        max-idle: 8
        min-idle: 0
        max-wait: 100ms

  1. 注入RedisTemplate
@Autowired
private RedisTemplate redisTemplate

Nacos

  1. 服务器启动Nacos
  2. java导入依赖
  • 父工程引入Alibaba的SpringCloud依赖
<dependency>
    <groupId>com.alibaba.cloudgroupId>
    <artifactId>spring-cloud-alibaba-dependenciesartifactId>
    <version>2.2.6.RELEASEversion>
    <type>pomtype>
    <scope>importscope>
dependency>
  • 客户端引入nacos-discovery依赖

<dependency>
    <groupId>com.alibaba.cloudgroupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>
  1. 在配置文件application.yml中配置nacos
spring:
  cloud:
    nacos:
      server-addr: localhost:8848

Feign

  1. 引入依赖
<dependency>
    <groupId>org.springframework.cloudgroupId>
    <artifactId>spring-cloud-starter-openfeignartifactId>
dependency>
  1. 在服务消费者的启动类上添加注解@EnableFeignClients开启Feign的功能
  2. 在服务消费者的包下定义接口,加上服务提供者的接口
  3. 使用该接口实现远程调用

网关Gateway

  1. 引入依赖

<dependency>
    <groupId>org.springframework.cloudgroupId>
    <artifactId>spring-cloud-starter-gatewayartifactId>
dependency>
  1. 编写基本配置
server:
  port: 10010 # 网关端口
spring:
  application:
    name: gateway # 服务名称
  cloud:
    nacos:
      server-addr: localhost:8848 # nacos地址
    gateway:
      routes: # 网关路由配置
        - id: user-service # 路由id,自定义,只要唯一即可
          # uri: http://127.0.0.1:8081 # 路由的目标地址 http就是固定地址
          uri: lb://userservice # 路由的目标地址 lb就是负载均衡,后面跟服务名称
          predicates: # 路由断言,也就是判断请求是否符合路由规则的条件
            - Path=/user/** # 这个是按照路径匹配,只要以/user/开头就符合要求

RabbitMQ

  1. 服务器启动RabbitMQ
  2. 引入依赖

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-amqpartifactId>
dependency>
  1. 配置
spring:
  rabbitmq:
    host: 192.168.133.128 # 主机名
    port: 5672 # 端口
    virtual-host: / # 虚拟主机
    username: root # 用户名
    password: root # 密码
  1. 注入RabbitTemplate
@Autowired
private RabbitTemplate rabbitTemplate
  1. 服务提供者rabbitTemplate.convertAndSend
  2. 服务提供者@RabbitListener

Elasticsearch

  1. 导入依赖
<dependency>
    <groupId>org.elasticsearch.clientgroupId>
    <artifactId>elasticsearch-rest-high-level-clientartifactId>
dependency>
  1. 初始化代码
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
        HttpHost.create("http://192.168.133.128:9200")
));

你可能感兴趣的:(项目经验和BUG,java,开发语言)