思路:

Dao层:

1 逆向工程生成mapper及其配置文件以及pojo

2 SqlMapConfig.xml,空文件即可,需要文件头

3 applicationContext-dao.xml

a 数据库连接池

b SqlSessionFactory对象,需要Spring和Mybatis整合包

c 配置mapper文件扫描器

Service层:

1、applicationContext-service.xml  包扫描器,扫描@service注解的业务层类

2、applicaitonContext.xml:配置事务

Controller层:

SpringMvc.xml

1包扫描器,扫描@Controller注解的控制层类

2 配置注解驱动

3 配置视图解析器,url前缀和后缀

web.xml

1 配置Spring容量监听器

2 配置前端控制器

3 配置post方式编码

包结构:

配置:

Dao层:

/SpringMvc01/config/SqlMapConfig.xml

/SpringMvc01/config/spring/applicationContext-dao.xml:jdbc配置文件,数据库连接池,SQLSessionFactory(datasource,加载核心配置文件,包扫描),动态代理(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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://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.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 加载配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties" /> <!-- 数据库连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${name}" />
<property name="password" value="${password}" />
<!-- 连接池的最大数据库连接数 -->
<property name="maxActive" value="10" />
<!-- 最大空闲数 -->
<property name="maxIdle" value="5" />
</bean> <!-- SqlSessionFactory配置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 加载mybatis核心配置文件 -->
<property name="configLocation" value="classpath:SqlMapConfig.xml" />
<!-- 别名包扫描 -->
<property name="typeAliasesPackage" value="com.springmvc01.pojo" />
</bean> <!-- 动态代理,第二种方式:包扫描(推荐): -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- basePackage多个包用","分隔 -->
<property name="basePackage" value="com.springmvc01.mapper" />
</bean>
</beans    

Service层:

/SpringMvc01/config/spring/applicationContext-service.xml:开启Service包扫描器,直接在业务层类上添加注解@service,免去Spring容器管理的繁琐

/SpringMvc01/config/spring/applicationContext-trans.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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://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.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource" />
</bean> <!-- 通知,事务的增强 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 传播行为 propagation="REQUIRED" :默认,如果有,用当前事务,没有,新建事务 -->
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
<tx:method name="query*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice> <!-- 切面 -->
<aop:config>
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* com.springmvc01.service.*.*(..))" />
</aop:config> </beans>

/SpringMvc01/src/com/springmvc01/service/impl/ItemServiceImpl.java:业务层方法,调用Dao实现相应的CRUD,返回给Controller

Controller层:

/SpringMvc01/config/spring/springmvc.xml:配置Controller包扫描,注解驱动(处理器映射器,处理器适配器,视图解析器)

<?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.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 配置controller扫描包 -->
<context:component-scan base-package="com.springmvc01.controller"/> <!-- 推荐使用:直接配置处理器适配器比较麻烦,可以配置注解驱动以替代之,对json数据响应提供支持-->
<mvc:annotation-driven/> <!-- 配置视图解析器,配置前缀和后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean> <!-- 配置处理器映射器 -->
<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
-->
<!-- 配置处理器适配器 -->
<!-- <bean class="org.springframework.web.servlet.mvc.RequestMappingHandlerAdapter"/>
-->
</beans>

/SpringMvc01/src/com/springmvc01/controller/ItemController.java:指定url,参数传递,页面跳转,实现和前端的交互

