springboot微信订餐系统总结

本文介绍了基于SpringBoot的微信订餐系统的技术实现,包括SpringBoot+Redis+Session的登录逻辑,JPA和Mybatis的数据操作,以及Redis缓存的使用。详细讲解了配置、注解和拦截器的应用,提供了整体框架和关键代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

整体框架

  • springboot框架+mysql+freemarker
  • 3层架构
  • springboot+Session
  • springboot+Redis
  • springboot+JPA
  • springboot+mybatis

springboot+Redis+session

首先引入pom引入redis

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

yml文件配置

 / /  配置 ip和 端口
  redis:
    host: 192.168.30.113
    port: 6379

登陆

根据登陆的流程看出利用redis存储和cookies存储token得到redis的openid value

  1. 查找数据库找是否有该openid
  2. 把token和openid用于redis的key和value存储到redis中
  3. 把token存储到cookies中
    在这里插入图片描述
代码
 //openid和数据库中的数据配对
        SellerInfo sellerInfo = sellerService.findByOpenid(openid);
        if (sellerInfo == null) {
            map.put("msg", ResultEnum.LOGIN_FAIL.getMessage());
            map.put("url","sell/seller/order/List");
            return new ModelAndView("common/error");
        }
        //把token放到redis中
        String token = UUID.randomUUID().toString();
        Integer expire= RedisConstant.EXPIRE;
        redisTemplate.opsForValue().set(String.format(RedisConstant.TOKEN_PREFIX,token),openid,expire, TimeUnit.SECONDS);
        //设置token到cookies中,从token中取到value值,然后从value值中得到reids中根据tokenid得到openid
        CookieUtil.set(response, CookieConstant.TOKEN,token,CookieConstant.EXPIRE);
        return new ModelAndView("redirect:/sell/seller/order/list");

 /**
     *  设置cookies
     * @param response 请求
     * @param name key
     * @param value value
      * @param maxAge 过期时间
     */
    public static void set(HttpServletResponse response,String name,String value,int maxAge){
        Cookie cookie= new Cookie(name,value);
        cookie.setPath("/");
        cookie.setMaxAge(maxAge);
        response.addCookie(cookie);
    }

退出登陆

  1. 首先从cookie中得到token
  2. 如果cookie不为空,则删除redis中数据
  3. 清楚cookis中的数据为null
 //cookies里面查询
        Cookie cookie = CookieUtil.get(request, CookieConstant.TOKEN);
        if (cookie != null) {
            //清除redis
            redisTemplate.opsForValue().getOperations().delete(String.format(RedisConstant.TOKEN_PREFIX,cookie.getName()));
            // 清楚cookies
            CookieUtil.set(response,CookieConstant.TOKEN,null,0);
        }
        map.put("msg",ResultEnum.LOGOUT_SUCCESS.getMessage());
        map.put("url","sell/seller/order/list");
        return new ModelAndView("common/success",map);

          /**
     * 得到cookie
     * @param request  响应
     * @param name
     * @return
     */
    public static Cookie get(HttpServletRequest request,String name){
        Map<String, Cookie> CookieMap = readCookieMap(request);
        if(CookieMap!=null){
            if(CookieMap.containsKey(name)){
                return CookieMap.get(name);
            }else {
                return null;
            }
        }
        return  null;
    }
        

springboot+JPA

1引入pom文件

	    <dependency>  
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>

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

		<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-jpa</artifactId>
		</dependency>

2 yml文件配置

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: judy
    url: jdbc:mysql://localhost/sell?characterEncoding=utf-8&useSSL=false
  jpa:
    show-sql: true

3 代码

JpaRepository<OrderDetailEntity,Integer> 表示表实体和id类型

@Component
public interface OrderDetilRepository  extends JpaRepository<OrderDetailEntity,Integer> {
   //订单详情表
    List<OrderDetailEntity> findByOrderId(Integer orderId);
}

@Query: 查询
@Modifying :更新

springboot+mybatis

引入pom文件

<dependency>
		<groupId>org.mybatis.spring.boot</groupId>
		<artifactId>mybatis-spring-boot-starter</artifactId>
		<version>1.2.0</version>
	</dependency>

配置mybatis

