1 总共需要以下的jar
mysql-connector-java-3.0.7-stable-bin.jar
Hibernate 3.2、
Hibernate Annotations 3.2(hibernate-annotations.jar、ejb3-persistence.jar)
2 mysql中新建数据库demo 表
CREATE TABLE user
( id INT(11) NOT NULL auto_increment PRIMARY KEY,
name VARCHAR(100) NOT NULL default '',
age INT);
3 myeclipse中加入hibernate
hibernate.cfg.xml:
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 显示实际操作数据库时的SQL --> <property name="show_sql">true</property> <!-- SQL方言,这边设定的是MySQL --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- JDBC驱动程序 --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <!-- JDBC URL --> <property name="connection.url">jdbc:mysql://localhost/demo</property> <!-- 数据库使用者 --> <property name="connection.username">root</property> <!-- 数据库密码 --> <property name="connection.password">123456</property> <!-- 以下设置对象与数据库表格映像类别 --> <mapping class="onlyfun.caterpillar.User"/> </session-factory> </hibernate-configuration>
4.对于这个表格user,您有一个User类别与之对应,表格中的每一个字段将对应
至User实例上的Field成员。
package onlyfun.caterpillar; import javax.persistence.*; @Entity @Table(name="user") // 非必要,在表格名称与类别名称不同时使用 public class User { @Id @GeneratedValue(strategy=GenerationType.AUTO) private Integer id; @Column(name="name") // 非必要,在字段名称与属性名称不同时使用 private String name; @Column(name="age") private Integer age; // 非必要,在字段名称与属性名称不同时使用 // 必须要有一个预设的建构方法 // 以使得Hibernate可以使用Constructor.newInstance()建立对象 public User() { } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
5,测试
package onlyfun.caterpillar; import org.hibernate.SessionFactory; import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.cfg.Configuration; public class HibernateAnnotationDemo { public static void main(String[] args) { // 需要AnnotationConfiguration读取Annotation讯息 Configuration config = new AnnotationConfiguration().configure(); // 根据 config 建立 SessionFactory // SessionFactory 将用于建立 Session SessionFactory sessionFactory = config.buildSessionFactory(); // 将持久化的物件 User user = new User(); user.setName("caterpillar"); user.setAge(new Integer(30)); // 开启Session,相当于开启JDBC的Connection Session session = sessionFactory.openSession(); // Transaction表示一组会话操作 Transaction tx= session.beginTransaction(); // 将对象映像至数据库表格中储存 session.save(user); tx.commit(); session.close(); sessionFactory.close(); System.out.println("新增资料OK!请先用MySQL观看结果!"); } }
6.执行结果中显示了Hibernate所实际使用的SQL,由于这个程序还没有查询功能
,所以要进入MySQL中看看新增的数据,如下:
mysql> select * from user; +----+-----------------+------+ | id | name | age | +----+-----------------+------+ | 1 | caterpillar | 30 | +----+-----------------+------+ 1 row in set (0.03 sec)
恭喜你,成功了~~~~
原贴:http://blog.youkuaiyun.com/axu20/archive/2007/06/08/1643985.aspx