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 ...
随机推荐
- c# ffmpeg常用参数
c# ffmpeg常用参数 转换文件格式的同时抓缩微图: ffmpeg -i "test.avi" -y -f image2 -ss 8 -t 0.001 -s 350x240 ...
- Linux下安装Python-3.3.2【转】
# 下载最新版本 cd /usr/local/src/ sudo wget http://www.python.org/ftp/python/3.3.2/Python-3.3.2.tar.bz2 su ...
- Linux下scp的用法
scp就是secure copy,一个在linux下用来进行远程拷贝文件的命令.有时我们需要获得远程服务器上的某个文件,该服务器既没有配置ftp服务器,也没有做共享,无法通过常规途径获得文件时,只需要 ...
- leetcode:Reverse Bits
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- 浅析JavaScript函数的参数
ECAMScript函数不介意传递进来多少个参数,也不介意传递的参数的类型,即使定义的函数只接受两个参数,当调用该函数时没有传递参数,甚至传递了三个参数等等都无所谓,这是因为在ECAMScript中参 ...
- Intent Flag介绍 intent.addFlags()
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); FLAG_ACTIVITY_BROUGHT_TO_FRONT 这个标志一般不是由程序代码设置的 ...
- html_表单
一.表单框架简介 表单是提供让读者在网页上输入,勾选和选取数据,以便提交给服务器数据库的工具.(用于将信息提交给服务器) <form>...</form>标记 作用:用于创建一 ...
- EF CodeFirst-----简单demo示例
关于EF CodeFirst的文章院子里有很多的学习资料,但大多数都是一些讲Model通过特性或是Fluent API与数据库之间形成映射的关系,看了相关的文章之后,Model如何映射到数据还是有些迷 ...
- 【第二篇】.NET用NPOI读取Excel表格并在页面预览
博主用的是npoi2.0.1,支持.xls也支持.xlsx 直接上代码吧. <table class="table table-bordered table-striped" ...
- C#中的lock关键字;就是lock住一个大家都共同访问的(静态static的)东东就行了
public class ChatService : IChat //继承IChat接口或者说IChat的实现类 { //定义一个静态对象用于线程部份代码块的锁定,用于lock操作 private s ...