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


相关内容

热门资讯

人民日报评河南考试作弊:基层工... 人民日报客户端 余双之针对2026年“三支一扶”计划招募舆情,河南省“三支一扶”工作领导小组协调办公...
普京登台踢足球,学生们兴奋大喊 俄罗斯总统普京8月3日到访克拉斯诺亚尔斯克,出席“大变革”竞赛的闭幕式。俄新社8月3日报道,“大变革...
河南发生5人灭门惨案?当地辟谣 近日,某平台账号“寻找下一站幸福”“陈哥药膳卤味(创业记录)”发布信息称西平县发生五人灭门惨案,引发...
“抗生素牛蛙”为何难杀? “抗生素牛蛙”,又重出江湖了!准确说,一直就隐匿在江湖根除不尽,太难杀了!近日,有媒体随机网购了一些...
没招了?美媒爆料:美军群发邮件... 据美国有线电视新闻网(CNN)8月3日报道,多名消息人士透露,美军中央司令部的高级官员上周发出一封群...
“荷兰弟”替身是个“河南弟”:... 封面新闻记者 王一理目前,电影《蜘蛛侠:崭新之日》正在热映中。8月2日,河南演员王壮壮发文表示,自己...
3人用AI合成700多份不雅照... 近日,山东德州,当地邮政部门工作人员发现多处邮箱出现笔迹一致的可疑信件,收件人多为企业高管、老板等。...
他扮演蜘蛛侠涨粉千万,流量密码... Joshua Watson(约书亚·沃森)是一位美国知名的视觉特效(VFX)内容创作者和社交媒体影响...
国企信创数字化改造,部署 AI... 国企信创数字化改造,最难的不是把 AI 智能体“装上去”,而是让它在国产电脑、国产操作系统和现有业务...
三宜电气申请户外高压限流熔断器... 国家知识产权局信息显示,浙江三宜电气有限公司申请一项名为“一种户外高压限流熔断器”的专利,公开号CN...