Day44——SSM整合实例——显示所有员工信息列表

一. 回顾

前面Day43——SSM整合前需要注意的事项、整合步骤讲到了ssm整合的注意事项以及步骤,今天用实例把ssm整合出来,显示所有员工信息列表

前往免费下载源码

二. 例子

  • 设计的表如下:两个表的主键都自增。tbl_employee中的d_id引用tbl_dept的id,外键引用,如下:
    在这里插入图片描述
    在这里插入图片描述

  • 插入数据
    在这里插入图片描述
    在这里插入图片描述

  • 导入jar包,项目的目录结构如下:
    在这里插入图片描述
    在这里插入图片描述

  • 搭建Spring与Springmvc的环境

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_2_5.xsd" id="WebApp_ID" version="2.5">

    <!-- 字符编码过滤器 -->
    <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>
    
    <!-- REST 过滤器 -->
    <filter>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
    
    <!-- 实例化Spring容器的监听器 -->
    <!-- needed for ContextLoaderListener -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring.xml</param-value>
	</context-param>

	<!-- Bootstraps the root web application context before servlet initialization -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
    
    
    <!-- SpringMVC前端控制器 -->
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
	<servlet>
		<servlet-name>springDispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springmvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<!-- Map all requests to the DispatcherServlet for handling -->
	<servlet-mapping>
		<servlet-name>springDispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

</web-app>

springmvc.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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
		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-4.0.xsd">


    <!-- 组件扫描 -->
    <context:component-scan base-package="com.atguigu.ssm" use-default-filters="false">
         <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    
    <!-- 视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    
    <!-- mvc -->
    <mvc:default-servlet-handler/>
    <mvc:annotation-driven/>

</beans>

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:tx="http://www.springframework.org/schema/tx"
	xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
	xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
		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-4.0.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <!-- 组件扫描 -->
    <context:component-scan base-package="com.atguigu.ssm">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    
    <!-- 数据源 -->
    <context:property-placeholder location="classpath:db.properties"/>
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
    
    <!-- 事务 -->
    <bean id="dataSourceTransactionManager" 
       class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
       <property name="dataSource" ref="dataSource"></property>   
    </bean>
    <tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>
       
    
    <!-- Spring整合MyBatis -->
    <!-- a.SqlSession对象的创建及管理
             MyBatis: 基于全局配置文件==>SqlSessionFactory==>SqlSession
             SSM: SqlSessionFactoryBean
    
    
     -->
     <bean class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 数据源 -->
        <property name="dataSource" ref="dataSource"></property>
        <!-- MyBatis的全局配置文件 -->
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
        
        <!-- 别名处理 -->
        <property name="typeAliasesPackage" value="com.atguigu.ssm.beans"></property>
        <!-- SQL映射文件 -->
        <property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"></property>
     </bean>

     <!-- 
         b. Mapper接口代理实现类对象的创建及管理
            MyBatis:session.getMapper(xxxMapper.class);
            SSM:  MapperScannerConfigurer
                  EmployeeMapper==>代理实现类对象==>IOC==>employeeMapper
     
      -->
     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
         <property name="basePackage" value="com.atguigu.ssm.mapper"></property>
     </bean>
     
   <!--   <mybatis-spring:scan base-package="com.atguigu.ssm.mapper"/> -->

</beans>

  • 搭建MyBatis的环境

EmployeeMapper.java

package com.atguigu.ssm.mapper;

import java.util.List;

import com.atguigu.ssm.beans.Employee;

public interface EmployeeMapper {

	public List<Employee> selectAllEmps();
	
}

EmployeeMapper.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.atguigu.ssm.mapper.EmployeeMapper">
	
	<!-- public List<Employee> selectAllEmps(); -->
	<select id="selectAllEmps" resultMap="myEmpsAndDept">
         select e.id eid, e.last_name, e.email, e.gender, d.id did, d.dept_name	     
	     from tbl_employee e, tbl_dept d
	     where e.d_id = d.id
	</select>
	<resultMap type="com.atguigu.ssm.beans.Employee" id="myEmpsAndDept">
	    <id column="eid" property="id"/>
	    <result column="last_name" property="lastName"/>
	    <result column="email" property="email"/>
	    <result column="gender" property="gender"/>
	    <association property="dept" javaType="com.atguigu.ssm.beans.Department">
	        <id column="did" property="id"/>
	        <result column="dept_name" property="departmentName"/>
	    </association>
	</resultMap>
	
</mapper>

mybatis-config.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>

     <!-- 
         2. settings:包含很多设置项
      
      -->
      <settings>
         <!-- mapUnderscoreToCamelCase:将下划线变成驼峰命名方式,比如last_name->lastName
                 map: 映射
                 underscore:下划线
                 Camel:驼峰
          -->
         <setting name="mapUnderscoreToCamelCase" value="true"/>
         <setting name="jdbcTypeForNull" value="NULL"/>
         <!-- 开启延迟加载 -->
         <setting name="lazyLoadingEnabled" value="true"/>
         <!-- 设置按需加载 -->
         <setting name="aggressiveLazyLoading" value="false"/>
      </settings>
     
</configuration>

  • 准备测试
    EmployeeService.java
package com.atguigu.ssm.service;

import java.util.List;

import com.atguigu.ssm.beans.Employee;

public interface EmployeeService {
    
	public List<Employee> getAllEmps();
	
}

EmployeeServiceImpl.java

package com.atguigu.ssm.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.atguigu.ssm.beans.Employee;
import com.atguigu.ssm.mapper.EmployeeMapper;

@Service
public class EmployeeServiceImpl implements EmployeeService{
	
	@Autowired
	private EmployeeMapper employeeMapper;

	public List<Employee> getAllEmps() {
		// TODO Auto-generated method stub
		return employeeMapper.selectAllEmps();
	}

}

EmployeeHandler.java

package com.atguigu.ssm.handler;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.atguigu.ssm.beans.Employee;
import com.atguigu.ssm.service.EmployeeService;

@Controller
public class EmployeeHandler {

	@Autowired
	private EmployeeService employeeService;
	
	
	/**
	 * 显示所有的员工信息列表
	 */
	@RequestMapping(value="/emps", method=RequestMethod.GET)
	public String listAllEmps(Map<String, Object> map) {
		
		List<Employee> emps = employeeService.getAllEmps();
		
		map.put("emps", emps);
		
		return "list";
		
	}
	
}

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <a href="emps">List All Emps</a>
</body>
</html>

list.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
     <h1 align="center">员工信息列表</h1>
     <table border="1px" align="center" width="70%">
         <tr>
             <th>ID</th>
             <th>LastName</th>
             <th>Email</th>
             <th>Gender</th>
             <th>DeptName</th>
             <th>Operation</th>
         </tr>
         
         <c:forEach items="${emps }" var="emp">
             <tr align="center">
                <td>${emp.id }</td>
                <td>${emp.lastName }</td>
                <td>${emp.email }</td>
                <td>${emp.gender==0?'女':'男' }</td>
                <td>${emp.dept.departmentName}</td>
                <td>
                   <a href="#">DELETE</a>
                   &nbsp;&nbsp;
                   <a href="#">UPDATE</a>
                </td>
             </tr>
         </c:forEach>
     </table>
     
     <h2 align="center"><a href="#">Add New Emp</a></h2>
     
</body>
</html>
  • 测试结果
    在这里插入图片描述
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值