[学习笔记] 在Eclipse中使用Hibernate,并创建第一个Demo工程,数据库为Oracle XE (2)

[学习笔记] 在Eclipse中使用Hibernate,并创建第一个Demo工程,数据库为Oracle XE

最后自动形成 如下的文件内容:[本例使用oracle数据库]

Oracle 11g xe 在windows安装请看如下链接:

Oracle 11g xe 在windows安装

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>oracle.jdbc.driver.OracleDriver</property> <property>123456</property> <property>jdbc:oracle:thin:@localhost:1521:xe</property> <property>test</property> <property>test</property> <property>org.hibernate.dialect.Oracle10gDialect</property> </session-factory> </hibernate-configuration>

再增加几个属性

[学习笔记] 在Eclipse中使用Hibernate,并创建第一个Demo工程,数据库为Oracle XE

配置文件更新后的内容如下: 注意要去掉name属性 更改 为

<?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>oracle.jdbc.driver.OracleDriver</property> <property>123456</property> <property>jdbc:oracle:thin:@localhost:xe:orcl</property> <property>test</property> <property>test</property> <property>org.hibernate.dialect.Oracle10gDialect</property> <property>true</property> <!-- 第一次加载hibernate时根据model类会自动建立起表的结构(前提是先建立好数据库),以后加载hibernate时根据 model类自动更新表结构,即使表结构改变了但表中的行仍然存在不会删除以前的行。要注意的是当部署到服务器后,表结构是不会被马上建立起来的,是要等 应用第一次运行起来后才会。 --> <property>update</property> <property>true</property> <property>pojo</property> </session-factory> </hibernate-configuration> 继续完善工程Hibernate_demo_001

新建一个包:mytest001.demo

[学习笔记] 在Eclipse中使用Hibernate,并创建第一个Demo工程,数据库为Oracle XE

[学习笔记] 在Eclipse中使用Hibernate,并创建第一个Demo工程,数据库为Oracle XE

在包mytest001.demo之下新建一个PO类: Emp

package mytest001.demo; public class Emp { // 员工的标识属性 private Integer id; // 姓名 private String name; // 年龄 private Integer age; // 工资 (分) private Integer salary; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Integer getSalary() { return salary; } public void setSalary(Integer salary) { this.salary = salary; } @Override public String toString() { return "Emp [id=" + id + ",, age=" + age + ", salary=" + salary + "]"; } }

此刻此PO Emp.java 尚不具备持久化能力。下面为其添加注解。

[学习笔记] 在Eclipse中使用Hibernate,并创建第一个Demo工程,数据库为Oracle XE

@Entity 注解声明该类是一个Hibernate持久化类
@Table 指定该类映射的表,对应的数据库表名是T_EMP
@Id 指定该类的标识属性,映射到数据库的主键列
@GeneratedValue(strategy=GenerationType.SEQUENCE) 指定了主键生成策略,由于本文使用Oracle Database, 因此指定了使用 SEQUENCE

在hibernate.cfg.xml中增加持久化映射类名

增加一个新类:EmpManager,用于管理员工。

package mytest001.demo; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.service.Service; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; public class EmpManager { public static void main(String[] args) throws Exception { //实例化配置 Configuration configuration = new Configuration() //不带参数则默认加载hibernate.cfg.xml .configure(); ServiceRegistry serviceRegistry = new ServiceRegistryBuilder() .applySettings(configuration.getProperties()).build(); SessionFactory sFactory = configuration.buildSessionFactory(serviceRegistry); //创建session Session session = sFactory.openSession(); //开始事务 Transaction tx = session.beginTransaction(); //创建员工对象 Emp emp = new Emp(); // 设置员工信息 emp.setAge(28); emp.setName("scott"); emp.setSalary(10000); session.save(emp); // 提交事务 tx.commit(); session.close(); sFactory.close(); } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zggxyj.html