SSM框架整合(实现从数据库到页面展示)

原文链接: http://www.cnblogs.com/meiLinYa/p/8746174.html

        SSM框架整合(实现从数据库到页面展示)

    首先创建一个spring-web项目,然后需要配置环境dtd文件的引入,环境配置,jar包引入。

首先让我来看一下ssm的基本项目配件。(代码实现)

     SSM框架整合(实现从数据库到页面展示)_第1张图片

1.首先编写web.xml文件。



    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">
    ssmdemo
    
    
        contextConfigLocation
        classpath:spring/application*.xml
    
    
    
        class>org.springframework.web.context.ContextLoaderListenerclass>
    
    
    
        ssm
        class>org.springframework.web.servlet.DispatcherServletclass>
        
            contextConfigLocation
            classpath:spring/spring-mvc.xml
        
    
    
        ssm
        *.action
    
    
        index.html
    

2.建mybatis-config.xml和spring-mvc.xml文建的配置。

    1.myatis-config.xml  




    
        <package name="com.bean.*" />
    

    2.spring-mvc.xml(使用注解扫描@Controller,@ResquestMapping(“指定路径”))



    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:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context.xsd
     http://www.springframework.org/schema/tx 
     http://www.springframework.org/schema/tx/spring-tx.xsd 
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc.xsd ">
    
    package="com.controller,com.service.impl" />
    
    
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        
        
    

3.application-dao.xml,application-tx,application-service配置

     1)application-dao.xml和IUsersDao.xml的配置以及IUsersDao.java类的实现。

    1.application-dao.xml:IUsersDao.java的配置(数据库查询数据)



    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:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context.xsd
     http://www.springframework.org/schema/tx 
     http://www.springframework.org/schema/tx/spring-tx.xsd 
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc.xsd ">
    
    
    class="com.mchange.v2.c3p0.ComboPooledDataSource">
        
        
        
        
    
    
    class="org.mybatis.spring.SqlSessionFactoryBean">
        
        
    
    
    

     2.IUsersDao.java类

package com.dao;

import java.util.List;
import com.bean.UsersBean;

public interface IUsersDao {

    /**
     * 过去所有用户
     * @return
     */
    public List getAllUser();
}

    3.IUsersDao.xml:Mapper映射。




    

    2)application-service.xml和UsersService.java

     1.application-service.xml:业务层的xml配置,创建bean是IUsersBean与UsersService关联。



    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:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context.xsd
     http://www.springframework.org/schema/tx 
     http://www.springframework.org/schema/tx/spring-tx.xsd 
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc.xsd ">
    class="com.service.impl.UsersServiceImpl" />

    2.UsersSerivce.java类

package com.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import com.bean.UsersBean;
import com.dao.IUsersDao;
import com.service.IUsersService;

public class UsersServiceImpl implements IUsersService {

    @Autowired
    private IUsersDao userDao;

    @Override
    public List findAllUser() {
        // TODO Auto-generated method stub
        return userDao.getAllUser();
    }

}

  3)application-tr.xml配置



    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:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context.xsd
     http://www.springframework.org/schema/tx 
     http://www.springframework.org/schema/tx/spring-tx.xsd 
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc.xsd ">

    
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        
    
    
    
        
            
            
            
            
            
            
            
            
        
    
    
    
        
            id="pcut" />
        
    

4.Controller类的配置(注解配置)

  1.返回路径相当于地址栏。

package com.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.bean.UsersBean;
import com.service.IUsersService;

/**
 * Handler
 * 
 * @author Ya
 *
 */
@Controller
public class UsersController {

    @Resource(name = "usersService")
    private IUsersService usersService;

    /**
     * 返回相等路径
     * 
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("/showAll")
    public String showAll(Model model) {
        // 调用业务方法
        List list = usersService.findAllUser();
        // 将list返回
        model.addAttribute("USER", list);
        // 跳转到pages/users/showUsers.jsp页面,转发或者重定向
        // return "forward:pages/users/showUsers.jsp";
        return "users/showUsers";
    }
}

  2.返回 累行为ModelAndView

package com.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.bean.UsersBean;
import com.service.IUsersService;

/**
 * Handler
 * 
 * @author Ya
 *
 */
@Controller
public class UsersController {

    @Resource(name = "usersService")
    private IUsersService usersService;

