Hibernate框架搭建实例

一,Hibernate是一个持久层,是一个专门负责管理数据库连接的框架;

二,Hibernate的搭建实例;

1.在Hibernate的官方网站(http://www.hibernate.org)可以找到hibernate项目的相关资源信息。

2.把下载好的jar包导入到项目的lib文件夹下,我这里只导入必需的几个包;

3.hibernate.cfg.xml文件里的配置:

<?xml version="1.0" encoding="utf-8"?>
<!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="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
        <property name="connection.url">jdbc:sqlserver://localhost:1433;DatabaseName=StuSys</property>
        <property name="connection.username">sa</property>
        <property name="connection.password">123456</property>
        <!-- 配置hibernate的基本信息  数据库方言 -->
        <property name="dialect">org.hibernate.dialect.SQLServer2008Dialect</property>
        <property name="connection.pool_size">5</property>
         <!-- 执行操作时是否在控制台打印 SQL -->
        <property name="show_sql">true</property>
         <!-- 是否对 SQL 进行格式化 -->
        <property name="format_sql">true</property>
        <!-- 指定自动生成数据表的策略 -->
        <property name="hbm2ddl.auto">update</property>
        <!-- 指定关联的 .hbm.xml 文件 -->
        <mapping resource="Student.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

4.然后创建一个student.java的实体类,用来映射数据库数据:

package entity;

public class Student {
    private int id;
    private String name;
    private String sex;
    private int age;
    private String addres;
    
    public Student(){}
    
    public Student(int id, String name, String sex, int age, String addres) {
        super();
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.addres = addres;
    }
    
    
    public Student(String name, String sex, int age, String addres) {
        super();
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.addres = addres;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getAddres() {
        return addres;
    }

    public void setAddres(String addres) {
        this.addres = addres;
    }
    
}

 5.然后再创建一个Student.hbm.xml(注意格式是类名.hbm.xml):

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <!-- class代表实体类跟数据库表之间的映射 -->
    <class name="entity.Student" table="student">
        <id name="id" column="ID"><!-- ID的类型要注意对应,否则会出现异常 -->
            <generator class="native"/><!-- class要根据不的数据库填写不同的值 -->
        </id>
        <!--类的每个属性对应表里的每个字段,要注意类型的对应  -->
        <property name="name" column="NAME"/>
        <property name="sex" column="SEX"/>
        <property name="age"  column="AGE"/>
        <property name="addres" column="ADDRES"/>
     </class>
</hibernate-mapping>

 6.然后再applicationContext.xml文件中加入hibernate.cfg.xml文件:

<bean id="sessionfactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" scope="prototype">
     <property name="configLocation" value="/WEB-INF/classes/hibernate.cfg.xml"></property>
</bean>

7.在Action类里用sessionfactory实例完成对数据库的连接:

package dao;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;

import ssh.entity.BookCard;
import ssh.util.MyConnection;

public class IndexDaoImpl implements IndexDao {

    //在SSH的设计理念:要使用某个实例,那么就定义声明一个对象,然后
    //给它添加set方法(用于spring注入进来),实现不要关注这个实例
    //来自于那里,以及怎么创建,或者它是谁
    private SessionFactory sessionFactory;
    public void setSessionFactory(SessionFactory sf) {
        this.sessionFactory = sf;
    }


    @Override
    public List<Student> getAllBookCard() {
        //sessionFactory这个实例可以自己按常规的hibernate传统写法创建
        //也可以交给spring去托管
        //sessionFactory = new Configuration().configure().buildSessionFactory();
        Session session = sessionFactory.openSession();
        System.out.println("sessionFactory:"+sessionFactory);
        System.out.println("session:"+session);
        //执行查询之前,先插入2条数据
        Student bc1 = new Student();
        //bc1.setCid(cid);
        bc1.setName("小红");
        bc1.setSex("妖");
        bc1.setCardDate(new java.util.Date());
        bc1.setDeposit(55.8);
        session.save(bc1);
        
//        //后面当使用JPA的时候,EntityManager 类似于 Session
        Query query = session.createQuery("from Student");
//        //System.out.println("query:"+query);
//        //这2行代码,会交给spring的AOP帮忙处理
        List<BookCard> list = query.getResultList();

        return list;
    }
}
posted @ 2016-09-06 13:00  程序文  阅读(208)  评论(0编辑  收藏  举报