springmvc经典教程(2)
admin
2023-05-20 11:22:55
0

springmvc教程系列

springmvc史上最好教程(2)

springmvc史上最好教程(1)

springmvc史上最好教程(3)

springmvc史上最好教程(4)


一、整合mybatis

为了更好的学习 springmvcmybatis整合开发的方法,需要将springmvcmybatis进行整合。

 

整合目标:控制层采用springmvc、持久层使用mybatis实现。

 

1.1 需求

实现商品查询列表,从MySQL数据库查询商品信息。

 

1.2 jar

 

包括:spring(包括springmvc)、mybatismybatis-spring整合包、数据库驱动、第三方连接池。

 

1.3 Dao

目标:

1spring管理SqlSessionFactorymapper

 

1.3.1 sqlMapConfig.xml

 

classpath下创建mybatis/sqlMapConfig.xml

 

  

  

  

PUBLIC "-//mybatis.org//DTD Config 3.0//EN"  

  

"http://mybatis.org/dtd/mybatis-3-config.dtd">  

  

  

  

  

  

  

  

  

  

  

  

  



1.3.2 applicationContext-dao.xml

配置数据源、事务管理,配置SqlSessionFactorymapper扫描器。

 

  

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"  

  

xmlns:context="http://www.springframework.org/schema/context"  

  

xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  

  

xsi:schemaLocation="http://www.springframework.org/schema/beans  

  

http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  

  

http://www.springframework.org/schema/mvc  

  

http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd  

  

http://www.springframework.org/schema/context  

  

http://www.springframework.org/schema/context/spring-context-3.2.xsd  

  

http://www.springframework.org/schema/aop  

  

http://www.springframework.org/schema/aop/spring-aop-3.2.xsd  

  

http://www.springframework.org/schema/tx  

  

http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">  

  

  

  

  

  

  

  

  

  

         

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

   

  

  

  

  

  

  



1.3.3 ItemsMapper.xml

 

  

  

  

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  

  

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">  

  

  

  

  

  

  

  

  

  

  

  

  

  

and items.name like '%${items.name}%'  

  

  

  

  

  

  

  

  

  

  

  

select * from items  

  

  

  

  

  

  

  

  

  

  



1.3.4 ItemsMapper.java

 

public interface ItemsMapper {  

  

//商品列表  

  

public List findItemsList(QueryVo queryVo) throws Exception;  

  

}  



1.4 Service

目标:

1Servicespring管理

2springService进行事务控制。

 

1.4.1 applicationContext-service.xml

配置service接口。

 

1.4.2 applicationContext-transaction.xml

配置事务管理器。

 

  

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"  

  

xmlns:context="http://www.springframework.org/schema/context"  

  

xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  

  

xsi:schemaLocation="http://www.springframework.org/schema/beans  

  

http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  

  

http://www.springframework.org/schema/mvc  

  

http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd  

  

http://www.springframework.org/schema/context  

  

http://www.springframework.org/schema/context/spring-context-3.2.xsd  

  

http://www.springframework.org/schema/aop  

  

http://www.springframework.org/schema/aop/spring-aop-3.2.xsd  

  

http://www.springframework.org/schema/tx  

  

http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

    

  

       

  

      

  

      

  

      

  

      

  

      

  

      

  

    

  

  

  

  

  

  

  

  

  

  pointcut="execution(* com.sihai.springmvc.service.impl.*.*(..))"/>  

  

  

  

  



1.4.3 OrderService


public interface OrderService {  

  

//商品查询列表  

  

public List findItemsList(QueryVo queryVo)throws Exception;  

  

}  

  

@Autowired  

  

private ItemsMapper itemsMapper;  

  

@Override  

  

public List findItemsList(QueryVo queryVo) throws Exception {  

  

//查询商品信息  

  

return itemsMapper.findItemsList(queryVo);  

  

}  

  

}  



1.5 Action

 

1.5.1 springmvc.xml

 

  

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"  

  

xmlns:context="http://www.springframework.org/schema/context"  

  

xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  

  

xsi:schemaLocation="http://www.springframework.org/schema/beans  

  

http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  

  

http://www.springframework.org/schema/mvc  

  

