为公司建设网站的意义,哪个平台开网店不收费,建设网站改版,深圳工信部网站前面讲到#xff1a;SpringSpringMVCMyBatis深入学习及搭建(十三)——SpringMVC入门程序(二)1.需求使用springmvc和mybatis完成商品列表查询。2.整合思路springmvcmybatis的系统架构#xff1a;第一步#xff1a;整合dao层mybatis和spring整合#xff0c;通过spring管理map…前面讲到SpringSpringMVCMyBatis深入学习及搭建(十三)——SpringMVC入门程序(二)1.需求使用springmvc和mybatis完成商品列表查询。2.整合思路springmvcmybatis的系统架构第一步整合dao层mybatis和spring整合通过spring管理mapper接口。使用mapper的扫描器自动扫描mapper接口在spring中进行注册。第二步整合service层通过spring管理service接口。使用配置方式将service接口配置在spring配置文件中。实现事务控制。第三步整合springMvc由于springmvc是spring的模块不需要整合。3.环境准备数据库环境mysql5.6java环境jdk1.7MyEclipse2014springmvc版本spring3.2所需要的jar包数据库驱动包mybatis的jar包mybatis的spring的整合包log4j包dbcp数据库连接池包spring3.2所有jar包jstl包过程结构目录4.整合daomybatis和spring进行整合。4.1 db.propertiesjdbc.drivercom.mysql.jdbc.Driverjdbc.urljdbc:mysql://localhost:3306/mybatisdemojdbc.usernamerootjdbc.password4.2 log4j.properties# Global logging configuration建议开发环境要用debuglog4j.rootLoggerDEBUG, stdout# Console output...log4j.appender.stdoutorg.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layoutorg.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern%5p [%t] - %m%n4.3 sqlMapConfig.xml在classpath下创建mybatis/sqlMapConfig.xml。?xml version1.0 encodingUTF-8?/pPUBLIC -//mybatis.org//DTD Config 3.0//ENhttp://mybatis.org/dtd/mybatis-3-config.dtd4.4 applicationContext-dao.xml在classpath下创建spring/applicationContext-dao.xml。配置数据源、事务管理、SqlSessionFactory、mapper扫描器。4.5逆向工程生成po类及mapper(即单表增删改查)详情见SpringSpringMVCMyBatis深入学习及搭建(十)——MyBatis逆向工程将生成的文件拷贝至工程中。4.6手动定义商品查询mapper针对综合查询mapper一般情况会有关联查询建议自定义mapper。4.6.1 ItemsMapperCustom.xmlsql语句SELECT * FROM items WHERE items.name LIKE %笔记本%?xml version1.0 encodingUTF-8 ?items.name LIKE %${itemsCustom.name}%resultTypejoanna.yan.ssm.po.ItemsCustomSELECT items.* FROM items4.6.2 ItemsMapperCustom.javapublic interface ItemsMapperCustom {//商品查询列表public List findItemsList(ItemsQueryVo itemsQueryVo) throws Exception;}5.整合service让spring管理service接口。5.1定义service接口package joanna.yan.ssm.service;import java.util.List;import joanna.yan.ssm.po.ItemsCustom;import joanna.yan.ssm.po.ItemsQueryVo;public interface ItemsService {//商品查询列表public List findItemsList(ItemsQueryVo itemsQueryVo) throws Exception;}package joanna.yan.ssm.service.impl;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import joanna.yan.ssm.mapper.ItemsMapperCustom;import joanna.yan.ssm.po.ItemsCustom;import joanna.yan.ssm.po.ItemsQueryVo;import joanna.yan.ssm.service.ItemsService;public class ItemsServiceImpl implements ItemsService{Autowiredprivate ItemsMapperCustom itemsMapperCustom;Overridepublic List findItemsList(ItemsQueryVo itemsQueryVo)throws Exception {//通过ItemsMapperCustom查询数据库return itemsMapperCustom.findItemsList(itemsQueryVo);}}5.2在spring容器配置service(applicationContext-service.xml)在classpath下创建spring/applicationContext-service.xml文件中配置service。5.3事务控制(applicationContext-transaction.xml)在classpath下创建spring/applicationContext-service.xml文件中使用spring声明式事务控制方法。6.整合springmvc6.1 springmvc.xml在classpath下创建spring/springvc.xml文件配置处理器映射器、适配器、视图解析器。6.2配置前端控制器参考入门程序SpringSpringMVCMyBatis深入学习及搭建(十二)——SpringMVC入门程序(一)在web.xml中配置SpringMVC_MyBatisindex.jspspringmvcorg.springframework.web.servlet.DispatcherServletcontextConfigLocationclasspath:spring/springmvc.xml1springmvc*.action6.3编写Controller(就是Handler)Controllerpublic class ItemsController {Autowiredprivate ItemsService itemsService;//商品查询http://localhost:8080/SpringMVC_MyBatis/queryItems.actionRequestMapping(/queryItems)public ModelAndView queryItems() throws Exception{//调用service查找数据库查询商品列表List itemsListitemsService.findItemsList(null); //返回ModelAndViewModelAndView modelAndViewnew ModelAndView();modelAndView.addObject(itemsList, itemsList);//指定视图// modelAndView.setViewName(/WEB-INF/jsp/items/itemsList.jsp);//下边的路径如果在视图解析器中配置jsp路径的前缀和jsp路径的后缀修改为modelAndView.setViewName(items/itemsList);return modelAndView;}}6.4编写jsp查询商品列表查询条件商品列表商品名称商品价格生产日期商品描述操作${item.name }${item.price }${item.detail }修改7.加载spring容器将mapper、service、controller加载到spring容器中。建议使用通配符加载上边的配置文件。在web.xml中添加spring容器监听器加载spring容器。contextConfigLocation/WEB-INF/classes/spring/applicationContext-*.xmlorg.springframework.web.context.ContextLoaderListener8.商品查询调试访问地址http://localhost:8080/SpringMVC_MyBatis/queryItems.action查询结果