首页 > 代码库 > Spring+Springmvc+Hibernate环境搭建与配置

Spring+Springmvc+Hibernate环境搭建与配置

  这里给出JavaWeb项目Spring+Springmvc+Hibernate框架环境的搭建与配置,方便以后快速创建项目。下面是以本人的一个项目为例给出的配置,不同项目具体的结构都是可以按一下结构完成的,至于配置内容则需要按照具体的项目需求更改部分内容。需要注意的是,本项目是以使用注解为前提完成的项目。

一.项目目录

  首先说明一下,本项目是maven项目,采用Spring+Springmvc+Hibernate的框架,前端模板引擎采用thymeleaf,html代码存放在下面结构的templates文件夹中。

总体项目结构如下:

技术分享

 

上图中src/main/java放每一层的代码,hibernate.cfg.xml也存在该路径下,而spring、springmvc的配置文件则存放在/WEB-INF/configs/spring下,当然这些文件也可以放在别的目录下,只需要在web.xml中配置路径即可,具体方法见web.xml的配置。

 

二.pom.xml的配置

       每个依赖的内容都在注释中写明,需要什么依赖或者不需要什么依赖只需在pom.xml中添加或删除该依赖即可。

<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>SYSU</groupId>
	<artifactId>MovieHub</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>MovieHub Maven Webapp</name>
	<url>http://maven.apache.org</url>

	<!-- 定义依赖库的版本变量 -->
	<properties>
		<commons-lang.version>2.6</commons-lang.version>
		<!-- spring 版本号 -->
		<spring.version>4.3.3.RELEASE</spring.version>
		<hibernate.version>3.6.10.Final</hibernate.version>
		<jackson.version>2.5.4</jackson.version>
		<log4j.version>1.2.17</log4j.version>
		<slf4j.version>1.7.7</slf4j.version>
		<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
		<jetty.version>9.3.7.v20160115</jetty.version>
	</properties>

	<!-- 依赖库定义 -->
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.10</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>commons-lang</groupId>
			<artifactId>commons-lang</artifactId>
			<version>${commons-lang.version}</version>
		</dependency>
		<!-- 防止 jsp 报错 -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.1.0</version>
		</dependency>
		<!-- 各种集合类和集合工具类 -->
		<dependency>
			<groupId>commons-collections</groupId>
			<artifactId>commons-collections</artifactId>
			<version>3.2.1</version>
		</dependency>
		<!-- Spring Begin -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>${spring.version}</version>
			<type>jar</type>
			<scope>compile</scope>
		</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-web</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-orm</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-beans</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<!-- Spring End -->

		<!-- mysql -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.25</version>
		</dependency>

		<!-- logging -->
		<dependency>
			<groupId>commons-logging</groupId>
			<artifactId>commons-logging</artifactId>
			<version>1.1.1</version>
		</dependency>
		<dependency>
			<groupId>cglib</groupId>
			<artifactId>cglib</artifactId>
			<version>2.2.2</version>
		</dependency>

		<!-- log4j & slf4j -->
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>${log4j.version}</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-simple</artifactId>
			<version>${slf4j.version}</version>
		</dependency>

		<!-- jackson -->
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-core</artifactId>
			<version>${jackson.version}</version>
		</dependency>
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>${jackson.version}</version>
		</dependency>

		<!--view thymeleaf模板引擎 -->
		<dependency>
			<groupId>org.thymeleaf</groupId>
			<artifactId>thymeleaf-spring4</artifactId>
			<version>${thymeleaf.version}</version>
		</dependency>

		<!-- hibernate -->
 		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core</artifactId>
			<version>4.2.4.Final</version>
		</dependency>
		<dependency>
			<groupId>org.hibernate.javax.persistence</groupId>
			<artifactId>hibernate-jpa-2.0-api</artifactId>
			<version>1.0.1.Final</version>
		</dependency>

		<dependency>
			<groupId>org.javassist</groupId>
			<artifactId>javassist</artifactId>
			<version>3.15.0-GA</version>
		</dependency>
		<dependency>
			<groupId>antlr</groupId>
			<artifactId>antlr</artifactId>
			<version>2.7.7</version>
		</dependency>
		<dependency>
			<groupId>dom4j</groupId>
			<artifactId>dom4j</artifactId>
			<version>1.6.1</version>
		</dependency>

		<!-- jboss -->
		<dependency>
			<groupId>org.jboss.logging</groupId>
			<artifactId>jboss-logging</artifactId>
			<version>3.1.0.GA</version>
		</dependency>
		<dependency>
			<groupId>org.jboss.spec.javax.transaction</groupId>
			<artifactId>jboss-transaction-api_1.2_spec</artifactId>
			<version>1.0.0.Final</version>
		</dependency>

	</dependencies>

	<build>
		<plugins>
			<!-- maven 编译插件 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.5.1</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<encoding>UTF-8</encoding>
					<compilerVersion>1.3</compilerVersion>
				</configuration>
			</plugin>
			
		</plugins>

		<!-- 最终的项目名称 -->
		<finalName>MovieHub</finalName>
	</build>
</project>

 

