当前位置:首页 > 有关hibernate相关知识点
北大青鸟中关村
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
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为例来看看:
北大青鸟中关村
共分享92篇相关文档