准备:
eclipseHibernate Tools for Eclipse PluginHibername Tools是由JBoss推出的一个Eclipse综合开发工具插件,该插件可以简化ORM框架Hibernate,以及JBoss Seam,EJB3等的开发工作 插件安装教程 http://blog.csdn.net/csc_cockroach/article/details/52923083 步骤
创建Hibernate的配置文件hibernate.cfg.xml创建持久化类(实体类)创建对象-关系映射文件,生成对应实体类的映射文件并添加到配置文档中(4)调用Hibemate API进行测试通过Hibernate API编写访问数据库的代码 为了方便,先在eclipse中导入所需的jar包Windows>preferences>java>build path>User Libraries 点击New… 输入 hibernate点击OK 导入Hibernate必须的jar包 hibernate-release-4.2.4.Final\lib\required 点击New…输入mysql-jdbc点击OK 导入mysql的jdbc驱动 mysql-connector-java-5.0.8.jar 点击New…输入Junit4点击OK 导入Junit4的jar包 Junit-4.0.jar 点击OK在eclipse中新建一个Java project工程,命名为hibernate,右键项目>build path>Add Libraary>User Library>选中刚才导入的库,点击finish
在src目录下新建Hibernate Configuration File(cfg.xml) 直接点击Next,然后Finish 在工程目录中打开刚刚创建的配置文档,在< session-factory>中添加以下标签
<property name="connection.username">root</property> <property name="connection.password">123</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://127.0.0.1:13306/hibernate?useUnicode=true&characterEncoding=utf-8</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="show_sql">true</property> <property name="format_sql">true</property> <property name="hbm2ddl.auto">create</property>在src目录下新建一个Student类,包名为com.entity
import java.util.Date; public class Student { //1公有类 //2无参构造方法 //3属性私有 //4get和set方法 private int sid;//学号 private String sname;//姓名 private String gender;//性别 private Date birthday;//生日 private String address;//地址 public Student() { } public Student(int sid, String sname, String gender, Date birthday, String address) { this.sid = sid; this.sname = sname; this.gender = gender; this.birthday = birthday; this.address = address; } 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 String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "Student [sid=" + sid + ", sname=" + sname + ", gender=" + gender + ", birthday=" + birthday + ", address=" + address + "]"; } }右键src新建
选中Student类,点击Finish 在src目录下就生成了一个名为Student.hbm.xml的对象关系-映射表,把实体类映射成数据库的一张表,属性映射成为表中的一个字段 文档生成之后,需要把它加入Hibernate文档当中,点击hibernate.cfg.xml,在< session-factory>添加
<mapping resource="Student.hbm.xml"/>右键项目,New>Source Folder,命名为text,在text中新建一个StudentText.java
import java.sql.Connection; import java.sql.SQLException; import java.util.Date; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.jdbc.Work; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; import org.junit.After; import org.junit.Before; import org.junit.Test; import com.imooc.entity.Student; public class StudentText { private SessionFactory sessionFactory; private Session session; private Transaction transaction; @Before public void init(){ //创建配置对象 Configuration config=new Configuration().configure(); //创建服务注册对象 ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry(); //创建会话工厂对象 sessionFactory=config.buildSessionFactory(serviceRegistry); //会话对象 session=sessionFactory.openSession(); //开启事务 transaction=session.beginTransaction(); } @After public void destroy(){ transaction.commit();//提交事务 session.close();//关闭会话 sessionFactory.close();//关闭会话工厂 } @Test public void textSaveStudents(){ session.save(s);//保存对象进数据库 session.flush(); } }运行textSaveStudents()方法 命令行输出