博客
关于我
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 slave 停了_slave 停止。求解决方法
查看>>
MySQL SQL 优化指南:主键、ORDER BY、GROUP BY 和 UPDATE 优化详解
查看>>
mysql sum 没返回,如果没有找到任何值,我如何在MySQL中获得SUM函数以返回'0'?
查看>>
mysql Timestamp时间隔了8小时
查看>>
Mysql tinyint(1)与tinyint(4)的区别
查看>>
mysql union orderby 无效
查看>>
mysql where中如何判断不为空
查看>>
mysql workbench6.3.5_MySQL Workbench
查看>>
MySQL Workbench安装教程以及菜单汉化
查看>>
MySQL Xtrabackup 安装、备份、恢复
查看>>
mysql [Err] 1436 - Thread stack overrun: 129464 bytes used of a 286720 byte stack, and 160000 bytes
查看>>
MySQL _ MySQL常用操作
查看>>
MySQL – 导出数据成csv
查看>>
MySQL —— 在CentOS9下安装MySQL
查看>>
mysql 不区分大小写
查看>>
mysql 两列互转
查看>>
MySQL 中开启二进制日志(Binlog)
查看>>
MySQL 中文问题
查看>>
MySQL 中日志的面试题总结
查看>>
mysql 中的all,5分钟了解MySQL5.7中union all用法的黑科技
查看>>