ssm整合(spring,springmvc,mybatis),实现一对多查询
第一步:导包(由于包太多,这里就不一一介绍),整个工程目录如下:
第二步:创建两张表(本人两张表分别为users和orders),如下图所示:
users表:
orders表:
第三步:将ssm框架搭起来(spring主配置文件,mybatis主配置文件,springmvc的web.xml配置文件与xxx-servlet.xml配置文件):
web.xml配置文件:
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>SSM</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>
<!-- 字符编码过滤器
注意:必须写在所以过滤器的前面 -->
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 中央前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<!-- springmvc-servlet.xml配置文件的命名 -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- 与上面的servlet-name保持一致 -->
<url-pattern>/</url-pattern>
<!-- /:拦截所有请求 -->
</servlet-mapping>
<!-- 容器启动监听事件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:config/applicationContext.xml</param-value>
<!-- classpath:类路径,*:所有,config:你的文件夹名,applicationContext.xml:spring主配置文件 -->
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
applicationContext.xml(spring主配置文件):
<?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:oxm="http://www.springframework.org/schema/oxm"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
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/oxm http://www.springframework.org/schema/oxm/spring-oxm-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
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<!-- spring主配置文件 -->
<!-- 配置数据源 -->
<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="1234"></property>
</bean>
<!-- 事务管理 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 回话工厂 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 获取数据源 -->
<property name="configLocation" value="classpath:config/config.xml"></property>
<!-- classpath:在src目录下去找 -->
<!-- 加载mybatis的主配置文件 -->
<property name="mapperLocations" value="classpath*:com/zhiyuan/frank/mapper/*.xml"></property>
<!-- classpath*:在整个项目下面去找 -->
<!-- 自动扫描mapping.xml文件 -->
</bean>
<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.zhiyuan.frank.mapper"></property>
</bean>
</beans>
config.xml(mybatis主配置文件):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "mybatis-3-config" "mybatis-3-config.dtd" >
<configuration>
<!-- 别名 -->
<typeAliases>
<package name="com.zhiyuan.frank.pojo"/>
</typeAliases>
</configuration>
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: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/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">
<!-- 开启注解并扫描controller所在的指定包 -->
<context:component-scan base-package="com.zhiyuan.frank.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>
框架搭好之后可以运行一下,如果没有报错信息,则代表框架搭建成功
第四步:创建改表相对应的实体类:
推荐类与表名同名(类命名规范:类名首字母大写)
Users类:
package com.zhiyuan.frank.pojo;
import java.util.List;
public class Users {
private int uId;
private String username;
private String mobile;
private List<Orders> orders;
public List<Orders> getOrders() {
return orders;
}
public void setOrders(List<Orders> orders) {
this.orders = orders;
}
public int getuId() {
return uId;
}
public void setuId(int uId) {
this.uId = uId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
}
Orders类:
package com.zhiyuan.frank.pojo;
public class Orders {
private int orderId;
private int usersId;
private String productName;
private String orderNo;
private double money;
private Users users;
public Users getUsers() {
return users;
}
public void setUsers(Users users) {
this.users = users;
}
public int getOrderId() {
return orderId;
}
public void setOrderId(int orderId) {
this.orderId = orderId;
}
public int getUsersId() {
return usersId;
}
public void setUsersId(int usersId) {
this.usersId = usersId;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getOrderNo() {
return orderNo;
}
public void setOrderNo(String orderNo) {
this.orderNo = orderNo;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
}
第五步:编写index.jsp页面:
<%@ page language="java" import="java.util.*"
pageEncoding="UTF-8"%>
<%
String basepath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
//一种获取页面的方式
%>
<!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>
<h1><a href="users">获取用户所有订单</a></h1>
<h1><%=basepath %></h1>
</body>
</html>
第六步:编写UsersController.java页面:
package com.zhiyuan.frank.controller;
/**
* 该类指的是SpringMVC中的C,代表控制器,业务的逻辑都写在该类中
*/
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.zhiyuan.frank.mapper.Userslist;
import com.zhiyuan.frank.pojo.*;
@Controller
//@Controller 用于标记在一个类上,通俗来说,被Controller标记的类就是一个控制器,这个类中的方法,就是相应的动作。
@RequestMapping("/users")//users与index中a标签中href属性保持一致
//@RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。
public class UsersController {
@Autowired
//@Autowired注解是按照类型(byType)装配依赖对象,默认情况下它要求依赖对象必须存在,如果允许null值,可以设置它的required属性为false
Userslist list;
@RequestMapping
public ModelAndView firstssm(){
ModelAndView mav = new ModelAndView("order_form");//"order_form":是返回页面的名字
//实例化一个ModelAndView对象
Users users = list.GetUsersID(1);
//调用接口层的方法并传入参数
List<Orders> list = users.getOrders();
mav.addObject("users",list);//"user":键,list:值(这里的值是接口层查询完后返回的值)
mav.addObject("username",users.getUsername());
//把查询结果封装到模型视图里面
return mav;
}
}
第七步:编写Userslist.java接口:
package com.zhiyuan.frank.mapper;
import com.zhiyuan.frank.pojo.Users;
public interface Userslist {
public Users GetUsersID(int id);
//这里的接口名要与mapper.xml中的select中id保持一致
}
第八步:编写usersmapper.xml配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "mybatis-3-mapper" "mybatis-3-mapper.dtd" >
<mapper namespace="com.zhiyuan.frank.mapper.Userslist">
<!-- namespace:这里面是接口的路径 -->
<resultMap type="users" id="MaMap">
<result property="uId" column="u_id"/>
<result property="username" column="username"/>
<result property="mobile" column="mobile"/>
<collection property="orders" ofType="orders" column="users_id">
<!-- collection:一对多时使用,association:多对一使用 -->
<result property="orderId" column="order_id"/>
<result property="usersId" column="users_id"/>
<result property="productName" column="product_name"/>
<result property="orderNo" column="order_no"/>
<result property="money" column="money"/>
</collection>
</resultMap>
<select id="GetUsersID" parameterType="int" resultMap="MaMap">
select * from users,orders where users.u_id = orders.users_id and users.u_id=#{id}
</select>
</mapper>
第九步:编写order_form.jsp返回jsp页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!-- prefix:别名,需要用forEach得通过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>查询结果页面</title>
</head>
<body>
<table border="5" bordercolor="blue">
<!-- border:边框宽度,bordercolor:边框颜色 -->
<tr>
<td colspan="3">用户名:${username }<!-- el表达式,${username }:el表达式中放的是传过来的键 --></td>
<!-- colspan:跨列合并,rowspan:跨行合并 -->
</tr>
<tr>
<td>商品名称</td>
<td>订单号</td>
<td>订单总额</td>
<!-- 如果传过来的值只有一条,则不需要使用循环,${users}:el表达式中放的是传过来的键 -->
<c:forEach items="${users}" var="orders">
<!-- c:通过prefix取的别名,可以该为任意值,取名为c是规范,作用是调用forEach方法,
forEach:相当于for循环,items:里面放循环对象,var:任意取名,然后通过该名调用要显示的值 -->
<tr>
<td>${orders.productName }<!-- el表达式 --></td>
<td>${orders.orderNo }</td>
<td>${orders.money }</td>
</tr>
</c:forEach>
</tr>
</table>
</body>
</html>
运行结果,如下图所示:
index.jsp页面:
order_form.jsp返回结果页面: