程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> 搭建SpringMVC+Spring+Hibernate平台,springmvchibernate

搭建SpringMVC+Spring+Hibernate平台,springmvchibernate

編輯:JAVA綜合教程

搭建SpringMVC+Spring+Hibernate平台,springmvchibernate


一. 開發環境

  1. 點擊此查看並下載需要的 Eclipse IDE for Java EE Developers 開發工具,推薦選用32位

    2. 點擊此查看並下載需要的 MySQL Server 數據庫,推薦選用32位

  3. 點擊此找到並下載  Java SE Development Kit 6u45,推薦選用32位

  4. 點擊此找到並下載  apache-tomcat-6.0.48.zip

   

二. 准備工作

  1. 點擊此找到並下載 spring-framework-4.3.4.RELEASE-dist.zip,解壓縮並在 libs 文件夾下得到:   

spring-aop-4.3.4.RELEASE.jar
spring-beans-4.3.4.RELEASE.jar
spring-context-4.3.4.RELEASE.jar
spring-core-4.3.4.RELEASE.jar
spring-expression-4.3.4.RELEASE.jar
spring-jdbc-4.3.4.RELEASE.jar
spring-orm-4.3.4.RELEASE.jar
spring-tx-4.3.4.RELEASE.jar
spring-web-4.3.4.RELEASE.jar
spring-webmvc-4.3.4.RELEASE.jar
View Code

    目前最高版本

 

  2. 點擊此找到並下載 aspectjweaver-1.8.9.jar 

    目前最高穩定版本,spring AOP 依賴 aspectj 來實現的


  3. 點擊此找到並下載 hibernate-release-4.2.21.Final.zip,解壓縮並在 libs/required 文件夾下得到:

antlr-2.7.7.jar
dom4j-1.6.1.jar
hibernate-commons-annotations-4.0.2.Final.jar
hibernate-core-4.2.21.Final.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
javassist-3.18.1-GA.jar
jboss-logging-3.1.0.GA.jar
jboss-transaction-api_1.1_spec-1.0.1.Final.jar
View Code

    支持 java 1.6 的最高版本

 

  4. 點擊此找到並下載 slf4j-1.7.21.zip,解壓縮得到:

jcl-over-slf4j-1.7.21.jar
slf4j-api-1.7.21.jar
slf4j-log4j12-1.7.21.jar
View Code

    目前最高版本,spring 的日記記錄采用:jc-over-slf4j ---> slf4j ---> log4j

 

  5. 點擊此找到並下載 log4j-1.2.17.jar 

    支持 java 1.6、spring-4.3.4 的最高穩定版本

 

  6. 點擊此找到並下載 log4jdbc4-1.2.1.jar.zip,解壓縮得到:log4jdbc4-1.2.1.jar

  7. 點擊此找到並下載 fastjson-1.2.21.jar

  8. 點擊此找到並下載 mysql-connector-java-5.1.40.zip,解壓縮得到: mysql-connector-java-5.1.40-bin.jar

  9. 點擊此找到並下載 commons-lang3-3.5.jar

    目前最高版本


三. 工程搭建

  1. Eclipse --> File --> New --> Dynamic Web Project --> 輸入工程名 ims --> Next -->

    修改 Default output folder 為:WebContent/WEB-INF/classes --> Finish

 

  2. 將(一)中得到的所有jar包放到 WebContent/WEB-INF/lib 文件夾下

 

  3. src 目錄下創建如下包結構:    

src
 |--com
 |   |--ims
 |   |   |--common
 |   |   |--persistence
 |   |   |       |--base
 |   |   |       |--dao
 |   |   |       |   |--xxx
 |   |   |       |       |--impl
 |   |   |       |--model
 |   |   |            |--xxx
 |   |   |--service
 |   |   |     |--xxx
 |   |   |         |--impl
 |   |   |--web
 |   |   |   |--controller
 |   |   |   |      |---xxx
View Code

    其中,xxx表示子項目

 

  4. src 目錄下創建如下文件:

    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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-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/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">  
    <context:component-scan base-package="com" use-default-filters="false">
        <context:include-filter type="aspectj" expression="com.ims.persistence.dao..*.impl.*DaoImpl"/>
        <context:include-filter type="aspectj" expression="com.ims.service..*.impl.*BSImpl"/>
    </context:component-scan>   
    
    <!-- log4jdbc,需要依賴slf4j框架,攔截輸出完整sql語句 -->
    <bean id="log4jdbcInterceptor" class="net.sf.log4jdbc.DataSourceSpyInterceptor" />
    <bean id="dataSourceLog4jdbcAutoProxyCreator"
        class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
        <property name="interceptorNames">
            <list>
                <value>log4jdbcInterceptor</value>
            </list>
        </property>
        <property name="beanNames">
            <list>                
                <value>sysDatasource</value>
                <value>mmsDatasource</value>
            </list>
        </property>
    </bean>