mybatis:
  mapper-locations: classpath:mapper/*.xml

扫描mapper

你不扫描mapper肯定不能读取sql啊

//mybatis的扫描包
@MapperScan(basePackages = "com.judy.demo.mapper")

代码

@ insert
@ select
@ update
@ delete

…在mapper写sql,不说了…

思路

  • 如果进行前后端分离写代码,(判空的时候怎么办)
  • 如果回复统一code码
  • 拦截器使用
  • 封装异常

注解使用

@SpringBootApplication:

@SpringBootApplication = @Configuration + @ EnableAutoConfiguration + @ComponentScan

@Configuration与@Bean 是一对,一般在项目中都是这样使用的

//表示这个类是bean定义的源
@Configuration
public class ComponentDemo{

    //负责告诉容器返回的对象会交给springioc容器管理
    @Bean
    public String judy(){
        String a = new String();
        return a;
    }

    @Bean
    public Integer judyInt(){
        Integer a = new Integer(2);
        return a;
    }
}

@ EnableAutoConfiguration : 自动载入应用程序所需的所有Bean, 依赖于springboot在类路径中查找

@ComponentScan : 会自动扫描指定包下所有标有@Component类,并且注为Bean, 还有@Component下的注解,@service @Repository @Controller

@MapperScan

一般是在项目启动的时候要扫描到mapper

@SpringBootApplication
//mybatis的扫描包
@MapperScan(basePackages = "com.judy.demo.mapper")
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

}

@JsonSerialize

一般是序列化为指定的类型

 @JsonSerialize(using = Date2LongSerializer.class)
    private Date updateTime;

@JsonIgnore

它是在属性上使用的,一般用于序列化的时候自动忽略该属性,用于前后端联调的时候使用

@Entity与@Table(name=“order_detail”)

@Entity实体类一般是和表的名字对应,如果不对应的时候可以使用@Table用来指定表的名字

@DynamicUpdate(true)

当设置为true的时候表示更新的时候如果属性的值为null则不会更新,默认为false

@JsonProperty

前端传值的时候序列化为另一个属性名

断言

一般在调试和测试的使用到,最好在项目中要保持一致性

0:预期值,实际值
  Assert.assertEquals(0,orderDTOPage.getTotalElements());
不为空
Assert.assertNotNull(byCategoryType);
不等于0则正确
  Assert.assertNotEquals(0,productCategoryEntityList.size());
  ........

拦截

例如判断用户是否登陆过
使用@compent容器管理
@Pointcut切点

@Pointcut("execution(public * com.judy.demo.Controler.Seller*.*(..))"+"&& !execution(public * com.judy.demo.Controler.SellerUserController.*(..))")
    public void verify(){}
  @Before("verify()")
    public void doVerify() throws SellerAuthorizeAspect {
       ServletRequestAttributes attributes =(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
       HttpServletRequest request = attributes.getRequest();
       Cookie cookie = CookieUtil.get(request, CookieConstant.TOKEN);
       if(cookie==null){
           log.warn("登陆失败,cookies中查不到token");
           throw  new SellerAuthorizeAspect();
       }
       //去redis中查询
       String tokenValue=redisTemplate.opsForValue().get(String.format(RedisConstant.TOKEN_PREFIX,cookie.getName()));
       if (StringUtils.isEmpty(tokenValue)){
           log.warn("登陆校验失败redis中没有token");
           throw new SellerAuthorizeAspect();
       }
   }

额外补充(redis缓存知识点)

@Cacheable(cacheNames=“product”,key=“123”)
当线程执行到这个方法的时候,返回值就会缓存上,当下次执行的时候直接从缓存中取出,首先说明cacheNames和key都是表示的redis的key,key可以不写,如果不写表示的是参数值

@CachePut: 更新

@CacheEvict : 删除
必须要提醒的是由缓存那么相应的也应该有删除,避免出现脏数据

unless : 表示如果为空则不存储, 如果不加会报错, 有一次项目上线的时候没有加unless 结果报错了

校园食堂订餐系统,是将计算机、通信等现代化技术运用到传统校园食堂服务的新型校园食堂服务方式。 校园食堂订餐系统为了解决以下几个问题:一是疫情期间,学生面临着开学,食堂是学生最聚集的场所之一,食堂订餐系统可以良好的解决学生饮食期间的拥挤等问题;二是让学生健康饮食,减轻目前的大学生吃外卖和不健康食品的问题;三是方便和改善学生的伙食,让学生可以随时随地的选购菜品;四是提高食堂商家的利润,改善商家的销售额。 本文在考虑到以上的问题的基础上,利用大学期间中所学到的的专业知识,独立开发一个基于Spring Boot和vue.js的校园食堂订餐系统。论文首先进行了系统功能的总体设计,使本系统具有以下主要功能:一是具有手机端让学生可以随时随地挑选食堂商家的菜品;二是可以让学生可以提交订单、一定时间范围修改和撤销订单;三是具有线上学生一卡通支付功能;四是对菜品销售情况具有统计功能方便商家查看与统计。 本文系统后台使用Spring Boot新型轻量开发框架,采用基本的B/S的互联网架构,前台技术使用可跨Android、IOS、H5、小程序的uni-app进行开发,使用IDEA的IntelliJ IDEA 2019.3.1 x64和WebStorm 2020.1 x64开发工具实现后台与前台的编码。使用MySQL数据库存储技术进行开发。最后完成了系统测试工作和调试工作,满足了校园食堂订餐系统的要求。 最后,对课题工作进行了总结,并对未来研究工作给予了展望。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王雪芬-ghqr-264962

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值