资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

springmvc经典教程(2)-创新互联

springmvc教程系列

网站前端页面设计会用DIV+CSS架构,布局出来的网站外观简洁大气。HTML静态,H5建站+CSS3网站,自适应电脑、手机、平板,符合用户体验的习惯,更容易与用户产生互动。专业网络公司的服务理念是“高性价比建站,让企业网站具备营销价值,促进长期合作共赢模式”。

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

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


本文名称:springmvc经典教程(2)-创新互联
文章地址:http://cdkjz.cn/article/epggh.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220