gpt4 book ai didi

java - Spring Controller /服务/存储库中的泛型

转载 作者:行者123 更新时间:2023-12-02 07:40:24 24 4
gpt4 key购买 nike

我想精简我的代码,只使用一个函数进行过滤。过滤应适用于 3 个不同的类:com.example.model.Category、com.example.model.Tag、java.util.Calendar。

这是我的 Controller

@Controller
@RequestMapping( "/blog" )
public class BlogController
{
@Autowired
private ArticleService articleService;

@Autowired
private CategoryService categoryService;

@Autowired
private TagService tagService;

private static final int PER_PAGE = 5;

private static final int ARCHIVE_MONTHS = 12;

@ModelAttribute
public void addGlobalObjects( Map<String, Object> map )
{
map.put( "section", "blog" );

SortedMap<Category, Integer> categories = new TreeMap<Category, Integer>();
for ( Category category : categoryService.list() )
{
categories.put( category, articleService.size( category, Category.class ) );
}

Calendar cal = Calendar.getInstance();
cal.set( Calendar.DAY_OF_MONTH, 1 );

cal.add( Calendar.MONTH, ARCHIVE_MONTHS * -1 );

SortedMap<Date, Integer> archive = new TreeMap<Date, Integer>();
for ( int i = 0; i < ARCHIVE_MONTHS; ++i )
{
cal.add( Calendar.MONTH, 1 );
archive.put( cal.getTime(), articleService.size( cal, Calendar.class ) );
}

SortedMap<Tag, Integer> tags = new TreeMap<Tag, Integer>();
for ( Tag tag : tagService.list() )
{
tags.put( tag, articleService.size( tag, Tag.class ) );
}

map.put( "categories", categories );
map.put( "archive", archive );
map.put( "tags", tags );

map.put( "categoriesSize", categoryService.size() );
map.put( "tagsSize", tagService.size() );

map.put( "date", new Date() );
}

@RequestMapping( "/index.html" )
public String index( HttpServletRequest request, Map<String, Object> map )
{
return list( request, map, null, null );
}

@RequestMapping( "/archive/{date}.html" )
public String archive( @PathVariable( "date" ) @DateTimeFormat( iso = ISO.DATE, style = "yyyy/MM" ) Date date, HttpServletRequest request, Map<String, Object> map )
{
Calendar cal = Calendar.getInstance();
cal.setTime( date );

return list( request, map, cal, Calendar.class );
}

private <T> String list( HttpServletRequest request, Map<String, Object> map, Object filterObject, Class<T> clazz )
{
int page = ServletRequestUtils.getIntParameter( request, "page", 1 );
map.put( "articles", articleService.list( page * PER_PAGE - PER_PAGE, PER_PAGE, filterObject, clazz ) );
map.put( "numPages", Math.ceil( articleService.size() / PER_PAGE ) );
map.put( "currentPage", page );
return "articles";
}
}

在我的 ArticleDAO 中,现在我需要实现方法 list/size:

@Repository
public class ArticleDAO extends BaseDAO<Article>
{
public <T> List<Article> list( int offset, int limit, Object filterObject, Class<T> clazz )
{
Criteria c = doFilter( filterObject, clazz );

if ( limit > 0 )
{
c.setFirstResult( offset );
c.setMaxResults( limit );
}

return c.list();
}

public <T> Integer size( Object filterObject, Class<T> clazz )
{
Number ret = ( Number ) doFilter( filterObject, clazz ).setProjection( Projections.rowCount() ).uniqueResult();
return ret.intValue();
}

private <T> Criteria doFilter( Object filterObject, Class<T> clazz )
{
Criteria criteria = sessionFactory.getCurrentSession().createCriteria( Article.class );

if ( filterObject != null && clazz != null )
{
T filter = clazz.cast( filterObject );

if ( filter instanceof Calendar )
{
// The method set(int, int) is undefined for the type T
filter.set( Calendar.DAY_OF_MONTH, 1 );
filter.set( Calendar.HOUR_OF_DAY, 0 );
filter.set( Calendar.MINUTE, 0 );
filter.set( Calendar.SECOND, 0 );
// The method getTime() is undefined for the type T
Date d1 = filter.getTime();

// The method getActualMaximum(int) is undefined for the type T
filter.set( Calendar.DAY_OF_MONTH, filter.getActualMaximum( Calendar.DAY_OF_MONTH ) );
filter.set( Calendar.HOUR_OF_DAY, 23 );
filter.set( Calendar.MINUTE, 59 );
filter.set( Calendar.SECOND, 59 );
Date d2 = filter.getTime();

criteria.add( Restrictions.between( "creationDate", d1, d2 ) );
}

if ( filter instanceof Category )
{
// The method getId() is undefined for the type T
criteria.createCriteria( "categories" ).add( Restrictions.eq( "id", filter.getId() ) );
}

if ( filter instanceof Tag )
{
// The method getId() is undefined for the type T
criteria.createCriteria( "tags" ).add( Restrictions.eq( "id", filter.getId() ) );
}
}

return criteria;
}
}

ArticleDAO.doFilter 应该是什么样子?我想我不明白泛型的东西。

最佳答案

我认为泛型在这里不会给你带来任何好处。这甚至不会编译 b/c T 未知,并且您正在尝试调用特定方法(例如 getId())。

有几种方法可以在不使用泛型的情况下实现此目的。最简单的方法是

 if ( filterObject instanceof Calendar ) {
Calendar filterCal = (Calendar) filterObject;

filterCal.set (Calendar.DAY_OF_MONTH, 1); // ... and so on

如果您这样做,则可以从方法中删除 clazz 参数。您不需要 clazz.cast() 调用。

简单说一下泛型,我通常发现自己在做同样的事情时使用泛型,无论类型如何。例如,看起来您正在为 BaseDAO 成功使用泛型。无论哪种类型,BaseDAO 都会保存、更新、删除等。在上面的例子中,您试图根据类型做不同的事情。它并不真正适合泛型。事实上,为每种类型做不同的事情通常意味着您可以通过多态性很好地完成它。

关于java - Spring Controller /服务/存储库中的泛型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11701556/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com