三 SprigMvc与Mybatis整合&实现商品列表功能
思路:
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整合&实现商品列表功能的更多相关文章
- 商城02——dubbo框架整合_商品列表查询实现_分页
1. 课程计划 1.服务中间件dubbo 2.SSM框架整合. 3.测试使用dubbo 4.后台系统商品列表查询功能实现. 5.监控中心的搭建 2. 功能分析 2.1. 后台系统所用的技术 框 ...
- 菜鸟级springmvc+spring+mybatis整合开发用户登录功能(上)
由于本人愚钝,整合ssm框架真是费劲了全身的力气,所以打算写下这篇文章,一来是对整个过程进行一个回顾,二来是方便有像我一样的笨鸟看过这篇文章后对其有所帮助,如果本文中有不对的地方,也请大神们指教. 一 ...
- 菜鸟级springmvc+spring+mybatis整合开发用户登录功能(下)
昨天介绍了mybatis与spring的整合,今天我们完成剩下的springmvc的整合工作. 要整合springmvc首先得在web.xml中配置springmvc的前端控制器DispatcherS ...
- JAVAEE——宜立方商城02:服务中间件dubbo、工程改造为基于soa架构、商品列表实现
1. 学习计划 第二天:商品列表功能实现 1.服务中间件dubbo 2.工程改造为基于soa架构 3.商品列表查询功能实现. 2. 将工程改造为SOA架构 2.1. 分析 由于宜立方商城是基于soa的 ...
- css3flex布局实现商品列表
首先看图 手机商场经常会有商品列表功能,这样其实可以用flex布局实现 注意两个地方: 1.商品列表平衡间距(flex布局的换行加两端对齐) 2.中间文字行数不一样,会出现下方留下空白,如何解决(fl ...
- SpringMVC学习记录三——8 springmvc和mybatis整合
8 springmvc和mybatis整合 8.1 需求 使用springmvc和mybatis完成商品列表查询. 8.2 整合思路 springmvc+mybaits的 ...
- 利用mybatis的分页插件实现商品列表的显示
分析思路: 当我们点击查询商品的时候,会出现商品的列表,并按上下页可以实现分页的查询的功能. 首先首先我们先找到商品查询商品的按钮在jsp的那个页面,即首页index.jsp 这里有个url即显示商品 ...
- vue实战记录(三)- vue实现购物车功能之渲染商品列表
vue实战,一步步实现vue购物车功能的过程记录,课程与素材来自慕课网,自己搭建了express本地服务器来请求数据 作者:狐狸家的鱼 本文链接:vue实战-实现购物车功能(三) GitHub:sue ...
- (转)淘淘商城系列——MyBatis分页插件(PageHelper)的使用以及商品列表展示
http://blog.csdn.net/yerenyuan_pku/article/details/72774381 上文我们实现了展示后台页面的功能,而本文我们实现的主要功能是展示商品列表,大家要 ...
随机推荐
- dfs(迷宫)
问题 J: 棋盘行走 时间限制: 1 Sec 内存限制: 128 MB[命题人:admin] 题目描述 小Biu在玩一个棋盘游戏,这个游戏给出一个n*m的棋盘,并且每个点上有一个棋子,棋子的颜色 用 ...
- Java方法升级
1. 方法格式 package cn.itcast.day04.demo02; /* 方法其实就是若干语句的功能集合. 方法好比是一个工厂. 蒙牛工厂 原料:奶牛.饲料.水 产出物:奶制品 钢铁工厂 ...
- POJ 1995 Raising Modulo Numbers(快速幂)
嗯... 题目链接:http://poj.org/problem?id=1995 快速幂模板... AC代码: #include<cstdio> #include<iostream& ...
- scipy1.3.0开始被弃用的imread,imresize,如何代替
scipy1.3.0开始被弃用的imread,imresize,如何代替 SciPy最新官方文档的说明(20190730): Functions from scipy.interpolate (spl ...
- 传奇定时器OnTimer功能详解(泡点、时间触发、任务活动)
传奇定时器OnTimer功能详解(泡点.时间触发.任务活动) 定时器功能,是传奇服务端中非常常见的一种功能,常见如:泡点脚本.赌博脚本,任务活动指定时间刷怪,时间触发一些都需要用到OnTimer功能, ...
- Centos610安装Jdk1.8
1.下载JDK 下载: https://pan.baidu.com/s/15TYsE_wfSb48pS4SpUQKHA 提取码:fsx6 2.上传安装包 上传到linux 并拷贝到/opt/jdk目 ...
- 【笔记3-用户模块】从0开始 独立完成企业级Java电商网站开发(服务端)
数据表结构设计 关系设计 为什么不用外键? 分库分表有外键会非常麻烦,清洗数据也很麻烦.数据库内置触发器也不适合采用. 查业务问题的后悔药--时间戳 create_time 数据创建时间 update ...
- Hibernate学习(七)
Hibernate缓存 1.一级缓存:Session 级别的缓存 2.二级缓存: SessionFactory 级别的缓存 3.查询缓存:需二级缓存的支持,查询缓存依赖二级缓存 一级缓存 1.依赖于 ...
- idea2018.3.6,离线使用maven的方法
(1)想办法拷贝联网电脑上已经下载好的仓库文件夹mavenRepository到离线环境的电脑上: (2)idea中按照如下图中配置: (3)打开 User settings file中设置的sett ...
- SpringBoot 集成JUnit
项目太大,不好直接测整个项目,一般都是切割成多个单元,单独测试,即单元测试. 直接在原项目上测试,会把项目改得乱七八糟的,一般是单独写测试代码. 进行单元测试,这就需要集成JUnit. (1)在pom ...