9-2完毕版

tech2022-07-14  173

9-2完毕版

UserController文件配置

配置UserController文件 package com.nmgdz.bigdata.entity; import lombok.Data; import javax.persistence.*; import java.io.Serializable; @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, int age){ } public User() { } }

UserDao文件配置

配置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文件配置

配置User文件 package com.nmgdz.bigdata.entity; import lombok.Data; import javax.persistence.*; import java.io.Serializable; @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, int age){ } public User() { } }

RedisServiceImpl文件配置

配置RedisServiceimpl文件 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; @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);} }

UserServiceImpl文件配置

配置UserServiceImpl文件 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; import java.util.Optional; @Service public class UserServiceImpl implements UserService { @Autowired private UserDao userDao; @Override public List<User> queryByAge(int age) { return userDao.findByAge(age); } @Override public User save(User user) { return userDao.save(user); } @Override public void deletUserById(int id) { userDao.deleteById(id); } @Override public Optional<User> queryById(int id){ return userDao.findById(id); } }

RedisService文件配置

配置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); Long increment(String key, long delta); }

UserService文件配置

配置UserService文件 package com.nmgdz.bigdata.service; import com.nmgdz.bigdata.entity.User; import java.util.List; import java.util.Optional; public interface UserService { List<User> queryByAge(int age); User save(User user); void deletUserById(int id); Optional<User> queryById(int id); }

BigdataApplication文件配置

配置BigdataApplication文件 package com.nmgdz.bigdata; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class BigdataApplication { public static void main(String[] args) { SpringApplication.run(BigdataApplication.class, args); } }

application文件配置

配置application文件 spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/mp?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 database: 0 port: 6379 password: 123456 jedis: pool: max-active: 8 max-wait: -1ms max-idle: 8 min-idle: 0 timeout: 3000ms

pom文件配置

配置pom文件 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.nmgdz</groupId> <artifactId>bigdata</artifactId> <version>0.0.1-SNAPSHOT</version> <name>bigdata</name> <description>init project</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </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>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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

结果图

遇到的问题

编码问题 解决方法:对工程进行编码 Redis密码问题,application代码格式问题 解决方法: jpa: hibernate: ddl-auto: update show-sql: true redis: host: localhost database: 0 port: 6379 password: 123456 #这一块是填写Redis密码的 jedis: pool: max-active: 8 max-wait: -1ms max-idle: 8 min-idle: 0 timeout: 3000ms
最新回复(0)