通过myclipse建立一个简单的Hibernate项目(PS:在单元测试中实现数据的向表的插入)
通过myclipse建立一个简单的Hibernate项目(PS:在单元测试中实现数据的向表的插入)
先看下项目的组成(PS:其实在构建Hibernate的时候自需要我们写3个东西①实体类②实体类.hbm.xml③hibernate.cfg.xml):
步骤(7步)
Ⅰ、新建一个web 项目;
Ⅱ、右击该项目选择myeclipse->Project Facets->install hibernate facet(PS:如果对jdk没有必要的要求就点击finish),酱紫就将hibernate所需要的基础包都配置好了;
Ⅲ、将工具类HibernateUtil拷入你的项目
package com.zp.hibernate.domain; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; public class HibernateUtil { // 专门负责获取session并将其关闭 private static SessionFactory sessionFactory; private static ThreadLocal session = new ThreadLocal(); private HibernateUtil() { } static { Configuration configuration = new Configuration().configure(); ServiceRegistryBuilder regbuilder = new ServiceRegistryBuilder() .applySettings(configuration.getProperties()); //比较早包用的是ServiceRegistryBuilder // StandardServiceRegistryBuilder 在以后就没用了 ServiceRegistry serviceRegistry = regbuilder.buildServiceRegistry(); sessionFactory = configuration.buildSessionFactory(serviceRegistry); } public static Session getThreadLocalSession() { Session s = (Session) session.get(); if (s == null) { s = sessionFactory.openSession(); session.set(s); } return s; } public static void closeSession() { Session s = (Session) session.get(); if (s != null) { s.close(); session.set(null); } } }View Code
Ⅳ、完成实体类的书写,及就是填写属性以及相应的get、set方法(student.java)
package com.zp.hibernate.domain; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; @Entity // 实体类,这里映射到数据库中就是表 @Table public class student { private Integer id; private String name; @Id // 设置变量id 为主键 @GeneratedValue // 设置id为自动增长 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; } }View Code
Ⅴ、新建XXX.hbm.xml (student.hbm.xml)(PS:XXX一定要和实体类的类名一致)
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- package: 指定当前映射文件对应的持久类的完整包名 --> <hibernate-mapping package="com.zp.hibernate.domain"> <!-- class中的name为对应实体类类名 --> <class name="student"> <!-- id中的name为对应实体类中的变量 --> <id name="id"> <!-- column中的name为生成数据库中列名 --> <column name="student_id"></column> <!-- generator的class指定native表明Hibernate根据底层数据库自行判断采用identity、hilo、sequence其中一种作为主键生成方式 --> <generator class="native" /> </id> <!-- property用于得到非主键的变量的名字,也要跟实体类一致 --> <property name="name"> <column name="student_name"></column> </property> </class> </hibernate-mapping>
Ⅵ、修改hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <!-- Generated by MyEclipse Hibernate Tools. --> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <!-- 所用数据库 --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 数据库url --> <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property> <!-- 自动建表 --> <property name="hibernate.hbm2ddl.auto">create</property> <!-- hibernate.connection.username : 连接数据库的用户名 --> <property name="hibernate.connection.username">root</property> <!-- hibernate.connection.password : 连接数据库的密码 --> <property name="hibernate.connection.password">1234</property> <!-- 将实体类的映射文件加载进来 --> <mapping resource="com/zp/hibernate/domain/student.hbm.xml"/> </session-factory> </hibernate-configuration>
Ⅶ、测试类的书写(test.java)
package com.zp.hibernate.test; import static org.junit.Assert.*; import org.hibernate.Session; import org.hibernate.Transaction; import org.junit.Test; import com.zp.hibernate.domain.HibernateUtil; import com.zp.hibernate.domain.student; public class test { @Test public void test() { student stu = new student(); stu.setId(1); stu.setName("getcharzp"); Session session = HibernateUtil.getThreadLocalSession(); Transaction tx = session.beginTransaction(); session.save(stu); tx.commit(); HibernateUtil.closeSession(); } }
总结:
hibernate的基础配置可以直接通过myeclipse的install..导入,其它的就只需要我们修改或添加相应的 ①、实体类 ②、XXX.hbm.xml ③、hibernate.cfg.xml
相关推荐
蛰脚踝的天蝎 2020-11-10
Cocolada 2020-11-12
TuxedoLinux 2020-09-11
snowphy 2020-08-19
83540690 2020-08-16
lustdevil 2020-08-03
83417807 2020-07-19
张文倩数据库学生 2020-07-19
bobljm 2020-07-07
83417807 2020-06-28
86427019 2020-06-28
86427019 2020-06-25
zhengzf0 2020-06-21
tobecrazy 2020-06-16
宿命java 2020-06-15
83417807 2020-06-15
84284855 2020-06-11