千家信息网

springmvc经典教程(2)

发表于:2025-02-01 作者:千家信息网编辑
千家信息网最后更新 2025年02月01日,springmvc教程系列springmvc史上最好教程(2)springmvc史上最好教程(1)springmvc史上最好教程(3)springmvc史上最好教程(4)一、整合mybatis为了更好
千家信息网最后更新 2025年02月01日springmvc经典教程(2)

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}%'



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


0