feat:记录访问次数

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