云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > 有关hibernate相关知识点

有关hibernate相关知识点

  • 62 次阅读
  • 3 次下载
  • 2025/6/3 9:52:41

北大青鸟中关村

import java.util.List; import java.util.Scanner;

public class test {

public static void main(String[] args) {

// 添加学生信息 // AddStudent();

// 显示所有学生信息 ShowAll();

// 显示单个学生信息 // ShowOne();

//更新学生信息 //Update();

//删除学生信息 //Delete();

ShowAll(); }

public static void ShowAll() {

StudentService service = new StudentService(); List list = service.GetAllStudent(); for (Student student : list) {

System.out.println(student.getStuNo() + \student.getStuName()); } }

public static void ShowOne() { String no = \

StudentService service = new StudentService(); Student student = service.GetStudentBystuNo(no); if (student != null) {

System.out.println(student.getStuNo() + \student.getStuName()); } else {

System.out.println(\

北大青鸟中关村

} }

public static void AddStudent() {

Scanner input = new Scanner(System.in); Student stu = new Student();

System.out.print(\请输入学生编号:(A001)\ stu.setStuNo(input.next());

System.out.print(\请输入学生姓名:(A001)\ stu.setStuName(input.next()); stu.setStuPass(\

System.out.print(\请输入学生年龄:(0-100)\ stu.setStuAge(input.nextInt());

System.out.print(\请输入学生手机号:(A001)\ stu.setMobile(input.next());

System.out.print(\请输入学生邮箱:(A001)\ stu.setEmail(input.next());

System.out.print(\请输入学生地址:(A001)\ stu.setAddress(input.next());

StudentService service = new StudentService(); service.AddStudent(stu); }

public static void Update() {

Scanner input = new Scanner(System.in); System.out.print(\输入要修改的学号:\ String stuNo = input.next();

System.out.print(\输入要修改的姓名:\ String newName = input.next();

StudentService service = new StudentService(); service.UpdateStudent(stuNo, newName); }

public static void Delete() {

Scanner input = new Scanner(System.in); System.out.print(\输入要修改的学号:\ String stuNo = input.next();

StudentService service = new StudentService(); service.DeleteStudent(stuNo); } }

北大青鸟中关村

说明:

1:SessionFactory sf = new Configuration().configure().buildSessionFactory();这句话的意思是读取hibernate.cfg.xml,创建Session工厂,是线程安全的。

默认是”hibernate.cfg.xml”,不用写出来,如果文件名不是”hibernate.cfg.xml”,那么需要显示指定,如下: SessionFactory sf = new Configuration(). configure( “javass.cfg.xml” ).buildSessionFactory();

2:Session是应用程序主要使用的Hibernate接口,约相当于JDBC的Connection+Statement/PreparedStatement的功能,是线程不安全的

3:在Hibernate4里面,已经不推荐使用Configuration类了,而改为使用 ServiceRegistryBuilder和MetadataSources来代替,新的写法大致如下: ServiceRegistryBuilder builder = new ServiceRegistryBuilder().configure();

builder.applySetting(\builder.applySetting(\builder.applySetting(\builder.applySetting(\builder.applySetting(\

builder.applySetting(\builder.applySetting(\

MetadataSources sources = new MetadataSources( builder.buildServiceRegistry() ); sources.addResource(\

MetadataImpl metadata = (MetadataImpl) sources.buildMetadata();

SessionFactory sf = metadata.getSessionFactoryBuilder().buildSessionFactory(); 这种写法,现在还没有实现完全,不太好用,所以官方给出的示例里面还是采用以前的方式,大家先了解一下。

4:这里使用的事务Transaction是Hibernate的Transaction,需要有,不能去掉。

为什么必须有这个Hibernate的事务呢?以HelloWorld为例来看看:

北大青鸟中关村

搜索更多关于: 有关hibernate相关知识点 的文档
  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

北大青鸟中关村 import java.util.List; import java.util.Scanner; public class test { public static void main(String[] args) { // 添加学生信息 // AddStudent(); // 显示所有学生信息 ShowAll(); // 显示单个学生信息 // ShowOne(); //更新学生信息 //Update(); //删除学生信息 //

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com