//注解控制器
@Controller
public class ItemController { @Autowired
private ItemService itemService; //注解url
@RequestMapping("/itemList.action")
public ModelAndView itemList(){ List<Item> list = itemService.getItemList();
//创建ModelView用来存放数据和视图
ModelAndView modelAndView = new ModelAndView();
//设置数据到模型中
modelAndView.addObject("itemList", list);
// modelAndView.setViewName("/WEB-INF/jsp/itemList.jsp");
//在SpringMVC核心配置文件里面配置前缀和后缀
modelAndView.setViewName("itemList");
return modelAndView;
} /* @RequestMapping("itemEdit")
//修改商品,根据查询商品跳转修改页面
public ModelAndView itemEdit(HttpServletRequest request){
String idStr = request.getParameter("id");
ModelAndView modelAndView = new ModelAndView();
//查询商品信息
Item item = itemService.getItemById(new Integer(idStr));
modelAndView.addObject("item", item);
modelAndView.setViewName("itemEdit");
return modelAndView;
}*/ // @RequestMapping("itemEdit")
/**
* 演示默认支持的参数传递
* @param model
* @param request
* @param response
* @param session
* @return
*/
/* public String itemEdit(Model model ,HttpServletRequest request , HttpServletResponse response , HttpSession session){
String idStr = request.getParameter("id");
System.out.println(request);
System.out.println(response);
System.out.println(session);
//查询商品信息
Item item = itemService.getItemById(new Integer(idStr));
model.addAttribute("item", item);
return "itemEdit";
}*/ @RequestMapping("itemEdit")
/**
* 简单参数的传递
* @RequestParam(value="id",required=true) Integer ids 必须传入id,赋给ids
* @param model
* @param id
* @return
*/
public String itemEdit(Model model ,@RequestParam(value="id",required=true) Integer ids){
//查询商品信息
Item item = itemService.getItemById(ids);
model.addAttribute("item", item);
return "itemEdit";
} @RequestMapping("updateItem")
/**
* 修改商品,显示pojo参数绑定
* @param item
* @return
*/
public String updateItem(Item item , Model model){
itemService.updateItem(item);
model.addAttribute("msg", "背包修改成功");
return "itemEdit";
} @RequestMapping("queryItem")
/**
* 包装类的使用
* @param vo
* @param model
* @return
*/
public String queryItem(QueryVo vo , Model model){
if(vo.getItem()!=null){
System.out.println(vo.getItem());
}
//模拟搜索商品
List<Item> itemList = itemService.getItemList();
model.addAttribute("itemList",itemList);
return "itemList";
} }

web.xml:

配置Spring,使用监听器加载Spring配置文件

使用Spring提供的过滤器解决post乱码问题

配置核心控制器DispatcherServlet,是SpringMVC的入口

<?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>SpringMvc01</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> <!-- 使用监听器加载Spring配置文件 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- 解决post乱码问题 -->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<!-- 设置编码参是UTF8 -->
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <!-- 配置核心控制器,SpringMVC入口是Servlet -->
<servlet>
<servlet-name>springmvc</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>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping> </web-app>

