使用Java编程和springboot和mybatis框架,连接mysql数据库,给出具体程序,包含Controller层、Service层、Service实现类、Dao层以及Mapper.xml文件,统计本周第一天 本月第一天 本年第一天起到今天为止的中奖趋势接口 并对接口使用redis进行缓存处理 过期时间10分钟
时间: 2024-03-26 14:39:07 浏览: 117
首先,需要添加相关依赖:
```xml
<!-- Spring Boot Starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- Spring Boot Starter Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot Starter Data JDBC -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- MySQL Connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<!-- Redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
```
接下来,创建相关类:
1. Trend.java
```java
public class Trend {
private String date;
private int count;
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
```
2. TrendMapper.xml
```xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.lottery.mapper.TrendMapper">
<resultMap id="TrendMap" type="com.example.lottery.entity.Trend">
<result column="date" property="date" jdbcType="VARCHAR" />
<result column="count" property="count" jdbcType="INTEGER" />
</resultMap>
<select id="selectTrend" resultMap="TrendMap">
SELECT DATE_FORMAT(lottery_time, '%Y-%m-%d') as date, COUNT(*) as count
FROM lottery
WHERE lottery_time BETWEEN #{weekBegin} AND NOW()
GROUP BY DATE_FORMAT(lottery_time, '%Y-%m-%d')
ORDER BY DATE_FORMAT(lottery_time, '%Y-%m-%d') DESC
</select>
</mapper>
```
3. TrendMapper.java
```java
@Mapper
public interface TrendMapper {
List<Trend> selectTrend(@Param("weekBegin") Date weekBegin);
}
```
4. TrendService.java
```java
public interface TrendService {
List<Trend> getTrend();
}
```
5. TrendServiceImpl.java
```java
@Service
public class TrendServiceImpl implements TrendService {
@Autowired
private TrendMapper trendMapper;
@Autowired
private RedisTemplate<String, List<Trend>> redisTemplate;
@Override
public List<Trend> getTrend() {
String key = "trend";
List<Trend> trendList = redisTemplate.opsForValue().get(key);
if (trendList != null) {
return trendList;
}
Date weekBegin = getWeekBegin();
trendList = trendMapper.selectTrend(weekBegin);
redisTemplate.opsForValue().set(key, trendList, Duration.ofMinutes(10));
return trendList;
}
private Date getWeekBegin() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
}
```
6. TrendController.java
```java
@RestController
@RequestMapping("/trend")
public class TrendController {
@Autowired
private TrendService trendService;
@GetMapping
public List<Trend> getTrend() {
return trendService.getTrend();
}
}
```
7. RedisConfig.java
```java
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, List<Trend>> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, List<Trend>> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(factory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(List.class));
return redisTemplate;
}
}
```
最后,修改application.properties或application.yml配置文件,添加数据库和Redis相关配置。
注意,以上代码仅供参考,具体实现可能需要根据实际情况进行调整。
阅读全文