博客
关于我
Hibernate 基本(入门)配置,HelloWorld
阅读量:198 次
发布时间:2019-02-28

本文共 3299 字,大约阅读时间需要 10 分钟。

写一个hibernate基本的配置,方便自己查找,也希望帮到更多的初学者。
因为需要用到一些配置文件,我已经上传了,你也可以自行下载。(本来是打算免费分享,不过好像最少要一个c币)
我上传地址

第一步导包(我这里连接的是mysql数据库)

    1、数据库连接包,mysql-connector-java-5.1.7-bin.jar
    2、hibernate基本包先要去下载(地址: )
    下载解压之后进入hibernate-release-5.0.7.Final\lib\required  把里面的包都拷贝到lib下

第二步准备工作

1、在这之前你要准备好一个表(因为hibernate主要就是操作数据库的嘛)
2、创建一个domain包,在包下创建一个表对应的实体(我的实体名称是Customer.java),创建一个xml后缀是.hbm.xml我的是(Customer.hbm.xml)

第三步准备文件

按照下面的图片操作
一直往下翻,直到找到

复制这句代码(你直接复制我后面这句就好了)http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd
点击 window -> preferences   然后在输入框输入cata  (按照下图操作)

继续跟着下图操作,这个文件,你可以下载我文章开头给的地址也可以自行去下载。
继续操作

继续下面的操作

把复制的代码,拷贝到刚刚那个xml中去

然后你出现下面这种提示,就表示你已经成功了
在src下创建一个名为 hibernate.cfg.xml 的xml(直接复制我的,名字都不可以改的)。

然后类似上面的操作 复制 后面的代码  http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd
弄好之后同样的,如下图,就表示成功了

第四步写配置文件

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="hibernate.domain">
 <class name="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>

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>  <!-- 三个///  表示链接本机  数据了url -->  <property name="hibernate.connection.url">jdbc:mysql:///hibernate</property>  <!-- 数据库链接名 -->  <property name="hibernate.connection.username">root</property>  <!-- 数据库链接密码 -->  <property name="hibernate.connection.password">123</property>
  <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  <property name="hibernate.show_sql">true</property>
  <property name="hibernate.format_sql">true</property>     <property name="hibernate.hbm2ddl.auto">update</property>   <mapping resource="domain/Customer.hbm.xml"/> </session-factory></hibernate-configuration>

第五步 测试

package test;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.junit.Test;import domain.Customer;//测试Hibernate框架public class Demo {		@Test	//保存	public void fun1(){		Configuration conf = new Configuration().configure();		SessionFactory sessionFactory = conf.buildSessionFactory();		Session session = sessionFactory.openSession();		Transaction tx =  session.beginTransaction();				Customer c = new Customer();		c.setCust_name("baidu");				session.save(c);						//----------------------------------				tx.commit();		session.close();		sessionFactory.close();			}		}

亲测没问题
如果你还想弄明白这个标签的含义 ,或者想知道怎么增删改查,可以去看这篇博客

你可能感兴趣的文章
MySql中怎样使用case-when实现判断查询结果返回
查看>>
Mysql中怎样使用update更新某列的数据减去指定值
查看>>
Mysql中怎样设置指定ip远程访问连接
查看>>
mysql中数据表的基本操作很难嘛,由这个实验来带你从头走一遍
查看>>
Mysql中文乱码问题完美解决方案
查看>>
mysql中的 +号 和 CONCAT(str1,str2,...)
查看>>
Mysql中的 IFNULL 函数的详解
查看>>
mysql中的collate关键字是什么意思?
查看>>
MySql中的concat()相关函数
查看>>
mysql中的concat函数,concat_ws函数,concat_group函数之间的区别
查看>>
MySQL中的count函数
查看>>
MySQL中的DB、DBMS、SQL
查看>>
MySQL中的DECIMAL类型:MYSQL_TYPE_DECIMAL与MYSQL_TYPE_NEWDECIMAL详解
查看>>
MySQL中的GROUP_CONCAT()函数详解与实战应用
查看>>
MySQL中的IO问题分析与优化
查看>>
MySQL中的ON DUPLICATE KEY UPDATE详解与应用
查看>>
mysql中的rbs,SharePoint RBS:即使启用了RBS,内容数据库也在不断增长
查看>>
mysql中的undo log、redo log 、binlog大致概要
查看>>
Mysql中的using
查看>>
MySQL中的关键字深入比较:UNION vs UNION ALL
查看>>