技术记录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
- 技术记录3 hibernate4.3上的eclipse配置以及简单的读写数据库程序
- eclipse下简单的配置hibernate4
- 记录学习hibernate4.3的日子之如何在hibernate中配置时间的<property/>
- eclipse界面以及注释的简单配置
- 串口的配置以及读写
- hibernate4的默认配置
- Hadoop的简单配置以及与Eclipse的整合
- Hibernate4学习(1) 建立简单的Hibernate程序
- ant的安装配置以及在Eclipse上集成使用
- servlet的一个简单实例以及配置到tomcat上
- Linux上go的安装配置以及简单使用
- 简单记录C++操作数据库的工程配置
- Hibernate4.3环境配置
- ns-3 的下载、编译以及 Eclipse 的相关配置
- Java NIO 的简单读写 以及理解
- eclipse的使用配置记录
- 关于配置xml的读写以及使用
- Hibernate4: Entity的annotation配置
- SQL Server中的Merge关键字
- 使用sql语句制造测试数据数量(Mysql)
- 《Python核心编程》第六章练习解析
- Mysql学习笔记二列类型之字符型
- 关于生成apk签名的一些技巧
- 技术记录3 hibernate4.3上的eclipse配置以及简单的读写数据库程序
- STL源码剖析 - 第5章 关联式容器 - map
- Notepad++ 编辑 Arduino文件后直接发送到编译器IDE
- java观察者模式
- QT问题记录
- 例题1.8 彩色立方体 UVa1352
- Centos安装metasploit
- 杭电 1012 u Calculate e
- 服务器通过浏览器下载文件到本地