SpringMvc与Mybatis整合
1.目的
学习SpringMvc与Mybatis怎样整合在一起。
2.环境准备
1) jdk版本:jdk1.7或jdk1.8
2) IDE:eclipse
3) Tomcat:Apache Tomcat v7.0
4) 数据库:mysql及navicat for mysql
3.整合
3.1.新建一个web项目
注意:这里选择Dynamic Web Module 2.5 ,因为2.5是主流,默认在eclipse的WebContent \WEB-INF\目录下创建l的,而3.0则默认没有l文件
3.2.添加整合所需的jar包
注:所有的包已经放到文件jar里面了
3.l配置
3.3.1.代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="/2001/XMLSchema-instance /schema/cache/springmodules-cache.xsd /schema/cache/springmodules-ehcache.xsd"
xmlns="java.sun/xml/ns/javaee"
xsi:schemaLocation="java.sun/xml/ns/javaee java.sun/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>SpringMvc-Mybatis</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>l</param-value>
</context-param>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<async-supported>true</async-supported>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener> <listener-class>org.t.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>l</param-value>
param name </init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
3.3.2.代码详解:
1) Spring配置文件:l(下面会介绍)
2) 编码过滤器:选择UTF-8,解决中文乱码问题
3) Spring:org.t.ContextLoaderListener
4) 添加对springmvc的支持
SpringMVC配置文件:l(下面会介绍)
3.l配置
3.4.1.代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xmlns:p="/schema/p"
xmlns:aop="/schema/aop"
xmlns:context="/schema/context"
xmlns:jee="/schema/jee"
xmlns:tx="/schema/tx"
xsi:schemaLocation="
/schema/aop /schema/aop/spring-aop-4.0.xsd
/schema/beans /schema/beans/spring-beans-4.0.xsd
/schema/context /schema/context/spring-context-4.0.xsd
/schema/jee /schema/jee/spring-jee-4.0.xsd
/schema/tx /schema/tx/spring-tx-4.0.xsd">
<context:component-scan base-package="ller" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
3.4.2.代码详解:
1) 使用注解的包,包括子集:自动扫描"ller"包下的所有类,作为controller层的类
2) 视图解析器:controller层返回的数据会解析到相应的 .jsp里面
3.l配置
3.5.1.代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xmlns:p="/schema/p"
xmlns:aop="/schema/aop"
xmlns:context="/schema/context"
xmlns:jee="/schema/jee"
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论