三 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>
最新文章
- 自制Azure中国版“加血包”
- Unity中Mesh分解与边缘高亮加上深度检测
- 如何修改git的当前登录信息
- Hibernate常用配置文件详解
- android 工程出现感叹号错误
- c++简单的ATL COM开发和调用实例(转)
- 搭了个hexo博客
- 利用PHP SOAP扩展实现简单Web Services
- 用Python从零开始创建区块链
- maven项目引入sqljdbc4 找不到包的完美 解决方案。
- <;数据结构基础学习>;(五)递归
- Vue(十一)计算属性
- 过滤器(filter),监听器(listener),与servlet的执行顺序
- PHP手册-函数参考-加密扩展
- java报错综合
- 在Excel中根据某一个单元格的出生日期自动精确计算年龄
- Android性能测试之Monkey使用
- FLEX拖动时复制图片
- 成都优步uber司机奖励政策(持续更新)
- 扫描线 - UVALive - 6864 Strange Antennas