hibernate框架搭建步骤

hibernate框架搭建步骤

1.创建 web project

2.导入所需要的包
    antlr-2.7.7.jar
    dom4j-1.6.1.jar
    geronimo-jta_1.1_spec-1.1.1.jar
    hibernate-commons-annotations-5.0.1.Final.jar
    hibernate-core-5.0.7.Final.jar
    hibernate-jpa-2.1-api-1.0.0.Final.jar
    jandex-2.0.0.Final.jar
    javassist-3.18.1-GA.jar
    jboss-logging-3.3.0.Final.jar
    //连接数据库驱动包

    mysql-connector-java-5.1.7-bin.jar

3.创建数据库 准备表和实体

CREATE DATABASE customer_management 
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; 
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;
	
	public Long getCust_id() {
		return cust_id;
	}
	public void setCust_id(Long 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 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;
	}

4.1导入约束

在hibernate-core-5.0.7.Final.jar中的org.hibernate 下的 hibernate-configuration-3.0.dtd

<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
配置约束hibernate-mapping-3.0.dtd
windows--preferences--输入cata-->XML Catalog--add--location:目录中的hibernate-mapping-3.0.dtd文件--
key type:选url--key:http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd
配置hibernate-configuration-3.0.dtd
windows--preferences--输入cata-->XML Catalog--add--location:目录中的hibernate-configuration-3.0.dtd文件--
key type:选url--key:http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd
4.2写映射文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="cn.itheima.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>


5.写主映射文件 (连接数据库)


<?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:///hibernate_day01</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</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="cn/itheima/domain/Customer.hbm.xml"/>
    </session-factory>
</hibernate-configuration>


6.测试代码(junit)

   @Test
    //保存客户
    public void fun1(){
        //配置实例允许应用程序指定属性和映射文档
         Configuration conf = new Configuration().configure();
        //创建Session
        SessionFactory sessionFactory = conf.buildSessionFactory();
        //获得Session
        Session session = sessionFactory.openSession();
        //开启事务
        Transaction tx = session.beginTransaction();
        //----------------------------------------------
        Customer c = new Customer();
        c.setCust_name("google公司");
        
        session.save(c);//执行保存
        //----------------------------------------------
        //提交事务
         tx.commit();
        //关闭资源
        session.close();
        sessionFactory.close();
    }

猜你喜欢

转载自blog.csdn.net/weixin_40471291/article/details/80796322