• 谷粒学苑_第十一天


    要开始做前台部分(用户环境)

    之前我们用的后台前端框架是vue-admin-template

    这次的前台框架是用的NUXT

    轮播图实现

    显示课程和老师

    redis缓存

    NUXT

    服务端渲染技术

    在这里插入图片描述

    解压guli_site

    在这里我们使用的是成品,页面也基本写好

    然后下载依赖:
    在这里插入图片描述

    开始运行:

    npm rum dev
    
    • 1

    在这里插入图片描述

    后面出错了,原因是错误的回车,换行,缩进,按照提示改成对应的缩进,就行(在_id.vue里重新改一下缩进)

    在这里插入图片描述

    启动成功:

    在这里插入图片描述

    目录结构

    .nuxt 编译后的文件

    assets 静态文件(css,img等)

    compents 项目使用相关组件

    layout 定义网页布局

    middleware 中间件

    node_modules 组件

    pages 页面

    nuxt.config.js 配置文件

    首页面

    下载幻灯片插件

    需要固定版本

    npm install vue-awesome-swiper@3.1.3
    
    • 1

    幻灯片配置(已配好)

    静态资源(已配好)

    复制default.vue页面

    banner接口

    放到service-cms

    创建模块

    service_cms

    启动类:

    package com.lkw.cmsservice;
    
    
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.ComponentScan;
    
    @SpringBootApplication
    @ComponentScan(basePackages = {"com.lkw.*"})
    @MapperScan("com.lkw.cmsservice.mapper")
    public class CmsApplication {
        public static void main(String[] args) {
    
            SpringApplication.run(CmsApplication.class, args);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    application.properties

    service_acl

    添加数据库(前面加过了)

    代码生成

    用的mybatisX

    在这里插入图片描述

    在这里插入图片描述

    在这里插入图片描述

    实体类可能需要小修改,需要检查一下

    在这里插入图片描述

    表改成id长度32

    在这里插入图片描述

    加application.yml

    server:
      port: 8004
    spring:
      application:
        name: service-cms
      profiles:
        active: dev  #环境设置: dev,test,prod
      datasource:
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/guli?serverTimezone=GMT%2B8
        username: root
        password: 148963
      jackson:
        date-format: yyyy-MM-dd HH:mm:ss
        time-zone: GMT+8
      cloud:
        nacos:
          discovery:
            server-addr: 192.168.199.100:8848 #服务地址
    
    mybatis-plus:
      global-config:
        db-config:
          id-type: ASSIGN_UUID
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    相关接口
    package com.lkw.cmsservice.controller;
    
    import com.baomidou.mybatisplus.core.metadata.IPage;
    import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
    import com.lkw.cmsservice.commonutils.R;
    import com.lkw.cmsservice.entity.CrmBanner;
    import com.lkw.cmsservice.service.CrmBannerService;
    import io.swagger.annotations.ApiOperation;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;
    
    import java.util.List;
    
    /**
     * 

    * 首页banner表 前端控制器 *

    * * @author atguigu * @since 2020-08-10 */
    @RestController @RequestMapping("/educms/banneradmin") @CrossOrigin public class BannerAdminController { @Autowired private CrmBannerService crmBannerService; //分页查询 @GetMapping() public R pageBanner(@PathVariable long page, long limit){ Page<CrmBanner> bannerPage = new Page<>(page,limit); IPage<CrmBanner> pages = crmBannerService.page(bannerPage, null); List<CrmBanner> records = pages.getRecords(); long total = pages.getTotal(); return R.ok().data("items",records).data("total",total); } //添加banner @PostMapping("addBanner") public R addBanner(@RequestBody CrmBanner crmBanner){ crmBannerService.save(crmBanner); return R.ok(); } //根据id获取Banner @ApiOperation(value = "获取Banner") @GetMapping("get/{id}") public R get(@PathVariable String id) { CrmBanner banner = crmBannerService.getById(id); return R.ok().data("item", banner); } //修改Banner @ApiOperation(value = "修改Banner") @PutMapping("update") public R updateById(@RequestBody CrmBanner banner) { crmBannerService.updateById(banner); return R.ok(); } //根据id删除Banner @ApiOperation(value = "删除Banner") @DeleteMapping("remove/{id}") public R remove(@PathVariable String id) { crmBannerService.removeById(id); return R.ok(); } }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    package com.lkw.cmsservice.controller;
    
    
    
    import com.lkw.cmsservice.commonutils.R;
    import com.lkw.cmsservice.entity.CrmBanner;
    import com.lkw.cmsservice.service.CrmBannerService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.CrossOrigin;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.List;
    
    /**
     * 

    * 首页banner表 前端控制器 *

    * * @author atguigu * @since 2020-08-10 */
    @RestController @RequestMapping("/educms/bannerfront") @CrossOrigin public class BannerFrontController { @Autowired private CrmBannerService crmBannerService; //查询所有banner @GetMapping("getAllBanner") public R getAllBanner(){ List<CrmBanner> List =crmBannerService.selectAllBanner(); return R.ok().data("list",List); } }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    经典白写

    impl

    package com.lkw.cmsservice.service.impl;
    
    import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import com.lkw.cmsservice.entity.CrmBanner;
    import com.lkw.cmsservice.service.CrmBannerService;
    import com.lkw.cmsservice.mapper.CrmBannerMapper;
    import org.springframework.cache.annotation.Cacheable;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;
    
    import java.util.List;
    
    /**
    * @author 李可文
    * @description 针对表【crm_banner(首页banner表)】的数据库操作Service实现
    * @createDate 2022-11-27 11:54:34
    */
    @Transactional
    @Service
    public class CrmBannerServiceImpl extends ServiceImpl<CrmBannerMapper, CrmBanner> implements CrmBannerService {
    
        @Cacheable(value = "banner",key = "'selectIndexList'")
        //查询所有banner
        @Override
        public List<CrmBanner> selectAllBanner() {
            //根据id降序排列,显示排列之后的前两条记录
            QueryWrapper<CrmBanner> bannerQueryWrapper = new QueryWrapper<>();
            bannerQueryWrapper.orderByDesc("id");
            //list拼接sql语句
            bannerQueryWrapper.last("limit 2");
            List<CrmBanner> list = baseMapper.selectList(null);
            return list;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    last方法

    直接拼接到sql的最后

    查询热门课程和名师的接口

    package com.lkw.eduservice.controller.front;
    
    
    import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    import com.lkw.cmsservice.commonutils.R;
    import com.lkw.eduservice.entity.EduCourse;
    import com.lkw.eduservice.entity.EduTeacher;
    import com.lkw.eduservice.service.EduCourseService;
    import com.lkw.eduservice.service.EduTeacherService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.CrossOrigin;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.List;
    
    @CrossOrigin
    @RestController
    @RequestMapping("/eduservice/indexfront")
    public class IndexFrontController {
        //查询
    
        @Autowired
        EduCourseService eduCourseService;
    
        @Autowired
        private EduTeacherService eduTeacherService;
    
        //查询前八条记录
        @GetMapping("index")
        public R index(){
            QueryWrapper courseQueryWrapper = new QueryWrapper<>();
            courseQueryWrapper.orderByDesc("id");
            courseQueryWrapper.last("limit 8");
            List courseList = eduCourseService.list(courseQueryWrapper);
     
            //查询前四个名师
            QueryWrapper teacherQueryWrapper = new QueryWrapper<>();
            teacherQueryWrapper.orderByDesc("id");
            teacherQueryWrapper.last("limit 4");
            List teacherList = eduTeacherService.list(teacherQueryWrapper);
    
            return R.ok().data("eduList",courseList).data("teacherList",teacherList);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    前端banner

    下载axios

    封装axios

    api

    banner.js

    import request from '@/utils/request'
    export default{
      getListBanner() {
        return request({
          url: 'http://localhost:8004/educms/bannerfront/getAllBanner',
          method: 'get'
        })
      }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    index.vue

    
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
    • 348
    • 349
    • 350
    • 351
    • 352
    • 353
    • 354
    • 355
    • 356
    • 357
    • 358
    • 359
    • 360
    • 361
    • 362
    • 363
    • 364
    • 365
    • 366

    alt:光标放置显示(相当于名字了)

    href:超链接地址

    src:地址

    启动效果

    没有图,可能老师没交钱,我们改改数据库就行

    在这里插入图片描述

    换了数据库之后:

    前端好像没加自适应,先不管了
    在这里插入图片描述

    热门课程和名师数据显示

    为了加快进度,前端先不自己写了,我在github上直接导入了一个

    index.js

    import request from '@/utils/request'
    export default{
      getIndexData() {
        return request({
          url: 'http://localhost:8001//eduservice/indexfront/index',
          method: 'get'
        })
      }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    热门课程删掉部分

    • v-for遍历eduList

      后端报错要检查nacos配置和nacos有没起来

      Redis解放mysql

      添加依赖:

              
              <dependency>
                  <groupId>org.springframework.bootgroupId>
                  <artifactId>spring-boot-starter-data-redisartifactId>
              dependency>
      
              
              <dependency>
                  <groupId>org.apache.commonsgroupId>
                  <artifactId>commons-pool2artifactId>
                  <version>2.6.0version>
              dependency>
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12

      创建redis配置类到common里

      package com.lkw.servicebase;
      
      import com.fasterxml.jackson.annotation.JsonAutoDetect;
      import com.fasterxml.jackson.annotation.PropertyAccessor;
      import com.fasterxml.jackson.databind.ObjectMapper;
      import org.springframework.cache.CacheManager;
      import org.springframework.cache.annotation.CachingConfigurerSupport;
      import org.springframework.cache.annotation.EnableCaching;
      import org.springframework.context.annotation.Bean;
      import org.springframework.context.annotation.Configuration;
      import org.springframework.data.redis.cache.RedisCacheConfiguration;
      import org.springframework.data.redis.cache.RedisCacheManager;
      import org.springframework.data.redis.connection.RedisConnectionFactory;
      import org.springframework.data.redis.core.RedisTemplate;
      import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
      import org.springframework.data.redis.serializer.RedisSerializationContext;
      import org.springframework.data.redis.serializer.RedisSerializer;
      import org.springframework.data.redis.serializer.StringRedisSerializer;
      
      import java.time.Duration;
      
      @EnableCaching
      @Configuration
      public class RedisConfig extends CachingConfigurerSupport {
      
          @Bean
          public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
              RedisTemplate<String, Object> template = new RedisTemplate<>();
              RedisSerializer<String> redisSerializer = new StringRedisSerializer();
              Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
              ObjectMapper om = new ObjectMapper();
              om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
              om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
              jackson2JsonRedisSerializer.setObjectMapper(om);
              template.setConnectionFactory(factory);
              //key序列化方式
              template.setKeySerializer(redisSerializer);
              //value序列化
              template.setValueSerializer(jackson2JsonRedisSerializer);
              //value hashmap序列化
              template.setHashValueSerializer(jackson2JsonRedisSerializer);
              return template;
          }
      
          @Bean
          public CacheManager cacheManager(RedisConnectionFactory factory) {
              RedisSerializer<String> redisSerializer = new StringRedisSerializer();
              Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
              //解决查询缓存转换异常的问题
              ObjectMapper om = new ObjectMapper();
              om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
              om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
              jackson2JsonRedisSerializer.setObjectMapper(om);
              // 配置序列化(解决乱码的问题),过期时间600秒
              RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
                      .entryTtl(Duration.ofSeconds(600))
                      .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
                      .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
                      .disableCachingNullValues();
              RedisCacheManager cacheManager = RedisCacheManager.builder(factory)
                      .cacheDefaults(config)
                      .build();
              return cacheManager;
          }
      }
      
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44
      • 45
      • 46
      • 47
      • 48
      • 49
      • 50
      • 51
      • 52
      • 53
      • 54
      • 55
      • 56
      • 57
      • 58
      • 59
      • 60
      • 61
      • 62
      • 63
      • 64
      • 65
      • 66

      注解:@cacheable

      加到api方法上,

      页面调用方法首先查redis ,redis没有再查后面的(mysql之类的)然后放到redis里

      注意导报

      注意key有两个引号

       @Cacheable(value = "banner",key = "'selectIndexList'")
      
      • 1

      启动redis

      连接redis可能的问题:

      防火墙没关

      配置文件注释掉 #127.0.0.1

      修改protected-mode no

      yml注释

      spring:
        redis:
          host: 192.168.199.100
          port: 6379
          database: 0
          timeout: 1800000
          password: 123456
          lettuce:
            pool:
              max-active: 20
              max-wait: -1
              #最大阻塞时间
              max-idle: 5
              min-idle: 0
      
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15

      controller不能加这个注解

  • 相关阅读:
    深入理解,2022最新版JDK源码笔记强势开源,基础到高级,应有尽有,太全了
    JavaScript之document对象最常用相关知识总结
    1024程序员节 | C++ NOIP2014 普及组 T3 螺旋矩阵
    【Oracle】数据库账号频繁被锁问题解决
    DS:八大排序之堆排序、冒泡排序、快速排序
    MyBatis完成增删改查案例(详细代码)
    【JVM调优实战100例】03——JVM堆调优四例
    《流畅的python》阅读笔记 - 第六章:使用一等函数实现设计模式
    [springMVC学习]8、解决springmvc中文乱码问题
    大数据Flink(一百零二):SQL 聚合函数(Aggregate Function)
  • 原文地址:https://blog.csdn.net/m0_52070517/article/details/128140630