创建数据库
1、用你最喜爱的database软件,创建一个hibernate_test的数据库。
2、在此数据库下,新建一个table名为CUSTOMER
CREATE TABLE CUSTOMER ( CID INTEGER NOT NULL PRIMARY KEY, USERNAME VARCHAR(12) NOT NULL, PASSWORD VARCHAR(12) ); |
编写Java文件
public class Customer { private int id; private String username; private String password;
public int getId() { return id; }
public String getPassword() { return password; }
public String getUsername() { return username; }
public void setId(int id) { this.id = id; }
public void setPassword(String password) { this.password = password; }
public void setUsername(String username) { this.username = username; }
} |
将此类存为c:/workspace/My1stHibernate/src/Customer.java文件。
编写Test类
import net.sf.hibernate.*; import net.sf.hibernate.cfg.*;
public class Test {
public static void main(String[] args) { try { SessionFactory sf = new Configuration().configure().buildSessionFactory(); Session session = sf.openSession(); Transaction tx = session.beginTransaction();
for (int i = 0; i < 200; i++) { Customer customer = new Customer(); customer.setUsername("customer" + i); customer.setPassword("customer"); session.save(customer); }
tx.commit(); session.close(); } catch (HibernateException e) { e.printStackTrace(); } } } |
将此类存为c:/workspace/My1stHibernate/src/Test.java文件。 
说明:本教程来源互联网或网友上传或出版商,仅为学习研究或媒体推广,wanshiok.com不保证资料的完整性。
2/2 首页 上一页 1 2 |