Hibernate4.2.21.Final创建入门的HelloHibernet工程
1.在hibernate官网下载hibernate-release-4.2.21.Final.zip并解压
2.新建一个java project工程(HelloHibernet)(myeclipes)
3.导入hibernate包和hibernate配置文件(hibernate.cfg.xml)
导入包只需导入hibernate-release-4.2.2.Final\lib\required目录下的所有包和MySQL的包mysql-connector-java-5.1.22-bin.jar hibernate.cfg.xml导入在classpath路径下,即src下
步骤:1. 导入包只需导入hibernate-release-4.2.2.Final\lib\required目录下的所有包
打开windows->preference-> 窗口-》首选项-》构建路径-》用户库-》新建Hibernate(名字随意) -》添加JAR->将hibernate-release-4.2.2.Final\lib\required目录下的所有包.jar全部导入-》确定回到主界面
2.右点击项目HelloHibernet,构建路径-》添加库-》选择用户库-》选择Hibernate-》确定ok
3.添加包mysql-connector-java-5.1.22-bin.jar 构建路径-》添加外部归档文档-》选择mysql-connector-java-5.1.22-bin.jar->ok
4.将那个hibernate.cfg.xml导入
5.建立数据库:
创建Student表:
CREATE TABLE `Student` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
`age` varchar(45) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf
点击返回项目主界面:
4.返回后hibernate在新建一个java实体类
package com.bjsxt.hibernate;
public class Student {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
建立另一个测试类Test.java:
package com.bjsxt.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
public class Test {
public static void main(String[] args) {
Student s = new Student();
s.setId(1);
s.setName("zhangsan");
s.setAge(8);
SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
session.save(s);
session.getTransaction().commit();
}
}
运行测试:
...(以上省略)
INFO: HHH000232: Schema update completeHibernate:
insert into Student (name, age, id) values (?, ?, ?)
即可以了
参考Myeclipse下配置struts2和hibernate
http://blog.csdn.net/lin062854/article/details/9128047
等