Skip to content

Commit

Permalink
规范了部分方法命令,逻辑本身未做修改
Browse files Browse the repository at this point in the history
  • Loading branch information
jiashuaizhang committed Dec 12, 2021
1 parent ef26fe5 commit 3b1320c
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 78 deletions.
5 changes: 3 additions & 2 deletions yyets-history-ui/src/view/Main.vue
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,12 @@
this.resourceLinksTableData = this.resourceLinks[linkGroup].items;
},
copyLink(link) {
let hiddenLink = document.getElementById('hiddenLink');
let textareaId = 'hiddenLink';
let hiddenLink = document.getElementById(textareaId);
if (!hiddenLink) {
hiddenLink = document.createElement('textarea');
hiddenLink.style.display = 'none';
hiddenLink.id = 'hiddenLink';
hiddenLink.id = textareaId;
}
hiddenLink.value = link;
hiddenLink.select();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@

import java.util.LinkedHashSet;

import static com.zhangjiashuai.yyetshistory.repository.impl.ResourceMapper.DEFAULT_PAGE_SIZE;
import static com.zhangjiashuai.yyetshistory.repository.ResourceRepository.DEFAULT_PAGE_SIZE;

@Data
public class YyetsHistoryProperties {
/**
* 保留的链接类型
*/
private LinkedHashSet<String> linkWayFilter;

/**
* 默认每页大小
*/
private int defaultPageSize;

public int getDefaultPageSize() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.zhangjiashuai.yyetshistory.controller;

import cn.hutool.db.Page;
import cn.hutool.db.PageResult;
import com.zhangjiashuai.yyetshistory.entity.Resource;
import com.zhangjiashuai.yyetshistory.service.ResourceService;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,38 @@
package com.zhangjiashuai.yyetshistory.repository;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.db.PageResult;
import com.zhangjiashuai.yyetshistory.entity.ResourceDO;

import java.util.List;

public interface ResourceRepository {

int DEFAULT_PAGE_SIZE = 20;

ResourceDO findById(long id);

List<ResourceDO> findByName(String name);
List<ResourceDO> findByNameLike(String name);

ResourceDO findOneByName(String name);

List<ResourceDO> findAll();

PageResult<ResourceDO> selectPage(String name, int pageNo);

PageResult<ResourceDO> selectPage(String name, int pageNo, int pageSize);

long countByNameLike(String name);
int countByNameLike(String name);

default PageResult<ResourceDO> selectPage(String name, int pageNo) {
return selectPage(name, pageNo, DEFAULT_PAGE_SIZE);
}

default PageResult<ResourceDO> selectPage(String name, int pageNo, int pageSize) {
int count = countByNameLike(name);
PageResult<ResourceDO> pageResult = new PageResult<>(pageNo, pageSize, count);
if(count == 0) {
return pageResult;
}
List<ResourceDO> list = findByNameLike(name);
List<ResourceDO> pageList = ListUtil.page(pageNo - 1, pageSize, list);
pageResult.addAll(pageList);
return pageResult;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.zhangjiashuai.yyetshistory.repository.impl;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.db.PageResult;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
Expand All @@ -14,49 +13,30 @@
@Mapper
public interface ResourceMapper extends BaseMapper<ResourceDO>, ResourceRepository {

int DEFAULT_PAGE_SIZE = 20;

default PageResult<ResourceDO> selectPage(String name, int pageNo) {
return selectPage(name, pageNo, DEFAULT_PAGE_SIZE);
}

default PageResult<ResourceDO> selectPage(String name, int pageNo, int pageSize) {
long count = countByNameLike(name);
PageResult<ResourceDO> pageResult = new PageResult<>(pageNo, pageSize, (int)count);
if(count == 0) {
return pageResult;
}
List<ResourceDO> list = selectByNameLike(name);
List<ResourceDO> pageList = ListUtil.page(pageNo - 1, pageSize, list);
pageResult.addAll(pageList);
return pageResult;
static LambdaQueryWrapper<ResourceDO> queryWrapperByNameLike(String name) {
return Wrappers.<ResourceDO>lambdaQuery()
.like(ResourceDO::getName, name).orderByAsc(ResourceDO::getName);
}

default long countByNameLike(String name) {
return selectCount(queryWrapperByNameLike(name));
default int countByNameLike(String name) {
return selectCount(queryWrapperByNameLike(name)).intValue();
}

default List<ResourceDO> selectByNameLike(String name) {
default List<ResourceDO> findByNameLike(String name) {
return selectList(queryWrapperByNameLike(name));
}

default LambdaQueryWrapper<ResourceDO> queryWrapperByNameLike(String name) {
return Wrappers.<ResourceDO>lambdaQuery()
.like(ResourceDO::getName, name).orderByAsc(ResourceDO::getName);
}

default ResourceDO findById(long id) {
return selectById(id);
}

default List<ResourceDO> findByName(String name) {
return selectList(Wrappers.<ResourceDO>lambdaQuery()
.eq(ResourceDO::getName, name));
}

default ResourceDO findOneByName(String name) {
return selectOne(Wrappers.<ResourceDO>lambdaQuery()
List<ResourceDO> list = selectList(Wrappers.<ResourceDO>lambdaQuery()
.eq(ResourceDO::getName, name));
if(CollectionUtil.isEmpty(list)) {
return null;
}
return list.get(0);
}

default List<ResourceDO> findAll() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.zhangjiashuai.yyetshistory.repository.impl;

import cn.hutool.db.PageResult;
import cn.hutool.core.collection.CollectionUtil;
import com.zhangjiashuai.yyetshistory.entity.ResourceDO;
import com.zhangjiashuai.yyetshistory.repository.ResourceRepository;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
Expand All @@ -10,9 +10,12 @@

public class ResourceRepositoryJdbcImpl implements ResourceRepository {

private static final String BASE_SQL = "SELECT ID, URL, NAME, EXPIRE, EXPIRE_CST, DATA FROM resource";
private static final String BASE_SQL = "SELECT ID, URL, NAME, EXPIRE, EXPIRE_CST, DATA FROM RESOURCE";
private static final String BASE_COUNT_SQL = "SELECT COUNT(*) FROM RESOURCE";
private static final String FIND_BY_ID_SQL = BASE_SQL + " WHERE ID = ?";
private static final String FIND_BY_NAME_SQL = BASE_SQL + " WHERE NAME like ?";
private static final String BY_NAME_CONDITION = " WHERE NAME like ?";
private static final String FIND_BY_NAME_SQL = BASE_SQL + BY_NAME_CONDITION;
private static final String COUNT_BY_NAME_SQL = BASE_COUNT_SQL + BY_NAME_CONDITION;
private static final String FIND_ONE_BY_NAME_SQL = BASE_SQL + " WHERE NAME = ?";

private JdbcTemplate jdbcTemplate;
Expand All @@ -27,15 +30,15 @@ public ResourceDO findById(long id) {
}

@Override
public List<ResourceDO> findByName(String name) {
public List<ResourceDO> findByNameLike(String name) {
String nameFilter = "%" + name + "%";
return jdbcTemplate.query(FIND_BY_NAME_SQL, new BeanPropertyRowMapper<>(ResourceDO.class), nameFilter);
}

@Override
public ResourceDO findOneByName(String name) {
List<ResourceDO> list = jdbcTemplate.query(FIND_ONE_BY_NAME_SQL, new BeanPropertyRowMapper<>(ResourceDO.class), name);
if(list.isEmpty()) {
if(CollectionUtil.isEmpty(list)) {
return null;
}
return list.get(0);
Expand All @@ -47,17 +50,7 @@ public List<ResourceDO> findAll() {
}

@Override
public PageResult<ResourceDO> selectPage(String name, int pageNo) {
throw new UnsupportedOperationException();
}

@Override
public PageResult<ResourceDO> selectPage(String name, int pageNo, int pageSize) {
throw new UnsupportedOperationException();
}

@Override
public long countByNameLike(String name) {
throw new UnsupportedOperationException();
public int countByNameLike(String name) {
return jdbcTemplate.queryForObject(COUNT_BY_NAME_SQL, Integer.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public interface ResourceService {

ResourceDO findById(long id);

List<ResourceDO> findByName(String name);
List<ResourceDO> findByNameLike(String name);

ResourceDO findOneByName(String name);

Expand All @@ -23,7 +23,7 @@ public interface ResourceService {

Resource getResourceById(long id);

List<Resource> getResourcesByName(String name);
List<Resource> getResourceByNameLike(String name);

List<Resource> getAllResources();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public ResourceDO findById(long id) {
}

@Override
public List<ResourceDO> findByName(String name) {
return resourceRepository.findByName(name);
public List<ResourceDO> findByNameLike(String name) {
return resourceRepository.findByNameLike(name);
}

@Override
Expand Down Expand Up @@ -153,8 +153,8 @@ public Resource getResourceById(long id) {
}

@Override
public List<Resource> getResourcesByName(String name) {
List<ResourceDO> resourceDOList = findByName(name);
public List<Resource> getResourceByNameLike(String name) {
List<ResourceDO> resourceDOList = findByNameLike(name);
if (CollectionUtil.isEmpty(resourceDOList)) {
return Collections.emptyList();
}
Expand Down
8 changes: 0 additions & 8 deletions yyets-history/src/main/resources/application-dev.yml

This file was deleted.

5 changes: 2 additions & 3 deletions yyets-history/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
spring:
profiles:
active: dev
datasource:
driver-class-name: org.sqlite.JDBC
url: jdbc:sqlite::resource:static/yyets.db
Expand All @@ -13,10 +11,11 @@ logging:
level:
root: info
file:
name: /var/logs/yyets-history.log
name: logs/yyets-history.log
mybatis-plus:
configuration:
map-underscore-to-camel-case: true
# log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
server:
port: 9000
yyets-history:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class YyetsHistoryApplicationTests {

@Test
public void jdbcTemplateTest() {
List<ResourceDO> resources = resourceRepository.findByName("绝命律师");
List<ResourceDO> resources = resourceRepository.findByNameLike("绝命律师");
Assert.notEmpty(resources);
}

Expand Down

0 comments on commit 3b1320c

Please sign in to comment.