首页 > 代码库 > maven+springMVC+hibernate4架构

maven+springMVC+hibernate4架构

    Spring的优越性不用我说大家也都很清楚,项目实际开发中最常用的非IOC和AOP莫属。其优越性深受广大ds男的热捧,在项目中如果不是自己写框架技术选型时Spring也是不可或缺的,尤其是跟一些ORM(hibernate,mybatis等)框架结合更是解决了很多人的问题。

    下面就讲解一些maven结合Spring跟主流的hibernate4是如何搭建开发环境,如有对spring,hibernate不清楚的地方可以去其他地方查查资料。

  Ok废话少说,且看下面配置:

1. 新建maven项目

  具体maven如何使用请参考maven分类中maven的用法,这里不再多说。

2. 打开父模块pom.xml 引入spring配置

 

        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-webmvc</artifactId>            <version>3.2.10.RELEASE</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-aop</artifactId>            <version>3.2.10.RELEASE</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-orm</artifactId>            <version>3.2.10.RELEASE</version>        </dependency>        

3. 引入hibernate4配置

<dependency>  <groupId>org.hibernate</groupId>  <artifactId>hibernate-core</artifactId>  <version>4.3.6.Final</version></dependency>

4. 配置spring-config.xml(放到classpath下)

<?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"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd">  
  <context:property-placeholder location="classpath:config.properties" />
<bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="url" value="${jdbc_url}" /> <property name="username" value="${jdbc_username}" /> <property name="password" value="${jdbc_password}" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="hibernateProperties"> <props> <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> <prop key="hibernate.format_sql">${hibernate.format_sql}</prop> </props> </property> <property name="packagesToScan"> <list> <value>com.duke.test.domain</value> </list> </property> </bean> <bean name="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <tx:annotation-driven transaction-manager="transactionManager" />

   <context:component-scan
    base-package="com.duke.test.dao.impl,com.duke.test.service.impl" />

</beans>

5. config.properties文件(放到classpath下)

hibernate.dialect=org.hibernate.dialect.Oracle10gDialectjdbc_driverClassName=oracle.jdbc.driver.OracleDriverjdbc_url=jdbc:oracle:thin:@127.0.0.1:1521:orcljdbc_username=scottjdbc_password=scotthibernate.hbm2ddl.auto=nonehibernate.show_sql=truehibernate.format_sql=falsejdbc_valid=SELECT 1 FROM DUAL

6. 配置spring-mvc.xml (放到classpath下)

  

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:mvc="http://www.springframework.org/schema/mvc" 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"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">    <context:component-scan base-package="com.duke.test.controller" />    <bean id="mappingJacksonHttpMessageConverter"        class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">        <property name="supportedMediaTypes">            <list>                <value>text/html;charset=UTF-8</value>            </list>        </property>    </bean>    <bean        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">        <property name="messageConverters">            <list>                <ref bean="mappingJacksonHttpMessageConverter" />            </list>        </property>    </bean>    <bean id="defaultViewResolver"        class="org.springframework.web.servlet.view.InternalResourceViewResolver"        p:order="3">        <property name="viewClass"            value="org.springframework.web.servlet.view.JstlView" />        <property name="contentType" value="text/html" />        <property name="prefix" value="/WEB-INF/views/" />        <property name="suffix" value=".jsp" />    </bean>    </beans>

7. 配置web.xml

<context-param>        <param-name>contextConfigLocation</param-name>        <param-value>classpath:spring-config.xml</param-value>    </context-param>    <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>    </filter>            <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>    <servlet>        <servlet-name>dukeMVC</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <init-param>            <param-name>contextConfigLocation</param-name>            <param-value>classpath:spring-mvc.xml</param-value>        </init-param>        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>dukeMVC</servlet-name>        <url-pattern>*.htm</url-pattern>    </servlet-mapping>

 

到此maven+spring+hibernate配置已结束。文中有不清楚的可及时给我留言!