2016.10.16-hibernate demo

hibernate操作数据库

首先创建对应的类

package demo;
import java.util.Date;

public class Student {
    private int sid;
    private String sname;
    private float score;
    private Date birthday;
    public Student() {
        super();
    }
    public int getSid() {
        return sid;
    }
    public void setSid(int sid) {
        this.sid = sid;
    }
    public String getSname() {
        return sname;
    }
    public void setSname(String sname) {
        this.sname = sname;
    }
    public float getScore() {
        return score;
    }
    public void setScore(float score) {
        this.score = score;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    public Student(int sid, String sname, float score, Date birthday) {
        super();
        this.sid = sid;
        this.sname = sname;
        this.score = score;
        this.birthday = birthday;
    }   
}

创建Student.hbm.xml





    
         //用id标签包裹的属性代表表的主键
            
            //assigned手动复制ID,native表示自增长
        
        
            
        
        
            
        
        
            
        
    

创建hibernate.cfg.xml




 
 
  com.mysql.jdbc.Driver
  123456
  jdbc:mysql:///test
  root
  
  org.hibernate.dialect.MySQLDialect
  
  true
  
  true
  
  update
  
  thread
  
  
  
 

写个test类测试

package demo;

import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class Test {
    public static void main(String[] args) {
        SessionFactory sf = new Configuration().configure().buildSessionFactory();
        Session session = sf.getCurrentSession();
        Transaction transaction = session.beginTransaction();
        Student stu = new Student(4, "test1", 100.0f, new Date());
        session.save(stu);
        transaction.commit();
    }
}

你可能感兴趣的:(2016.10.16-hibernate demo)