</beans>
View Code

    

    applicationContextXxx.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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-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/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    
    <bean id="xxxDatasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">        
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />        
        <property name="url" value="jdbc:mysql://localhost:3306/xxx?characterEncoding=utf8" />        
        <property name="username" value="root" />        
        <property name="password" value="123456" />    
    </bean>
    
    <bean id="xxxSessionFactory" 
            class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">    
          <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
        <property name="dataSource" ref="xxxDatasource" />                                                    
        <property name="packagesToScan">
            <list>
                <value>com.ims.persistence.model.xxx</value>
            </list>    
        </property>        
    </bean>
    
    <bean id="xxxTransactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="xxxSessionFactory"/>
    </bean>
    
    <tx:advice id="xxxTxAdvice" transaction-manager="xxxTransactionManager">   
        <tx:attributes>   
            <tx:method name="find*" isolation="READ_COMMITTED" read-only="true"/>
            <tx:method name="get*" isolation="READ_COMMITTED" read-only="true"/>
            <tx:method name="*" isolation="READ_COMMITTED" rollback-for="Throwable" />
        </tx:attributes>   
    </tx:advice>
    
    <aop:config>   
        <aop:pointcut id="xxxTxPointcut" expression="execution(* com.ims.service.xxx.impl..*BSImpl.*(..))"/>   
        <aop:advisor advice-ref="xxxTxAdvice" pointcut-ref="xxxTxPointcut" order="0"/>   
    </aop:config>   
    
    <bean id="xxxBaseDao" class="com.ims.persistence.dao.xxx.impl.XxxBaseDaoImpl">
        <property name="sessionFactory" ref="xxxSessionFactory"/>
    </bean> 
    
</beans>
View Code

    其中,xxx表示子項目的名稱

    

    controllerServlet.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-4.3.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
    
    <mvc:annotation-driven />
    <context:component-scan base-package="com" use-default-filters="false">
        <context:include-filter type="aspectj" expression="com.ims.web.controller..*Controller"/>
    </context:component-scan>
    
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/view/" />
    </bean>
</beans>
View Code

    

    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="hibernate.order_updates">true</property>        
        <property name="hibernate.show_sql">false</property>
        <!-- none -->
        <property name="hibernate.hbm2ddl.auto">update</property>        
        
        <property name="hibernate.c3p0.max_size">30</property>   
         <property name="hibernate.c3p0.min_size">5</property>    
         <property name="hibernate.c3p0.timeout">3000</property>
         <property name="hibernate.c3p0.max_statements">50</property>         
        <!--每隔多少秒檢測連接是否可正常使用  -->    
        <property name="c3p0.idle_test_period">121</property>  
        <!--當池中的連接耗盡的時候,一次性增加的連接數量,默認為3-->    
        <property name="c3p0.acquire_increment">1</property>  
        <property name="c3p0.validate">true</property>
        
        <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
    </session-factory>
    
</hibernate-configuration>
View Code

    

    log4j.properties

#輸出級別和輸出位置 ; 包中只有級別大於或等於配置級別,才允許輸出
log4j.rootLogger=WARN,console,roll

#控制台輸出
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d %p [%c] - %m%n

#按日期滾動輸出
log4j.appender.roll=org.apache.log4j.DailyRollingFileAppender
log4j.appender.roll.File=${catalina.base}/logs/ims/ims.log
log4j.appender.roll.layout=org.apache.log4j.PatternLayout
log4j.appender.roll.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS}    %p    %t    %c    %m%n

#繼承rootLogger的輸出位置
log4j.logger.com.ims = WARN
log4j.logger.org.hibernate.cache = ERROR
log4j.logger.org.directwebremoting = ERROR
#顯示sql語句的參數
#log4j.logger.org.hibernate.type=trace 

log4j.logger.jdbc.connection=OFF
log4j.logger.jdbc.audit=OFF
log4j.logger.jdbc.resultset=OFF
log4j.logger.jdbc.sqlonly=OFF
#INFO、OFF
log4j.logger.jdbc.sqltiming=INFO
View Code

 

  5. 創建基礎controller類:

    創建工程的基礎controller類:

      詳見 springMVC基礎controller類

    創建子項目的基礎controller類:

package com.ims.web.controller.xxx;

import com.ims.web.controller.BaseController;

public class XxxBaseController extends BaseController{
    
}
View Code


  6. 創建基礎dao類:

    創建工程的基礎dao類:

      詳見 hibernate基礎dao類

    創建子項目的基礎dao類:

      基礎接口類:XxxBaseDao.java

package com.ims.persistence.dao.xxx;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import com.ims.persistence.base.BaseDao;

/**
 * 統一數據訪問接口
 */
public interface XxxBaseDao<T extends Serializable> extends BaseDao<T> {
    
}
View Code

      基礎接口實現類:XxxBaseDaoImpl.java

package com.ims.persistence.dao.xxx.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;

import com.ims.persistence.base.BaseDaoImpl;
import com.ims.persistence.dao.xxx.XxxBaseDao;

public class XxxBaseDaoImpl<T extends Serializable> extends BaseDaoImpl<T> implements XxxBaseDao<T>{    
    @Resource(name = "xxxSessionFactory")  
    public void setSuperSessionFactory(SessionFactory xxxSessionFactory) {  
        super.setSessionFactory(xxxSessionFactory);  
    }
    
}
View Code

 

  7. WebContent 目錄下創建如下目錄結構:

WebContent
    |--common
    |--content
    |     |--css
    |     |--images
    |     |--js
    |--sql
    |   |--xxx
    |--view
    |   |--xxx
    |--viewModel
    |      |--xxx
View Code

  

  8. 修改 WebContent/WEB-INF/web.xml 為:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_4.xsd 
        http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    version="2.4">
  <session-config>
    <session-timeout>30</session-timeout>
  </session-config>
  
  <context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath:log4j.properties</param-value>
  </context-param>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
            classpath:applicationContext.xml,
            classpath:applicationContextXxx.xml
    </param-value>
  </context-param>
  <context-param>
    <param-name>webAppRootKey</param-name>
    <param-value>webapp.root</param-value>
  </context-param>
  
  <listener>   
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>   
  </listener>
  <listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
  </listener>
  <listener>
    <listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
  </listener>
    
  <filter>
    <filter-name>encodingFilter</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>
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  
  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
      
  <servlet>
    <servlet-name>controller</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:controllerServlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
    
  <servlet-mapping>
    <servlet-name>controller</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>
View Code

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved