一、导入jar包。
1.hibernate的jar包
2.jdbc的jar包

二、配置Hibernate。建立hibernate.cfg.xml

mysql配置如下:

<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password"></property>
        <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/fruit?characterEncoding=GBK</property>
        <mapping resource="com/itnba/maya/bean/Fruit.hbm.xml"/>
  
    </session-factory>
</hibernate-configuration>

Oracle配置如下:

<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.Oracle8iDialect</property>
        <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password"></property>
        <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
        <mapping resource="com/itnba/maya/bean/Fruit.hbm.xml"/>
  
    </session-factory>
</hibernate-configuration>

ORM
三、准备数据库

四、准备实体(建议使用:引用类型。建议类名属性名与列名一致)

package com.itnba.maya.bean;

public class Fruit {
	private String ids;
	private String name;
	private Double price;
	private String source;
	private Integer numbers;
	private String image;
	public String getIds() {
		return ids;
	}
	public void setIds(String ids) {
		this.ids = ids;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Double getPrice() {
		return price;
	}
	public void setPrice(Double price) {
		this.price = price;
	}
	public String getSource() {
		return source;
	}
	public void setSource(String source) {
		this.source = source;
	}
	public Integer getNumbers() {
		return numbers;
	}
	public void setNumbers(Integer numbers) {
		this.numbers = numbers;
	}
	public String getImage() {
		return image;
	}
	public void setImage(String image) {
		this.image = image;
	}
	

}

 

五、准备映射文件 实体类名.hbm.xml 例:Fruit.hbm.xml

<!DOCTYPE hibernate-mapping PUBLIC
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.itnba.maya.bean">

    <class name="Fruit" table="Fruit">
        <id name="ids">
            <generator class="assigned"/>
        </id>
        <property name="name"/>
        <property name="price"/>
        <property name="source"/> 
        <property name="numbers" />
        <property name="image"/>
    </class>
</hibernate-mapping>

六、通过hibernate进行数据库的增删改查

1.增: 

package com.itnba.maya.test;
import org.hibernate.*;
import org.hibernate.cfg.*;
import com.itnba.maya.bean.Fruit;
public class Test1111 {
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Configuration config =new Configuration().configure();
        SessionFactory factory =config.buildSessionFactory();
        Session session =factory.openSession();
        Fruit data = new Fruit();
        data.setIds("s002");
        data.setName("猕猴桃");
        data.setPrice(10.0);
        data.setNumbers(100);
        data.setSource("博山");
        try{
        session.beginTransaction();
        session.save(data);
        session.getTransaction().commit();
        }
        catch(Exception ex){
        	session.getTransaction().rollback();
        }
        session.close();
	}

}

2.查找

package com.itnba.maya.test;
import java.util.*;
import org.hibernate.*;
import org.hibernate.cfg.*;
import com.itnba.maya.bean.Fruit;
public class Test1111 {
	public static void main(String[] args) {
		Configuration config =new Configuration().configure();
        SessionFactory factory =config.buildSessionFactory();
        Session session =factory.openSession();
        Fruit data =session.load(Fruit.class, "k001");//查单条数据
        List<Fruit> list = session.createQuery("from Fruit").getResultList();//查所有数据
	}
}

3.删除(先查再删)

package com.itnba.maya.test;
import org.hibernate.*;
import org.hibernate.cfg.*;
import com.itnba.maya.bean.Fruit;
public class Test1111 {
	public static void main(String[] args) {
		Configuration config =new Configuration().configure();
        SessionFactory factory =config.buildSessionFactory();
        Session session =factory.openSession();
         Fruit data =session.load(Fruit.class, "k001"); 
         if(data != null){
        	try{
 		session.beginTransaction();
 		session.delete(data);
 		session.getTransaction().commit();
        	}
        	catch(Exception ex){
           	session.getTransaction().rollback();
                }
 	}
       }
}

4.改(先查再改) 

package com.itnba.maya.test;
import org.hibernate.*;
import org.hibernate.cfg.*;
import com.itnba.maya.bean.Fruit;
public class Test1111 {
	public static void main(String[] args) {
	Configuration config =new Configuration().configure();
        SessionFactory factory =config.buildSessionFactory();
        Session session =factory.openSession();
	      Fruit data =session.load(Fruit.class, "k002"); 
	      if(data != null){
	     	try{
				session.beginTransaction();
				data.setPrice(8.0);
				data.setNumbers(110);
				session.update(data);
				session.getTransaction().commit();
	     	}
	     	catch(Exception ex){
	        	session.getTransaction().rollback();
	         }
			}
	}

}

 

 

 

posted on 2017-03-03 14:50  二蒙  阅读(300)  评论(0编辑  收藏  举报