feat:记录访问次数
This commit is contained in:
@@ -2,7 +2,9 @@ package top.naccl.dwz;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.scheduling.annotation.EnableAsync;
|
||||
|
||||
@EnableAsync
|
||||
@SpringBootApplication
|
||||
public class DwzApplication {
|
||||
|
||||
|
||||
@@ -48,6 +48,7 @@ public class IndexController {
|
||||
public String redirect(@PathVariable String shortURL) {
|
||||
String longURL = urlService.getLongUrlByShortUrl(shortURL);
|
||||
if (longURL != null) {
|
||||
urlService.updateUrlViews(shortURL);
|
||||
//查询到对应的原始链接,302重定向
|
||||
return "redirect:" + longURL;
|
||||
}
|
||||
|
||||
@@ -20,11 +20,13 @@ public class UrlMap {
|
||||
private Long id;
|
||||
private String surl;//短链接
|
||||
private String lurl;//长链接
|
||||
private Integer views;//访问次数
|
||||
private Date createTime;//创建时间
|
||||
|
||||
public UrlMap(String surl, String lurl) {
|
||||
this.surl = surl;
|
||||
this.lurl = lurl;
|
||||
this.views = 0;
|
||||
this.createTime = new Date();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,4 +15,6 @@ public interface UrlMapper {
|
||||
String getLongUrlByShortUrl(String surl);
|
||||
|
||||
int saveUrlMap(UrlMap urlMap);
|
||||
|
||||
int updateUrlViews(String surl);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
package top.naccl.dwz.service;
|
||||
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
|
||||
public interface UrlService {
|
||||
String getLongUrlByShortUrl(String shortURL);
|
||||
|
||||
String saveUrlMap(String shortURL, String longURL, String originalURL);
|
||||
|
||||
@Async
|
||||
void updateUrlViews(String shortURL);
|
||||
}
|
||||
|
||||
@@ -81,4 +81,9 @@ public class UrlServiceImpl implements UrlService {
|
||||
}
|
||||
return shortURL;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateUrlViews(String shortURL) {
|
||||
urlMapper.updateUrlViews(shortURL);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,10 +2,19 @@
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
|
||||
<mapper namespace="top.naccl.dwz.mapper.UrlMapper">
|
||||
<select id="getLongUrlByShortUrl" resultType="java.lang.String">
|
||||
select lurl from url_map where surl=#{surl}
|
||||
select lurl
|
||||
from url_map
|
||||
where surl = #{surl}
|
||||
</select>
|
||||
|
||||
<insert id="saveUrlMap" parameterType="top.naccl.dwz.entity.UrlMap" useGeneratedKeys="true" keyProperty="id">
|
||||
insert into url_map (surl, lurl, create_time) values (#{surl}, #{lurl}, #{createTime})
|
||||
insert into url_map (surl, lurl, views, create_time)
|
||||
values (#{surl}, #{lurl}, #{views}, #{createTime})
|
||||
</insert>
|
||||
|
||||
<update id="updateUrlViews">
|
||||
update url_map
|
||||
set views=views + 1
|
||||
where surl = #{surl}
|
||||
</update>
|
||||
</mapper>
|
||||
Reference in New Issue
Block a user