SSH整合:Spring整合Hibernate并生成数据库

2025-11-14 02:51:35

1、新建一个Maven web项目:ssh。

SSH整合:Spring整合Hibernate并生成数据库

2、在maven配置中加入spring依赖支持。

     <dependency>

      <!-- 引入spring mvc -->

      <groupId>org.springframework</groupId>

      <artifactId>spring-webmvc</artifactId>

      <version>4.3.7.RELEASE</version>

    </dependency>

    <!-- spring jdbc -->

    <dependency>

      <groupId>org.springframework</groupId>

      <artifactId>spring-jdbc</artifactId>

      <version>4.3.7.RELEASE</version>

    </dependency>

    <!-- spring面向切面編程 -->

    <dependency>

      <groupId>org.springframework</groupId>

      <artifactId>spring-aspects</artifactId>

      <version>4.3.7.RELEASE</version>

    </dependency>

   <dependency>       

      <groupId>org.springframework</groupId>      

       <artifactId>spring-orm</artifactId>     

        <version>4.3.7.RELEASE</version>  

 </dependency>

   

SSH整合:Spring整合Hibernate并生成数据库

3、 在web.xml文件中配置spring的listener:

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

         xmlns="http://java.sun.com/xml/ns/javaee"

         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

         id="WebApp_ID" version="3.0">

  <context-param>

    <param-name>contextConfigLocation</param-name>

    <param-value>classpath:applicationContext.xml</param-value>

  </context-param>

  

  <listener>

    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

  </listener>

</web-app>

SSH整合:Spring整合Hibernate并生成数据库

4、在resources资源目录中新建一个applicationContext.xml文件。

SSH整合:Spring整合Hibernate并生成数据库

5、加入hibernate,mysql数据库依赖包:

<dependency>

      <groupId>org.hibernate</groupId>

      <artifactId>hibernate-core</artifactId>

      <version>4.2.4.Final</version>

    </dependency>

<dependency>

            <groupId>mysql</groupId>

            <artifactId>mysql-connector-java</artifactId>

            <version>5.0.2</version>

        </dependency>

SSH整合:Spring整合Hibernate并生成数据库

6、在类路径下加入hibernate.cfg.xml文件,在其中配置hibernate的基础属性。

<?xml version="1.0"?><!DOCTYPE hibernate-configuration PUBLIC        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- show_sql: 操作数据库时,会 向控制台打印sql语句 -->

        <property name="show_sql">true</property>

        <!-- format_sql: 打印sql语句前,会将sql语句先格式化  -->

        <property name="format_sql">true</property>

        <!-- hbm2ddl.auto: 生成表结构的策略配置

             update(最常用的取值): 如果当前数据库中不存在表结构,那么自动创建表结构. 

                     如果存在表结构,并且表结构与实体一致,那么不做修改

                     如果存在表结构,并且表结构与实体不一致,那么会修改表结构.会保留原有列.

             create(很少):无论是否存在表结构.每次启动Hibernate都会重新创建表结构.(数据会丢失)

             create-drop(极少): 无论是否存在表结构.每次启动Hibernate都会重新创建表结构.每次Hibernate运行结束时,删除表结构.

             validate(很少):不会自动创建表结构.也不会自动维护表结构.Hibernate只校验表结构. 如果表结构不一致将会抛出异常.

          -->

       <property name="hibernate.hbm2ddl.auto">create</property>

        <!-- 数据库方言配置 

         org.hibernate.dialect.MySQLDialect (选择最短的)

         -->

        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>

        

        <!-- hibernate.connection.autocommit: 事务自动提交  -->

        <property name="hibernate.connection.autocommit">true</property>

        <!-- 将Session与线程绑定=> 只有配置了该配置,才能使用getCurrentSession -->

        <property name="hibernate.current_session_context_class">thread</property>

        <!-- 引入ORM 映射文件  -->

    </session-factory>

</hibernate-configuration>

SSH整合:Spring整合Hibernate并生成数据库

7、建立持久化类和其对应的.hbm.xml文件。

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

    <class name="com.gwolf.ssh.entity.Department"

           table="ssh_department">

        <id name="id">

            <column name="id" />

            <generator class="native"/>

        </id>

        

        <property name="departmentName">

            <column name="department_name"/>

        </property>

    </class>

</hibernate-mapping>

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>   

 <class name="com.gwolf.ssh.entity.Employee" table="ssh_employee">        <id name="id">           

          <generator class="native"/>    

    </id>               

 <property name="lastName">         

         <column name="last_name"    

 </property>        

<property name="email">        

    </property>     

  

 <property name="birth" type="java.util.Date">  

      </property>   

   

  <property name="createTime" type="java.util.Date">          

  <column name="create_time"/>     

   </property>              

  <!--单向n-1关联关系-->       

 <many-to-one name="department" class="com.gwolf.ssh.entity.Department">            

      <column name="department_id"/>      

  </many-to-one>  

        </class>

</hibernate-mapping>

SSH整合:Spring整合Hibernate并生成数据库

SSH整合:Spring整合Hibernate并生成数据库

8、hibernate和spring进行整合,在maven中加入c3p0依赖包。

<!-- 数据库连接池,驱动 -->

    <dependency>

        <groupId>c3p0</groupId>

        <artifactId>c3p0</artifactId>

        <version>0.9.1</version>

    </dependency>

SSH整合:Spring整合Hibernate并生成数据库

9、在resources资源文件中新建一个dbconfig.properties文件:

jdbc.jdbcUrl=jdbc:mysql://localhost:3306/sshjdbc.driverClass=com.mysql.jdbc.Driverjdbc.user=rootjdbc.password=root

SSH整合:Spring整合Hibernate并生成数据库

10、hibernate和spring进行整合,配置数据源。

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns:p="http://www.springframework.org/schema/p"

    xmlns:context="http://www.springframework.org/schema/context"

    xmlns:aop="http://www.springframework.org/schema/aop"

    xmlns:tx="http://www.springframework.org/schema/tx"

    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd

        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd

        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

            

    <context:property-placeholder location="classpath:dbconfig.properties"/>

    <!-- Spring的配置文件,主要配置和业务逻辑有关的 -->

    <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="comboPooledDataSource">

        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>

        <property name="driverClass" value="${jdbc.driverClass}"></property>

        <property name="user" value="${jdbc.user}"></property>

        <property name="password" value="${jdbc.password}"></property>

    </bean>

    

</beans>

11、hibernate和spring进行整合配置SessionFactory。

<bean id="sessionFactory" 

          class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

        <property name="dataSource" ref="comboPooledDataSource"/>

        <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>

        <property name="mappingLocations" value="classpath:com/gwolf/ssh/entity/*.hbm.xml"/>

    </bean>

SSH整合:Spring整合Hibernate并生成数据库

12、hibernate和spring进行整合,配置声明式事务。

    <bean class="org.springframework.orm.hibernate4.HibernateTransactionManager"

          id="dataSourceTransactionManager">

        <property name="sessionFactory" ref="sessionFactory"></property>

    </bean>

    

    <aop:config>

        <aop:pointcut expression="execution(* com.gwolf.ssh.service..*(..))"

                      id="txPoint"/>

        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>

    </aop:config>

    

    <!-- 配置事务如何切入 -->

    <tx:advice id="txAdvice" transaction-manager="dataSourceTransactionManager">

        <tx:attributes>

            <!-- 所有方法都是事务方法 -->

            <tx:method name="*"/>

            <tx:method name="get*" read-only="true"/>

        </tx:attributes>

    </tx:advice>

SSH整合:Spring整合Hibernate并生成数据库

13、启动项目,会看到生成的数据表。

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