SSM整合对数据库表的查询
这次的学习的任务是从我们之前搭建好的框架上加一些业务逻辑(查)
**概述:**运行的效果是一个页面有个查询表中数据的超链接,在点击查询链接的时候,把数据库中的表里面的数据全部查出来。
以订单做个例子:
第一步:在数据库中创建一个表,表名为goods,分别有(id,name,money)个字段,实例如下:
第二步:创建一个WEB动态工程,结构如下
第三步:搭建好SSM整合的框架
-
创建一个xxx.xxx.xxx.controller包用来存放controller文件
-
创建一个xxx.xxx.xxx.dao包用来存放”接口+实体映射文件”
-
创建一个xxx.xxx.xxx.pojo包用来存放实体映射文件
-
创建一个config包用来存放spring主配置文件+Mabatis主配置文件
包名自定义,但是你要知道对应的包是用来干什么的
-
在WEB-INF目录下创建一个包用来存放你的自定义jsp文件
第四步:导ssm整合的jar包(放在lib包下)
第五步:配置web.xml(
- 配置中央前端控制器
- 配置容器监听器
- 配置过滤器 )
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的主配置文件,且配置:
- 开启注解并扫描指定包
- 配置视图资源解析器(配置前缀,后缀)
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.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://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的主配置文件,且配置:
- 配置数据源
- 配置事物管理
- 配置数据库会话工厂
- 配置映射扫描器
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.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
http://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的主配置文件,且配置:
- 配置别名(实体类)
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 {
@Autowired
IGoods 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>
这样我们简单的框架加上一个查询业务就完成啦!