航达建设集团有限公司网站,wordpress怎么降级,asp.net 手机网站模板,沈阳男科医院哪家好点文章出处#xff1a;课程资料 web.xml等配置文件的解释#xff1a;打开博客 为了更好的学习 springmvc和mybatis整合开发的方法#xff0c;需要将springmvc和mybatis进行整合。 整合目标#xff1a;控制层采用springmvc、持久层使用mybatis实现。
步骤详解#xff1a;
…文章出处课程资料 web.xml等配置文件的解释打开博客 为了更好的学习 springmvc和mybatis整合开发的方法需要将springmvc和mybatis进行整合。 整合目标控制层采用springmvc、持久层使用mybatis实现。
步骤详解
创建数据库表
需要的jar包
spring包括springmvcmybatismybatis-spring整合包数据库驱动第三方连接池。
整合思路
Dao层
1、SqlMapConfig.xml空文件即可但是需要文件头。 2、applicationContext-dao.xml 数据库连接池
b) SqlSessionFactory对象需要spring和mybatis整合包下的。 c) 配置mapper文件扫描器。
Service层
1、applicationContext-service.xml包扫描器扫描service注解的类。 2、applicationContext-trans.xml配置事务。
Controller层
1、Springmvc.xml a) 包扫描器扫描Controller注解的类。 b) 配置注解驱动 c) 配置视图解析器
Web.xml文件
1、配置spring 2、配置前端控制器。
创建工程
创建动态web工程springmvc-web选2.5可以自动生成web.xml 加入jar包
复制jar包到/WEB-INF/lib中 工程自动加载jar包 加入配置文件
创建资源文件夹config 在其下创建mybatis和spring文件夹用来存放配置文件如下图 sqlMapConfig.xml
使用逆向工程来生成Mapper相关代码不需要配置别名。 在config/mybatis下创建SqlMapConfig.xml
?xml version1.0 encodingUTF-8?
!DOCTYPE configuration
PUBLIC -//mybatis.org//DTD Config 3.0//EN
http://mybatis.org/dtd/mybatis-3-config.dtd
configuration/configurationapplicationContext-dao.xml
配置数据源、配置SqlSessionFactory、mapper扫描器。
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:contexthttp://www.springframework.org/schema/context xmlns:phttp://www.springframework.org/schema/pxmlns:aophttp://www.springframework.org/schema/aop xmlns:txhttp://www.springframework.org/schema/txxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://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 locationclasspath:db.properties /!-- 数据库连接池 --bean iddataSource classorg.apache.commons.dbcp.BasicDataSourcedestroy-methodcloseproperty namedriverClassName value${jdbc.driver} /property nameurl value${jdbc.url} /property nameusername value${jdbc.username} /property namepassword value${jdbc.password} /property namemaxActive value10 /property namemaxIdle value5 //bean!-- 配置SqlSessionFactory --bean idsqlSessionFactory classorg.mybatis.spring.SqlSessionFactoryBean!-- 数据库连接池 --property namedataSource refdataSource /!-- 加载mybatis的全局配置文件 --property nameconfigLocation valueclasspath:mybatis/SqlMapConfig.xml //bean!-- 配置Mapper扫描 --bean classorg.mybatis.spring.mapper.MapperScannerConfigurer!-- 配置Mapper扫描包 --property namebasePackage valuecn.itcast.ssm.mapper //bean/beansdb.properties
配置数据库相关信息
jdbc.drivercom.mysql.jdbc.Driver
jdbc.urljdbc:mysql://localhost:3306/springmvc?characterEncodingutf-8
jdbc.usernameroot
jdbc.passwordrootapplicationContext-service.xml
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:contexthttp://www.springframework.org/schema/context xmlns:phttp://www.springframework.org/schema/pxmlns:aophttp://www.springframework.org/schema/aop xmlns:txhttp://www.springframework.org/schema/txxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://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-packagecn.itcast.ssm.service //beansspringmvc.xml
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlns:phttp://www.springframework.org/schema/pxmlns:contexthttp://www.springframework.org/schema/contextxmlns:mvchttp://www.springframework.org/schema/mvcxsi:schemaLocationhttp://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-packagecn.itcast.ssm.controller /!-- 注解驱动 --mvc:annotation-driven /!-- Example: prefix/WEB-INF/jsp/, suffix.jsp, viewnametest - /WEB-INF/jsp/test.jsp --!-- 配置视图解析器 --beanclassorg.springframework.web.servlet.view.InternalResourceViewResolver!-- 配置逻辑视图的前缀 --property nameprefix value/WEB-INF/jsp/ /!-- 配置逻辑视图的后缀 --property namesuffix value.jsp //bean/beansweb.xml
?xml version1.0 encodingUTF-8?
web-app xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlnshttp://java.sun.com/xml/ns/javaeexsi:schemaLocationhttp://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsdidWebApp_ID version2.5display-namespringmvc-web/display-namewelcome-file-listwelcome-fileindex.html/welcome-filewelcome-fileindex.htm/welcome-filewelcome-fileindex.jsp/welcome-filewelcome-filedefault.html/welcome-filewelcome-filedefault.htm/welcome-filewelcome-filedefault.jsp/welcome-file/welcome-file-list!-- 配置spring --context-paramparam-namecontextConfigLocation/param-nameparam-valueclasspath:spring/applicationContext*.xml/param-value/context-param!-- 使用监听器加载Spring配置文件 --listenerlistener-classorg.springframework.web.context.ContextLoaderListener/listener-class/listener!-- 配置SrpingMVC的前端控制器 --servletservlet-namespringmvc-web/servlet-nameservlet-classorg.springframework.web.servlet.DispatcherServlet/servlet-classinit-paramparam-namecontextConfigLocation/param-nameparam-valueclasspath:spring/springmvc.xml/param-value/init-param/servlet!-- 1. /* 拦截所有 jsp js png .css 真的全拦截 建议不使用2. *.action *.do 拦截以do action 结尾的请求 肯定能使用 ERP 3. / 拦截所有 不包括jsp) (包含.js .png.css) 强烈建议使用 前台 面向消费者 www.jd.com/search /对静态资源放行--servlet-mappingservlet-namespringmvc-web/servlet-name!-- 配置所有以action结尾的请求进入SpringMVC --url-pattern*.action/url-pattern/servlet-mapping/web-app加入jsp页面
itemList.jsp
% page languagejava contentTypetext/html; charsetUTF-8pageEncodingUTF-8%
% taglib urihttp://java.sun.com/jsp/jstl/core prefixc %
% taglib urihttp://java.sun.com/jsp/jstl/fmt prefixfmt%
!DOCTYPE html PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN http://www.w3.org/TR/html4/loose.dtd
html
head
meta http-equivContent-Type contenttext/html; charsetUTF-8
title查询商品列表/title
/head
body
form action${pageContext.request.contextPath }/item/queryitem.action methodpost
查询条件
table width100% border1
tr
tdinput typesubmit value查询//td
/tr
/table
商品列表
table width100% border1
trtd商品名称/tdtd商品价格/tdtd生产日期/tdtd商品描述/tdtd操作/td
/tr
c:forEach items${itemList } varitem
trtd${item.name }/tdtd${item.price }/tdtdfmt:formatDate value${item.createtime} patternyyyy-MM-dd HH:mm:ss//tdtd${item.detail }/tdtda href${pageContext.request.contextPath }/itemEdit.action?id${item.id}修改/a/td/tr
/c:forEach/table
/form
/body/html
itemEdit.jsp
% page languagejava contentTypetext/html; charsetUTF-8pageEncodingUTF-8%
% taglib urihttp://java.sun.com/jsp/jstl/core prefixc %
% taglib urihttp://java.sun.com/jsp/jstl/fmt prefixfmt%
!DOCTYPE html PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN http://www.w3.org/TR/html4/loose.dtd
html
head
meta http-equivContent-Type contenttext/html; charsetUTF-8
title修改商品信息/title/head
body !-- 上传图片是需要指定属性 enctypemultipart/form-data --!-- form iditemForm action methodpost enctypemultipart/form-data --form iditemForm action${pageContext.request.contextPath }/updateitem.action methodpostinput typehidden nameitems.id value${item.id } / 修改商品信息table width100% border1trtd商品名称/tdtdinput typetext nameitems.name value${item.name } //td/trtrtd商品价格/tdtdinput typetext nameitems.price value${item.price } //td/trtrtd商品生产日期/tdtdinput typetext nameitems.createtimevaluefmt:formatDate value${item.createtime} patternyyyy-MM-dd HH:mm:ss/ //td/tr%-- trtd商品图片/tdtdc:if test${item.pic !null}img src/pic/${item.pic} width100 height100/br//c:ifinput typefile namepictureFile/ /td/tr--%trtd商品简介/tdtdtextarea rows3 cols30 nameitems.detail${item.detail }/textarea/td/trtrtd colspan2 aligncenterinput typesubmit value提交 //td/tr/table/form
/body/html