星空网 > 软件开发 > Java

hibernate初步

Hibernate开发步骤
1.新创建工程并且导入相关的包

主要是hibernate、mysql相关的JAR包。

注意:新导入的hibernate相关的JAR包是否与你当前所使用的jdk版本是否兼容,且不要忘记导入数据库的驱动JAR包。如5.2的hibernate需要jdk1.8才能编译。

hibernate初步
2.持久化类POJO的设计-------javabean与数据库表相关联的类。

public class UserInfo {  private int id;  private String name;  private String password;  private String role;  public UserInfo() {      }  public UserInfo(String name, String password, String role) {    super();    this.name = name;    this.password = password;    this.role = role;  }  public UserInfo(int id, String name, String password, String role) {    super();    this.id = id;    this.name = name;    this.password = password;    this.role = role;  }  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 String getPassword() {    return password;  }  public void setPassword(String password) {    this.password = password;  }  public String getRole() {    return role;  }  public void setRole(String role) {    this.role = role;  }  @Override  public String toString() {    return "UserInfo [id=" + id + ", name=" + name + ", password="        + password + ", role=" + role + "]";  }  }

 


3.编写*.hbm.主要是描述实体类与数据库中表之间的映射关系。

<?class name="net.togogo.pojo.UserInfo" table="t_user"><!-- 指定数据库中的表名 -->    <id name="id" type="java.lang.Integer">      <column name="id" />      <!-- 主键值生成方式 -->      <generator class="increment" />    </id>    <property name="name" type="java.lang.String"><!-- property对应与POJO中的属性 -->      <column name="name" length="20" />  <!-- column对应与数据库中表的属性 -->    </property>    <property name="password" type="java.lang.String">      <column name="password" length="20" />    </property>    <property name="role" type="java.lang.String">      <column name="role" length="20" />    </property>  </class></hibernate-mapping>

 


4.编写hibernate配置文件-------数据库的连接信息和显示底层sql语句。

<!-- ~ Hibernate, Relational Persistence for Idiomatic Java ~ ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later. ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. --><!DOCTYPE hibernate-configuration PUBLIC  "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">  <hibernate-configuration>  <session-factory>    <!-- 数据库连接方言信息 -->    <property name="dialect">      org.hibernate.dialect.MySQLDialect    </property>        <!-- 你所使用的数据库驱动为:mysql -->    <property name="hibernate.connection.driver_class">      com.mysql.jdbc.Driver    </property>        <!-- 你所使用的数据库名字为:hibernate_db -->    <property name="hibernate.connection.url">      jdbc:mysql://localhost:3306/hibernate_db    </property>        <!-- 你所使用的数据库账号为:root -->    <property name="connection.username">root</property>        <!-- 你所使用的数据库密码为:root -->    <property name="connection.password">root</property>        <!-- 显示底层的sql语句,开发阶段设为true,项目发布阶段设为false -->    <property name="show_sql">true</property>    <mapping resource="net/togogo/pojo/userinfo.hbm.

 


5.使用hibernate api操作数据库.其中又包括如下几步。
        1)获取 Configuration 对象。
        2)获取 SessionFactory 对象。
        3)获取 Session,打开事务。
        4)用面向对象的方式操作数据库。
        5)关闭事务,关闭 Session。

 

public class HibernateUtils {  //往数据库中插入数据信息。  public static void save(Object obj){
        1)获取 Configuration 对象。 Configuration config = new Configuration(); config.configure("hibernate.cfg.);
        2)获取 SessionFactory 对象。 SessionFactory sessionFactory = config.buildSessionFactory();
         3)获取 Session Session session = sessionFactory.openSession();
         3)打开事务。 Transaction tx = session.beginTransaction();
        4)用面向对象的方式操作数据库。 session.save(obj);
         5)关闭事务,关闭 Session。 tx.commit(); session.close(); } public static void deleteById(int id){ Configuration config = new Configuration(); config.configure("hibernate.cfg.); SessionFactory sessionFactory = config.buildSessionFactory(); Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); UserInfo userInfo = session.get(UserInfo.class, id); session.delete(userInfo); tx.commit(); session.close(); } public static void update(Object obj){ Configuration config = new Configuration(); config.configure("hibernate.cfg.); SessionFactory sessionFactory = config.buildSessionFactory(); Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); session.update(obj); tx.commit(); session.close(); } public static Object findObjById(Class c,Serializable id){ Configuration config = new Configuration(); config.configure("hibernate.cfg.); SessionFactory sessionFactory = config.buildSessionFactory(); Session session = sessionFactory.openSession(); Object obj = session.get(c, id); session.close(); return obj; }}

6.测试类

public class IDUQTest {  @Test  public void testSave(){    UserInfo userInfo = new UserInfo("lili","123","admin");    HibernateUtils.save(userInfo);  }    @Test  public void testDelete(){    HibernateUtils.deleteById(4);  }    @Test  public void testUpdate(){    UserInfo userInfo = (UserInfo) HibernateUtils.findObjById(UserInfo.class, 3);    userInfo.setPassword("123456");    HibernateUtils.update(userInfo);  }}

 

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Hibernate的运行过程

Hibernate的运行过程如下:

1) 应用程序先调用Configuration类,该类读取Hibernate配置文件及映射文件中的信息,并用这些信息生成一个SessionFactory对象

2) 从SessionFactory对象生成一个Session对象

3) 用Session对象生成Transaction对象

4) 可通过Session对象的get(),load(),save(),update(),delete()和saveOrUpdate()等方法对POJO类进行加载、保存、更新、删除、等操作;

5) 在查询的情况下,可通过Session对象生成一个Query对象,然后利用Query对象执行查询操作;如果没有异常,Transaction对象将提交这些操作到数据库中。

 

//end




原标题:hibernate初步

关键词:Hibernate

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流