设为首页 收藏本站
查看: 855|回复: 0

[经验分享] SpringMVC+MyBatis+Freemarker 简单框架搭建(一)

[复制链接]

尚未签到

发表于 2016-11-27 09:04:42 | 显示全部楼层 |阅读模式
一、开发环境:
  Eclipse、Tomcat、SVN等请参见如下的帖子,很详细了。
  http://www.iyunv.com/topic/982182
  svn和maven插件的安装:
  1、先安装gef插件
  地址:http://download.eclipse.org/tools/gef/updates/interim/
  2、安装svn插件
  地址:http://subclipse.tigris.org/update_1.6.x
  3、maven插件
  m2eclipse-core Update 地址: http://m2eclipse.sonatype.org/sites/m2e
  m2eclipse-extras Update 地市: http://m2eclipse.sonatype.org/sites/m2e-extras
  4、安装可能出现的问题
   直接在线安装maven2 会出现依赖插件找不到的问题,无法安装。必须先安装gef 插件后才能安  装m2eclipse-core 插件,然而安装m2eclipse-extras
插件又依赖subclipse 插件。所以,三个插  件的正确的安装顺序是:gef插件 subclipse插件 m2eclipse插件

二、web.xml 和 applicationContext.xml 配置
  1、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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- Spring MVC 配置 -->
<servlet>
<servlet-name>springServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:contextConfigLocation-springService.xml</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 事件监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>   
<!-- shiro 权限控制的过滤器 -->

<!-- Spring 刷新Introspector防止内存泄露 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<!-- session超时定义,单位为分钟 -->
<session-config>
<session-timeout>20</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<!-- 设置servlet编码开始 -->
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GB2312</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/</url-pattern>
</filter-mapping>
<!-- 设置servlet编码结束 -->


<display-name>Archetype Created Web Application</display-name>
</web-app>

    <context-param>节点和<listener>节点是web项目启动时最先读取的两个节点,所以这两个节点里面所配置的内容是web项目启动时最先加载的部分。
  <context-param>节点中配置的applicationContext.xml里面主要是数据库的配置信息,以及事务等等
  <listener>节点配置的是web请求的监听器
  2、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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
">
<!-- 自动注解除Controller以外的Component -->
<context:component-scan base-package="com.weiluo.example">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 此配置可以让我们以${xxx}的形式来读取property.properties里面的信息 -->
<!-- <context:property-placeholder/> -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url"
value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<!-- Connection Pooling Info -->
<property name="maxActive" value="${dbcp.maxActive}" />
<property name="maxIdle" value="${dbcp.maxIdle}" />
<property name="defaultAutoCommit" value="false" />
<!-- 连接Idle一个小时后超时 -->
<property name="timeBetweenEvictionRunsMillis" value="360000" />
<property name="minEvictableIdleTimeMillis" value="360000" />
</bean>
<!-- 配置SessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
p:dataSource-ref = "dataSource"
p:configLocation = "classpath:sqlMapConfig.xml" />
<!-- 采用spring与mybatis整合的第二种方法 -->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0"  ref="sqlSessionFactory" />
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref = "dataSource" />
<!-- MapperScanner配置,自动搜索mapper里面的对象,并注入 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
p:basePackage = "com.weiluo.example.entity" />
<!-- 启动Spring注解事务 -->
<tx:annotation-driven/>
</beans>
   3、web.xml中的<servlet>节点配置的是与请求处理相关的一些内容,主要是 url路由处理器,视图解析器,等等,如下
  

<beans xmlns="http://www.springframework.org/schema/beans"
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"
xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<!-- url映射拦截器 -->
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="order" value="1" />
</bean>
<!-- 配置数据格式转换器 -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonHttpMessageConverter" />
</list>
</property>
</bean>
<bean id="jsonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
</bean>
<!-- 开启controller注解支持 -->
<!-- 注:如果base-package=cn.javass 则注解事务不起作用 TODO 读源码 -->
<context:component-scan base-package="com.weiluo.example.controller">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 因为web-inf目录下面的静态资源文件是不能直接通过目录过去的,所以需要特殊声明-->
<mvc:resources location="/static/" mapping="/static/**" />
<!-- 视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="cache" value="true" />
<property name="prefix" value="" />
<property name="suffix" value=".html" />
<property name="exposeSpringMacroHelpers" value="true" />
<property name="exposeRequestAttributes" value="true" />
<property name="exposeSessionAttributes" value="true" />
<property name="requestContextAttribute" value="rc" />
<property name="contentType" value="text/html;charset=GB2312" />
<property name="order" value="0"/>  
</bean>

<!-- 自定义的freemarker标签 -->
<bean id="blockDirective"
class="com.weiluo.example.freemarker.directive.BlockDirective" />
<bean id="extendsDirective"
class="com.weiluo.example.freemarker.directive.ExtendsDirective" />
<bean id="overrideDirective"
class="com.weiluo.example.freemarker.directive.OverrideDirective" />
<bean id="superDirective"
class="com.weiluo.example.freemarker.directive.SuperDirective" />
<!-- freemarker的配置项 -->
<bean id="freemarkerConfig"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/views/" />
<property name="freemarkerSettings">
<props>
<prop key="template_update_delay">5</prop>
<prop key="defaultEncoding">GB2312</prop>
<prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
<prop key="date_format">yyyy-MM-dd</prop>
<prop key="time_format">HH:mm:ss</prop>
<prop key="number_format">0.######</prop>
<prop key="whitespace_stripping">true</prop>
</props>
</property>
<property name="freemarkerVariables">
<map>
<entry key="extends" value-ref="extendsDirective"></entry>
<entry key="override" value-ref="overrideDirective"></entry>
<entry key="block" value-ref="blockDirective"></entry>
<entry key="super" value-ref="superDirective"></entry>
</map>
</property>
</bean>

</beans>
  4、另外,一些常常变化的信息习惯于存放在application.properties文件里面,如:
  

#oracle version database setting
database=sqlserver
#jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.driver=net.sourceforge.jtds.jdbc.Driver
#jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:jtds:sqlserver://127.0.0.1:1433;databaseName=SpringMyBatisExample
#jdbc.url=jdbc:mysql://localhost:3306/springmvcdemo
jdbc.username=sa
jdbc.password=sasa
#dbcp settings
dbcp.maxIdle=5
dbcp.maxActive=20
  到这里,一个spring mvc的基本框架就已经搭建起来了。
  后续再完成数据库mybatis的配置以及freemarker的配置。
  欢迎大家提出宝贵意见~~~

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-306011-1-1.html 上篇帖子: MyBatis在非Spring环境下第三方DataSource设置 下篇帖子: spring/struts/mybatis搭建开发环境
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表