maven+Spring+SpringMVC+Hibernate快速搭建

目录结构:

spring-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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 对静态文件的拦截处理方法 -->
    <!-- <mvc:default-servlet-handler /> -->
     <!-- 启用spring mvc 注解 -->
    <context:annotation-config />

    <!-- 设置使用注解的类所在的jar包 -->
    <context:component-scan base-package="com.geostar.gfstack.operationcenter.*.action,com.geostar.gfstack.operationcenter.*.*.action,com.geostar.gfstack.operationcenter.*.*.*.action,com.geostar.gfstack.operationcenter.*.*.*.*.action"></context:component-scan>
        <!-- 自动注入处理器映射器和适配器 和springmvc拦截器-->
    <mvc:annotation-driven></mvc:annotation-driven>

    <!-- 完成请求和注解POJO的映射
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
    -->

    <!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/platform/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <!-- 上传文件MultipartResolver处理器 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="UTF-8"></property>
        <property name="maxUploadSize" value="1024000"></property>
    </bean>

 </beans>

applicationContext-database.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: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.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
          destroy-method="close">
        <property name="driverClassName">
            <value>${jdbc.driver}</value>
        </property>
        <property name="url">
            <value>${jdbc.url}</value>
        </property>
        <property name="username">
            <value>${jdbc.username}</value>
        </property>
        <property name="password">
            <value>${jdbc.password}</value>
        </property>
        <property name="maxActive">
            <value>${jdbc.maxActive}</value>
        </property>
        <property name="maxIdle">
            <value>${jdbc.maxIdle}</value>
        </property>
        <property name="maxWait">
            <value>${jdbc.maxWait}</value>
        </property>
        <property name="logAbandoned">
            <value>${jdbc.logAbandoned}</value>
        </property>
        <property name="removeAbandoned">
            <value>${jdbc.removeAbandoned}</value>
        </property>
        <property name="removeAbandonedTimeout">
            <value>${jdbc.removeAbandonedTimeout}</value>
        </property>
        <property name="testWhileIdle">
            <value>${jdbc.testWhileIdle}</value>
        </property>
        <property name="timeBetweenEvictionRunsMillis">
            <value>${jdbc.timeBetweenEvictionRunsMillis}</value>
        </property>
        <property name="validationQuery">
            <value>${jdbc.validationQuery}</value>
        </property>
    </bean>

    <!-- Hibernate配置 -->
    <bean id="sessionFactory"
          class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource"/>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">
                    ${hibernate.dialect}
                </prop>
                <prop key="hibernate.show_sql">
                    ${hibernate.show_sql}
                </prop>
                <prop key="hibernate.query.factory_class">
                    ${hibernate.query.factory_class}
                </prop>
                <prop key="current_session_context_class">
                    ${current_session_context_class}
                </prop>
                <prop key="hibernate.hbm2ddl.auto">
                    update
                </prop>
            </props>
        </property>
        <property name="packagesToScan">
            <list>
                <!-- 添加解析XML的扫描路径 -->
                <value>com.geostar.gfstack.operationcenter.simple.model</value>
            </list>
        </property>
    </bean>

    <bean id="transactionManager"
          class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
        <property name="globalRollbackOnParticipationFailure" value="false"/>
    </bean>

    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="save*" propagation="REQUIRED"/>
            <tx:method name="delete*" propagation="REQUIRED"/>
            <tx:method name="use*" propagation="REQUIRED"/>
            <tx:method name="freed*" propagation="REQUIRED"/>
            <tx:method name="*" propagation="REQUIRED" read-only="true"/>
        </tx:attributes>
    </tx:advice>

    <aop:config>
        <aop:advisor
                pointcut="execution(* com.geostar.gfstack.operationcenter.*.service.*Service.*(..))"
                advice-ref="txAdvice"/>
        <aop:advisor
                pointcut="execution(* com.geostar.gfstack.operationcenter.*.*.service.*Service.*(..))"
                advice-ref="txAdvice"/>
        <aop:advisor
                pointcut="execution(* com.geostar.gfstack.operationcenter.*.*.*.service.*Service.*(..))"
                advice-ref="txAdvice"/>
        <aop:advisor
                pointcut="execution(* com.geostar.gfstack.operationcenter.*.*.*.*.service.*Service.*(..))"
                advice-ref="txAdvice"/>
    </aop:config>

</beans>

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:context="http://www.springframework.org/schema/context"
       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">

    <context:component-scan base-package="com.geostar.gfstack.operationcenter"/>
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:config/GeoStar.properties</value>
            </list>
        </property>
    </bean>
    <import resource="classpath:config/spring/applicationContext-database.xml"/>

</beans>

GeoStar.properties

#MySQL|Oracle
#jdbc.db.type=Oracle
jdbc.db.type=Mysql

#apache dbcp jdbc
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc\:mysql\://db-exte.gfstack.geo\:3306/operationcenter
jdbc.username=gfstack
jdbc.password=gfstack

jdbc.maxActive=10
jdbc.maxIdle=3
jdbc.maxWait=500
jdbc.logAbandoned=false
jdbc.removeAbandoned=true
jdbc.removeAbandonedTimeout=5000
jdbc.timeBetweenEvictionRunsMillis=120000
jdbc.testWhileIdle=true
jdbc.validationQuery=select 1 from dual

hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.show_sql=false
hibernate.query.factory_class=org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory
current_session_context_class=thread

log4j.properties

log4j.rootLogger=INFO,CONSOLE,APPLOG

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.Threshold=INFO
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p [%t] %c %x - %m%n

