项目结构:
在这里有必要跟大家说说Hibernate中的各个jar包的含义:。。。。。上面的log4j-1.2.11.jar包没有都可以哦!!!
1 /** 2 * 3 */ 4 package com.b510.hibernate.readme; 5 6 /** 7 * @author hongten<br> 8 * @date 2013-3-25 9 */ 10 public class ReadMe { 11 /** 12 * Hibernate中各个jar文件的含义: 13 * 14 * Hibernate的主要类库: 15 * ----------// begin //----------------- 16 * hibernate3.jar Hibernate主类库 17 * commons-collections-3.1.jar Commons的集合类库 18 * antlr-2.7.6.jar Java开源语法分析生成器 19 * dom4j-1.6.1.jar 解析XML的类库 20 * javassist-3.12.0.GA.jar 开源的分析,编辑和创建Java自己码的类库 21 * slf4j-api-1.6.6.jar 各种日志处理接口 22 * slf4j-simple-1.6.6.jar 各种日志处理接口的实现 23 * jta-1.1.jar Java中的事务处理API 24 * ----------// end 8个 //----------------- 25 * 26 * 数据库连接类库: 27 * ----------// begin //----------------- 28 * mysql-connector-java-5.1.22-bin.jar mysql数据库连接类库 29 * ----------// end 1个 //----------------- 30 * 31 * Hibernate的注解Annotation类库: 32 * ----------// begin //----------------- 33 * hibernate-commons-annotations-4.0.1.Final.jar 34 * hibernate-jpa-2.0-api-1.0.1.Final.jar 35 * ----------// end 2个 //----------------- 36 * 37 * 38 * 39 * Hibernate的注解EntityManager类库: 40 * ----------// begin //----------------- 41 * hibernate-entitymanager.jar 42 * jboss-archive-browsing.jar 43 * ----------// end 2个 //----------------- 44 * 45 * 46 * Hibernate的注解org/apache/commons/logging/LogFactory类库: 47 * 如果缺少,会出现:Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory 48 * ----------// begin //----------------- 49 * commons-logging-1.0.4.jar 50 * ----------// end 1个 //----------------- 51 * 52 * 53 * Hibernate的注解org/jboss/logging/BasicLogger类库: 54 * 如果缺少,会出现:java.lang.NoClassDefFoundError: org/jboss/logging/BasicLogger 55 * ----------// begin //----------------- 56 * jboss-logging-3.0.0.GA.jar 57 * ----------// end 1个 //----------------- 58 * 59 * 60 * 共:15个 61 */ 62 }
运行结果:
=================================================================
代码部分:
=================================================================
/hibernate_demo/src/com/b510/hibernate/bean/Student.java
1 /** 2 * 3 */ 4 package com.b510.hibernate.bean; 5 6 import javax.persistence.Column; 7 import javax.persistence.Entity; 8 import javax.persistence.GeneratedValue; 9 import javax.persistence.Id; 10 11 /** 12 * 学生类 13 * @author hongten<br> 14 * @date 2013-3-25 15 */ 16 @Entity 17 public class Student { 18 19 private Integer id; 20 /** 21 * 姓名 22 */ 23 private String name; 24 /** 25 * 性别 26 */ 27 private String sex; 28 /** 29 * 学号 30 */ 31 private String no; 32 /** 33 * 年龄 34 */ 35 private int age; 36 37 @Id 38 @GeneratedValue 39 public Integer getId() { 40 return id; 41 } 42 43 public void setId(Integer id) { 44 this.id = id; 45 } 46 47 public String getName() { 48 return name; 49 } 50 51 public void setName(String name) { 52 this.name = name; 53 } 54 55 public String getSex() { 56 return sex; 57 } 58 59 public void setSex(String sex) { 60 this.sex = sex; 61 } 62 63 public String getNo() { 64 return no; 65 } 66 67 public void setNo(String no) { 68 this.no = no; 69 } 70 71 @Column(columnDefinition = "int default 0") 72 public int getAge() { 73 return age; 74 } 75 76 public void setAge(int age) { 77 this.age = age; 78 } 79 80 }
/hibernate_demo/src/com/b510/hibernate/utils/EntityManagerUtil.java
1 /** 2 * 3 */ 4 package com.b510.hibernate.utils; 5 6 import javax.persistence.EntityManager; 7 import javax.persistence.EntityManagerFactory; 8 import javax.persistence.Persistence; 9 10 /** 11 * EntityManager工具类 12 * @author hongten<br> 13 * @date 2013-3-25 14 */ 15 public class EntityManagerUtil { 16 17 private static final String PERSISTENCE_UNIT_NAME = "myPersistence"; 18 private static final EntityManagerFactory ENTITY_MANAGER_FACTORY = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME); 19 private static final EntityManager ENTITY_MANAGER; 20 21 private EntityManagerUtil(){ 22 //不做任何事儿 23 } 24 25 //创建EntityManager实例 26 static { 27 ENTITY_MANAGER = ENTITY_MANAGER_FACTORY.createEntityManager(); 28 } 29 30 /** 31 * 获取EntityManager实例 32 * @return 33 */ 34 public static EntityManager getEntityManagerInstance(){ 35 return ENTITY_MANAGER == null ? ENTITY_MANAGER_FACTORY.createEntityManager() : ENTITY_MANAGER; 36 } 37 38 }
/hibernate_demo/src/com/b510/hibernate/test/StudentTest.java
1 /** 2 * 3 */ 4 package com.b510.hibernate.test; 5 6 import java.util.List; 7 8 import javax.persistence.EntityManager; 9 import javax.persistence.EntityTransaction; 10 11 import com.b510.hibernate.bean.Student; 12 import com.b510.hibernate.utils.EntityManagerUtil; 13 14 /** 15 * 测试类 16 * @author hongten<br> 17 * @date 2013-3-25 18 */ 19 public class StudentTest { 20 21 public static void main(String[] args) { 22 //studentAdd(); 23 studentList(); 24 //studentFindById(new Integer(1)); 25 //studentMerge(); 26 } 27 28 /** 29 * 添加一条记录 30 */ 31 public static void studentAdd(){ 32 EntityManager entityManager = EntityManagerUtil.getEntityManagerInstance(); 33 EntityTransaction entityTransaction = entityManager.getTransaction(); 34 Student student = new Student(); 35 student.setName("Tom Jhone"); 36 student.setAge(21); 37 student.setSex("女"); 38 student.setNo("2009081513"); 39 40 entityTransaction.begin(); 41 entityManager.persist(student); 42 entityTransaction.commit(); 43 entityManager.close(); 44 } 45 46 /** 47 * 查询所有的记录 48 */ 49 @SuppressWarnings("unchecked") 50 public static void studentList(){ 51 EntityManager entityManager = EntityManagerUtil.getEntityManagerInstance(); 52 List<Student> students = entityManager.createQuery("from Student").getResultList(); 53 54 for(Student student : students){ 55 System.out.println("学号:"+student.getNo()+" 姓名:"+student.getName()+" 性别:"+student.getSex()+" 年龄:"+student.getAge()); 56 } 57 58 entityManager.close(); 59 } 60 61 /** 62 * 根据id查询出相应记录 63 */ 64 public static void studentFindById(Integer id){ 65 EntityManager entityManager = EntityManagerUtil.getEntityManagerInstance(); 66 Student student = entityManager.find(Student.class, id); 67 if(student != null){ 68 System.out.println("学号:"+student.getNo()+" 姓名:"+student.getName()+" 性别:"+student.getSex()+" 年龄:"+student.getAge()); 69 } 70 71 entityManager.close(); 72 } 73 74 /** 75 * 更新数据 76 */ 77 public static void studentMerge(){ 78 EntityManager entityManager = EntityManagerUtil.getEntityManagerInstance(); 79 EntityTransaction entityTransaction = entityManager.getTransaction(); 80 entityTransaction.begin(); 81 Integer id = 2; 82 Student student = entityManager.find(Student.class, id); 83 84 if(student != null){ 85 System.out.println("更新前...."); 86 System.out.println("学号:"+student.getNo()+" 姓名:"+student.getName()+" 性别:"+student.getSex()+" 年龄:"+student.getAge()); 87 student.setName("Hanyuan"); 88 entityManager.merge(student); 89 entityTransaction.commit(); 90 } 91 System.out.println("更新后...."); 92 studentFindById(id); 93 entityManager.close(); 94 } 95 96 }
/hibernate_demo/src/META-INF/persistence.xml
1 <persistence xmlns="http://java.sun.com/xml/ns/persistence" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 4 http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" 5 version="2.0"> 6 7 <persistence-unit name="myPersistence"> 8 9 <provider>org.hibernate.ejb.HibernatePersistence</provider> 10 <properties> 11 <property name="hibernate.archive.autodetection" value="class" /> 12 <property name="hibernate.show_sql" value="true" /> 13 <property name="hibernate.format_sql" value="false" /> 14 <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" /> 15 <property name="use_sql_comments" value="true" /> 16 17 <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" /> 18 <property name="hibernate.connection.url" value="jdbc:mysql://10.5.116.107:3306/mytest?characterEncoding=UTF-8" /> 19 <property name="hibernate.connection.username" value="root" /> 20 <property name="hibernate.connection.password" value="root" /> 21 <property name="hibernate.hbm2ddl.auto" value="update" /> 22 </properties> 23 </persistence-unit> 24 25 </persistence>
很多初学者很头疼的一件事儿就是,老是配置不好Hibernate,其原因多数是Hibernate中的jar包不是很明白....
这里提供源码下载,希望能帮助那些该帮助的人....
源码下载:http://files.cnblogs.com/hongten/hibernate_demo.rar
转载自 http://www.cnblogs.com/hongten/archive/2013/03/25/hongten_hibernate_demo.html