怎么搭建springmvc和mybatis

作者&投稿:寸眉 (若有异议请与网页底部的电邮联系)
~ SpringMVC+MyBatis+Freemarker 简单框架搭建(一)

一、开发环境:
Eclipse、Tomcat、SVN等请参见如下的帖子,很详细了。
http://www.iteye.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的配置。


myeclipse10怎么搭建spring框架
在MyEclipse中快速配置spring框架的步骤如下:1、建工程; File -> New -> Project ->Web Project,"ProjectName":MySpringTest,然后"Finish";2、导入spring包; 选中MySpringTest,右击,MyEclipse-> Add Spring Capabilities……,都默认即可;3、建立项目所需类; (1)、接口Action:(MySpri...

如何搭建spring mvc + hibrante框架
WebRoot:前端根目录,后续的jsp\/html\/js\/css,网站配置文件,lib需要在这里创建 到这一步,我们就完成了项目的基本创建,然后我们需要发布到tomcat,看看我们的项目是否构建成功,右键该项目名 选择Run as,添加到tomcat里,具体操作如图所示 接下来,当你看到控制台如下信息的时候,就说明项目启动,成功哦...

springboot搭建控制台项目
本篇文章将介绍如何使用springboot搭建一个非web项目,即控制台项目。 springboot是一个快速构建微服务的框架,几乎是傻瓜式的一键生成项目。我们知道用它实现web服务很方便,有时我们想要实现非web项目,任务跑完之后就结束运行。经过查阅官方文档,springboot也提供了该方法。下面进入正题。 划重点:不能引入spring-boot-sta...

SpringCloud微服务实战——搭建企业级开发框架(三十八):搭建ELK日 ...
搭建企业级开发框架:构建ELK日志采集与分析系统(实践篇三十八)在微服务架构中,日志分析系统是性能监控和问题定位的基石。Elasticsearch(存储)、Logstash(处理)和Kibana(展示)组成的ELK堆栈,配合Kafka处理高并发场景下的日志增长,Filebeat作为高效日志采集器,使得整个系统无缝协作。我们推荐使用以下版本:...

Spring Boot 搭建的一个企业级快速开发脚手架
Spring Boot 搭建的一个企业级快速开发脚手架BootDo 是高效率,低封装,面向学习型,面向微服的开源 Java EE 开发框架。 BootDo 是在 SpringBoot 基础上搭建的一个 Java 基础开发平台,MyBatis 为数据访问层,ApacheShiro 为权限授权层,Ehcahe 对常用数据进行缓存。BootDo 主要定位于后台管理系统学习交流,已内置后台管理系...

请问下Ecplise如何搭建hibernate,spring环境。。。具体一步一步怎么...
①官网下载spring、hibernate包 ②选取用到jar HIbernate jar(版本参考你自己的):antlr-2.7.7.jar classmate-0.5.4.jar commons-collections-3.2.1.jar dom4j-1.6.1.jar hibernate-core-4.0.0.CR5.jar hibernate-jpa-2.0-api-1.0.1.Final.jar jandex-1.0.3.Final.jar javassist-3....

SpringCloud微服务实战——搭建企业级开发框架(十九):Gateway使用knife4j...
具体实现代码如下:通过访问gitegg-gateway服务地址(http:\/\/127.0.0.1\/doc.html),可以直观地看到聚合后的文档展示。本篇内容的源代码已放置在开源项目gitee.com\/wmz1930\/GitEg...的chapter-19分支中。GitEgg-Cloud是基于SpringCloud整合搭建的企业级微服务应用开发框架,旨在提供一站式解决方案,帮助...

SpringCloud微服务实战——搭建企业级开发框架(三十三):整合Skywalk...
启动微服务后,通过访问127.0.0.1:8080\/查看拓扑图以了解微服务之间的关系。对于需要搭建基于SpringCloud的企业级微服务应用开发框架,可参考GitEgg-Cloud项目。该项目提供了基于SpringCloud的微服务应用开发框架,适用于企业级应用开发。Gitee: [GitEgg-Cloud Gitee地址]GitHub: [GitEgg-Cloud GitHub地址]

手把手教你用Spring Boot搭建AI原生应用
作者 | 文心智能体平台 本文旨在引导快速构建AI原生应用,Spring AI提供了全面的核心功能,包括对话模型、提示词模板、Function Calling、结构化输出、图片生成、向量化和向量数据库等。这些功能旨在简化AI应用程序的开发,让AI成为应用开发的自然组成部分,而非附加。1. AI原生应用构建 AI原生应用是将AI...

SpringBoot + ES基本项目搭建实例
之前一直没有写ES相关的博文,现在开始补课,预计5-6篇博文将es的使用姿势展示给各位小伙伴;本文将作为es结合springboot的第一篇博文,基本项目环境搭建 本项目借助 SpringBoot 2.2.1.RELEASE + maven 3.5.3 + IDEA 进行开发 开一个web服务用于测试 配置文件application.yml,注意下面的配置...

吴堡县17122798160: 如何搭建spring mvc框架 -
亓龚环磷: 方法/步骤1第一步创建项目,打开myEclipse,点击工具栏“File”,选择“New”选项,在选择“Web Project”. 2点击完创建web项目后,出现如图所示的信息,需要输入红框标识的项目名,选择好j2ee specification lever,选择5.0以上...

吴堡县17122798160: 如何搭建spring mvc spring hibernate 框架 -
亓龚环磷: 一、Spring MVC环境搭建:(Spring 2.5.6 + Hibernate 3.2.0) 1. jar包引入 Spring 2.5.6:spring.jar、spring-webmvc.jar、commons-logging.jar、cglib-nodep-2.1_3.jar Hibernate 3.6.8:hibernate3.jar、hibernate-jpa-2.0-api-1.0.1.Final.jar、antlr-2....

吴堡县17122798160: 如何搭建springmvc和mybatis的框架 -
亓龚环磷: 使用SSM(Spring、SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没有记录SSM整合的过程,这次刚刚好基于自己的一个小项目重新搭建了一次,而且比项目搭建的要更好一些.以前解决问题的过程和方法并没有及时记录,以后在自己的小项目中遇到我再整理分享一下.这次,先说说三大框架整合过程.个人认为使用框架并不是很难,关键要理解其思想,这对于我们提高编程水平很有帮助.不过,如果用都不会,谈思想就变成纸上谈兵了!!!先技术,再思想.实践出真知.

吴堡县17122798160: 如何搭建springmvc+mybatis+mysql+bootstrap的框架 -
亓龚环磷: 选择框架你要知道自己的目的,是要做个什么东西,把框架层划分好啊.你提供的这几个框架,dao层, 也就是数据持久层, 用mybatis 数据库, 用mysql oracle, 这两个一般选择一个就好了吧 bootstrap, 我没用过, 不过这个和html5都是用来写页面的 spring用来整合框架, 实现层次间的松耦合 还有控制层, 用来实现控制请求转发, 如果你不想用springmvc的话, 你可以用struts或者直接servlet

吴堡县17122798160: 如何搭建springMVC开发环境 -
亓龚环磷: 1、去spring官网下载框架的包 下载依赖包:spring-framework-3.0.5.RELEASE-dependencies.zip 注意官网上3.0.3版本以后同版本依赖包不提供下载2、导入所需jar包 引入dist目录下除了下面三个其余所有包 org.springframework.web.struts-3.1.0...

吴堡县17122798160: eclipse如何搭建springmvc +mybatis -
亓龚环磷: SpringMVC+MyBatis+Freemarker 简单框架搭建(一)一、开发环境: Eclipse、Tomcat、SVN等请参见如下的帖子,很详细了. http://www.iteye.com/topic/982182 svn和maven插件的安...

吴堡县17122798160: 如何搭建一个spring mvc 整合mybatis -
亓龚环磷: springMVC是前后端交互框架即实现了一个传统意义上的mvc模型,封装了model层

吴堡县17122798160: 在myeclipse中怎么创建springmvc的配置文件 -
亓龚环磷: 本经验教你一步一步使用MAVEN搭建springmvc开发环境,最后一步有最终的项目结构图 方法/步骤 使用eclipse新建maven项目 生成的目录没有src/main/java,直接右键,NEW-FOLDER,会自动变成SOURCE FOLDER 步骤阅读 修改pom文件<...

吴堡县17122798160: 如何用maven创建springmvc -
亓龚环磷: 1.新建项目选择maven项目2.默认,下一步3.选择maven-archetype-webapp,其他保持默认即可4.如下填写完成后,点击完成即可5.创建完成后的maven项目结构如下其中index.jsp报错,错误信息:Multiple annotations found at this line: - ...

吴堡县17122798160: 如何开始创建第一个基于Spring MVC的Controller -
亓龚环磷: 以下示例怎么开始创建我们的第一个Spring MVC控制器Controller1.新建一个java类,命名为:MyFirstController,包含以下代码,其中有2个函数,分别返回jsp页面和一个json字符串.2.新建一个jsp页面,命名为: /modules/demo/myFirstJsp.jsp ...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网