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);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值