总结篇-Spring-SpringMVC-Hibernate-jpa整合

之前介绍了许多hibernate和JPA的关系,但是说的再多,不如亲手实践一下,本文主要是Spring-SpringMVC-Hibernate-jpa整合,充分用到实践中来。

开发步骤:

1.导入SSH框架整合的jar包

1.1导入SpringMVCj的ar包

1.2导入Hibernate的jar包

1.3导入三方依赖的包

1.4导入mysql驱动包


2.配置web.xml

2.1配置Spring IOC容器

2.2配置SpringMVC的控制器(servlet)

2.3配置编码等过滤器


3.配置SpringMVC

3.1配置自动扫描的包

3.2配置视图解析器

3.3配置静态资源

3.4配置注解


4.配置Spring

4.1配置自动扫描的包

4.2配置数据源

4.3配置DataSource

4.4整合Hibernate

4.4.1配置entityManagerFactory 实体管理器

4.4.2配置事务管理器

4.4.3配置支持注解的事务

4.4.4配置spring-data


工程截图如下:




具体配置如下:

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
  <display-name>Archetype Created Web Application</display-name>

  <!-- 配置Spring IOC容器 -->
  <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>

  <!-- 配置SpringMVC DispatcherServlet核心控制器-->
  <servlet>
    <servlet-name>SpringMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>SpringMVC</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>

  <!-- 配置编码方式过滤器,注意,配置在所有过滤器前-->
  <filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>


SpringMvc.xml:

<?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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- 配置自动扫描的包-->
    <context:component-scan base-package="com.yiibai.controller" use-default-filters="false">
        <!-- 加了@Controller的类会被扫描加载-->
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <!-- 配置视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <!-- 配置静态资源-->
    <mvc:default-servlet-handler/>

    <!-- 配置注解-->
    <mvc:annotation-driven/>
</beans>


ApplicationContext.xml:

<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       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.xsd http://www.springframework.org/schema/tool http://www.springframework.org/schema/tool/spring-tool.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

    <!-- 配置自动扫描的包-->
    <context:component-scan base-package="com.yiibai" use-default-filters="false">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <!-- 配置数据源-->
    <context:property-placeholder location="classpath:db.properties"/>

    <!-- 配置DataSource-->
    <bean id="DataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driverClass}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.user}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <!-- 配置entityManagerFactory 实体管理器-->
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="DataSource"/>
        <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/>
        <property name="persistenceUnitName" value="lab"/>
    </bean>

    <!-- 配置事务管理器-->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>

    <!-- 配置支持注解的事务-->
    <tx:annotation-driven transaction-manager="transactionManager"/>

    <!-- -配置Spring data-->
    <jpa:repositories base-package="com.yiibai" entity-manager-factory-ref="entityManagerFactory" transaction-manager-ref="transactionManager"/>
</beans>


persistence.xml:

<?xml version="1.0"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
             version="1.0">
    <persistence-unit name="lab" transaction-type="RESOURCE_LOCAL">
        <!-- 选择jap持久化实现厂商,这里以hibernate为例-->
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <!-- 数据库语言-->
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
            <!-- 数据库表自动更新-->
            <property name="hibernate.hbm2ddl.auto" value="update"/>
            <!-- 数据库显示sql语句操作-->
            <property name="hibernate.show_sql" value="false"/>
            <!-- 数据库表sql格式化显示-->
            <property name="hibernate.format_sql" value="false"/>

            <property name="hibernate.jdbc.batch_size" value="10"/>

            <property name="hibernate.jdbc.fetch_size" value="18"/>
            <!-- 数据库表最大连接数-->
            <property name="hibernate.max_fetch_depth" value="8"/>
            <!-- 关闭Load方法的延迟加载-->
            <property name="hibernate.enable_lazy_load_no_trans" value="true"/>
            <!--关联重复问题-->
            <property name="hibernate.event.merge.entity_copy_observer" value="allow"/>
        </properties>
    </persistence-unit>

</persistence>


db.properties:

jdbc.user=root
jdbc.password=workhard
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/yiibai

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值