本文共 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 (按照下图操作)
继续跟着下图操作,这个文件,你可以下载我文章开头给的地址也可以自行去下载。
在src下创建一个名为 hibernate.cfg.xml 的xml(直接复制我的,名字都不可以改的)。
然后类似上面的操作 复制 后面的代码 http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd
第四步写配置文件
<?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>
<?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(); } }
如果你还想弄明白这个标签的含义 ,或者想知道怎么增删改查,可以去看这篇博客