导入基础架包
导入hibernate的基础架包 + mysql jdbc架包
基础jar包
创建实体Bean
package com.project.bean;
public class UserBean {
private int id;
private String name;
private int age;
private String adress;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getname() {
return name;
}
public void setname(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAdress() {
return adress;
}
public void setAdress(String adress) {
this.adress = adress;
}
@Override
public String toString() {
return "nameBean [id=" + id + ", name=" + name + ", age=" + age + ", adress=" + adress + "]";
}
}
创建 Hibernate 配置文件
在 src/创建 Hibernate.cfg.xml 文件:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 配置数据库连接环境 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://127.0.0.1:3306/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<!-- 配置运行环境 -->
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>
<!-- 输出执行的sql语句 -->
<property name="show_sql">true</property>
<!-- 格式化sql语句 -->
<property name="format_sql">true</property>
<!-- 设置自动创建数据库, 根据实体Bean与映射文件,自动创建表 -->
<property name="hbm2ddl.auto">create</property>
<!-- 引入映射文件 -->
<mapping resource="com/project/bean/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
创建 映射文件
在实体Bean的包下,也是就 com/project/bean 创建 User.hbm.xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.project.bean.UserBean" table="t_user">
<!-- 映射主键 -->
<id name="id" column="u_id">
<!-- 设置主键生成策略 -->
<generator class="increment" />
</id>
<!-- name:bean属性 column:对应的字段名 -->
<property name="name" column="u_name"></property>
<property name="age" column="u_age"></property>
<property name="adress" column="u_adress"></property>
</class>
</hibernate-mapping>
使用 Hibernate 操作数据库
package com.project.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
import com.project.bean.UserBean;
public class TestMain {
public static void main(String[] args) {
// 自动读取src下按照命名规范写的配置文件,如果没有按规范写,需要在configure中指定配置文件
// Configuration cfg = new Configuration().configure();
Configuration cfg = new Configuration().configure();
System.out.println("cfg = " + cfg);
// 获取 session 工厂对象
SessionFactory fa = cfg.buildSessionFactory();
// 获取 session(连接)对象
Session session = fa.openSession();
// 创建一个事务对象, commit、rollback
Transaction tran = session.beginTransaction();
UserBean user = new UserBean();
user.setname("解锋镝");
user.setAge(19);
user.setAdress("苦境");
// 执行添加操作
session.save(user);
tran.commit();
session.close();
}
@Test
public void test01() {
// 自动读取src下按照命名规范写的配置文件,如果没有按规范写,需要在configure中指定配置文件
Configuration cfg = new Configuration().configure();
// 获取 session 工厂对象
SessionFactory fa = cfg.buildSessionFactory();
// 获取 session(连接)对象
Session session = fa.openSession();
// 创建一个事务对象, commit、rollback
Transaction tran = session.beginTransaction();
UserBean user = new UserBean();
user.setname("解锋镝");
user.setAge(19);
user.setAdress("苦境");
// 执行添加操作
session.save(user);
// 提交事务
tran.commit();
// 关闭连接
session.close();
}
}
华丽的分隔线
添加增删改操作
首先对获取工厂类与获取session进行封装
package com.project.util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
/**
* 获取 session 的工厂对象
* @return
*/
public static SessionFactory getSessionFactory() {
Configuration config = new Configuration().configure();
// 获取 sessionFactory;
SessionFactory fa = config.buildSessionFactory();
return fa;
}
/**
* 获取session对象(相当于jdbc的Connection),需要手动关闭
* @return
*/
public static Session getSession() {
Session session = getSessionFactory().openSession();
return session;
}
/**
* 获取瞬时的session对象,提交或回滚事务后,连接自动关闭
* @return
*/
public static Session getCurrentSession() {
Session session = getSessionFactory().getCurrentSession();
return session;
}
/**
* 关闭Session对象
* @return
*/
public static void closeSession(Session session) {
if (session != null) {
session.close();
}
}
}
增
@Test
public void test01() {
Session session = HibernateUtil.getSession();
Transaction tran = session.beginTransaction();
UserBean bean = new UserBean();
bean.setName("墨倾池");
bean.setAdress("九轮天");
bean.setAge(34);
// 插入数据
session.save(bean);
// 提交事务
tran.commit();
// 关闭连接
HibernateUtil.closeSession(session);
删
// 删
@Test
public void test03() {
Session session = HibernateUtil.getSession();
Transaction tran = session.beginTransaction();
UserBean user = new UserBean();
user.setId(2);
session.delete(user);
tran.commit();
HibernateUtil.closeSession(session);
}
查
// 查
@Test
public void test2() {
Session session = HibernateUtil.getSession();
// get 方式查询
UserBean bean = (UserBean) session.get(UserBean.class, 2);
System.out.println(bean);
// load 方式查询
UserBean bean2 = (UserBean) session.load(UserBean.class, 2);
System.out.println(bean2);
session.close();
}
改
@Test
public void test04() {
Session session = HibernateUtil.getSession();
Transaction tran = session.beginTransaction();
UserBean user = new UserBean();
user = (UserBean) session.get(UserBean.class, 3);
// 设置需要更新的内容
user.setName("孤星泪");
session.update(user);
tran.commit();
HibernateUtil.closeSession(session);
}
用 hql 面对对象语句 查询
@Test
public void test05() {
Session session = HibernateUtil.getSession();
Transaction tran = session.beginTransaction();
// hql 语句, 面对对象语句
Query query = session.createQuery("from UserBean where name = '墨倾池'");
List<UserBean> list = query.list();
for (UserBean userBean : list) {
System.out.println(userBean);
}
}
网友评论