使用Maven搭建Spring MVC+Hibernate开发环境

本文详细介绍了如何在Eclipse中使用Maven搭建开发环境,包括导入jar包、配置数据库(PostgreSQL)、设置Maven与Eclipse插件、以及配置Spring框架与相关依赖,同时提供了Spring MVC的初始化配置与Web应用开发步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在Eclipse中使用Maven搭建开发环境,首先要下载Eclipse,地址:http://www.eclipse.org/downloads/;安装好Mavne插件后才能进行,教程:http://blog.youkuaiyun.com/lfsfxy9/article/details/9397937;我的数据库是使用的开源的关系型数据库PostgreSQL数据库。

第一步:导入jar包,这一步在Maven中特别简单,Maven构建的项目中有一个pom.xml文件,这个是管理jar包和其他一些配置信息的文件。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.**</groupId>
  <artifactId>artifactName</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>porjectName Maven Webapp</name>
  <url>http://maven.apache.org</url>
  
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <spring.version>3.1.4.RELEASE</spring.version>
  </properties>
  
  <!-- jar包下载的地址 -->
  <repositories>
  <!-- 中央仓库 -->
    <repository>
            <id>mvn-repo</id>
            <name>mvn Repository</name>
            <url>http://mvnrepository.com/</url>
        </repository>
        
    </repositories>
    
  <dependencies>
  <!-- mail相关类 -->
 <dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
 </dependency>
  
  <!-- 测试用 junit -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <!-- spring -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-asm</artifactId>
      <version>3.1.4.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-expression</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-instrument</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-instrument-tomcat</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jms</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-orm</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-oxm</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>3.1.4.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc-portlet</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-struts</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <!-- hibernate jar -->
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>3.5.0-Final</version>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-entitymanager</artifactId>
      <version>3.5.0-Final</version>
    </dependency>
    
    <!-- 添加验证jar -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>4.2.0.Final</version>
    </dependency>
    <!-- servlet jar -->
     <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.4</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.1</version>
      <scope>provided</scope>
    </dependency>
    <!-- codehaus  页面跳转 ??-->
    <dependency>              
   <groupId>org.codehaus.plexus</groupId>     
   <artifactId>plexus-compiler-javac</artifactId>     
   <version>1.8.1</version>  
</dependency>

    <!-- slf4j jar包 日志有关 -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.6.0</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.6.0</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.14</version>
    </dependency>
    <!-- jdbc -->
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>8.4-701.jdbc4</version>
</dependency>
<!-- jstl 标签 -->
<dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.1.2</version>
    </dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- http和json -->
<dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpclient</artifactId>
      <version>4.2</version>
    </dependency>
    <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>1.5</version>
    </dependency>
    <!-- 
    <dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.2</version>
</dependency>
    <dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
     -->
     <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-core-asl</artifactId>
            <version>1.9.13</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.13</version>
        </dependency>
     
    <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpmime</artifactId>
      <version>4.2</version>
    </dependency>
    <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpclient-cache</artifactId>
      <version>4.2</version>
    </dependency>
    <!-- 读取xml -->
    <dependency>
<groupId>jdom</groupId>
<artifactId>jdom</artifactId>
<version>1.0</version>
</dependency>
    <dependency>
<groupId>org.glassfish.main.common</groupId>
<artifactId>common-util</artifactId>
<version>4.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.10-FINAL</version>
</dependency>

<!-- 切面编程相关jar -->
<dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.8.2</version>
    </dependency>
    <dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.2</version>
</dependency>
<!-- axis2 -->
    <dependency>
<groupId>org.apache.ws.commons.axiom</groupId>
<artifactId>axiom-api</artifactId>
<version>1.2.8</version>
</dependency>
    <dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-kernel</artifactId>
<version>1.5</version>
</dependency>
    <dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-adb</artifactId>
<version>1.5</version>
</dependency>
    <dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-transport-local</artifactId>
<version>1.5</version>
</dependency>
    <dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-transport-http</artifactId>
