springmvc整合mybatis之准备阶段与文件配置
文章出處:課程資料
web.xml等配置文件的解釋:打開博客
為了更好的學(xué)習(xí) springmvc和mybatis整合開發(fā)的方法,需要將springmvc和mybatis進行整合。
整合目標:控制層采用springmvc、持久層使用mybatis實現(xiàn)。
步驟詳解:
創(chuàng)建數(shù)據(jù)庫表
需要的jar包
整合思路
Dao層:
1、SqlMapConfig.xml,空文件即可,但是需要文件頭。
2、applicationContext-dao.xml
數(shù)據(jù)庫連接池
b) SqlSessionFactory對象,需要spring和mybatis整合包下的。
c) 配置mapper文件掃描器。
Service層:
1、applicationContext-service.xml包掃描器,掃描@service注解的類。
2、applicationContext-trans.xml配置事務(wù)。
Controller層:
1、Springmvc.xml
a) 包掃描器,掃描@Controller注解的類。
b) 配置注解驅(qū)動
c) 配置視圖解析器
Web.xml文件:
1、配置spring
2、配置前端控制器。
創(chuàng)建工程
創(chuàng)建動態(tài)web工程springmvc-web(選2.5可以自動生成web.xml)
加入jar包
復(fù)制jar包到/WEB-INF/lib中
工程自動加載jar包
加入配置文件
創(chuàng)建資源文件夾config
在其下創(chuàng)建mybatis和spring文件夾,用來存放配置文件,如下圖:
sqlMapConfig.xml
使用逆向工程來生成Mapper相關(guān)代碼,不需要配置別名。
在config/mybatis下創(chuàng)建SqlMapConfig.xml
applicationContext-dao.xml
配置數(shù)據(jù)源、配置SqlSessionFactory、mapper掃描器。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context" 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:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"><!-- 加載配置文件 --><context:property-placeholder location="classpath:db.properties" /><!-- 數(shù)據(jù)庫連接池 --><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close"><property name="driverClassName" value="${jdbc.driver}" /><property name="url" value="${jdbc.url}" /><property name="username" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /><property name="maxActive" value="10" /><property name="maxIdle" value="5" /></bean><!-- 配置SqlSessionFactory --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 數(shù)據(jù)庫連接池 --><property name="dataSource" ref="dataSource" /><!-- 加載mybatis的全局配置文件 --><property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" /></bean><!-- 配置Mapper掃描 --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><!-- 配置Mapper掃描包 --><property name="basePackage" value="cn.itcast.ssm.mapper" /></bean></beans>db.properties
配置數(shù)據(jù)庫相關(guān)信息
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/springmvc?characterEncoding=utf-8 jdbc.username=root jdbc.password=rootapplicationContext-service.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context" 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:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"><!-- 配置service掃描 --><context:component-scan base-package="cn.itcast.ssm.service" /></beans>springmvc.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: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-4.0.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"><!-- 配置controller掃描包 --><context:component-scan base-package="cn.itcast.ssm.controller" /><!-- 注解驅(qū)動 --><mvc:annotation-driven /><!-- Example: prefix="/WEB-INF/jsp/", suffix=".jsp", viewname="test" -> "/WEB-INF/jsp/test.jsp" --><!-- 配置視圖解析器 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><!-- 配置邏輯視圖的前綴 --><property name="prefix" value="/WEB-INF/jsp/" /><!-- 配置邏輯視圖的后綴 --><property name="suffix" value=".jsp" /></bean></beans>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"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"id="WebApp_ID" version="2.5"><display-name>springmvc-web</display-name><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list><!-- 配置spring --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring/applicationContext*.xml</param-value></context-param><!-- 使用監(jiān)聽器加載Spring配置文件 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 配置SrpingMVC的前端控制器 --><servlet><servlet-name>springmvc-web</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring/springmvc.xml</param-value></init-param></servlet><!-- 1. /* 攔截所有 jsp js png .css 真的全攔截 建議不使用2. *.action *.do 攔截以do action 結(jié)尾的請求 肯定能使用 ERP 3. / 攔截所有 (不包括jsp) (包含.js .png.css) 強烈建議使用 前臺 面向消費者 www.jd.com/search /對靜態(tài)資源放行--><servlet-mapping><servlet-name>springmvc-web</servlet-name><!-- 配置所有以action結(jié)尾的請求進入SpringMVC --><url-pattern>*.action</url-pattern></servlet-mapping></web-app>加入jsp頁面
itemList.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%> <!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>查詢商品列表</title> </head> <body> <form action="${pageContext.request.contextPath }/item/queryitem.action" method="post"> 查詢條件: <table width="100%" border=1> <tr> <td><input type="submit" value="查詢"/></td> </tr> </table> 商品列表: <table width="100%" border=1> <tr><td>商品名稱</td><td>商品價格</td><td>生產(chǎn)日期</td><td>商品描述</td><td>操作</td> </tr> <c:forEach items="${itemList }" var="item"> <tr><td>${item.name }</td><td>${item.price }</td><td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td><td>${item.detail }</td><td><a href="${pageContext.request.contextPath }/itemEdit.action?id=${item.id}">修改</a></td></tr> </c:forEach></table> </form> </body></html>itemEdit.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%> <!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>修改商品信息</title></head> <body> <!-- 上傳圖片是需要指定屬性 enctype="multipart/form-data" --><!-- <form id="itemForm" action="" method="post" enctype="multipart/form-data"> --><form id="itemForm" action="${pageContext.request.contextPath }/updateitem.action" method="post"><input type="hidden" name="items.id" value="${item.id }" /> 修改商品信息:<table width="100%" border=1><tr><td>商品名稱</td><td><input type="text" name="items.name" value="${item.name }" /></td></tr><tr><td>商品價格</td><td><input type="text" name="items.price" value="${item.price }" /></td></tr><tr><td>商品生產(chǎn)日期</td><td><input type="text" name="items.createtime"value="<fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/>" /></td></tr><%-- <tr><td>商品圖片</td><td><c:if test="${item.pic !=null}"><img src="/pic/${item.pic}" width=100 height=100/><br/></c:if><input type="file" name="pictureFile"/> </td></tr>--%><tr><td>商品簡介</td><td><textarea rows="3" cols="30" name="items.detail">${item.detail }</textarea></td></tr><tr><td colspan="2" align="center"><input type="submit" value="提交" /></td></tr></table></form> </body></html>總結(jié)
以上是生活随笔為你收集整理的springmvc整合mybatis之准备阶段与文件配置的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: webmagic抓取实例
- 下一篇: 【转载】jvm内存回收