这次的学习的任务是从我们之前搭建好的框架上加一些业务逻辑(查)
**概述:**运行的效果是一个页面有个查询表中数据的超链接,在点击查询链接的时候,把数据库中的表里面的数据全部查出来。
以订单做个例子:

第一步:在数据库中创建一个表,表名为goods,分别有(id,name,money)个字段,实例如下:

第二步:创建一个WEB动态工程,结构如下

第三步:搭建好SSM整合的框架

  1. 创建一个xxx.xxx.xxx.controller包用来存放controller文件

  2. 创建一个xxx.xxx.xxx.dao包用来存放”接口+实体映射文件

  3. 创建一个xxx.xxx.xxx.pojo包用来存放实体映射文件

  4. 创建一个config包用来存放spring主配置文件+Mabatis主配置文件

    包名自定义,但是你要知道对应的包是用来干什么的

  5. 在WEB-INF目录下创建一个包用来存放你的自定义jsp文件

第四步:导ssm整合的jar包(放在lib包下)

点击我下载SSM整合需要的jar包

第五步:配置web.xml(

  1. 配置中央前端控制器
  2. 配置容器监听器
  3. 配置过滤器 )

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"><display-name>SSMZhengHeDemo</display-name><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><!--  配置中央前端控制器 --><servlet><servlet-name>springmvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>/</url-pattern></servlet-mapping>
<!--   配置容器监听器 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath*:configFO/spring.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><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><init-param><param-name>forceRequestEncoding</param-name><param-value>true</param-value></init-param><init-param><param-name>forceResponseEncoding</param-name><param-value>true</param-value></init-param></filter><filter-mapping><filter-name>CharacterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping>
</web-app>

第六步:创建一个SpringMvc的主配置文件,且配置:

  1. 开启注解并扫描指定包
  2. 配置视图资源解析器(配置前缀,后缀)

springmvc-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"><context:component-scan base-package="com.anzhuo.cm.controller"></context:component-scan><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/pages/"></property><property name="suffix" value=".jsp"></property></bean>
</beans>

第七步:在configFO包下创建一个spring的主配置文件,且配置:

  1. 配置数据源
  2. 配置事物管理
  3. 配置数据库会话工厂
  4. 配置映射扫描器
    spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"xmlns:lang="http://www.springframework.org/schema/lang" xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsdhttp://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsdhttp://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsdhttp://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"><!-- 连接数据源 --><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver"></property><property name="url" value="jdbc:mysql://localhost:3306/ssm"></property><property name="username" value="root"></property><property name="password" value="123"></property></bean><!-- 事物管理 --><bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean><!-- 生产SqlSessionFactoryBean的bean --><bean id="sessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"></property><property name="configLocation" value="classpath:configFO/config.xml"></property><property name="mapperLocations" value="classpath*:com/anzhuo/cm/dao/*.xml"></property></bean><!-- MapperScannerConfigurer:(配置映射掃描儀)专门用来扫描包下面的类或接口 --><bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.anzhuo.cm.dao"></property></bean>
</beans>

第八步:在configFO包下创建一个config的主配置文件,且配置:

  1. 配置别名(实体类)
    config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "config" "mybatis-3-config.dtd" >
<configuration>
<typeAliases>
<package name="com.anzhuo.cm.pojo"/>
</typeAliases>
</configuration>

第九步:在WebContent下面建一个index.jsp,在index.jsp里面写一个链接,这个链接是链接到控制器里面去的,实例如下:

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>查询表中数据</title>
</head>
<body>
<a href="./goods/select">点我查询表中数据!!!</a>
</body>
</html>

第十步:在com.anzhuo.cm.controller包下创建一个ControllerDemo.java文件用来写业务逻辑,实例如下:

ControllerDemo.java

package com.anzhuo.cm.controller;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;import com.anzhuo.cm.dao.IGoods;@Controller
@RequestMapping("goods")
public class ControllerDemo {@AutowiredIGoods god;@RequestMapping("/select")public ModelAndView model(){ModelAndView mv = new ModelAndView("list");List li = god.getGoods();mv.addObject("key", li);return mv;}
}

第十一步:在com.anzhuo.cm.pojo包下写对应表的实体类,实体类名与表名保持一致,首字母大写,实例如下:

Goods.java

package com.anzhuo.cm.pojo;public class Goods {int id;String name;double money;
public int getId() {return id;
}
public void setId(int id) {this.id = id;
}
public String getName() {return name;
}
public void setName(String name) {this.name = name;
}
public double getMoney() {return money;
}
public void setMoney(double money) {this.money = money;
}}

第十二步:在com.anzhuo.cm.dao包下创建实体类的映射文件,在实体类的映射文件中编写查询表的Sql语句,实例如下:

mapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "mapper" "mybatis-3-mapper.dtd" >
<mapper namespace="com.anzhuo.cm.dao.IGoods"><select id="getGoods" resultType="Goods">select * from goods  </select>
</mapper>

第十三步:在com.anzhuo.cm.dao包下创建一个接口,里面写调用时需要使用的方法,实例如下:

IGoods.java

package com.anzhuo.cm.dao;import java.util.List;public interface IGoods {public List  getGoods();
}

第十四步:当你controller里面的值都拿到了需要返回给modelAndView指定的jsp页面中,那么我对着modelAndView里面返回的的jsp页面,上面是返回给list.jsp,所有我们需要在指定文件夹中新建一个jsp页面(这个指定文件夹是你Springmvc-servlet.xml里面配置的前缀),且里面写遍历你所有查询到的值,实例如下:

list.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><!-- 标签库,指令,prefix="c"的意思是给uri定义一个名字,jstl:Alt+/:看后缀、jstl/core --><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body><c:forEach items="${key}" var="list"><h1>${list.name}</h1><h1>${list.money}</h1></c:forEach>
</body>
</html>

这样我们简单的框架加上一个查询业务就完成啦!

SSM整合对数据库表的查询相关推荐

  1. mysql表空间名字查询_数据库表空间信息查询

    数据库表空间信息查询 --查看表空间的名称及大小 www.2cto.com SELECT T.TABLESPACE_NAME, ROUND(SUM(D.BYTES / (1024 * 1024)), ...

  2. Oracle数据库表连接查询并分页SQL语句提示未明确定义列

    Oracle数据库表连接查询并分页SQL语句提示未明确定义列 两张表中的字段: t_product t_category product_id category_id product_name cat ...

  3. mysql学习笔记(五) 数据库表的查询基本操作

    数据库表的查询基本操作 DQL(Data Query Language): 查询操作. 一.单表查询: 一.普通查询 --查询student表中的所有数据 select *from student - ...

  4. mysql查一个表3到5行的数据类型_MySQL入门(三) 数据库表的查询操作【重要】

    序言 本节比较重要,对数据表数据进行查询操作,其中可能大家不熟悉的就对于INNER JOIN(内连接).LEFT JOIN(左连接).RIGHT JOIN(右连接)等一些复杂查询. 通过本节的学习,可 ...

  5. mysql水果表查询_最全MySQL数据库表的查询操作

    序言 本节比较重要,对数据表数据进行查询操作,其中可能大家不熟悉的就对于INNER JOIN(内连接).LEFT JOIN(左连接).RIGHT JOIN(右连接)等一些复杂查询. 通过本节的学习,可 ...

  6. DQL操作(数据库表数据查询操作)

    DQL数据查询语言 数据库执行DQL语句不会对数据进行改变,而是让数据库发送结果集给客户端. 查询返回的结果集是一张虚拟表. 查询关键字:SELECT 语法: SELECT 列名 FROM表名[WHE ...

  7. 黑马ssm整合mysql数据库

    ssm整合的mysql数据库完整版 关注我,有更多黑马程序员的笔记,干货也会慢慢发出来了哦 关注我不迷路,点击我,一起来看看吧. -- 会员 CREATE TABLE member(id VARCHA ...

  8. 不同服务器数据库表连接查询修改,如何连接多个数据库,mysql中的服务器和查询两个表中的对方?...

    我期待从不同服务器连接两个不同的数据库.此外,我想运行一个查询,从两个数据库中获取数据到一个单一的结果.我正在使用mysql在PHP脚本中执行此操作.这里是如何很期待做[没有成功:)]如何连接多个数据 ...

  9. mysql数据库表子查询语句_MySQL使用子查询教程

    #MYSQL#这是我MyySQL教程的第四篇了,可能对于一些大神来说这些都是小儿科,但是我还是相信这些东西会对一些人有帮助的,本篇主要会介绍上面是子查询以及如何使用它们.大概会从,什么是子查询,利用子 ...

最新文章

  1. “AI独角兽”半年巨亏52亿 旷视科技的IPO之路会好走吗?
  2. 免费申请Firefly-RK3288开源板
  3. 淘宝搜索中基于embedding的召回
  4. OpenCASCADE:使用 XSTEPDRAW
  5. 收集Java面试题知识点(Java基础部分三)
  6. 无监督学习 k-means_监督学习-它意味着什么?
  7. codeforces1454 F. Array Partition
  8. redis 队列_Redis与Rabbitmq消息队列的区别
  9. 怪哉翻译软件测试,[东方朔传翻译]东方朔传·怪哉原文与翻译
  10. web mysql 界面表命名规范_MySql数据库表字段命名及设计规范
  11. C++基础知识(二)命名空间
  12. The Stanford Geostatistical Modeling Software(地质统计软件)
  13. 时光 php,ctphp-php教程-时光划过那刹那-PHP教程--创业的风,吹向了年轻之长藤个人博客网站...
  14. 数学建模大赛考什么计算机基础知识,华为杯数学建模竞赛
  15. SpringBoot全局异常处理(三十)
  16. 手机处理器排名2019_2019手机处理器性能排行,第一实至名归,第二太冷门
  17. 真人玩计算机图片大全集,微信真人表情图片大全 用自己的照片做微信真人表情包(好玩),各类搞笑素材任你选择...
  18. 18-Halcon机器视觉实例入门:图像滤波-各向异性滤波
  19. 2022最新可用免费天气预报API接口
  20. React Native 动画(Animated)

热门文章

  1. c语言把结构体首地址放入指针,C语言基础———指针,结构体指针,函数指针
  2. 影之刃2服务器维护,影之刃2一个忠实玩家的心声和建议
  3. 如何给数据添加高斯白噪声?
  4. Java猿社区—Http digest authentication 请求代码最全示例
  5. 推荐5个免费的字体转换网站工具
  6. matlab中如何处理复数,matlab中复数的处理函数
  7. 我的世界python——“玻璃行者”
  8. 蒂特ft232_[经验分享] 最近调试FT232H遇到的坑
  9. 第二十四章 Caché 变量大全 $ZA 变量
  10. CentOS7无法解压7za包,解决办法!!!