<version>1.5</version>
</dependency>
    
  </dependencies>
  <build>
    <finalName>violationsdrivecar</finalName>
    <!-- 设置/src/main/resources下的.properties文件可以访问pom.xml里的值 -->
   <resources>
            <resource>
                <directory>${basedir}/src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>${basedir}/src/main/java</directory>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>${basedir}/src/main/webapp</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
  </build>
  
  <!-- 各个环境下不同的配置 -->
  <profiles>
  <profile>
            <id>local</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
             
            <!-- -->
            <properties>


                
                 
                <hibernate.dialect>org.hibernate.dialect.PostgreSQLDialect</hibernate.dialect>
                <hibernate.show_sql>true</hibernate.show_sql>
                <hibernate.format_sql>true</hibernate.format_sql>
                
                <com.magus.encoding>utf-8</com.magus.encoding>
                
                
                
                
            </properties>
            
        </profile>
        
        <profile>
            <id>qa</id>
            <properties>
              <
                <hibernate.dialect>org.hibernate.dialect.PostgreSQLDialect</hibernate.dialect>
                <hibernate.show_sql>true</hibernate.show_sql>
                <hibernate.format_sql>true</hibernate.format_sql>
                
                <com.magus.encoding>utf-8</com.magus.encoding>
                
                <log4j.rootLogger>INFO,sys,file</log4j.rootLogger>
                <log4j.appender.file.File>/srv/logs/violationsdrivecar_cor/violationsdrivecar_cor.log</log4j.appender.file.File>
           
           
            </properties>
            
            <build>
                <plugins>
<!-- 
   <plugin>
   
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-compiler-plugin</artifactId>
       <version>2.3.1</version>
       <dependencies>
           <dependency>
               <groupId>org.codehaus.plexus</groupId>
               <artifactId>plexus-compiler-javac</artifactId>
               <version>1.8.1</version>
           </dependency>
       </dependencies>
       
   </plugin>
    -->
                     <!-- 
                    <plugin>
                   
                        <groupId>org.codehaus.cargo</groupId>
                        <artifactId>cargo-maven2-plugin</artifactId>
                        <version>1.3.3</version>
                        <configuration>
                            <container>
                                <containerId>glassfish3x</containerId>
                                <type>remote</type>
                            </container>
                            
                            <configuration>
                                <type>runtime</type>
                                <properties>
                                    <cargo.hostname>qa.fun-guide.mobi:4747</cargo.hostname>
                                    <cargo.remote.username>admin</cargo.remote.username>
                                    <cargo.remote.password>adminadmin</cargo.remote.password>
                                </properties>
                            </configuration>
                            
                            <deployables>
                                <deployable>
                                    <groupId>${project.groupId}</groupId>
                                    <artifactId>${project.artifactId}</artifactId>
                                    <type>war</type>
                                    <properties>
                                        <context>/${project.artifactId}</context>
                                    </properties>
                                </deployable>
                            </deployables>
                        </configuration>
                        
                        <dependencies>
                            <dependency>
                                <groupId>org.glassfish.deployment</groupId>
                                <artifactId>deployment-client</artifactId>
                                <version>3.0.1</version>
                            </dependency>
                        </dependencies>
                         
                    </plugin>
                    -->
                </plugins>
            </build>
        </profile>
        <!-- 待修改 -->
        <profile>
            <id>online</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
             
            <!-- -->
            <properties>


               
                 
                <hibernate.dialect>org.hibernate.dialect.PostgreSQLDialect</hibernate.dialect>
                <hibernate.show_sql>true</hibernate.show_sql>
                <hibernate.format_sql>true</hibernate.format_sql>
                
                <com.magus.encoding>utf-8</com.magus.encoding>
                
                <log4j.rootLogger>INFO,sys,file</log4j.rootLogger>
                <log4j.appender.file.File>/srv/logs/violationsdrivecar_cor/violationsdrivecar_cor.log</log4j.appender.file.File>


           
           
            </properties>
            
        </profile>
        
        
  </profiles>
</project>


看我的注释基本就能明白我的意思了。

还需要有spring mvc独有的配置文件

