您的位置:首页 > 其它

初识hibernate

2017-01-31 10:42 344 查看
1.引入对应的核心jar包



2.src目录下引入hibernate.cfg.xml核心配置文件

<!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节点代表一个数据库 -->
<session-factory>

<!-- 1. 数据库连接配置 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hib01</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<!--
数据库方法配置, hibernate在运行的时候,会根据不同的方言生成符合当前数据库语法的sql
-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>

<!-- 2. 其他相关配置 -->
<!-- 2.1 显示hibernate在运行时候执行的sql语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 2.2 格式化sql -->
<property name="hibernate.format_sql">true</property>
<!-- 2.3 自动建表 -->
<property name="hibernate.hbm2ddl.auto">update</property>

<!-- 3. 加载所有映射 -->
<mapping resource="cn/itcast/a_hello/Employee.hbm.xml"/>

</session-factory>
</hibernate-configuration>

实体Employee.java
package cn.itcast.a_hello;

import java.util.Date;

//一、 对象
public class Employee {

private int empId;
private String empName;
private Date workDate;

public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public Date getWorkDate() {
return workDate;
}
public void setWorkDate(Date workDate) {
this.workDate = workDate;
}
@Override
public String toString() {
return "Employee [empId=" + empId + ", empName=" + empName
+ ", workDate=" + workDate + "]";
}
}

4.Employee.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="cn.itcast.a_hello">

<class name="Employee" table="employee">

<!-- 主键 ,映射-->
<id name="empId" column="id">
<generator class="native"/>
</id>

<!-- 非主键,映射 -->
<property name="empName" column="empName"></property>
<property name="workDate" column="workDate"></property>

</class>

</hibernate-mapping>


5.增删改查的测试App.java
public class App{

private static SessionFactory sf;
static {
/*
//1. 创建配置管理类对象
Configuration config = new Configuration();
// 加载配置文件 (默认加载src/hibernate.cfg.xml)
config.configure();
//2. 根据加载的配置管理类对象,创建SessionFactory对象
sf = config.buildSessionFactory();
*/

// 创建sf对象
sf = new Configuration().configure().buildSessionFactory();
}

//1.保存对象
@Test
public void testSave() throws Exception {
// 对象
Employee emp = new Employee();
emp.setEmpName("张三123");
emp.setWorkDate(new Date());

//根据session的工厂,创建session对象
Session session = sf.openSession();
// 开启事务
Transaction tx = session.beginTransaction();
//-----执行操作-----
session.save(emp);

// 提交事务/ 关闭
tx.commit();
session.close();
}

//2.更新
public void testUpdate() throws Exception {
// 对象
Employee emp = new Employee();
emp.setEmpId(2);
emp.setEmpName("张三3");

// 创建session
Session session = sf.openSession();
Transaction tx = session.beginTransaction();

//-------执行操作-------
// 没有设置主键,执行保存;有设置主键,执行更新操作; 如果设置主键不存在报错!
session.saveOrUpdate(emp);

tx.commit();
session.close();
}

//HQL查询 【适合有数据库基础的】
public void testQuery() throws Exception {

Session session = sf.openSession();
Transaction tx = session.beginTransaction();

// 主键查询
//Employee emp = (Employee) session.get(Employee.class, 1);

// HQL查询,查询全部
Query q = session.createQuery("from Employee where empId=1 or empId=2");
List<Employee> list = q.list();

System.out.println(list);

tx.commit();
session.close();
}

//SQL 查询
@Test
public void testSQL() throws Exception {
Session session = sf.openSession();
Transaction tx = session.beginTransaction();

// 把每一行记录封装为对象数组,再添加到list集合
// SQLQuery sqlQuery = session.createSQLQuery("select * from employee");
// 把每一行记录封装为 指定的对象类型
SQLQuery sqlQuery = session.createSQLQuery("select * from employee").addEntity(Employee.class);
List list = sqlQuery.list();

System.out.println(list);

tx.commit();
session.close();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: