Hibernate学习笔记(一)

来源:互联网 发布:淘宝网时尚女鞋 编辑:程序博客网 时间:2024/06/05 19:08
<pre name="code" class="html"><pre name="code" class="html">
<strong>第一步:新建一个名为hibernate.cfg.xml的配置文件</strong>
<?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"><hibernate-configuration> <session-factory>  <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>  <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/world?characterEncoding=UTF-8</property>  <property name="hibernate.connection.username">root</property>  <property name="hibernate.connection.password">latewind</property>  <property name="dialect">org.hibernate.dialect.MySQLDialect</property>  <property name="show_sql">true</property>
<-- 注意是resource 和"/" -->  <mapping resource="com/latewind/hbn/bean/City.hbm.xml"/> </session-factory></hibernate-configuration>


<pre name="code" class="java"><strong>第二步:新建一个实体类City</strong>package com.latewind.hbn.bean;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.Table;public class City {private Integer id;private String name;private String countryCode;private String district;private Integer population;public Integer getId() {<span style="white-space:pre"></span>return id;}public void setId(Integer id) {<span style="white-space:pre"></span>this.id = id;}public String getName() {<span style="white-space:pre"></span>return name;}public void setName(String name) {<span style="white-space:pre"></span>this.name = name;}public String getCountryCode() {<span style="white-space:pre"></span>return countryCode;}public void setCountryCode(String countryCode) {<span style="white-space:pre"></span>this.countryCode = countryCode;}public String getDistrict() {<span style="white-space:pre"></span>return district;}public void setDistrict(String district) {<span style="white-space:pre"></span>this.district = district;}public Integer getPopulation() {<span style="white-space:pre"></span>return population;}public void setPopulation(Integer population) {<span style="white-space:pre"></span>this.population = population;}}
<strong>第三步:新建一个City.hbm.xml将City类与World里面的city Table建立映射</strong>
<?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 package="com.latewind.hbn.bean"><class name="City" table="city"><span style="white-space:pre"></span><id name="id" column="id" ><span style="white-space:pre"></span><generator class="native"/><span style="white-space:pre"></span></id><span style="white-space:pre"></span><span style="white-space:pre"></span>  <property name="name" type="string" column="name" /><span style="white-space:pre"></span><property name="countryCode" type="string" column="CountryCode" />          <property name="district" type="string" column="District" />     <property name="population" type="integer" column="Population" /></class></hibernate-mapping>
<strong>第四步:创建一个HibernateUtil类</strong>
package com.latewind.hbn.util;import org.hibernate.SessionFactory;import org.hibernate.boot.registry.StandardServiceRegistryBuilder;import org.hibernate.cfg.Configuration;import org.hibernate.service.ServiceRegistry;public class HibernateUtil {  <span style="white-space:pre"></span>      private static final SessionFactory sessionFactory = buildSessionFactory();        private static SessionFactory buildSessionFactory() {          try {              // Create the SessionFactory from hibernate.cfg.xml              return new Configuration().configure().buildSessionFactory();          }          catch (Throwable ex) {              // Make sure you log the exception, as it might be swallowed              System.err.println("Initial SessionFactory creation failed." + ex);              throw new ExceptionInInitializerError(ex);          }      }        public static SessionFactory getSessionFactory() {          return sessionFactory;      }    }
<strong>第五步:Test类</strong>
import java.util.List;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import com.latewind.hbn.bean.City;import com.latewind.hbn.util.HibernateUtil;public class Test {<span style="white-space:pre"></span><span style="white-space:pre"></span>public static void main(String[] args) {<span style="white-space:pre"></span> Session session = HibernateUtil.getSessionFactory().openSession();<span style="white-space:pre"></span>Transaction transaction=session.beginTransaction();<span style="white-space:pre"></span>List<City> list=session.createQuery(" from City ").list();<span style="white-space:pre"></span>System.out.println(list.toString());//print <span style="white-space:pre"></span>for(City city:list){<span style="white-space:pre"></span>System.out.print( city.getId()+" ");<span style="white-space:pre"></span>System.out.print( city.getName()+" ");<span style="white-space:pre"></span>System.out.println(city.getPopulation());//print <span style="white-space:pre"></span>}<span style="white-space:pre"></span><span style="white-space:pre"></span>transaction.commit();<span style="white-space:pre"></span>session.close();<span style="white-space:pre"></span><span style="white-space:pre"></span>}}

                                             
0 0
原创粉丝点击