广告
返回顶部
首页 > 资讯 > 数据库 >springmvc经典教程(2)
  • 230
分享到

springmvc经典教程(2)

2024-04-02 19:04:59 230人浏览 安东尼
摘要

springmvc教程系列springMVC史上最好教程(2)springmvc史上最好教程(1)springmvc史上最好教程(3)springmvc史上最好教程(4)一、整合mybatis为了更好的学习

springmvc教程系列

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

 

<?xml version="1.0" encoding="UTF-8" ?>  

  

<!DOCTYPE configuration  

  

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

  

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

  

<configuration>  

  

<!—使用自动扫描器时,mapper.xml文件如果和mapper.java接口在一个目录则此处不用定义mappers -->  

  

<mappers>  

  

<package name="com.sihai.SSM.mapper" />  

  

</mappers>  

  

</configuration>  



1.3.2 applicationContext-dao.xml

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

 

<beans xmlns="http://www.springframework.org/schema/beans"  

  

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

  

<!-- 加载配置文件 -->  

  

<context:property-placeholder location="classpath:db.properties"/>  

  

<!-- 数据库连接池 -->  

  

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  

  

       <property name="driverClassName" value="${jdbc.driver}"/>  

  

<property name="url" value="${jdbc.url}"/>  

  

<property name="username" value="${jdbc.username}"/>  

  

<property name="passWord" value="${jdbc.password}"/>  

  

<property name="maxActive" value="30"/>  

  

<property name="maxIdle" value="5"/>  

  

</bean>  

  

<!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->  

  

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  

  

<!-- 数据库连接池 -->  

  

<property name="dataSource" ref="dataSource" />  

  

<!-- 加载mybatis的全局配置文件 -->  

  

<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />  

  

</bean>  

  

<!-- mapper扫描器 -->  

  

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  

  

 <property name="basePackage" value="com.sihai.springmvc.mapper"></property>  

  

<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>  

  

</bean>  

  

</beans>  



1.3.3 ItemsMapper.xml

 

<?xml version="1.0" encoding="UTF-8" ?>  

  

<!DOCTYPE mapper  

  

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

  

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

  

<mapper namespace="com.sihai.ssm.mapper.ItemsMapper">  

  

<!-- sql片段 -->  

  

<!-- 商品查询条件 -->  

  

<sql id="query_items_where">  

  

<if test="items!=null">  

  

<if test="items.name!=null and items.name!=''">  

  

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

  

</if>  

  

</if>  

  

</sql>  

  

<!-- 查询商品信息 -->  

  

<select id="findItemsList" parameterType="queryVo" resultType="items">  

  

select * from items  

  

<where>  

  

<include refid="query_items_where"/>  

  

</where>  

  

</select>  

  

</mapper>  



1.3.4 ItemsMapper.java

 

public interface ItemsMapper {  

  

//商品列表  

  

public List<Items> 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

配置事务管理器。

 

<beans xmlns="http://www.springframework.org/schema/beans"  

  

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

  

<!-- 事务管理器 -->  

  

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  

  

<!-- 数据源 -->  

  

<property name="dataSource" ref="dataSource"/>  

  

</bean>  

  

<!-- 通知 -->  

  

<tx:advice id="txAdvice" transaction-manager="transactionManager">  

  

  <tx:attributes>  

  

     <!-- 传播行为 -->  

  

    <tx:method name="save*" propagation="REQUIRED"/>  

  

    <tx:method name="insert*" propagation="REQUIRED"/>  

  

    <tx:method name="delete*" propagation="REQUIRED"/>  

  

    <tx:method name="update*" propagation="REQUIRED"/>  

  

    <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>  

  

    <tx:method name="get*" propagation="SUPPORTS" read-only="true"/>  

  

  </tx:attributes>  

  

</tx:advice>  

  

<!-- 切面 -->  

  

<aop:config>  

  

  <aop:advisor advice-ref="txAdvice"  

  

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

  

</aop:config>  

  

</beans>  



1.4.3 OrderService


public interface OrderService {  

  

//商品查询列表  

  

public List<Items> findItemsList(QueryVo queryVo)throws Exception;  

  

}  

  

@Autowired  

  

private ItemsMapper itemsMapper;  

  

@Override  

  

public List<Items> findItemsList(QueryVo queryVo) throws Exception {  

  

//查询商品信息  

  

return itemsMapper.findItemsList(queryVo);  

  

}  

  

}  



1.5 Action

 

1.5.1 springmvc.xml

 

<beans xmlns="http://www.springframework.org/schema/beans"  

  

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

  

<!-- 扫描controller注解,多个包中间使用半角逗号分隔 -->  

  

<context:component-scan base-package="com.sihai.ssm.controller"/>  

  

<!--注解映射器 -->  

  

