【框架整合】spring+springmvc+mybatis整合

Spingmvc +mybatis整合

思路

第一步:整合dao层

             mybatis和spring整合,通过spirng管理mapper接口。

使用mapper扫描器自动扫描mapper接口在spring中进行注册。

第二步:整合service层,

 通过spring管理service接口

使用配置方式在spring中对service接口进行注册

实现事务控制

第三步:整合springmvc

 

1、dao整合

1.1、sqlMapConfig.xml

1.2、applicationContext-dao.xml

配置:

数据源

sqlSessionFactory

mapper扫描器

1.3、配置 applicationContext-service.xml

配置:

事务

通知

aop

     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
     	<!-- 数据源 -->
     	<property name="dataSource" ref="dataSource"></property>
     </bean>
     
     <!-- 通知 -->
     <tx:advice id="txadvice" transaction-manager="transactionManager">
     	<tx:attributes>
     		<tx:method name="save*" propagation="REQUIRED"/>
     		<tx:method name="delete*" propagation="REQUIRED"/>
     		<tx:method name="insert*" propagation="REQUIRED"/>
     		<tx:method name="update*" propagation="REQUIRED"/>
     		<tx:method name="find*" propagation="REQUIRED"/>
     	</tx:attributes>
     </tx:advice>
     
     <!-- aop -->
     <aop:config>
     	<aop:advisor advice-ref="txadvice" pointcut="execution(* cn.ssm.service.impl.*.*(..))"/>
     </aop:config>
      
    </beans> 

1.4、逆向工程生成pojo和 mapper

查询数据库的表

items

 创建包装类和扩展类

因为可能不仅仅只查询Items一张表,当涉及到多张表的查询时,只有Items表的属性是远远不够的,所以为了系统的扩展性,需要定义扩展类和包装类。

扩展类:逆向工程生成的pojo一般不要改动,当后期需要对商品的属性进行扩展,就可以在扩展类里面添加即可

包装类:包装类可以包装任何pojo类型的信息,可能有商品信息(商品类),用户信息(用户类),订单信息(订单类)等等。那只需要将我们定义的很多的扩展类引用到包装类中,包装类就可以使用所有的原始属性和扩展属性了。

这样做的好处:在有很多类很多属性的情况下清晰明了,便于管理;并可以将包装类从表现层一直传递到持久层没有一点鸭梨。。。。。

自定义商品查询mapper 

需求:根据商品名称查询商品

 ItemsMapperCustom.xml

包装类作为parameterType ,如果有多张表的子查询,可以传递所有被包装类的属性。

扩展类作为resultType。现在的例子我们查询的是items表,如果定义成Items,有可能会出现属性不够用的情况,所以定义成扩展类。

ItemsMapperCustom.java

 

2、整合service

定义service接口

实现service接口实现类

在applicaation-service.xml中配置service

整合springmvc

web.xml配置前端控制器

 

srpingmvc.xml

	<!-- 可以扫描controller、service、 这里扫描的是controller包 -->
	<context:component-scan base-package="com.ssm.controller"></context:component-scan>

	<!-- 视图解析器 解析jsp,默认使用jstl标签, -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>

	<!-- 使用mvc:annotation-driven代替上面的注解映射器和注解适配器 mvc:annotation-driven默认加载很多的参数绑定,比如json转换解析器就默认加载了 
		实际开发使用mvc:annotation-driven -->
	<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
	<bean id="conversionService"
		class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
		<property name="converters">
			<!-- 自定义转换器的类名 -->
			<bean class="com.ssm.controller.converter.CustomDateConverter"></bean>
		</property>
	</bean>
</beans>

编写Controller(就是Handler)

@Controller
@RequestMapping("/item")
public class ItemsHandler {
	@Autowired
	private ItemsService itemsService;
	
	@RequestMapping("/itemsQuery")
	public ModelAndView ItemsQueryList() throws Exception{
		
		//System.out.println(request.getParameter("id"));
		List<ItemsCustom> list = itemsService.findItemsList(null);
		
		ModelAndView modelAndView = new ModelAndView();
		
		modelAndView.addObject("itemsList", list);
		
		modelAndView.setViewName("/items/itemslist");
		
		return modelAndView;
	}

 编写itemsList.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>查询商品列表</title>
</head>

<body>
<form action="${pageContext.request.contextPath }/item/itemsQuery.action" method="get">
查询条件
	<table width="100%" border="1px">
		
		<tr>
		
			<td>商品名称
			</td>
			
