Spring 整合Hibernate 示例

虽然Spring整合Hibernate早就会了,但经常在创建项目整合这两个框架的时候出一些低级错误。所以在这里写一个示例,以后再遇到错误时,再把遇到的错误或异常添加上。

一、创建一个动态WEB工程,添加 jar 包。

  ①、Spring的jar包

  ②、Hibernate的jar包

  ③、数据库驱动

  ④、c3p0

  ⑤、common

二、创建hibernate.cfg.xml文件(数据库的基本配置。)

name属性最好加hibernate前缀。

 <!-- 配置数据库的基本配置 -->
    <!-- 配置数据库方言 -->
    <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
    
    <!-- 是否打印SQL语句 -->
    <property name="hibernate.show_sql">true</property>
    
    <!-- 是否格式化SQL语句 -->
    <property name="hibernate.format_sql">true</property>
    
    <!-- 设置生成数据表的策略 -->
    <property name="hibernate.hbm2ddl.auto">update</property>

三、创建Spring配置文件 applicationContext.xml

========================================================================

<!-- 扫描包 -->
    <context:component-scan base-package="com.zcd.springSpringmvcHibernate"></context:component-scan>
    
    <!-- 导入资源文件 -->
    <context:property-placeholder location="db.properties" />
    
    <!-- 配置数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
        <property name="user" value="${user}"></property>
        <property name="password" value="${password}"></property>
        <property name="driverClass" value="${driverClass}"></property>
        <property name="jdbcUrl" value="${jdbcUrl}"></property>
        <property name="maxPoolSize" value="${maxPoolSize}"></property>
        <property name="minPoolSize" value="${minPoolSize}"></property>
        <property name="initialPoolSize" value="${initialPoolSize}"></property>
        <property name="acquireIncrement" value="${acquireIncrement}"></property>
    </bean>
    
    <!-- 配置SessionFactory -->
    <bean id="sessionFactoryBean" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
        <property name="mappingLocations" value="classpath:com/zcd/springSpringmvcHibernate/beans/*.hbm.xml"></property>
    </bean>

 

=======================================================================

四、创建实体类。有两个实体类Department 和 Employee 两个

/**
 * 员工实体类,属性:id、name、email、gender、department
 * @author Administrator
 */
public class Employee
{
    private Integer id;
    private String name;
    private String email;
    private Integer gender;
    private Department department;
    
    public Employee()
    {
    }

    public Employee(Integer id, String name, String email, Integer gender, Department department)
    {
        super();
        this.id = id;
        this.name = name;
        this.email = email;
        this.gender = gender;
        this.department = department;
    }

    public Integer getId()
    {
        return id;
    }

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

    public String getName()
    {
        return name;
    }

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

    public String getEmail()
    {
        return email;
    }

    public void setEmail(String email)
    {
        this.email = email;
    }

    public Integer getGender()
    {
        return gender;
    }

    public void setGender(Integer gender)
    {
        this.gender = gender;
    }

    public Department getDepartment()
    {
        return department;
    }

    public void setDepartment(Department department)
    {
        this.department = department;
    }

    @Override
    public String toString()
    {
        return "Employee [id=" + id + ", name=" + name + ", email=" + email + ", gender=" + gender + ", department="
                + department + "]";
    }
    
}
/**
 * 部门实体类。属性: id 、 部门名称 departmentName
 * @author Administrator
 *
 */
public class Department
{
    private Integer id;
    private String departmentName;

    public Department()
    {
    }

    public Department(Integer id, String departmentName)
    {
        super();
        this.id = id;
        this.departmentName = departmentName;
    }

    public Integer getId()
    {
        return id;
    }

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

    public String getDepartmentName()
    {
        return departmentName;
    }

    public void setDepartmentName(String departmentName)
    {
        this.departmentName = departmentName;
    }

    @Override
    public String toString()
    {
        return "Department [id=" + id + ", departmentName=" + departmentName + "]";
    }

}

=====================================================================

五、给实体类创建 .hbm.xml 文件。

    <!-- Department 实体类的映射文件。 -->
    <class name="com.zcd.springSpringmvcHibernate.beans.Department" table="DEPARTMENTS">
        <id name="id" type="java.lang.Integer">
            <column name="ID" />
            <!-- 改为native -->
            <generator class="native" />
        </id>
        <property name="departmentName" type="java.lang.String">
            <column name="DEPARTMENT_NAME" />
        </property>
    </class>

=====================================================================

    <!-- Employee的实体类映射文件 -->
    <class name="com.zcd.springSpringmvcHibernate.beans.Employee" table="EMPLOYEES">
        <id name="id" type="java.lang.Integer">
            <column name="ID" />
            <!-- 改为native -->
            <generator class="native" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="NAME" />
        </property>
        <property name="email" type="java.lang.String">
            <column name="EMAIL" />
        </property>
        <property name="gender" type="java.lang.Integer">
            <column name="GENDER" />
        </property>

     <!-- 单项多对一关联关系 -->
<many-to-one name="department" class="com.zcd.springSpringmvcHibernate.beans.Department"> <column name="DEPARTMENT_ID" /> </many-to-one>
    </class>

 

====================================================================

六、测试类代码

public class SpringmvcSpringHibernateTest
{
    private ApplicationContext ac;
    private DataSource dataSource;
    
    {
        ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        
        dataSource = ac.getBean(DataSource.class);
    }
    
    /*
     * 测试是否能成功获取连接,并且能自动创建数据表。
     */
    @Test
    public void testConnection() throws SQLException
    {
        System.out.println(dataSource.getConnection().getClass().getName());
        
    }

}

=====================================================================

posted @ 2017-07-06 10:33  钓鱼翁  阅读(308)  评论(0编辑  收藏  举报