<bean class="org.springframework.WEB.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>  

  

<!--注解适配器 -->  

  

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>  

  

<!-- ViewResolver -->  

  

<bean  

  

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

  

<property name="viewClass"  

  

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

  

<property name="prefix" value="/WEB-INF/jsp/" />  

  

<property name="suffix" value=".jsp" />  

  

</bean>  

  

</beans>  



1.5.2 web.xml

 

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

 

<?xml version="1.0" encoding="UTF-8"?>  

  

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  

  

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

  

<display-name>springmvc</display-name>  

  

<!-- 加载spring容器 -->  

  

<context-param>  

  

<param-name>contextConfigLocation</param-name>  

  

<param-value>/WEB-INF/classes/spring/applicationContext.xml,/WEB-INF/classes/spring/applicationContext-*.xml</param-value>  

  

</context-param>  

  

<listener>  

  

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  

  

</listener>  

  

<!-- 解决post乱码 -->  

  

<filter>  

  

<filter-name>CharacterEncodingFilter</filter-name>  

  

<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  

  

<init-param>  

  

<param-name>encoding</param-name>  

  

<param-value>utf-8</param-value>  

  

</init-param>  

  

</filter>  

  

<filter-mapping>  

  

<filter-name>CharacterEncodingFilter</filter-name>  

  

