网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

springmvc经典教程(2)

springmvc教程系列

10余年的延长网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。网络营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整延长建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。成都创新互联公司从事“延长网站设计”,“延长网站推广”以来,每个客户项目都认真落实执行。

springmvc史上最好教程(2)

springmvc史上最好教程(1)

springmvc史上最好教程(3)

springmvc史上最好教程(4)

一、整合mybatis

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

 

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

 

1.1 需求

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

 

1.2 jar包

 

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

 

1.3 Dao

目标:

1、spring管理SqlSessionFactory、mapper

 

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

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

 

  

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

目标:

1、Service由spring管理

2、spring对Service进行事务控制。

 

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



网页题目:springmvc经典教程(2)
文章位置:http://bjjierui.cn/article/psheis.html

其他资讯