导入包
1. antlr-2.7.7.jar
2. dom4j-1.6.1.jar
3. geronimo-jta_1.1_spec-1.1.1.jar
4. hibernate-commons-annotations-5.0.1.Final.jar
5. hibernate-core-5.0.7.Final.jar
6. hibernate-jpa-2.1-api-1.0.0.Final.jar
7. jandex-2.0.0.Final.jar
8. javassist-3.18.1-GA.jar
9. jboss-logging-3.3.0.Final.jar
10. mysql-connector-java-5.1.7-bin.jar
1-9为hibernate必须的包,10为mysql的驱动包
创建数据库表
CREATE TABLE cst_customer( cust_id BIGINT(32) NOT NULL AUTO_INCREMENT COMMENT '客户编号(主键)', cust_name VARCHAR(32) NOT NULL COMMENT '客户名称(公司名称)', cust_source VARCHAR(32) DEFAULT NULL COMMENT '客户信息来源', cust_industry VARCHAR(32) DEFAULT NULL COMMENT '客户所属行业', cust_level VARCHAR(32) DEFAULT NULL COMMENT '客户级别', cust_linkman VARCHAR(64) DEFAULT NULL COMMENT '联系人', cust_phone VARCHAR(64) DEFAULT NULL COMMENT '固定电话', cust_mobile VARCHAR(16) DEFAULT NULL COMMENT '移动电话', PRIMARY KEY(cust_id) )ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
ORM元数据(对象与表的映射配置文件)
导入约束
eclipse---Window---Preferences---XML Catalog,点击Add
1. 点击File System选择hibernate-mapping-3.0.dtd文件。
2. 将Key types修改微URI,将Libraries---hibernate-core-5.0.7.Final.jar---org.hibernate--hibernate-mapping-3.0.dtd文件打开,
拷贝其中的http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd到此处。
3. hibernate-mapping-3.0.dtd约束用于类和表映射xml文件中。
创建实体
public class Customer { private Long cust_id; private String cust_name; private String cust_source; private String cust_industry; private String cust_level; private String cust_linkman; private String cust_phone; private String cust_mobile; }
ORM元数据
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.heima.domain.Customer" table="cst_customer"> <id name="cust_id" column="cust_id"> <generator class="native"></generator> </id> <property name="cust_name" column="cust_name"></property> <property name="cust_source" column="cust_source"></property> <property name="cust_industry" column="cust_industry"></property> <property name="cust_level" column="cust_level"></property> <property name="cust_linkman" column="cust_linkman"></property> <property name="cust_phone" column="cust_phone"></property> <property name="cust_mobile" column="cust_mobile"></property> </class> </hibernate-mapping>
创建主配置文件
导入约束
eclipse---Window---Preferences---XML Catalog,点击Add
1. 点击File System选择hibernate-configuration-3.0.dtd文件。
2. 将Key types修改微URI,将Libraries---hibernate-core-5.0.7.Final.jar---org.hibernate--hibernate-configuration-3.0.dtd文件打开,
拷贝其中的http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd到此处。
3. hibernate-configuration-3.0.dtd约束用于主配置文件中。
书写配置文件
1. 配置文件的位置在src目录下
2.文件的名称固定为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"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql:///hibernate</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">123456</property> <property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">true</property> <property name="hibernate.hbm2ddl.auto">update</property> <mapping resource="com/heima/domain/Customer.hbn.xml"/> </session-factory> </hibernate-configuration>
测试代码
public class Demo1 { @Test public void fun(){ Configuration conf = new Configuration().configure(); SessionFactory sessionFactory = conf.buildSessionFactory(); Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); Customer customer = new Customer(); customer.setCust_name("百度公司"); session.save(customer); tx.commit(); session.close(); sessionFactory.close(); } }