您的位置:首页 > 其它

【菜鸟学框架】——hibernate入门Demo

2017-07-28 12:21 260 查看

目的

    记录小编从零入门hibernate框架的过程,算是对自己学习的总结,同时也希望能帮助正在经历这个过程同行。文中如有错误之处,欢迎各位拍砖,不胜感激!

    关于hibernate的入门介绍详情见【Hibernate】——初识hibernate。本文主要讲解

hibernate环境搭建过程

1、导包

    在hibernate的解压文件中有lib—required里面是hibernate开发的必须jar,同时需要数据库驱动包,以及日志记录包,具体包见下图,在实际的开发过程根据自己的需要选择。

 


2、创建数据库和表

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;


3、 创建实体类

     注意记得生成getter和setter,否则不能称为属性,篇幅限制就忽略getter和setter部分了。
Customer.java文件
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;

4、创建实体映射文件

Customer.hbm.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 package="cn.hibernate.domain">
<class name="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>
<!-- 以下内容为必填内容 -->
<!--数据库驱动-->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 数据库url -->
<property name="hibernate.connection.url">jdbc:mysql:///hibernate_day01</property>
<!-- 数据库连接用户名 -->
<property name="hibernate.connection.username">root</property>
<!-- 数据库连接密码 -->
<property name="hibernate.connection.password">123</property>

<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

<!--  将hibernate生成的sql语句打印到控制台 -->
<property name="hibernate.show_sql">true</property>
<!-- hibernate生成的sql格式化 -->
<property name="hibernate.format_sql">true</property>

<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 引入orm元数据
路径:填写src下的路径 -->
<mapping resource="cn/hibernate/domain/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>

6、编写测试代码

@Test
//保存客户
public void fun1(){
//代表应用程序到SQL数据库的配置信息
Configuration conf = new Configuration().configure();
//创建sessionFactory
SessionFactory sessionFactory = conf.buildSessionFactory();
//创建session
Session session = sessionFactory.openSession();
//打开事务
Transaction tx = session.beginTransaction();
//--------------------------------------------
Customer c = new Customer();
c.setCust_name("百度公司");
session.save(c);

//-----------------------------------------
tx.commit();
session.close();
sessionFactory.close();

}

【总结】

    这是只是一个hibernate实践的Demo,目的做点小东西找点存在感。后续会从hibernate的映射文件以及配置文件,还有hibernate三种操作数据库的方式进行深入。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: