Spring MVC + Spriing + MyBatis整合,写给新人
开发环境:
开发工具:MyEclipse 8.6
数据库:MySQL
操作系统:WIN8.1
Jar包:
Spirng和SpringMVC版本:3.2.9
MyBatis版本:3.2.8
其他关联Jar包如图:

---------------------------------------------------------------------------------
OK , 准备的东西齐全了 。这就动手,先用MyEclipse新建一个网站工程,然后将所有的Jar包放到WebRoot/WEB-INF/lib文件夹下面。
首先配置web.xml文件,增加Spring的支持
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationDataSource.xml</param-value>
</context-param>
接着在src下建立
applicationDataSource.xml
jdbc.properties文件
mybatis-config.xml
以上三个文件
mybatis-config.xml 文件自然就是MyBatis的配置了,其实只用到了拦截器,后面我们会看到。jdbc.properties文件里面配置具体的数据库信息,在applicationDataSource.xml文件中读取
先看jdbc.properties的内容:

这里简单配置了数据库的一些信息,我们现在看applicationDataSource.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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
">
<!-- 这里我用了aop:aspectj-autoproxy,如果使用aop有对类的配置的话就派上用场了。个人习惯而已 -->
<aop:aspectj-autoproxy proxy-target-class="true"/> <!-- 载入jdbc.properties文件 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list><value>classpath:jdbc.properties</value></list>
</property>
</bean> <!-- 配置数据源 ,注意value部分的配置写法 -->
<bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driverClassName}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<!-- 数据库连接池保持的最小连接数 -->
<property name="minIdle" value="${minIdle}" />
<!-- 数据库连接池保持的最大连接数 -->
<property name="maxIdle" value="${maxIdle}" />
<!--
当数据库连接因为某种原因断掉之后,再重新从连接池中拿另外一个连接时实际上这个连接可能
已经无效,所以为了确保所拿到的连接全都有效需要在获取连接,返回连接以及连接空闲时进行 有效性验证
下面3个设置为ture时进行验证,默认为false
-->
<!-- 取得连接时是否进行有效性验证 -->
<property name="testOnBorrow" value="true" />
<!-- 返回连接时是否进行有效性验证 -->
<property name="testOnReturn" value="true" />
<!-- 连接空闲时是否进行有效性验证 -->
<property name="testWhileIdle" value="true" /> </bean>
<!-- 配置sqlSessionFactory ,注意mapperLocations的配置,这个路径里面存放了MyBatis的Mapper文件,configLocation则加载了mybatis-config.xml文件,不需要的可以不配置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="dataSource" ref="datasource"/>
<property name="mapperLocations">
<list>
<value>classpath:org/springmvc_demo/mapper/*.xml</value>
</list>
</property>
</bean> <!-- JDBC的事务管理 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="datasource"/>
</bean> <!-- 事务管理对应的方法规则 -->
<tx:advice transaction-manager="transactionManager" id="txAdvice">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="modify*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="query*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice> <aop:config>
<!-- 这里的配置应用事务管理的包名我提前准备好了,根据自己的实际情况修改包名,但是事物管理还是建议在业务逻辑层控制 -->
<aop:pointcut expression="execution(* org.springmvc_demo.service.impl.*.*(..))" id="transactionAop"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="transactionAop"/>
</aop:config>
<!-- 我们需要一个 sqlSessionTemplate对象,使用看之后的代码配置-->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean> </beans>
Spring的配置到此结束,我们在来到web.xml中增加SpringMVC的配置。
这里配置文件我没有使用默认的路径,而是将springmvc的配置文件放到了src根目录下,<load-on-startup>1</load-on-startup>这个一定要加上。注意url-pattern的配置,不要修改。
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
web.xml文件修改完毕后,我们来看SpringMVC的配置文件的内容
<?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:p="http://www.springframework.org/schema/p"
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-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
"> <!-- 包扫描的配置放在了SpringMVC加载的配置文件中,否则将无法正常工作 -->
<context:component-scan base-package="org.springmvc_demo"/>
<mvc:annotation-driven/> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
</bean>
</beans>
OK ,这里的配置就到此为止,需要其他功能的自行配置,唯一需要注意的是我没有在这里对静态资源做配置,这样的话html,css等静态资源按照上面web.xml中的配置将无法正常使用。所以我们还需要再次返回web.xml增加对静态资源的配置。
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping> <servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping> <servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping> <servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping> <servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping> <servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.png</url-pattern>
</servlet-mapping> <servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.gif</url-pattern>
</servlet-mapping>
加上以上的配置,静态资源就可以正常访问了。
到此为止,SpringMVC +Spring + MyBatis的配置文件就基本配置完毕了。
Spring MVC + Spriing + MyBatis整合,写给新人的更多相关文章
- 转载 Spring、Spring MVC、MyBatis整合文件配置详解
Spring.Spring MVC.MyBatis整合文件配置详解 使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用法最好还是看官方文档. ...
- Spring MVC、MyBatis整合文件配置详解
Spring:http://spring.io/docs MyBatis:http://mybatis.github.io/mybatis-3/ Building a RESTful Web Serv ...
- Mybaits-从零开始-Spring、Spring MVC、MyBatis整合(未万待续)
Spring.Spring MVC.MyBatis整合(未万待续)
- Spring、Spring MVC、MyBatis整合文件配置详解
原文 http://www.cnblogs.com/wxisme/p/4924561.html 主题 MVC模式MyBatisSpring MVC 使用SSM框架做了几个小项目了,感觉还不错是时候总 ...
- 【转】Spring、Spring MVC、MyBatis整合文件配置详解
见:http://www.tuicool.com/articles/eyINveF web.xml的配置 web.xml应该是整个项目最重要的配置文件了,不过servlet3.0中已经支持注解配置方式 ...
- Spring、Spring MVC、MyBatis整合文件配置详解2
使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用法最好还是看官方文档. Spring:http://spring.io/docs MyBatis ...
- Spring、Spring MVC、MyBatis 整合文件配置详解
使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用法最好还是看官方文档. Spring:http://spring.io/docs MyBatis ...
- spring mvc与mybatis整合错误提示
java.lang.AbstractMethodError: org.mybatis.spring.transaction.SpringManagedTransaction.getTimeout()L ...
- spring mvc 和mybatis整合 的异常处理
1.自定义异常信息类 通过构造函数来实现异常信息的接收 public class CustomException extends Exception { //异常信息 private String m ...
随机推荐
- FFmpeg FFmpeg的使用及常用参数
FFmpeg的使用及常用参数 一.下载: 官网:http://ffmpeg.org/ 二.demo: 1 class Program 2 { 3 static void Main(string[] a ...
- (原创)3.2 AddOwner和OverrideMetadata的区别
1 AddOwner和OverrideMetadata 1.1 分析 从源代码上看,AddOwner函数中调用了OverrideMetadata, 并且把本类和依赖属性的哈希值加入到依赖属性的一张哈希 ...
- 二分查找和hash查找
转载:http://blog.csdn.net/feixiaoxing/article/details/6844723 无论是数据库,还是普通的ERP系统,查找功能数据处理的一个基本功能.数据查找并不 ...
- html5 移动端单页面布局
序 移动端的web网页使用的是响应式设计,但一般我们看到的网站页面都是跳转刷新得到的,比如通过点击一个menu后进入到另一个页面 今天来说下是移动端的单页面的布局.单页面就是一切操作和布局都是 ...
- 编写类String的构造函数、析构函数和赋值函数
已知类String的原型为: class String { public: String(const char *str = NULL); // 普通构造函数 String(const Str ...
- JQuery_给元素添加或删除类等以及CSS()方法
一.addClass() - 向被选元素添加一个或多个类 <script src="jquery-1.11.1.min.js"></script> < ...
- 多线程操作(循环往listbox中添加数据)
一.先造一个窗体,其中就开始按钮,暂停按钮,以及listbox文本框 二.当点击开始的时候,数据会无限的往listbox中加,为了防止卡住和提升效率,便造了一个新的线程来执行开始操作 namespac ...
- 在maven项目中使用mybatis-generator-maven-plugin生成mybatis代码
项目整体的目录结构如下: pom.xml如下: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&q ...
- MySQL的性能调优工具:比mysqlreport更方便的tuning-primer.sh
年初的时候收藏过一篇关于mysqlreport的报表解读,和内置的show status,和show variables相比mysqlreport输出一个可读性更好的报表:但Sundry MySQL提 ...
- 【转】QT中QWidget、QDialog及QMainWindow的区别
QWidget类是所有用户界面对象的基类. 窗口部件是用户界面的一个基本单元:它从窗口系统接收鼠标.键盘和其它事件,并且在屏幕上绘制自己.每一个窗口部件都是矩形的,并且它们按Z轴顺序排列.一个窗口部件 ...