Hibernate基础

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class ExportDB { //工具类,将对象生成表,将实体类生成数据库表

public static void main(String[] args) {

//读取hibernate.cfg.xml文件
//Configuration 加载系统配置文件
Configuration cfg = new Configuration().configure();

SchemaExport export = new SchemaExport(cfg); //将类导成表

export.create(true, true);
}
}


//Annotation
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class ExportDB { //工具类,将对象生成表,将实体类生成数据库表

public static void main(String[] args) {

//读取hibernate.cfg.xml文件
//Configuration 加载系统配置文件
new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);


new SchemaExport(new AnnotationConfiguration().configure()).create(
true, true);
// 第一个输出SQL语句 第二个导出表
}
}
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

//创建一个工具类,对SessionFactory和session对象进行封装

public class HibernateUtils {

private static SessionFactory factory;

static { //只要创建初始化一次
try {
//读取hibernate.cfg.xml文件
Configuration cfg = new Configuration().configure();
//创建SessionFactory,一个数据库对应一个SessionFactory
factory = cfg.buildSessionFactory();
}catch(Exception e) {
e.printStackTrace();
}
}

public static SessionFactory getSessionFactory() {
return factory;
}

public static Session getSession() {
return factory.openSession();
}

public static void closeSession(Session session) {
if (session != null) {
if (session.isOpen()) {
session.close();
}
}
}
}


//Annotation 默认自动close session
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

//创建一个工具类,对SessionFactory和session对象进行封装

public class HibernateUtil {

private static final SessionFactory sessionFactory;

static {
try {
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new AnnotationConfiguration().configure()
.buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
//ex.printStackTrace();
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}

public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
// Annotation test
import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

public class HibernateIDTest {
private static SessionFactory sessionFactory;
// 创建一个 SessionFactory
@BeforeClass //注解
public static void beforeClass() {
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
}
@AfterClass
public static void afterClass() {
sessionFactory.close();
}

@Test
public void testStudentSave() {
StudentPK pk = new StudentPK();
pk.setId(1);
pk.setName("zhangsan");
Student s = new Student();
s.setPk(pk);
s.setAge(8);


Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
session.save(s);
session.getTransaction().commit();
}

@Test
public void testTeacherSave() {

Teacher t = new Teacher();
t.setId(1);
t.setName("t1");
t.setTitle("middle");
t.setBirthDate(new Date());

Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
session.save(t);
session.getTransaction().commit();
}

// 用main函数和try catch调试找junit的bug
public static void main(String[] args) {
beforeClass();
}
}
import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class Client {

public static void main(String[] args) {

//读取hibernate.cfg.xml文件
//Configuration 加载系统配置文件
Configuration cfg = new Configuration().configure();

//创建SessionFactory,绑定数据库,一个数据库对应一个SessionFactory
SessionFactory factory = cfg.buildSessionFactory();

Session session = null;
try {
//可以看成是对connection进行包装,还管理缓存
session = factory.openSession();

//开启事务 Transaction
session.beginTransaction();

User1 user = new User1();
user.setName("张三");
user.setPassword("123");
user.setCreateTime(new Date());
user.setExpireTime(new Date());

//保存数据
session.save(user);

//提交事务
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
//回滚事务
session.getTransaction().rollback();
}finally {
if (session != null) {
if (session.isOpen()) {
//关闭session
session.close();
}
}
}

}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值