<url-pattern>/*</url-pattern>  

  

</filter-mapping>  

  

<!-- springmvc的前端控制器 -->  

  

<servlet>  

  

<servlet-name>springmvc</servlet-name>  

  

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  

  

<!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation, springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" -->  

  

<init-param>  

  

<param-name>contextConfigLocation</param-name>  

  

<param-value>classpath:spring/springmvc.xml</param-value>  

  

</init-param>  

  

<load-on-startup>1</load-on-startup>  

  

</servlet>  

  

<servlet-mapping>  

  

<servlet-name>springmvc</servlet-name>  

  

<url-pattern>*.action</url-pattern>  

  

</servlet-mapping>  

  

<welcome-file-list>  

  

<welcome-file>index.html</welcome-file>  

  

<welcome-file>index.htm</welcome-file>  

  

<welcome-file>index.jsp</welcome-file>  

  

<welcome-file>default.html</welcome-file>  

  

<welcome-file>default.htm</welcome-file>  

  

<welcome-file>default.jsp</welcome-file>  

  

</welcome-file-list>  

  

</web-app>  

  

1.5.3 OrderController


@Controller  

  

public class OrderController {  

  

@Autowired  

  

private OrderService orderService; 

  

@RequestMapping("/queryItem.action") 

public ModelAndView queryItem() throws Exception {  

  

// 商品列表  

  

List<Items> 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


您可能感兴趣的文档:

--结束END--

本文标题: springmvc经典教程(2)

本文链接: https://www.lsjlt.com/news/44626.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • springmvc经典教程(2)
    springmvc教程系列springmvc史上最好教程(2)springmvc史上最好教程(1)springmvc史上最好教程(3)springmvc史上最好教程(4)一、整合mybatis为了更好的学习...
    99+
    2022-10-18
  • springmvc经典教程(3)
    springmvc史上最好教程(2)springmvc史上最好教程(1)1.1 需求使用springmvc+mybatis架构实现商品信息维护。  1.2 商品修改1.2.1 dao使用逆向工程自动生成的代码:ItemsMapper.Jav...
    99+
    2023-01-31
    教程 经典 springmvc
  • python游戏库pygame经典教程
    目录 一.Pygame程序基本搭建过程         1.初始化化程序         2.创建Surface对象         3.事件监听         4.游戏循环  二.Pygame Display显示模块详解        ...
    99+
    2023-08-31
    python pygame 游戏
  • Python基本语法经典教程
    本文讲述了Python基本语法。分享给大家供大家参考,具体如下: 概述: 这里主要讲述以下内容: ① 缩进 ② 流程控制语句 ③ 表达式 ④ 函数 ⑤ 对象的方法 ⑥ 类型 ⑦ 数学运算 1. 缩进 Pyt...
    99+
    2022-06-04
    语法 教程 经典
  • 经典教程|10 分钟速成 Python3
        Python 是由吉多·范罗苏姆(Guido Van Rossum)在 90 年代早期设计。 它是如今最常用的编程语言之一。它的语法简洁且优美,几乎就是可执行的伪代码。 注意:这篇教程是基于 Python 3 写的。  ...
    99+
    2023-01-30
    教程 经典
  • python游戏库pygame经典教程(推荐!)
    目录一.Pygame程序基本搭建过程1.初始化化程序2.创建Surface对象3.事件监听4.游戏循环 二.Pygame Display显示模块详解1.将Surface对象...
    99+
    2022-12-08
    python pygame模块 python pygame小游戏 python中pygame库的功能
  • 【经典PageRank 】02/2 算法和线性代数
    系列前文:【经典 PageRank 】01/2 PageRank的基本原理-CSDN博客 一、说明         并非所有连接都同样重要!         该算法由 Sergey 和 Lawrence 开发,用于在 ...
    99+
    2023-10-26
    机器学习 人工智能 数据挖掘
  • Python正则表达式经典入门教程
    本文实例总结了Python正则表达式基本用法。分享给大家供大家参考,具体如下: 正则表达式在平时做文本处理(爬虫程序去解析html中的字段,在分析log文件的时候需要抓取一些关键数据)的时候经常会用到。一般...
    99+
    2022-06-04
    入门教程 经典 正则表达式
  • win10系统主题改为经典模式教程
    许多用户习惯了经典Windows界面,所以更新系统后,想知道win如何将10个主题改为经典模式,其实我们只需要进入个性化的主题设置就可以改变。下面小编就来介绍一下。win10系统主题改为经典模式教程,让我们一起来了解一下。win10系统主题...
    99+
    2023-07-10
  • win10系统主题变成了经典模式教程
    很多用户习惯于传统的Windows界面,所以当系统更新之后,想要知道win10的主题是如何转变为经典模式,其实我们只需进入个性化主题设置即可。来学习一下win10系统主题介绍的经典模式教程。win10系统主题变成了经典模式教程。首先,点击左...
    99+
    2023-07-10
  • java版十大排序经典算法:完整代码(2)
    目录快速排序完整代码:直接选择排序完整代码:堆排序完整代码:总结快速排序 简单解释: 快速排序就是每次找一个基点(第一个元素),然后两个哨兵,一个从最前面往后走,一个从最后面往前面...
    99+
    2022-11-12
  • python经典书籍:Python编程实
    Python编程实战主要关注了四个方面 即:优雅编码设计模式、通过并发和编译后的Python(Cython)使处理速度更快、高层联网和图像。书中展示了在Python中已经过验证有用的设计模式,用专家级的代码阐释了这些设计模式,并解释了...
    99+
    2023-01-31
    书籍 经典 python
  • PHP初级教程------------------(2)
    目录 运算符 赋值运算符 算术运算符 比较运算符 逻辑运算符 连接运算符 错误抑制符 三目运算符 自操作运算符 ​编辑 计算机码 位运算符 运算符优先级 流程控制 控制分类 顺序结构 分支结构 If分...
    99+
    2023-09-12
    java 开发语言
  • Tensorflow快餐教程(2) -
    Tensorflow的Tensor意为张量。一般如果是0维的数组,就是一个数据,我们称之为标是Scalar;1维的数组,称为向量Vector;2维的数组,称为矩阵Matrics;3维及以上的数组,称为张量Tensor。在机器学习中,用途最...
    99+
    2023-01-31
    快餐 教程 Tensorflow
  • python教程(七)·字典
    本文介绍本系列教程最后一个数据结构——字典 在现实生活中,查英语字典的时候,我们通常根据单词来查找意思。而python中的字典也是类似的,根据特定的 “键”(单词)来查找 “值”(意思)。 字典的基本使用 下面以电话簿为例,我们的电话...
    99+
    2023-01-31
    字典 教程 python
  • Django网站实战之制作一个经典网站的方法教程
    这篇文章主要介绍“Django网站实战之制作一个经典网站的方法教程”,在日常操作中,相信很多人在Django网站实战之制作一个经典网站的方法教程问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,...
    99+
    2022-10-19
  • 【SpringMVC】上篇,超详细的教程带你学会SpringMVC
    ✅作者简介:热爱Java后端开发的一名学习者,大家可以跟我一起讨论各种问题喔。 🍎个人主页:Hhzzy99 🍊个人信条:坚持就是胜利! 💞当前专栏:【Spring】 🥭本文...
    99+
    2023-08-18
    java servlet spring
  • shiro教程(2)- shiro介绍
    shiro教程系列shiro教程(3)-shiro授权1 shiro介绍 1.1 什么是shiroShiro是apache旗下一个开源框架,它将软件系统的安全认证相关的功能抽取...
    99+
    2022-10-18
  • Java经典面试题汇总--多线程
    目录1.并行和并发有什么区别?2.线程和进程的区别?3.守护线程是什么?4.实现多线程的方式有哪些?5.说一下runnable和callable有什么区别?6.sleep()和wai...
    99+
    2022-11-12
  • Java经典面试题汇总:多线程
    目录1. 并行和并发有什么区别?2.线程和进程的区别?3.守护线程是什么?4.实现多线程的方式有哪些?5.说一下runnable和callable有什么区别?6.sleep...
    99+
    2022-11-12
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作