标签:etc his app 日志 table ack factory main gen
hibernate介绍:简单来讲就是提供了对数据库访问的支持,方便数据库的访问
1导入jar包
创建数据库和表
创建对应的表实体类
package com.heima.daomain; public class Customer { // `cust_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT ‘客户编号(主键)‘, // `cust_name` varchar(32) NOT NULL COMMENT ‘客户名称(公司名称)‘, // `cust_user_id` bigint(32) DEFAULT NULL COMMENT ‘负责人id‘, // `cust_create_id` bigint(32) DEFAULT NULL COMMENT ‘创建人id‘, // `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 ‘移动电话‘, private int cust_id; private String cust_name; private int cust_user_id; private int cust_create_id; private String cust_source; private String cust_industry; private String cust_level; private String cust_linkman; private String cust_phone; private String cust_mobile; public int getCust_id() { return cust_id; } public void setCust_id(int cust_id) { this.cust_id = cust_id; } public String getCust_name() { return cust_name; } public void setCust_name(String cust_name) { this.cust_name = cust_name; } public int getCust_user_id() { return cust_user_id; } public void setCust_user_id(int cust_user_id) { this.cust_user_id = cust_user_id; } public int getCust_create_id() { return cust_create_id; } public void setCust_create_id(int cust_create_id) { this.cust_create_id = cust_create_id; } public String getCust_source() { return cust_source; } public void setCust_source(String cust_source) { this.cust_source = cust_source; } public String getCust_industry() { return cust_industry; } public void setCust_industry(String cust_industry) { this.cust_industry = cust_industry; } public String getCust_level() { return cust_level; } public void setCust_level(String cust_level) { this.cust_level = cust_level; } public String getCust_linkman() { return cust_linkman; } public void setCust_linkman(String cust_linkman) { this.cust_linkman = cust_linkman; } public String getCust_phone() { return cust_phone; } public void setCust_phone(String cust_phone) { this.cust_phone = cust_phone; } public String getCust_mobile() { return cust_mobile; } public void setCust_mobile(String cust_mobile) { this.cust_mobile = cust_mobile; } }
对应的映射关系xml
<?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="/Test/src/com/heima/daomain/Customer.java" 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_user_id" column="cust_user_id"></property> <property name="cust_create_id" column="cust_create_id"></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>
映关系和实体类放在同一目录下
创建hibernate 的配置文件 放在 src目录下
<?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> <!-- #hibernate.dialect org.hibernate.dialect.MySQLDialect #hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect #hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect #hibernate.connection.driver_class com.mysql.jdbc.Driver #hibernate.connection.url jdbc:mysql:///test #hibernate.connection.username gavin #hibernate.connection.password --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql:///test</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name=""></property> <property name=""></property> <property name=""></property> <!-- #hibernate.show_sql true 打印日志 hibernate.format_sql true 打印日志时格式化日志 看起方便 --> <property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">true</property> <!-- ## auto schema export #hibernate.hbm2ddl.auto create-drop #hibernate.hbm2ddl.auto create #hibernate.hbm2ddl.auto update #hibernate.hbm2ddl.auto validate --> <property name="hibernate.hbm2ddl.auto">update</property> <!--导入 映射 --> <mapping resource="com/heima/daomain/Customer.hbm.xml" /> </session-factory> </hibernate-configuration>
向eclipse中导入dtd文件 绑定xml配置
dtd文件在hibernate框架中有
访问数据库测试
import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import com.heima.daomain.Customer; public class HelloHibernate { public void insert () { Configuration configuration = new Configuration().configure(); SessionFactory sessionFactory = configuration.buildSessionFactory(); Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); Customer customer = new Customer(); customer.setCust_name("baidu"); session.save(customer); tx.commit(); session.close(); sessionFactory.close(); } }
标签:etc his app 日志 table ack factory main gen
原文地址:https://www.cnblogs.com/xing001/p/10966417.html