    /**
     * ModelAndView
     * 
     * @return
     */
    @RequestMapping("/showAll")
    public ModelAndView showAll() {
        // 获取业务的查询结果
        List list = usersService.findAllUser();
        // 将查询的结果集合显示到pages/showUsers.jsp
        ModelAndView m = new ModelAndView();
        // 存储list
        m.addObject("USER", list);
        // 指定试图资源:
        // 由于spring-mvc.xml文件中配置了试图解析器的前缀和后缀 m.setViewName("users/showUsers");
        return m;
    }

}

  3.返回forward转发

package com.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.bean.UsersBean;
import com.service.IUsersService;

/**
 * Handler
 * 
 * @author Ya
 *
 */
@Controller
public class UsersController {

    @Resource(name = "usersService")
    private IUsersService usersService;

    /**
     * 转发
     * 
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping("/showAll")
    public void showAll(HttpServletRequest request, HttpServletResponse response) throws Exception {
        // 调用业务方法
        List list = usersService.findAllUser();
        // 将list返回
        request.setAttribute("USER", list);
        // 跳转到pages/users/showUsers.jsp页面,转发或者重定向
        // request.getRequestDispatcher("pages/users/showUsers.jsp").forward(request,
        // response);
        response.sendRedirect("pages/users/showUsers.jsp");
    }

   4.返回为重定向。

package com.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.bean.UsersBean;
import com.service.IUsersService;

/**
 * Handler
 * 
 * @author Ya
 *
 */
@Controller
public class UsersController {

    @Resource(name = "usersService")
    private IUsersService usersService;

    /**
     * 重定向
     * 
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping("/showAll")
    public void showAll1(HttpServletRequest request, HttpServletResponse response) throws Exception {
        // 调用业务方法
        List list = usersService.findAllUser();
        // 将list返回
        HttpSession session = request.getSession();
        session.setAttribute("USER", list);
        // 跳转到pages/users/showUsers.jsp页面,转发或者重定向
        response.sendRedirect("pages/users/showUsers.jsp");
    }

    5.直接return “redirect:pages/users/showUsers.jsp”

package com.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.bean.UsersBean;
import com.service.IUsersService;

/**
 * Handler
 * 
 * @author Ya
 *
 */
@Controller
public class UsersController {

    @Resource(name = "usersService")
    private IUsersService usersService;

    /**
     * 返回值为String
     * 
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/showAll")
    public String showAll(HttpServletRequest request) throws Exception {
        // 调用业务方法
        List list = usersService.findAllUser();
        // 将list返回
         HttpSession session=request.getSession();
        session.setAttribute("USER", list);
        // 跳转到pages/users/showUsers.jsp页面,转发或者重定向
        return "forward:pages/users/showUsers.jsp";
        // return "redirect:pages/users/showUsers.jsp";
    }

  7.直接return“forward:pages/users/showUsers.jsp”;

package com.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.bean.UsersBean;
import com.service.IUsersService;

/**
 * Handler
 * 
 * @author Ya
 *
 */
@Controller
public class UsersController {

    @Resource(name = "usersService")
    private IUsersService usersService;

    /**
     * 返回值为String
     * 
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/showAll")
    public String showAll(HttpServletRequest request) throws Exception {
        // 调用业务方法
        List list = usersService.findAllUser();
        // 将list返回
        request.setAttribute("USER", list);
        // 跳转到pages/users/showUsers.jsp页面,转发或者重定向
        return "forward:pages/users/showUsers.jsp";
    }

以上随便一种方式都是可以的。

    总结:

      1.我们主要是学习了ssm整合,把数据库的数据的数据展示到网页中。主要分为

      dao-->service-->controller-->jsp

      2.

        1)dao主要是辅助取出数据库的数据,dao到daoMapper批量映射。获取数据,链接数据库是又application-dao.xml文件来集成配置管理。

        2)service 为业务层,由application-service.xml文件管理。主要是配置IUsersDao,使service中不用newIUsersDao。

        3)controller主要是将业务层取来数据传递到jsp页面中,然后将数据携带到要跳转的jsp页面中。

        4)jsp接受并展示数据controller中传递来的参数。

      3.spring中的application*.xml和spring-mvc.xml的xml文件在WEB-INF/web.xml中进行解析;

       mybatis中的mybatis-config.xml中的xml文件在application-dao.xml文件中被解析。

      

    有问题请留言!!

         

 

转载于:https://www.cnblogs.com/meiLinYa/p/8746174.html

你可能感兴趣的:(SSM框架整合(实现从数据库到页面展示))