sun论坛上当下来的java的dao例子

Here's the model object, with Hibernate annotations so you can generate the mappings an schema:

package 
 model;
 
import 
 javax.persistence.*;
import 
 java.io.Serializable;
import 
 java.util.Date;
import 
 java.util.Calendar;
import 
 java.util.GregorianCalendar;
 
/** * Created by IntelliJ IDEA. * User: Michael * Date: Sep 13, 2006 * Time: 5:14:17 PM * To change this template use File | Settings | File Templates. */

@SuppressWarnings({
"JavaDoc"
}
)
@Entity
@Table(name = "students"
)
public 
 class 
 Student implements 
 Serializable
{

    private 
 Long id;
    private 
 String name;
    private 
 Date birthday;
 
    private 
 Student()
    {

        this 
(null, ""
, new 
 Date());
    }

 
    public 
 Student(String name, Date birthday)
    {

        this 
(null, name, birthday);
    }

 
    public 
 Student(Long id, String name, Date birthday)
    {

        this.id = id;
        this.name = name;
        this.birthday = new 
 Date(birthday.getTime());
    }

 
 
    @Id
    @Column(name = "student_id"
)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "students_student_id_seq"
)
    @SequenceGenerator(name = "students_student_id_seq"
, sequenceName = "students_student_id_seq"
)
    public 
 Long getId()
    {

        return 
 id;
    }

 
    private 
 void 
 setId(Long id)
    {

        this.id = id;
    }

 
    @Column(name = "full_name"
, unique = false, nullable = false, length = 32)
    public 
 String getName()
    {

        return 
 name;
    }

 
    private 
 void 
 setName(String name)
    {

        this.name = name;
    }

 
    @Column(name = "birthday"
, unique = false, nullable = false 
)
    public 
 Date getBirthday()
    {

        return 
 birthday;
    }

 
    private 
 void 
 setBirthday(Date birthday)
    {

        this.birthday = birthday;
    }

 
    public 
 int 
 getAge()
    {

        int 
 age = 0;
 
        Calendar calendar = GregorianCalendar.getInstance();
 
        Date today = new 
 Date();
        calendar.setTime(today);
        int 
 currentYear = calendar.get(Calendar.YEAR);
 
        calendar.setTime(birthday);
        int 
 birthYear   = calendar.get(Calendar.YEAR);
 
        age = currentYear - birthYear;
        
        return 
 age;
    }

 
    public 
 boolean 
 equals(Object o)
    {

        if 
 (this 
 == o)
        {

            return 
 true 
;
        }

        if 
 (o == null 
 || getClass() != o.getClass())
        {

            return 
 false 
;
        }

 
        Student student = (Student) o;
 
        if 
 (id != null 
 ? !id.equals(student.id) : student.id != null 
)
        {

            return 
 false 
;
        }

 
        return 
 true 
;
    }

 
    public 
 int 
 hashCode()
    {

        return 
 (id != null 
 ? id.hashCode() : 0);
    }

 
 
    public 
 String toString()
    {

        return 
 "Student{"
 +
                "id="
 + id +
                ", name='"
 + name + '\''
 +
                ", birthday="
 + birthday +
                '}'
;
    }

}




Here's the DAO interface:

package 
 persistence;
 
public 
 interface 
 StudentDao
{

    public 
 Student find(Long id);
    public 
 List<Student> findAll();
    public 
 List<Student> find(String name);
 
    public 
 void 
 saveOrUpdate(Student s);
    public 
 void 
 delete(Long id);
    public 
 void 
 delete(Student s);
}


你可能感兴趣的:(java,DAO,Hibernate,sun,idea)