http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd  

  

http://www.springframework.org/schema/context  

  

http://www.springframework.org/schema/context/spring-context-3.2.xsd  

  

http://www.springframework.org/schema/aop  

  

http://www.springframework.org/schema/aop/spring-aop-3.2.xsd  

  

http://www.springframework.org/schema/tx  

  

http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

class="org.springframework.web.servlet.view.InternalResourceViewResolver">  

  

  

value="org.springframework.web.servlet.view.JstlView" />  

  

  

  

  

  

  

  

  



1.5.2 web.xml

 

加载spring容器,配置springmvc前置控制器。

 

  

  

  

xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  

  

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">  

  

springmvc  

  

  

  

  

  

contextConfigLocation  

  

/WEB-INF/classes/spring/applicationContext.xml,/WEB-INF/classes/spring/applicationContext-*.xml  

  

  

  

  

  

org.springframework.web.context.ContextLoaderListener  

  

  

  

  

  

  

  

CharacterEncodingFilter  

  

org.springframework.web.filter.CharacterEncodingFilter  

  

  

  

encoding  

  

utf-8  

  

  

  

  

  

  

  

CharacterEncodingFilter  

  

/*  

  

  

  

  

  

  

  

springmvc  

  

org.springframework.web.servlet.DispatcherServlet  

  

  

  

  

  

contextConfigLocation  

  

classpath:spring/springmvc.xml  

  

  

  

1  

  

  

  

  

  

springmvc  

  

*.action  

  

  

  

  

  

index.html  

  

index.htm  

  

index.jsp  

  

default.html  

  

default.htm  

  

default.jsp  

  

  

  

  

  

1.5.3 OrderController


@Controller  

  

public class OrderController {  

  

@Autowired  

  

private OrderService orderService; 

  

@RequestMapping("/queryItem.action") 

public ModelAndView queryItem() throws Exception {  

  

// 商品列表  

  

List itemsList = orderService.findItemsList(null); 

  

// 创建modelAndView准备填充数据、设置视图  

  

ModelAndView modelAndView = new ModelAndView();  

  

// 填充数据  

  

modelAndView.addObject("itemsList", itemsList);  

  

// 视图  

  

modelAndView.setViewName("order/itemsList");  

  

return modelAndView;  

  

}  

  

}  

  

1.6 测试

http://localhost:8080/springmvc_mybatis/queryItem.action


相关内容

热门资讯

官方通报柳州5.2级地震:3人... 据中国地震台网正式测定,2026年5月18日00时21分,在广西柳州市柳南区发生5.2级地震。在此前...
广西柳州发生5.2级地震,已致... 新华社南宁5月18日电(记者农冠斌、杨驰)5月18日0时21分,广西柳州市柳南区发生5.2级地震,震...
国家信息光电子创新中心成功研发... 5月15日消息,据“中国光谷”公众号,国家信息光电子创新中心(NOEIC)近日成功自主研发了一款超宽...
特朗普称如果伊朗不迅速行动,“... 新华社华盛顿5月17日电(记者徐剑梅 黄强)美国总统特朗普17日在社交媒体发文称,如果伊朗不迅速行动...
柳州5.2级地震致楼房倒塌,店... 据中国地震台网消息,5月18日00时21分在广西柳州市柳南区发生5.2级地震,震源深度8千米。有网友...
“华南第一商圈”再升级!全国首... 5月16日,“万兆AI惠商,联通美好未来”2026年517电信日暨中国联通品牌与产品广东宣传推广会在...
原创 3... 如果今年618你手里预算在3000多元,又特别看重拍照,我反而不建议只盯着那些刚发布、热度很高的新机...
苹果首款折叠屏iPhone U... 快科技5月17日消息,今年4月曾有消息称,苹果首款折叠屏手机iPhoneUltra(iPhone F...
广西柳州发生5.2级地震,南宁... 据中国地震台网正式测定,5月18日0时21分在广西柳州市柳南区发生5.2级地震,震源深度8公里,震中...
购药新规落地!多地药房称买“减... 5月17日消息,近日有消息称,自5月15日起,司美格鲁肽、替尔泊肽等GLP-1药物均需要凭有效期内的...