<?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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:p="http://www.springframework.org/schema/p"
       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-3.0.xsd
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-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" 
       xmlns:context="http://www.springframework.org/schema/context"
       default-autowire="byName">
           
    <aop:aspectj-autoproxy />   
    
    <context:component-scan base-package="com.magus.violationsdrivecar" >
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> 
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" /> 
    </context:component-scan>
    <!--
    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
    --> 
    <!--  访问静态内容  -->
    <mvc:resources mapping="/js/**" location="/js/"/>  
    <mvc:resources mapping="/css/**" location="/css/"/> 
    
    <bean id="validator"
          class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
          
    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="webBindingInitializer">
            <bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
                <!--                <property name="conversionService" ref="conversionService" />-->
                <property name="validator" ref="validator" />
            </bean>
        </property>
        
        <property name="messageConverters">
            <list>
                <!--
                <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                    <property name="supportedMediaTypes">
                        <list>
                            <value>text/plain;charset=UTF-8</value>
                        </list>
                    </property>
                </bean>
                -->
                <!---->
                <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" >
                    <property name="supportedMediaTypes">
                        <list>
                            <value>application/json;charset=UTF-8</value>
                            <value>application/x-www-form-urlencoded;charset=UTF-8</value>
                        </list>
                    </property>
                </bean>
                
                
                <!--                <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
                <bean class="org.springframework.http.converter.FormHttpMessageConverter" />
                <bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter" />
                -->
            </list>
        </property>
        
    </bean>
    
    <bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"/>
    
    <!--
    Most controllers will use the ControllerClassNameHandlerMapping above, but
    for the index controller we are using ParameterizableViewController, so we must
    define an explicit mapping for it.
    -->
    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="index.htm">indexController</prop>
            </props>
        </property>
    </bean>
  <!-- 视图解析器 -->
    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/jsp/"
          p:suffix=".jsp" />


    <!--
    The index controller.
    -->
    
    <bean name="indexController"
          class="org.springframework.web.servlet.mvc.ParameterizableViewController"
          p:viewName="index" />


</beans>

第二步,修改web.xml这个配置文件。

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


<web-app>
  <display-name>Archetype Created Web Application</display-name>
  
   <!-- Spring 配置 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
 
<!-- Spring Encoding Filter  -->
    <filter>
        <filter-name>springEncoding</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>springEncoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
    
    <!-- Spring mvc 配置 -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<load-on-startup>2</load-on-startup>
 
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.json</url-pattern>
</servlet-mapping>
<!-- Config Session Lazy Loading -->
    <filter>
        <filter-name>springOpenSessionView</filter-name>
        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
        <init-param> 
            <param-name>flushMode</param-name> 
            <param-value>AUTO</param-value>         
        </init-param>
    </filter>


    <filter-mapping>
            <filter-name>springOpenSessionView</filter-name>
            <url-pattern>*.json</url-pattern>
    </filter-mapping>
    
    
    

<!-- 指定Spring bean 的配置文件所在目录,默认为在WEB-INFO目录下 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml,classpath*:spring/*.xml</param-value>
</context-param>

</web-app>

接下来就是开发了

设置控制器层

新建一个类

@Controller
@Scope("prototype")
public class Test
{

    @RequestMapping(value = "/affirm")
    public String test( ){
       System.out.println("Hello Word!");
        return "welcome";
    }
}

Dao层,注意:增删改的操作在方法前需加上 注释@Transactional方可

@Component("baseDaoImpl")
public class BaseDaoImpl extends HibernateDaoSupport implements BaseDao
{
    
    @Transactional
    @Override
    public void save(Object o)
    {
        this.getHibernateTemplate().save(o);
    }

}

Service层

@Component("testUserServiceImpl")
public class TestUserServiceImpl  implements TestUserService
{

    @Resource(name = "testUserDaoImpl")
    private  TestUserDao testUserDao;

    @Transactional
    @Override
    public void saveaa(ViolationsOrder obj)
    {
        testUserDao.save(obj);
    }

}

以此类推就行了!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值