1、 下载hibernate-release-4.1.8.Final.zip包
2、 部署
将lib下required文件夹下的所有jar包复制到WEB-INF\lib\目录下
在src下创建hibernate.cfg.xml配置文件
hibernate.cfg.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 5 6 <hibernate-configuration> 7 <session-factory> 8 9 <!-- 数据库驱动名称 --> 10 <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 11 <!-- 数据库链接地址 --> 12 <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property> 13 <!-- 数据库用户名称 --> 14 <property name="hibernate.connection.username">root</property> 15 <!-- 数据库密码 --> 16 <property name="connection.password">admin</property> 17 <!-- 设置数据库连接池默认个数 --> 18 <property name="connection.pool_size">1</property> 19 <!-- 设置数据库SQL语言类型 --> 20 <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 21 <!-- 设置是否显示SQL语句--> 22 <property name="show_sql">true</property> 23 <!-- 设置是否格式化SQL语句 --> 24 <property name="format_sql">true</property> 25 <!-- 设置使用线程--> 26 <property name="current_session_context_class">thread</property> 27 <!-- 设置hibernate的映射文件--> 28 <mapping resource="edu/dzu/vo/UsersVo.hbm.xml"/> 29 30 </session-factory> 31 </hibernate-configuration>
3、 应用
创建数据库,数据库名hibernate
1 -- ---------------------------- 2 -- Table structure for `users` 3 -- ---------------------------- 4 DROP TABLE IF EXISTS `users`; 5 CREATE TABLE `users` ( 6 `id` int(11) NOT NULL, 7 `name` varchar(20) DEFAULT NULL, 8 `age` int(11) DEFAULT NULL, 9 `tel` varchar(20) DEFAULT NULL, 10 `address` varchar(50) DEFAULT NULL, 11 PRIMARY KEY (`id`) 12 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
edu.dzu.vo包中
UsersVo.hbm.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Hibernate-mapping DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 6 <hibernate-mapping> 7 8 <class name="edu.dzu.vo.Users" table="users" catalog="hibernate"> 9 10 <id name="id" type="java.lang.Integer"> 11 <column name="id"></column> 12 <generator class="assigned"></generator> 13 </id> 14 15 <property name="name" type="java.lang.String"> 16 <column name="name" length="20"></column> 17 </property> 18 19 <property name="age" type="java.lang.Integer"> 20 <column name="age"></column> 21 </property> 22 23 <property name="tel" type="java.lang.String"> 24 <column name="tel" length="20"></column> 25 </property> 26 27 <property name="address" type="java.lang.String"> 28 <column name="address" length="50"></column> 29 </property> 30 31 </class> 32 33 </hibernate-mapping>
Users.java
1 package edu.dzu.vo; 2 3 import java.io.Serializable; 4 5 public class Users implements Serializable { 6 7 /** 8 * 9 */ 10 private static final long serialVersionUID = 1L; 11 12 private Integer id; 13 private String name; 14 private Integer age; 15 private String tel; 16 private String address; 17 18 public Users(){ 19 20 } 21 22 public Users(Integer id){ 23 this.id = id; 24 } 25 26 public Users(Integer id, String name, 27 Integer age, String tel, String address){ 28 this.id = id; 29 this.name = name; 30 this.age = age; 31 this.tel = tel; 32 this.address = address; 33 } 34 35 public int getId() { 36 return id; 37 } 38 public void setId(int id) { 39 this.id = id; 40 } 41 public String getName() { 42 return name; 43 } 44 public void setName(String name) { 45 this.name = name; 46 } 47 public int getAge() { 48 return age; 49 } 50 public void setAge(int age) { 51 this.age = age; 52 } 53 public String getTel() { 54 return tel; 55 } 56 public void setTel(String tel) { 57 this.tel = tel; 58 } 59 public String getAddress() { 60 return address; 61 } 62 public void setAddress(String address) { 63 this.address = address; 64 } 65 66 }
HibernateSessionFactory.java
package edu.dzu.vo; import org.hibernate.*; import org.hibernate.cfg.Configuration; @SuppressWarnings("deprecation") public class HibernateSessionFactory { private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml"; private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>(); private static Configuration configuration = new Configuration(); private static SessionFactory sessionFactory; private static String configFile = CONFIG_FILE_LOCATION; static { sessionFactory = configuration.configure(configFile).buildSessionFactory(); } private HibernateSessionFactory(){ } public static Session getSession(){ Session session = threadLocal.get(); if(session == null || !session.isOpen()){ session = (sessionFactory != null ) ? sessionFactory.openSession():null; threadLocal.set(session); } return session; } }
TestHibernate.java
1 package edu.dzu.vo; 2 3 import java.awt.Font; 4 5 import javax.swing.JOptionPane; 6 7 import org.hibernate.Session; 8 import org.hibernate.Transaction; 9 10 public class TestHibernate { 11 12 /** 13 * @param args 14 */ 15 public static void main(String[] args) { 16 17 Users users = new Users(); 18 users.setId(20121108); 19 users.setName("张三"); 20 users.setAge(24); 21 users.setTel("010-87654321"); 22 users.setAddress("上海黄浦"); 23 24 Users users2 = new Users(); 25 users2.setId(20121109); 26 users2.setName("李四"); 27 users2.setAge(23); 28 users2.setTel("010-12345678"); 29 users2.setAddress("北京朝阳"); 30 31 Session session = HibernateSessionFactory.getSession(); 32 Transaction trans = session.beginTransaction(); 33 session.persist(users); 34 session.persist(users2); 35 trans.commit(); 36 37 StringBuffer result = new StringBuffer(); 38 result.append("添加成功!往数据库中添加如下数据:\n"); 39 result.append("编号"+"地址"+"姓名"+"年龄"+"联系电话\t\r\n\r\n"); 40 result.append(users.getId()+" "); 41 result.append(users.getAddress()+" "); 42 result.append(users.getName()+" "); 43 result.append(users.getAge()+" "); 44 result.append(users.getTel()+"\t\n\r"); 45 46 result.append(users2.getId()+" "); 47 result.append(users2.getAddress()+" "); 48 result.append(users2.getName()+" "); 49 result.append(users2.getAge()+" "); 50 result.append(users2.getTel()+"\t\n\r"); 51 52 session.close(); 53 54 JOptionPane.getRootFrame().setFont(new Font("Arial", Font.BOLD, 14)); 55 JOptionPane.showMessageDialog(null, result.toString()); 56 57 } 58 59 }
微笑,给世界,也给自己Smile, to world, to yourself
此文章由 http://www.ositren.com 收集整理 ,地址为: http://www.ositren.com/htmls/68559.html