开发实例三(增删改查)增强版

开发实例(增删改查)增强版

第一步:检查redis服务是否开启

1

第二步:检查mysql服务是否开启

2

第三步:导入pom.xml依赖包

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!-- druid数据库连接池-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.10</version>
        </dependency>
        <!--lombok依赖-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!--redis依赖配置-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <!--Swagger-UI API文档生产工具-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.7.0</version>
        </dependency>

3

第四步:导入配置信息

4

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&serverTimezone=Asia/Shanghai
    username: root
    password: 123456
    initialSize: 20
    minIdle: 50
    maxActive: 500
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true

  redis:
    host: localhost # Redis服务器地址
    database: 0 # Redis数据库索引(默认为0)
    port: 6379 # Redis服务器连接端口
    password: 123456 # Redis服务器连接密码(默认为空)
    jedis:
      pool:
        max-active: 8 # 连接池最大连接数(使用负值表示没有限制)
        max-wait: -1ms # 连接池最大阻塞等待时间(使用负值表示没有限制)
        max-idle: 8 # 连接池中的最大空闲连接
        min-idle: 0 # 连接池中的最小空闲连接
    timeout: 3000ms # 连接超时时间(毫秒)

第五步:页面配置Swapperconfig

6

package com.nmgdz.bigdata.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 作者: xhd
 创建时间: 2020/3/13 17:32
 版本: V1.0
 */

/**
 * Swagger2API文档的配置
 */
@Configuration
@EnableSwagger2
public class Swagger2Config {
    @Bean
    public Docket createRestApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //为当前包下controller生成API文档
                .apis(RequestHandlerSelectors.basePackage("com.nmgdz.bigdata.controller"))
                //为有@Api注解的Controller生成API文档
//                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                //为有@ApiOperation注解的方法生成API文档
//                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    .title("SwaggerUI演示")
                    .description("内蒙古电子大数据工坊")
                    .contact("xhd")
                    .version("1.0")
                    .build();
    }
}

第六步:配置userController.java

6
7

package com.nmgdz.bigdata.controller;

import com.nmgdz.bigdata.entity.User;
import com.nmgdz.bigdata.service.RedisService;
import com.nmgdz.bigdata.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @auther: xhd
 * @Date: 2020/9/1/11:15
 */
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private RedisService redisService;

    /**
     * 查询
     *
     * @param id
     * @return
     */
    @GetMapping("/queryById")
    public String queryById(int id) {
        //从redis获取key为Id User
        String userStr = redisService.get(id + "");
        //判断是否存在
        if (userStr != null) {
            //查询 设置到redis
            redisService.set(id + "", userService.queryById(id).toString());

        }

        return userStr;
    }


    /**
     * 增 改
     * @param user
     * @return
     */
    @PostMapping("/saveOrUpdate")
    public User saveOrUpdateUser(User user) {

        return userService.save(user);
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    public Boolean deleteUser(int id) {
        userService.deletUserById(id);
        return true;
    }

}

第七步:进行User.java配置

8

package com.nmgdz.bigdata.entity;

import lombok.Data;
import org.hibernate.annotations.CreationTimestamp;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.*;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;

/**
 * 作者: xhd
 * 创建时间: 2020/3/12 19:43
 * 版本: V1.0
 */
@Table(name = "company_monitor_sensor")
@Entity
@Data
public class User implements Serializable {
    @Id //主键
    @GeneratedValue(strategy = GenerationType.IDENTITY) //自增主键
    private Integer id;
    private String company_code;
    private Integer monitor_type;
    private String sys_name;
    private Integer equipment_type_id;
    private String equipment_type_name;
    private Integer sensor_type_id;
    private String sensor_type_name;
    private Integer technology_type_id;
    private String technology_type_name;
    private String department;
    private String sensor_code;
    private String location;
    private String storage_material;
    private BigDecimal level_alarm_high;
    private BigDecimal secondary_alarm_high;
    private BigDecimal level_alarm_low;
    private BigDecimal secondary_alarm_low;
    private Integer operate_id;
    private String operate_username;
    @CreationTimestamp
    @Column(name="create_time")
    @DateTimeFormat(pattern = "yyyy-mm-dd")
    private Date create_time;
    @CreationTimestamp
    @Column(name="update_time")
    @DateTimeFormat(pattern = "yyyy-mm-dd")
    private Date update_time;

    /*public User(String company_code, Integer monitor_type,String sys_name,Integer equipment_type_id,String equipment_type_name,Integer sensor_type_id,String sensor_type_name,Integer technology_type_id) {
        this.company_code=company_code ;
        this.monitor_type = monitor_type;
        this.sys_name = sys_name;
        this.equipment_type_id = equipment_type_id;
        this.equipment_type_name = equipment_type_name;
        this.sensor_type_id = sensor_type_id;
        this.sensor_type_name = sensor_type_name;
        this.technology_type_id = technology_type_id;
*/


    //}


    //public User() {
    //}
}

第八步:配置RedisServiceImpl.java

9

package com.nmgdz.bigdata.service.impl;

import com.nmgdz.bigdata.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @auther: xhd
 * @Date: 2020/9/2/0:26
 */
@Service
public class RedisServiceImpl implements RedisService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void set(String key, String value) {
        stringRedisTemplate.opsForValue().set(key, value);
    }

    @Override
    public String get(String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }

    @Override
    public boolean expire(String key, long expire) {
        return stringRedisTemplate.expire(key, expire, TimeUnit.SECONDS);
    }

    @Override
    public void remove(String key) {
        stringRedisTemplate.delete(key);
    }

    @Override
    public Long increment(String key, long delta) {
        return stringRedisTemplate.opsForValue().increment(key, delta);
    }
}

第九步:UserService.java配置

10

package com.nmgdz.bigdata.service;

import com.nmgdz.bigdata.entity.User;

import java.util.List;

/**
 * @auther: xhd
 * @Date: 2020/9/1/11:10
 */
public interface UserService {
    /**
     * 根据年龄查询用户
     *
     * @param age
     * @return
     */
    //List<User> queryByAge(int age);
    /**
     * 保存用户
     *
     * @param user
     * @return
     */
    User save(User user);

    /**
     * 根据主键删除用户
     *
     * @param id
     */
    void deletUserById(int id);
    /**
     * 根据主键查询
     * @param id
     * @return
     */
    User queryById(int id);
}

问题:1.连接数据库时出现报错
2.java源代码语法出错

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值