使用Java编程和springboot和mybatis框架,连接mysql数据库,给出具体程序,包含Controller层、Service层、Service实现类、Dao层以及Mapper.xml,获取中奖奖品的周排名 月排名 年排名 降序排列 取前5条数据
时间: 2024-03-29 20:35:40 浏览: 111
好的,以下是基于SpringBoot和MyBatis的Java程序,包含Controller层、Service层、Service实现类、Dao层以及Mapper.xml文件,可以连接MySQL数据库并获取中奖奖品的周排名、月排名、年排名,降序排列并取前5条数据:
1.首先创建一个实体类PrizeRecord
```java
public class PrizeRecord {
private int id;
private String name;
private String prize;
private Date time;
//省略getter和setter方法
}
```
2.在resources目录下创建application.yml文件,并配置数据库信息
```yaml
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
username: root
password: 123456
```
3.创建Mapper.xml文件,命名为PrizeRecordMapper.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.mapper.PrizeRecordMapper">
<select id="getWeeklyRank" parameterType="java.util.Date" resultType="com.example.entity.PrizeRecord">
SELECT name,prize,time,count(*) as count FROM prize_record WHERE YEARWEEK(date_format(time,'%Y-%m-%d'),1) = YEARWEEK(date_format(#{date},'%Y-%m-%d'),1) GROUP BY name ORDER BY count DESC LIMIT 5
</select>
<select id="getMonthlyRank" parameterType="java.util.Date" resultType="com.example.entity.PrizeRecord">
SELECT name,prize,time,count(*) as count FROM prize_record WHERE date_format(time,'%Y-%m') = date_format(#{date},'%Y-%m') GROUP BY name ORDER BY count DESC LIMIT 5
</select>
<select id="getYearlyRank" parameterType="java.util.Date" resultType="com.example.entity.PrizeRecord">
SELECT name,prize,time,count(*) as count FROM prize_record WHERE YEAR(time)=YEAR(#{date}) GROUP BY name ORDER BY count DESC LIMIT 5
</select>
</mapper>
```
4.创建Dao层,命名为PrizeRecordDao,并在其中定义查询方法
```java
@Mapper
public interface PrizeRecordDao {
List<PrizeRecord> getWeeklyRank(Date date);
List<PrizeRecord> getMonthlyRank(Date date);
List<PrizeRecord> getYearlyRank(Date date);
}
```
5.创建Service层,命名为PrizeRecordService,并在其中定义调用Dao层的方法
```java
public interface PrizeRecordService {
List<PrizeRecord> getWeeklyRank(Date date);
List<PrizeRecord> getMonthlyRank(Date date);
List<PrizeRecord> getYearlyRank(Date date);
}
```
6.创建Service实现类,命名为PrizeRecordServiceImpl,并在其中实现Service层的方法,调用Dao层的方法
```java
@Service
public class PrizeRecordServiceImpl implements PrizeRecordService {
@Autowired
private PrizeRecordDao prizeRecordDao;
@Override
public List<PrizeRecord> getWeeklyRank(Date date) {
return prizeRecordDao.getWeeklyRank(date);
}
@Override
public List<PrizeRecord> getMonthlyRank(Date date) {
return prizeRecordDao.getMonthlyRank(date);
}
@Override
public List<PrizeRecord> getYearlyRank(Date date) {
return prizeRecordDao.getYearlyRank(date);
}
}
```
7.创建Controller层,命名为PrizeRecordController,并在其中定义请求接口和调用Service层的方法
```java
@RestController
@RequestMapping("/prizeRecord")
public class PrizeRecordController {
@Autowired
private PrizeRecordService prizeRecordService;
@GetMapping("/weeklyRank")
public List<PrizeRecord> getWeeklyRank(@RequestParam("date") Date date) {
return prizeRecordService.getWeeklyRank(date);
}
@GetMapping("/monthlyRank")
public List<PrizeRecord> getMonthlyRank(@RequestParam("date") Date date) {
return prizeRecordService.getMonthlyRank(date);
}
@GetMapping("/yearlyRank")
public List<PrizeRecord> getYearlyRank(@RequestParam("date") Date date) {
return prizeRecordService.getYearlyRank(date);
}
}
```
8.启动SpringBoot程序,并访问以下接口即可获取中奖奖品的周排名、月排名、年排名,降序排列并取前5条数据:
```java
http://localhost:8080/prizeRecord/weeklyRank?date=2022-03-06
http://localhost:8080/prizeRecord/monthlyRank?date=2022-03-01
http://localhost:8080/prizeRecord/yearlyRank?date=2022-01-01
```
以上就是使用Java编程和SpringBoot和MyBatis框架连接MySQL数据库并获取中奖奖品的周排名、月排名、年排名,降序排列并取前5条数据的具体程序。
阅读全文