比较详细的hibernate与Oracle连接的小例子吧~ 直接上代码。
1、首先需要的jar包列出来~
附上jar包和项目 有需要的小伙伴自行下载哦~
jar:https://pan.baidu.com/s/1UFpNF0AwC6_XRPDNvWQXRg
项目代码:https://pan.baidu.com/s/1JNKrqFi1nInzciLRDmtCBQ
2、目录结构也看一下吧~
3、先编写一个用户的实体类,给定属性和get,set方法。
package cn.com.entity;
public class User {
private int uids;
private String username;
private String password;
private String adress;
public int getUids() {
return uids;
}
public void setUids(int uids) {
this.uids = uids;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getAdress() {
return adress;
}
public void setAdress(String adress) {
this.adress = adress;
}
}
然后编写实体类的映射文件,一般命名规则是类名.hbm.xml。例如我这个叫User.hbm.xml。到这里感觉应该都能看懂吧~ 在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>
<class name="cn.com.entity.User" table="h_user">
<!-- 配置实体类id和表的字段对应
hibernate要求实体类有一个属性唯一值
hibernate要求表有字段作为唯一值
-->
<!-- name属性:实体类里面id属性名称 column属性:生成的表字段名称 -->
<id name="uids" column="uids">
<!-- 设置数据库表id增长策略
native:生成表主键值就是自动增长
-->
<generator class="native"></generator>
</id>
<!-- 配置其他属性和表的字段对应 -->
<property name="username" column="username"></property>
<property name="password" column="password"></property>
<property name="adress" column="adress"></property>
</class>
</hibernate-mapping>
4、核心配置文件的编写,可以先写核心配置文件再写对应的实体类映射文件(数据库的配置不想写在这里的就写个jdbc.properties文件引进来哦~ 这都不是关键^_^)
<?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">
oracle.jdbc.driver.OracleDriver
</property>
<!-- 提供url,我的数据库安在本机就是localhost,如果不在本机就写上相应的ip地址 -->
<property name="hibernate.connection.url">
jdbc:oracle:thin:@localhost:1521:orcl
</property>
<!-- 登录数据库时用的用户名和密码 -->
<property name="hibernate.connection.username">用户名</property>
<property name="hibernate.connection.password">密码</property>
<!-- 第二部分配置hibernate信息(可选的) -->
<!-- 输出底层的sql语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 对底层SQL语句进行格式化 -->
<property name="hibernate.format_sql">true</property>
<!-- hibernate配置之后自动创建表
update:如果已经有表了就更新,如果没有表就创建,不加这个就不会自动创建表
-->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 配置数据库方言,11g也是这么配,不影响使用 -->
<property name="hibernate.dialect">
org.hibernate.dialect.Oracle10gDialect
</property>
<!-- 第三部分把实体类映射文件放到核心配置文件中去 必须做的 -->
<mapping resource="cn/com/entity/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
5、到这里我们的hibernate就算是已经配置完成了,是不是很简单呢~,下一步就是编写测试类啦~
这个是我编写的一个工具类,用来获得sessionFactory,可以写也可以不写,看个人喜好。
package cn.com.utils;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtils {
static Configuration cfg = null;
static SessionFactory sessionFactory = null;
//静态代码块实现 只用一个sessionFactory
static {
//加载核心配置文件
cfg = new Configuration();
cfg.configure();
sessionFactory = cfg.buildSessionFactory();
}
//提供方法返回sessionfactory
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void main(String[] args) {
}
}
然后是编写测试的HibernateTest类
package cn.com.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.junit.Test;
import cn.com.entity.User;
import cn.com.utils.HibernateUtils;
public class HibernateTest {
@Test
public void testAdd() {
//加载配置文件 与上面工具类的作用相同
// Configuration cfg = new Configuration();
// cfg.configure();
// SessionFactory sessionFactory = cfg.buildSessionFactory();
//利用工具类获得sessionFactory对象
SessionFactory sessionFactory = HibernateUtils.getSessionFactory();
//得到Session对象
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
User user = new User();
user.setUsername("举个栗子");
user.setPassword("123456");
user.setAdress("localhost");
session.save(user);
tx.commit();
session.close();
sessionFactory.close();
}
}
好啦~ 到这里就完美收工啦! 右击runAs测试类看看你的数据库里是不是多了张表吧~ 有什么问题欢迎私信哦~