优惠券项目---------------第四章

了解方案。

-----------------------------------------------------------------4-1------------------------------------------------------------------

-----------------------------------------------------------------4-2------------------------------------------------------------------

数据库的设计:优惠券表 用户优惠券表

CREATE TABLE `t_coupon`(
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `code` varchar(128) NOT NULL DEFAULT '' COMMENT '优惠券码',
  `pic_url`varchar(255) NOT NULL DEFAULT '' COMMENT '优惠券图',
  `achieve_amount` int(11) NOT NULL DEFAULT 0 COMMENT '达到满减资格⾦额',
  `reduce_amount` int(11) NOT NULL DEFAULT 0 COMMENT '所减⾦额',
  `stock` int(11) NOT NULL DEFAULT 0 COMMENT '库存,当库存为0不可领取',
  `title`varchar(64) NOT NULL DEFAULT '' COMMENT '优惠券名称',
  `status` int(1) NOT NULL DEFAULT 0 COMMENT '状态为0表示可⽤,1为不可⽤',
  `create_time` datetime DEFAULT NULL,
PRIMARY KEY(`id`)
)ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8 COMMENT '优惠券定
义表';
CREATE TABLE `t_user_coupon`(
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_coupon_code` varchar(128) NOT NULL DEFAULT '' COMMENT '⽤户优惠券
码',
  `pic_url`varchar(255) NOT NULL DEFAULT '' COMMENT '优惠券图',
  `coupon_id` int(11) NOT NULL DEFAULT 0 COMMENT 't_coupon表外键ID',
  `user_id` int(11) NOT NULL DEFAULT 0 COMMENT '所领取⽤户id',
  `status` int(1) NOT NULL DEFAULT 0 COMMENT '状态为0表示未使⽤,1为已使⽤',
  `order_id` int(11) NOT NULL DEFAULT 0 COMMENT '对应t_order表外键',
  `create_time` datetime DEFAULT NULL,
  PRIMARY KEY(`id`)
)ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8 COMMENT '⽤户优惠
券';

-----------------------------------------------------------------4-3------------------------------------------------------------------

mysql的配置:

第一步:引入依赖

第二步:yml文件。

server:
   port: 8091
druid:
  allow:
     ip: 192.168.244.130
  login:
     user_name: root
     password: 123456
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://192.168.244.130:3306/shop?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false
    username: root
    password: 123456
    type: com.alibaba.druid.pool.DruidDataSource
    initial-size: 1
    min-idle: 1
    max-idle: 5
    max-wait-millis: 60000
    timeBetweenEvictionRunsMillis: 60000
      # 配置一个连接在池中最小生存的时间,单位是毫秒
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: true
    testOnReturn: false
    maxPoolPreparedStatementPerConnectionSize: 20
      # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
    filters: stat,wall
      # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000

第四步:配置文件

package com.xdclass.couponapp.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class DruidConfig {


    @Value("${druid.login.user_name}")
    private String userName;

    @Value("${druid.login.password}")
    private String password;


    @Bean(name = "default_datadatasource")
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource druidDataSource(){
        return new DruidDataSource();
    }


    @Bean
    public ServletRegistrationBean druidServlet() {
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
        servletRegistrationBean.setServlet(new StatViewServlet());
        servletRegistrationBean.addUrlMappings("/druid/*");
        Map<String, String> initParameters = new HashMap<>();
        initParameters.put("loginUsername", userName);// 用户名
        initParameters.put("loginPassword", password);// 密码
        initParameters.put("resetEnable", "false");// 禁用HTML页面上的“Reset All”功能
        servletRegistrationBean.setInitParameters(initParameters);
        return servletRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new WebStatFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return filterRegistrationBean;
    }

}

第五步:启动测试

看下druid的后台:http://localhost:8091/druid/login.html

-----------------------------------------------------------------4-4------------------------------------------------------------------

逆向工程。

第一步:引入jar包pom

第二步:插件

第四步:

第六步:

我们要配置:

1.数据库连接

2.文件放在哪里

第七部:run去generator

-----------------------------------------------------------------4-5------------------------------------------------------------------

Junit的使用:

基于springboot的单元测试:

这里是为了读取main函数的resources。

-----------------------------------------------------------------4-6------------------------------------------------------------------

新增等记录。

-----------------------------------------------------------------4-7------------------------------------------------------------------

查询有无索引

-----------------------------------------------------------------4-8------------------------------------------------------------------

example:

TCouponExample example = new TCouponExample();
        example.createCriteria().andCodeEqualTo("d7bb27c0-90f0-4d9e-a8f5-774ef8f21392").andStatusEqualTo(0)
                .andAchieveAmountBetween(100,1000).andTitleNotLike("111");
        List<TCoupon> tCoupon =  tCouponMapper.selectByExample(example);

-----------------------------------------------------------------4-9-------------------------------------------------------------------

代码分层规范:

-----------------------------------------------------------------4-10------------------------------------------------------------------

发布了374 篇原创文章 · 获赞 12 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_28764557/article/details/104761134