总结篇-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"?>
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">
  Archetype Created Web Application

  
  
    contextConfigLocation
    classpath:applicationContext.xml
  

  
    org.springframework.web.context.ContextLoaderListener
  

  
  
    SpringMVC
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      classpath:springmvc.xml
    
  
  
    SpringMVC
    /*
  

  
  
    CharacterEncodingFilter
    org.springframework.web.filter.CharacterEncodingFilter
    
      encoding
      UTF-8
    
  
  
    CharacterEncodingFilter
    /*
  



SpringMvc.xml:

xml version="1.0" encoding="UTF-8"?>
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">
        
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    context:component-scan>

    
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        name="prefix" value="/WEB-INF/views/"/>
        name="suffix" value=".jsp"/>
    

    
    <mvc:default-servlet-handler/>

    
    <mvc:annotation-driven/>


ApplicationContext.xml:

xml version="1.0" encoding="UTF-8"?>
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"/>

    
    id="DataSource" class="com.alibaba.druid.pool.DruidDataSource">
        name="driverClassName" value="${jdbc.driverClass}"/>
        name="url" value="${jdbc.url}"/>
        name="username" value="${jdbc.user}"/>
        name="password" value="${jdbc.password}"/>
    

    
    id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        name="dataSource" ref="DataSource"/>
        name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/>
        name="persistenceUnitName" value="lab"/>
    

    
    id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        name="entityManagerFactory" ref="entityManagerFactory"/>
    

    
    <tx:annotation-driven transaction-manager="transactionManager"/>

    
    <jpa:repositories base-package="com.yiibai" entity-manager-factory-ref="entityManagerFactory" transaction-manager-ref="transactionManager"/>


persistence.xml:

xml version="1.0"?>
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">
    name="lab" transaction-type="RESOURCE_LOCAL">
        
        org.hibernate.ejb.HibernatePersistence
        
            
            name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
            
            name="hibernate.hbm2ddl.auto" value="update"/>
            
            name="hibernate.show_sql" value="false"/>
            
            name="hibernate.format_sql" value="false"/>

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

            name="hibernate.jdbc.fetch_size" value="18"/>
            
            name="hibernate.max_fetch_depth" value="8"/>
            
            name="hibernate.enable_lazy_load_no_trans" value="true"/>
            
            name="hibernate.event.merge.entity_copy_observer" value="allow"/>
        
    



db.properties:

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

你可能感兴趣的:(Summary)