前面几篇文章已经讲到了mybatis与spring 的集成。但这个时候,所有的工程还不是web工程,虽然我一直是创建的web 工程。今天将直接用mybatis与Spring mvc 的方式集成起来,源码在本文结尾处下载.主要有以下几个方面的配置
1. web.xml 配置 spring dispatchservlet ,比如为:mvc-dispatcher
2. mvc-dispatcher-servlet.xml 文件配置
3. spring applicationContext.XML文件配置(与数据库相关,与mybatis sqlSessionFaction 整合,扫描所有mybatis mapper 文件等.)
4. 编写controller 类
5. 编写页面代码.
先有个大概映像,整个工程图如下:
1. web.xml 配置 spring dispatchservlet ,比如为:mvc-dispatcher
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>zw-mybatis</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:/config/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener> <listener-class> org.springframework.web.context.ContextCleanupListener </listener-class> </listener> <servlet> <servlet-name>mvc-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
2. 在web.xml 同目录下配置 mvc-dispatcher-servlet.xml 文件,这个文件名前面部分必须与你在web.xml里面配置的DispatcherServlet 的servlet名字对应.其内容为:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> <context:component-scan base-package="com.handly.mybatis.controller" /> <mvc:annotation-driven /> <mvc:resources mapping="/static/**" location="/WEB-INF/static/" /> <mvc:default-servlet-handler /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/WEB-INF/pages/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> </beans>
3. 在源码目录 config 目录下配置 spring 配置文件 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:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" 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/util http://www.springframework.org/schema/util/spring-util-2.0.xsd"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://127.0.0.1:3306/mis4_produce?characterEncoding=utf8" /> <property name="username" value="root" /> <property name="password" value="123456" /> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.handly.mybatis.dao" /> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!--dataSource属性指定要用到的连接池 --> <property name="dataSource" ref="dataSource" /> <!--configLocation属性指定mybatis的核心配置文件 --> <property name="configLocation" value="classpath:/config/Configuration.xml" /> </bean> </beans>
4. 编写 controller 层
package com.handly.mybatis.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import com.handly.mybatis.dao.UserDao; import com.handly.mybatis.model.User; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller @RequestMapping("/user")//请求路径 public class UserController { @Autowired UserDao userDao; @RequestMapping("/list")//请求方法 public ModelAndView listall(HttpServletRequest request,HttpServletResponse response){ List<User> list = userDao.selectUsers("admin"); if(null!=list && list.size()>0){ for(User user: list){ System.out.println(user.getUsername()); System.out.println(user.getDate()); } }else{ System.out.println("list is null;"); } ModelAndView mav=new ModelAndView("user/list");// 访问页面目录 mav.addObject("users",list); return mav; } }
添加jar包:
servlet-api.jar、spring-web.jar、spring-webmvc.jar
5. 页面文件:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <c:forEach items="${users}" var="user"> ${user.id }--${user.username }--${user.id }<br /> </c:forEach> </body> </html>
添加jar包:
jstl-1.2.jar
相关推荐
mybatis实战教程mybatis in action之六与Spring MVC 的集成 mybatis实战教程mybatis in action之七实现mybatis分页源码下载 mybatis实战教程mybatis in action之八mybatis 动态sql语句 mybatis实战教程mybatis in ...
mybatis实战教程(mybatis in action)之六:与Spring MVC 的集成 ..................................... 22 mybatis实战教程(mybatis in action)之七:实现mybatis分页 ............................................ ...
mybatis实战教程mybatis in action之六与Spring MVC 的集成 mybatis实战教程mybatis in action之七实现mybatis分页源码下载 mybatis实战教程mybatis in action之八mybatis 动态sql语句 mybatis实战教程mybatis in ...
使用Java平台,采用SpringMVC+Mybatis等主流框架 数据库:使用免费MYSQL 前端:使用Jquery和Easyui技术.界面清晰简洁,易操作 权限:对菜单,按钮控制.仅展示有权限的菜单和按钮. 拦截:对所有无权限URL进行拦截,防止...
spring MVC+easyUI+mybatis开发网站后台管理系统(源码+sql文件) urls.properties中需要修改为你本地的静态文件的访问路径。 使用Java平台,采用SpringMVC+Mybatis等主流框架. 数据库:使用免费MYSQL. 前端:...
使用Java平台,采用SpringMVC+Mybatis等主流框架 数据库:使用免费MYSQL 前端:使用Jquery和Easyui技术.界面清晰简洁,易操作 权限:对菜单,按钮控制.仅展示有权限的菜单和按钮. 拦截:对所有无权限URL进行拦截,防止...
使用Java平台,采用SpringMVC+Mybatis等主流框架 数据库:使用免费MYSQL 前端:使用Jquery和Easyui技术.界面清晰简洁,易操作 权限:对菜单,按钮控制.仅展示有权限的菜单和按钮. 拦截:对所有无权限URL进行拦截,防止...
如涉及作品内容、版权和其它问题,请在七日内与本人联系,我将在第一时间删除内容! [声明]本站文章版权归原作者所有,内容为作者个人观点,本人只提供参考并不构成任何投资及应用建议。 本人拥有对此声明的最终解释...
使用Java平台,采用SpringMVC+Mybatis等主流框架 数据库:使用免费MYSQL 前端:使用Jquery和Easyui技术.界面清晰简洁,易操作 权限:对菜单,按钮控制.仅展示有权限的菜单和按钮. 拦截:对所有无权限URL进行拦截,防止...
架构:Spring MVC+easyUI+mybatis 开发语言:Java 数据库:使用免费MYSQL 前端:使用Jquery和Easyui技术.界面清晰简洁,易操作 权限:对菜单,按钮控制.仅展示有权限的菜单和按钮. 拦截:对所有无权限URL进行拦截,...
该项目是一个大学生校园兼职平台,主要用户是商家、学生、管理员。商家可以发布兼职信息,发布后,管理员进入后台确认信息是否有效,并且审核该兼职信息。...spring+spring mvc+mybatis+layui+jquery
业余时间,整合以前项目技术,创建springMVC示例,目前升级到4.3.13版本,您的宝贵意见,是我们进步的动力。...- 配置全局异常处理,mybatis,pagehelper分页。 - 配置通用日志打印,采用异步线程池日志写入
项目描述 ...spring+spring mvc+mybatis+jquery+ajax http://localhost:8080/house/login.action 房东账号admin admin 注意事项(可选) 1.需要配置Tomcat的默认路径为/house 2.需要配置imgs的访问路径
Portal-Basic Java Web应用开发框架(简称 Portal-Basic)是一套功能完备的高性能Full-Stack Web应用开发框架,内置稳定高效的MVC基础架构和DAO框架(已内置Hibernate、MyBatis和JDBC支持),集成 Action拦截、Form ...
ssm实现的流浪猫狗领养管理系统 运行环境 jdk7+tomcat7+mysql+IntelliJ IDEA+maven 项目技术(必填) spring+spring mvc+mybatis+bootstrap+jquery http://localhost:8080/admin/logout.action admin 111111
JessMA Java MVC & REST应用开发框架(简称 JessMA)是一套功能完备的高性能Full-Stack Web应用开发框架,内置稳定高效的MVC基础架构和DAO框架(已内置Hibernate、MyBatis和JDBC支持),集成 Action拦截、Form Bean ...
该jar包可以根据数据库表,自动生成action(struts2)/controller(springmvc)、model、service、dao各个层的代码。 实用框架:struts2/springmvc + mybatis + spring 可根据项目需要选择: 框架类型...
│ 第13节:Spring MVC实现Web层开发.avi │ 第14节:新增和列表页面和分页tag.avi │ 第15节:带查询的分页、修改和删除页面.avi │ 第16节:Mybatis动态查询和Json自动拼装.avi │ 第17节:X-gen生成所需的DAO部分...
《我的博客园》项目基于MVC设计开发模式,采用了(Struts2 + Spring + MyBatis )框架及 MySql数据库编写的一款能及时发表自己所思所想的博客即B/S端应用。 使用Struts2框架实现了业务请求的转发和重定向的跳转功能,...
Basic)是一套功能完备的高性能Full-StackWeb应用开发框架,内置稳定高效的MVC基础架构和DAO框架(已内置Hibernate、MyBatis和JDBC支持),集成Action拦截、FormBean/DaoBean/SpringBean装配、国际化、文件上传下载...