三 SprigMvc与Mybatis整合&实现商品列表功能的更多相关文章

  1. 商城02——dubbo框架整合_商品列表查询实现_分页

    1.   课程计划 1.服务中间件dubbo 2.SSM框架整合. 3.测试使用dubbo 4.后台系统商品列表查询功能实现. 5.监控中心的搭建 2.   功能分析 2.1. 后台系统所用的技术 框 ...

  2. 菜鸟级springmvc+spring+mybatis整合开发用户登录功能(上)

    由于本人愚钝,整合ssm框架真是费劲了全身的力气,所以打算写下这篇文章,一来是对整个过程进行一个回顾,二来是方便有像我一样的笨鸟看过这篇文章后对其有所帮助,如果本文中有不对的地方,也请大神们指教. 一 ...

  3. 菜鸟级springmvc+spring+mybatis整合开发用户登录功能(下)

    昨天介绍了mybatis与spring的整合,今天我们完成剩下的springmvc的整合工作. 要整合springmvc首先得在web.xml中配置springmvc的前端控制器DispatcherS ...

  4. JAVAEE——宜立方商城02:服务中间件dubbo、工程改造为基于soa架构、商品列表实现

    1. 学习计划 第二天:商品列表功能实现 1.服务中间件dubbo 2.工程改造为基于soa架构 3.商品列表查询功能实现. 2. 将工程改造为SOA架构 2.1. 分析 由于宜立方商城是基于soa的 ...

  5. css3flex布局实现商品列表

    首先看图 手机商场经常会有商品列表功能,这样其实可以用flex布局实现 注意两个地方: 1.商品列表平衡间距(flex布局的换行加两端对齐) 2.中间文字行数不一样,会出现下方留下空白,如何解决(fl ...

  6. SpringMVC学习记录三——8 springmvc和mybatis整合

    8      springmvc和mybatis整合 8.1      需求 使用springmvc和mybatis完成商品列表查询. 8.2      整合思路 springmvc+mybaits的 ...

  7. 利用mybatis的分页插件实现商品列表的显示

    分析思路: 当我们点击查询商品的时候,会出现商品的列表,并按上下页可以实现分页的查询的功能. 首先首先我们先找到商品查询商品的按钮在jsp的那个页面,即首页index.jsp 这里有个url即显示商品 ...

  8. vue实战记录(三)- vue实现购物车功能之渲染商品列表

    vue实战,一步步实现vue购物车功能的过程记录,课程与素材来自慕课网,自己搭建了express本地服务器来请求数据 作者:狐狸家的鱼 本文链接:vue实战-实现购物车功能(三) GitHub:sue ...

  9. (转)淘淘商城系列——MyBatis分页插件(PageHelper)的使用以及商品列表展示

    http://blog.csdn.net/yerenyuan_pku/article/details/72774381 上文我们实现了展示后台页面的功能,而本文我们实现的主要功能是展示商品列表,大家要 ...

随机推荐

  1. Java面向对象内存图

    1. java虚拟机的内存划分 2. 苹果手机类 package cn.itcast.day06.demo02; /* 定义一个类,用来模拟“手机”事物. 属性:品牌.价格.颜色 行为:打电话.发短信 ...

  2. streamsets 安装

    这里有很多坑点 1. 我下载的是rpm 包,full 直接安装, 安装好后,路径在 /opt 目录 2. 启动    -注意报错 2.1  先给他创建 一个 日志目录, 安装好后是不存在这个目录 mk ...

  3. Ubuntu 18 设置静态 IP

    在Ubuntu 18中使用 netplan 命令 首先 cd /etc/netplan/ 找到 *.yaml文件(每个人可能不一样),编辑它 root@waydeserver:/etc/netplan ...

  4. Net Core解决ZipFile解压中文出现乱码

    一.在main方法中添加 Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); 二.解压添加 //sourceArchiveFi ...

  5. php 基础 判断类型

    八.PHP中判断类型 is_bool():判断是否是布尔型 is_int().is_integer()和 is_long():判断是否为整型. is_float().is_double()和 is_r ...

  6. 最常用的CountDownLatch, CyclicBarrier你知道多少? (Java工程师必会)

    CountdownLatch,CyclicBarrier是非常常用并发工具类,可以说是Java工程师必会技能了.不但在项目实战中经常涉及,而且在编写压测程序,多线程demo也是必不可少,所以掌握它们的 ...

  7. 实现一个简易的Unity网络同步引擎——netgo

    实现一个简易的Unity网络同步引擎Netgo 目前GOLANG有大行其道的趋势,尤其是在网络编程方面.因为和c/c++比较起来,虽然GC占用了一部分机器性能,但是出错概率小了,开发效率大大提升,而且 ...

  8. python写入文件中遇到 UnicodeEncodeError: ‘gbk’ codec can’t encode character 错误的解决办法

    在写入TXT文件时,某些页面总是报UnicodeEncodeError: ‘gbk’ codec can’t encode character错误,网上找了半天也没找到解决办法. 后来终于找到了解决办 ...

  9. ThinkPHP3.2.2的函数扩展

    ThinkPHP的函数扩展:为了更好的在前台模板中显示变量,例如,商品分类中,分类名称之间的缩进.此时,在APP/Common/Common文件夹下(APP为新建的应用目录),新建一个php文件,如: ...

  10. 兔子与兔子(字符串hash)

    传送门 很久很久以前,森林里住着一群兔子. 有一天,兔子们想要研究自己的 DNA 序列. 我们首先选取一个好长好长的 DNA 序列(小兔子是外星生物,DNA 序列可能包含 26 个小写英文字母). 然 ...