技术记录3 hibernate4.3上的eclipse配置以及简单的读写数据库程序

来源:互联网 发布:简单游戏编程 编辑:程序博客网 时间:2024/05/01 07:31

1. 项目结构图


2. 需要用的hibernate Jar包


这些都是在hibernate官网上下载的jar包,注意是在 hibernate-release-4.3.8.Final\lib\required下的所有jar,再加上mysqlJdbc连接的jar,还有Annotation的hibernate-commons-annotations.jar和ejb3-persistence.jar,注意hibernate-annotations.jar不能被引入,不然会报错。

3. 编写bean User,java类

package com.vo;public class User {private Integer id;     private String username;     private String password;    public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}}

4. 编写Users.hbm.xml 该xml是将bean类中的变量和数据库中的变量一一对应起来的

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"          "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  <hibernate-mapping>     <class name="com.vo.User" table="users">        <id name="id">            <generator class="assigned" />         </id>        <property name="username" />         <property name="password" />   </class></hibernate-mapping>

5. 编写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"><!-- Generated by MyEclipse Hibernate Tools.                   --><hibernate-configuration>    <session-factory>        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>        <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>        <property name="connection.username">root</property>        <property name="connection.password">123456</property>        <property name="connection.driver_class">org.gjt.mm.mysql.Driver</property>    <property name="show_sql">true</property>    <property name="hbm2ddl.auto">create</property>    <property name="current_session_context_class">thread</property>    <mapping resource="com/vo/Users.hbm.xml"/>    </session-factory></hibernate-configuration>

6. 编写HibernateUtil.java工具类

package com.vo;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import org.hibernate.service.ServiceRegistry;import org.hibernate.service.ServiceRegistryBuilder;@SuppressWarnings("deprecation")public final class HibernateUtil {private HibernateUtil(){}private static SessionFactory sessionFactory;static{Configuration cfg = new Configuration();cfg.configure();ServiceRegistry  sr = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();  sessionFactory = cfg.buildSessionFactory(sr);}public static SessionFactory getSessionFactory() {return sessionFactory;}public static Session getSession(){return sessionFactory.openSession();}}

7. 编写测试类Test.java

package com.vo;import java.util.List;import org.hibernate.HibernateException;import org.hibernate.Session;import org.hibernate.Transaction;public class Test {public static void main(String[] args) {try {Session session = HibernateUtil.getSession();Transaction tx = session.beginTransaction();User user = new User();user.setId(1);user.setUsername("name1");user.setPassword("pass1");session.save(user);StringBuffer result = new StringBuffer();@SuppressWarnings("unchecked")List<User> usersList = session.createQuery("from User").list();for(User aUser : usersList){result.append(" " + aUser.getId());result.append(" " + aUser.getUsername());result.append(" " + aUser.getPassword());}tx.commit();session.close();System.out.println(result);} catch (HibernateException e) {e.printStackTrace();}}}

***********************************************

我也是初次接触hibernate,遇到了很多问题

1. 上面说到的hibernate-annotations.jar不能被引入,不然会报错

2. 在Test,java中的List<User> usersList = session.createQuery("from User").list(); "from User" 这个User对应的是Bean的类名,而不是数据库表的名字

3. 不要粗心,代码打正确。

***********************************************


0 0
原创粉丝点击