• 【SSH三大框架】Hibernate基础第一篇:编写第一个Hibernate程序


    接下来这几章节学习的是Hibernate,Hibernate的主要作用就是用来和数据库进行连接,简化了JDBC的操作。


    首先,我们创建项目,然后把Hibernate的jar包和sqlserver的驱动导入进去。

    接下来,我们须要写一个实体类:User

    package cn.itcast.hibernate.domain;
    
    import java.util.Date;
    
    public class User {
     private int id;
     //private String name;
     private String name;
     private Date  birthday;
    
     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 Date getBirthday() {
    	return birthday;
    }
    public void setBirthday(Date birthday) {
    	this.birthday = birthday;
    }
    }
    
    提供了三个属性:id、name、birthday


    然后,我们在User所在的包下,配置User实体类的映射文件:User.hbm.xml

    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC 
    	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <hibernate-mapping 
    	package="cn.itcast.hibernate.domain">
    
    	<class name="User" table="uuser">
    		<id name="id">
    			<generator class="native"/> 
    		</id>
    		<property name="name" />
    		<property name="birthday" />
    	</class>
    </hibernate-mapping>
    能够看到,我们配置了一个<class>,name属性是这个类的雷鸣,table是要映射的数据库的表

    <id><property><property>这三个标签是定义了表中的属性列,当中为id设置了<generator>标签,规定了主键生成的策略(后边会介绍)


    接下来,我们就要写配置文件了,在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.microsoft.sqlserver.jdbc.SQLServerDriver
    		</property>
    		<property name="connection.url">
    			jdbc:sqlserver://localhost:1433;DatabaseName=testhibernate
    		</property>
    		<property name="connection.username">sa</property>
    		<property name="connection.password">123456</property>
    
    		<property name="dialect">
    			org.hibernate.dialect.SQLServerDialect
    		</property>
    		<property name="connection.password">123456</property>
    		<property name="hbm2ddl.auto">create</property>
    
    		<property name="show_sql">true</property>
    
    		<mapping resource="cn/itcast/hibernate/domain/User.hbm.xml" />
    		
    	</session-factory>
    </hibernate-configuration>
    在这上边配置了数据库驱动、数据库地址及数据库、用户名、密码、sqlserver数据库方言,还有建表方式、是否显示sql语句,最后一个<mapping>是把user.hbm.xml这个映射文件引入进来。


    然后,我们写一个測试类实现一下:

    package cn.itcast.hibernate;
    
    import java.util.Date;
    
    
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;
    import org.hibernate.cfg.Configuration;
    
    import cn.itcast.hibernate.domain.User;
    
    
    
    public class Base {
    	public static void main(String[] args) {
    		Configuration cfg = new Configuration();
    		cfg.configure();
    		SessionFactory sf = cfg.buildSessionFactory();
    		
    		Session s = sf.openSession();
    		Transaction tx =  s.beginTransaction();
    		User u = new User();
    		u.setBirthday(new Date());
    		u.setName("name1");
    		
    		s.save(u);
    		tx.commit();
    		s.close();
    		System.out.println("end");
    	}
    }
    
    在这段代码中,首先载入配置文件,然后打开session,再利用session打开事务,然后把user对象保存在session中,提交事务,关闭session。

    在Hibernate中,默认是不会插入数据的,仅仅有打开事务才可以插入。


    我们执行之后,就能够发现数据库中插入了一条数据。




  • 相关阅读:
    Java实现监控目录下文件变化
    Postgresql 修改用户密码
    Swing清空jtable中的数据
    delphi登录用友的信息
    用友U8的SQL SERVER 数据库结构说明表
    候老师的讲堂:视频录制、笔记软件、思维导图、画图等工具
    DELPHI 关于内存数据与 JSON
    Delphi国内优秀网站及开源项目
    SQL Server 阻止了对组件Ad Hoc Distributed Queries访问的方法
    SQL Server跨服务器查询
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/4185482.html
Copyright © 2020-2023  润新知