1.创建bigdata项目 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>3.将文件改名为application.yml 添加
spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/guery?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone=UTC 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 # 连接超时时间(毫秒)4.创建文件 5.在UserController 写入
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.*; @RestController @RequestMapping("user") public class UserController { @Autowired private UserService userService; @Autowired private RedisService redisService; @GetMapping("/gueryBuId") public String gueryById(int id) { String userStr = redisService.get(id + ""); if (userStr==null) { redisService.set(id + "", userService.gueryById(id).toString()); } return userStr; } @PatchMapping("/save") public User saveUser(String name,int age){ return userService.save(new User(name,age)); } }6.在UserDao写入
package com.nmgdz.bigdata.dao; import com.nmgdz.bigdata.entity.User; import org.springframework.data.jpa.repository.JpaRepository; import java.util.List; public interface UserDao extends JpaRepository<User,Integer>{ List<User> findByAge(int age); User findById(int id); }7.在User写入
package com.nmgdz.bigdata.entity; import lombok.Data; import java.io.Serializable; import javax.persistence.*; @Table(name ="t_bigdata_user")数据库表名称 @Entity @Data public class User implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; private String name; private Integer age; public User(String name, Integer age){ this.name = name; this.age = age; } public User() { } }8.在RedisServicelmpl写入
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; import java.security.Key; @Service public class RedisServicelmpl 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); } }9.在UserServicelmpl写入
package com.nmgdz.bigdata.service.impl; import com.nmgdz.bigdata.dao.UserDao; import com.nmgdz.bigdata.entity.User; import com.nmgdz.bigdata.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserServicelmpl implements UserService { @Autowired private UserDao userDao; @Override public List<User> gueryByAge(int age) { return userDao.findByAge(age); } @Override public User save(User user) { return userDao.save(user); } @Override public User gueryById(int id) { return userDao.findById(id); } @Override public void deleUserById(int id) { userDao.deleteById(id); } }10.RedisService写入
package com.nmgdz.bigdata.service; public interface RedisService { /** * 存储数据 */ void set (String key, String value); /** * 获取数据 */ String get(String key); /** * 设置超期时间 */ boolean expire(String key,long expire); /** * 删除数据 */ void remove(String key); /** * 自增操作 * * @param delta 自增步长 */ Long increment(String key, long delta); }11.UserService写入
package com.nmgdz.bigdata.service; import com.nmgdz.bigdata.entity.User; import java.util.List; public interface UserService { /** * 根据年龄查询用户 * @param age * @return */ List<User> gueryByAge(int age); /** * 保存用户 * @param user * @return */ User save(User user); /** * 根据主键删除用户 * @param id */ void deleUserById(int id); /** * 根据主键查询 * @param id * @return */ User gueryById(int id); }运行