log4j.appender.APPLOG=org.apache.log4j.DailyRollingFileAppender
log4j.appender.APPLOG.File=${catalina.home}/logs/geostack-simple.log
log4j.appender.APPLOG.DatePattern=‘.‘yyyy-MM-dd
log4j.appender.APPLOG.Append=true
log4j.appender.APPLOG.layout=org.apache.log4j.PatternLayout
log4j.appender.APPLOG.layout.ConversionPattern=[%d{yyyy-MM-dd hh:mm:ss}]-[%5p] [%l] %m%n

log4j.logger.com.geostar.gfstack=ALL,CONSOLE,APPLOG
log4j.logger.com.geostar.geostack=ALL,CONSOLE,APPLOG
log4j.additivity.com.geostar.gfstack=false
log4j.additivity.com.geostar.geostack=false

web.xml

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

    <display-name>geostack-dispatch</display-name>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:config/spring/applicationContext.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>
        <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>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:config/spring/spring-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

</web-app>

SimpleAction.java

package com.geostar.gfstack.operationcenter.simple.action;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@Controller
@RequestMapping("simpleAction")
public class SimpleAction {

    @ResponseBody
    @RequestMapping("test")
    public void test(){
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html");
        response.setHeader("Content-type", "text/html;charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html");
        response.setHeader("Access-Control-Allow-Origin", "*");
        PrintWriter write = null;
        try {
            write = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        write.print("test");
    }

}
时间: 2024-10-10 20:50:24

maven+Spring+SpringMVC+Hibernate快速搭建的相关文章

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

这里给出JavaWeb项目Spring+Springmvc+Hibernate框架环境的搭建与配置,方便以后快速创建项目.下面是以本人的一个项目为例给出的配置,不同项目具体的结构都是可以按一下结构完成的,至于配置内容则需要按照具体的项目需求更改部分内容.需要注意的是,本项目是以使用注解为前提完成的项目. 一.项目目录 首先说明一下,本项目是maven项目,采用Spring+Springmvc+Hibernate的框架,前端模板引擎采用thymeleaf,html代码存放在下面结构的templat

Maven+Spring+SpringMVC+MyBatis框架搭建

看了一段时间Android,学了学C++,搭个SSM的框架复习复习老本行. 原来的SSH——Struts,spring,hibernate,逐渐被现在的SSM取代,当然了,还有各有优缺点的. 搭的这个框架中的SpringMVC并不是返回页面,而是返回json数据,在前端的js中处理页面的展现,我是为了让Android客户端能够访问SpringMVC的controller,并给Android客户端返回json数据考虑的. 一.还是一样的,要先看maven中都引入什么包: Java代码 复制代码 收

spring+springmvc+hibernate 框架搭建

1.新建web项目,将所需jar包放到 lib 目录下 2.配置web.xml 配置文件 1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLo

本地跑起来!IntellliJ IDEA+maven+spring+springMVC+tomcat+mongodb搭建本地开发环境

在前面搭建的环境上加上mongodb配置文件,pom.xml增加mongo相关依赖,增加一些代码即可搭建成功. 1.增加mongdb-context.xml和mongodb.properties mongdb-context.xml 1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans&quo

用Eclipse 搭建一个Maven Spring SpringMVC 项目

1: 先创建一个maven web  项目: 可以参照之前的文章:  用Maven 创建一个 简单的 JavaWeb 项目 创建好之后的目录是这样的; 2: 先配置maven  修改pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="htt

spring+springmvc+hibernate架构、maven分模块开发例子小项目案例

maven分模块开发例子小项目案例 spring+springmvc+hibernate架构 以用户管理做测试,分dao,sevices,web层,分模块开发测试!因时间关系,只测查询成功,其他的准备在ext上做个完整的案例来的,可惜最近时间很紧, 高级部分也没做测试,比如建私服,其他常用插件测试之类的,等用时间了我做个完整ext前端和maven 完整的例子出来,在分享吧! 不过目前这些撑握了,在项目中做开发是没有问题的,其他高级部分是架构师所做的. 之前我有的资源都加上了积分,有些博友向我要,

spring+springmvc+hibernate架构、maven分模块开发样例小项目案例

maven分模块开发样例小项目案例 spring+springmvc+hibernate架构 以用户管理做測试,分dao,sevices,web层,分模块开发測试!因时间关系.仅仅測查询成功.其它的准备在ext上做个完整的案例来的,可惜近期时间非常紧. 高级部分也没做測试,比方建私服,其它经常使用插件測试之类的,等用时间了我做个完整ext前端和maven 完整的样例出来,在分享吧. 只是眼下这些撑握了.在项目中做开发是没有问题的,其它高级部分是架构师所做的. 之前我有的资源都加上了积分,有些博友

springMVC系列之(四) spring+springMVC+hibernate 三大框架整合(转)

https://blog.csdn.net/lishehe/article/details/38356261 基于spring+springmvc+hibernate的maven项目搭建 https://blog.csdn.net/fancy_t/article/details/70989754 原文地址:https://www.cnblogs.com/1987721594zy/p/9708567.html

Spring mvc 源码 和 Spring+springmvc+hibernate整合源码

本来想着再写下spring+springmvc+hibernate整合的文章 暂时就不写了 直接附上源码,有需要的直接下载看吧.还有前面文章中的springmvc源码  前面文章只是简单的说了下 搭建环境 访问页面成功.这个源码中有模拟的增删改查和文件上传,转换json 实体类验证等功能.有问题可以留言我. http://download.csdn.net/detail/qinyanbin123/8631175 springmvc源码下载地址 http://download.csdn.net/d