哪个大侠在项目中做过博客的归档功能,就是对博文以时间进行归档的,如 
博文归档:  
  2010 ^
    9月(2)
    8月(2)
  2009 ^
    10(1)
这个功能具体实现的过程,如表结构设计时注意什么,和实现的思路?

解决方案 »

  1.   

    假设你表为t1,t1中用于记录归档时间得字段为tim,
    select year(tim) as '年份',month(tim) as '月份',count(*) as '总数'
      from t1 group by year(tim),month(tim)
    则执行效果为:lz,你看一下,我觉得大致可以实现你要的效果了
      

  2.   


    package YM;import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;public class Get {
    @SuppressWarnings("unchecked")
    public static void main(String args[]){
    /*注意取得文章时按时间排序*/

    /*构造文章list start */
    List<Note> list = new ArrayList<Note>();

    Note n1 = new Note();
    n1.setContent("1");
    n1.setDate("2009-01-12 12:30");
    n1.setId("1");
    n1.setName("1");
    list.add(n1);

    Note n2 = new Note();
    n2.setContent("2");
    n2.setDate("2009-01-22 09:30");
    n2.setId("2");
    n2.setName("2");
    list.add(n2);

    Note n3 = new Note();
    n3.setContent("3");
    n3.setDate("2010-02-20 09:00");
    n3.setId("3");
    n3.setName("3");
    list.add(n3);

    Note n4 = new Note();
    n4.setContent("4");
    n4.setDate("2010-05-13 15:30");
    n4.setId("4");
    n4.setName("4");
    list.add(n4);

    Note n5 = new Note();
    n5.setContent("5");
    n5.setDate("2009-08-25 09:30");
    n5.setId("5");
    n5.setName("5");
    list.add(n5);
    /*构造文章list end */

    Map<String,Map<String,Month>> yearMap = new HashMap<String,Map<String,Month>>();

    for (Iterator iter = list.iterator(); iter.hasNext();) {  
    Note note = (Note)iter.next();
    String date = note.getDate();
    String year = date.split(" ")[0].split("-")[0];
    String month = date.split(" ")[0].split("-")[1];
    Map<String,Month> l = new HashMap<String,Month>();
    Month m = new Month();
    m.setId(year+"-"+month);
    List<Note> li = new ArrayList<Note>();
    li.add(note);
    m.setNoteList(li);
    l.put(year+"-"+month, m);
    if (!yearMap.containsKey(year)) {
    yearMap.put(year, l);
    } else {
    Map<String,Month> mm = yearMap.get(year);
    Month m1 = mm.get(year+"-"+month);
    if (m1 != null) {
    mm.remove(year+"-"+month);
    List<Note> nl = m1.getNoteList();
    nl.add(note);
    m1.setNoteList(nl);
    mm.put(year+"-"+month, m1);
    } else {
    List<Note> nl = new ArrayList<Note>();
    nl.add(note);
    m.setNoteList(nl);
    mm.put(year+"-"+month, m);
    }
    }
    }
    for (Iterator k = yearMap.keySet().iterator(); k.hasNext();) {
    String key = (String) k.next();
    System.out.println(key);
    for (Iterator i = yearMap.values().iterator(); i.hasNext();) { 
    Map<String,Month> mmm = (Map<String, Month>) i.next();
    for (Iterator ii = mmm.values().iterator(); ii.hasNext();) { 
    Month m = (Month) ii.next();
    if (key.equals(m.getId().split("-")[0])) {
    List<Note> nl = m.getNoteList();
    System.out.println(m.getId().split("-")[1]+"("+nl.size()+")");
    }
    }
    }
    }
    }
    }
    package YM;import java.util.List;
    import java.util.ArrayList;
    public class Month {
    private String id;      //月编号
    private List<Note>  noteList = new ArrayList<Note>(); //文章列表
    public String getId() {
    return id;
    }
    public void setId(String id) {
    this.id = id;
    }
    public List<Note> getNoteList() {
    return noteList;
    }
    public void setNoteList(List<Note> noteList) {
    this.noteList = noteList;
    }}
    [code=Java]
    package YM;public class Note {
    private String id;      //文章编号
    private String name;    //文章名称
    private String content; //文章内容
    private String date;    //文章时间
    public String getContent() {
    return content;
    }
    public void setContent(String content) {
    this.content = content;
    }
    public String getDate() {
    return date;
    }
    public void setDate(String date) {
    this.date = date;
    }
    public String getId() {
    return id;
    }
    public void setId(String id) {
    this.id = id;
    }
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    }
    [/code]