			<td>商品价格
			</td>
			
			<td>生产日期
			</td>
			
			<td>商品描述
			</td>
			
			<td>操作
			</td>
			
		</tr>
		
		<c:forEach items="${itemsList}" var="item">
			<tr>
				<td>${item.name }
				</td>
				
				<td>${item.price }
				</td>
				
				<td><fmt:formatDate value="${item.createtime }" pattern="yyyy-MM-dd HH:mm:ss"/></td>
				
				<td>${item.detail }
				</td>
				
				<td><a href="${pageContext.request.contextPath }/item/editItems.action?id=${item.id}" >修改</a>
				</td>
				<%-- id=${item.id} --%>
			</tr>
		</c:forEach>
	
	</table>
</form>
</body>

</html>

加载spring容器 

项目启动时自动加载applicationContext.xml配置信息

	<!-- 加载spring容器 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<!--1. 编译后applicationaContextx.xml问价加载到该目录下 -->
		<!-- <param-value>/WEB-INF/classes/spring/applicationContext-*.xml</param-value> -->
		<!--2. 项目中applicationContext-*.xml文件的位置 
			两种方式都可行
		-->
		<param-value>classpath:spring/applicationContext-*.xml</param-value>
	</context-param>

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

 ContextLoaderListener的作用

Spring提供ServletContentListener的一个实现类ContextLoaderListener监听器,该类可以作为Listener使用,在启动Tomcat

容器的时候,该类的作用就是自动装载ApplicationContext的配置信息,如果没有设置contextConfigLocation的初始参数则会

使用默认参数WEB-INF路径下的application.xml文件。
--------------------- 
原文:https://blog.csdn.net/java_wliang/article/details/18044507 
 

 

商品修改功能开发

需求

操作流程: 
1、进入商品查询列表页面

2、点击修改,进入商品修改页面,页面中显示了要修改的商品(从数据库查询) 要修改的商品从数据库查询,根据商品id(主键)查询商品信息

3、在商品修改页面,修改商品信息,修改后,点击提交

开发mapper

1、首先根据id查询出要修改的商品

Items selectByPrimaryKey(Integer id);

2、根据id修改表数据

int updateByPrimaryKeyWithBLOBs(ItemsCustom itemsCustom);

逆向工程已有

2、开发service

接口功能:

根据id查询商品信息

参数:id    返回值:ItemsCustom

修改商品信息

参数  :id,接收修改后的信息ItemsCusotm

3、开发Controller

@Controller
@RequestMapping("/item")
public class ItemsHandler {
	@Autowired
	private ItemsService itemsService;
	
	@RequestMapping(value="/itemsQuery",method={RequestMethod.POST,RequestMethod.GET})
	public ModelAndView ItemsQueryList() throws Exception{
		
		//System.out.println(request.getParameter("id"));
		List<ItemsCustom> list = itemsService.findItemsList(null);
		
		ModelAndView modelAndView = new ModelAndView();
		
		modelAndView.addObject("itemsList", list);
		
		modelAndView.setViewName("/items/itemslist");
		
		return modelAndView;
	}
	
//	@RequestMapping(value="/editItems" ,method={RequestMethod.POST,RequestMethod.GET})
//	public ModelAndView editItems() throws Exception{
//	
//		ItemsCustom itemsCustom = itemsService.findItemsById(1);
//		
//		ModelAndView modelAndView = new ModelAndView();
//		
//		modelAndView.addObject("itemsCustom", itemsCustom);
//		
//		modelAndView.setViewName("/items/editItems");
//		return modelAndView;
//		
//		
//	}
	
	@RequestMapping(value="/editItems" ,method={RequestMethod.POST,RequestMethod.GET})
	public String editItems(HttpServletRequest request,Model model,@RequestParam(value="id") Integer item_id) throws Exception{
	
		//String name = new String(request.getParameter("name").getBytes("ISO8859-1"),"utf-8");
		ItemsCustom itemsCustom = itemsService.findItemsById(item_id);
		
		model.addAttribute("itemsCustom", itemsCustom);
		
		//modelAndView.setViewName("/items/editItems")
		return "/items/editItems";
	}
	
	@RequestMapping("/editItemsSubmit")
	public String editItemsSubmit(HttpServletRequest request,Integer id,ItemsCustom itemsCustom) throws Exception{
		
		//String name = new String(request.getParameter("name").getBytes("ISO8859-1"),"utf-8");
		itemsService.updateItems(id, itemsCustom);
		
		return "forward:itemsQuery.action";
	}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值