三.web.xml的配置

       web.xml主要配置spring、springmvc配置文件的路径,如果项目需要也可以配置各种拦截器监听器等等。

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

	<display-name>MovieHub</display-name>

	<!-- Spring应用上下文, 理解层次化的ApplicationContext -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/configs/spring/beans.xml</param-value>
	</context-param>

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- DispatcherServlet, Spring MVC的核心 -->
	<servlet>
		<servlet-name>mvc-dispatcher</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- DispatcherServlet对应的上下文配置, 默认为/WEB-INF/$servlet-name$-servlet.xml -->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/configs/spring/mvc-dispatcher-servlet.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>mvc-dispatcher</servlet-name>
		<!-- mvc-dispatcher拦截所有的请求 -->
		<url-pattern>/</url-pattern>
	</servlet-mapping>

</web-app>

 

四.Spring的配置

  由于使用注解,所以spring的配置beans.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: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/context
       http://www.springframework.org/schema/context/spring-context-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/aop
       http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"
       default-autowire="byName">
   
   	<context:component-scan base-package="com">
	</context:component-scan>
   
</beans>

 

五.Springmvc的配置

  springmvc的配置同样需配置注解驱动,同时,静态资源的映射、模板引擎等配置也在mvc-dispatcher-servlet.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">

	<!-- 本配置文件是工名为mvc-dispatcher的DispatcherServlet使用,提供其相关的Spring MVC配置 -->

	<!-- 启用Spring基于annotation的DI, 使用户可以在Spring MVC中使用Spring的强大功能。 激活 @Required 
		@Autowired,JSR 250‘s @PostConstruct, @PreDestroy and @Resource 等标注 -->
	<context:annotation-config />

    <!--启用mvc注解-->
    <mvc:annotation-driven/>

	<!-- DispatcherServlet上下文, 只管理@Controller类型的bean, 忽略其他型的bean, 如@Service -->
	<context:component-scan base-package="com">
		<context:include-filter type="annotation"
			expression="org.springframework.stereotype.Controller" />
	</context:component-scan>

	<!-- 扩充了注解驱动,可以将请求参数绑定到控制器参数 -->
	<mvc:annotation-driven />

	<!--静态资源映射-->

	<mvc:resources mapping="/rs-plugin/**" location="/static/rs-plugin/" />
	<mvc:resources mapping="/images/**" location="/static/images/" />
	<mvc:resources mapping="/css/**" location="/static/css/" />
	<mvc:resources mapping="/js/**" location="/static/js/" />
	<mvc:resources mapping="/fonts/**" location="/static/fonts/" />
	<mvc:resources mapping="/favicon.ico" location="/static/favicon.ico" />

	<!--模板引擎-->
	<bean id="templateResolver"
		  class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">
		<property name="prefix" value="http://www.mamicode.com/WEB-INF/templates/" />
		<property name="suffix" value="http://www.mamicode.com/.html" />
		<!-- HTML is the default value, added here for the sake of clarity.          -->
		<property name="templateMode" value="http://www.mamicode.com/HTML" />
		<!-- Template cache is true by default. Set to false if you want             -->
		<!-- templates to be automatically updated when modified.                    -->

		<!--缓存-->
		<!--<property name="cacheable" value="http://www.mamicode.com/true" />-->


		<property name="characterEncoding" value="http://www.mamicode.com/UTF-8"/>
	</bean>

	<!-- SpringTemplateEngine automatically applies SpringStandardDialect and      -->
	<!-- enables Spring‘s own MessageSource message resolution mechanisms.         -->
	<bean id="templateEngine"
		  class="org.thymeleaf.spring4.SpringTemplateEngine">
		<property name="templateResolver" ref="templateResolver" />
		<property name="enableSpringELCompiler" value="http://www.mamicode.com/true" />
	</bean>

	<bean id="viewResolver" class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
		<property name="templateEngine" ref="templateEngine" />
		<property name="characterEncoding" value="http://www.mamicode.com/UTF-8"/>
	</bean>

</beans>

 

六.Hibernate的配置

  由于都是使用注解,所以hibernate的配置也格外简单,只需要配置各种属性,包括数据库用户名密码、url、方言等,还有映射的实体类等就可以了,其他交由注解搞定。

<?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="connection.username">root</property>
    	<property name="connection.password"></property>
    	<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    	<property name="connection.url">jdbc:mysql:///movie?useUnicode=true&characterEncoding=UTF-8</property>
    	<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    	
    	<property name="show_sql">true</property>
    	<property name="format_sql">true</property>
    	<property name="hbm2ddl.auto">update</property>
    	
    	<!-- 映射实体类 -->
        <mapping class="com.DzGeeks.repository.entity.Actor" />
        <mapping class="com.DzGeeks.repository.entity.Address" />
        <mapping class="com.DzGeeks.repository.entity.Cinema" />
        <mapping class="com.DzGeeks.repository.entity.Film" />
        <mapping class="com.DzGeeks.repository.entity.FilmType" />
        <mapping class="com.DzGeeks.repository.entity.Order" />
        <mapping class="com.DzGeeks.repository.entity.OrderItem" />
        <mapping class="com.DzGeeks.repository.entity.PlaySession" />
        <mapping class="com.DzGeeks.repository.entity.Seat" />
        
    </session-factory>
</hibernate-configuration>

 

Spring+Springmvc+Hibernate环境搭建与配置