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

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

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

    

1.首先编写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>ssmdemo</display-name>
<!-- 创建spring容器:为了查找spring容器要管理的所有bean -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/application*.xml</param-value>
</context-param>
<!-- g该类在web.jar的context包下的ContextLoaderListener,该类实现了监听器接口 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 创建前端控制器 -->
<servlet>
<servlet-name>ssm</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>ssm</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>

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

    1.myatis-config.xml  

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "http://mybatis.org/dtd/mybatis-3-config.dtd" "mybatis-3-config.dtd" >
<configuration>
<typeAliases>
<package name="com.bean.*" />
</typeAliases>
</configuration>

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

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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 ">
<!-- 开启注解扫描 -->
<context:component-scan base-package="com.controller,com.service.impl" />
<!-- 开启处理器 -->
<mvc:annotation-driven />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>

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

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

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

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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 ">
<!-- 配置数据源 c3p0数据库连接池。到对应的连接池jar中找 -->
<context:property-placeholder location="classpath:db.properties" />
<bean id="ds" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="DriverClass" value="${Adirver}"></property>
<property name="JdbcUrl" value="${Aurl}"></property>
<property name="User" value="${Ausername}"></property>
<property name="Password" value="${Apassword}"></property>
</bean>
<!-- factory:mybatis-spring整合包中找。 -->
<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property>
<property name="dataSource" ref="ds"></property>
</bean>
<!-- 批量创建dao代理类对象:符合mybatis的mapper代理方式规范 -->

     2.IUsersDao.java类

package com.dao;

import java.util.List;
import com.bean.UsersBean; public interface IUsersDao { /**
* 过去所有用户
* @return
*/
public List<UsersBean> getAllUser();
}

    3.IUsersDao.xml:Mapper映射。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "http://mybatis.org/dtd/mybatis-3-mapper.dtd" "mybatis-3-mapper.dtd" >
<mapper namespace="com.dao.IUsersDao">
<select id="getAllUser" resultType="com.bean.UsersBean">
select * from users
</select>
</mapper>

    2)application-service.xml和UsersService.java

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

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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 ">
<bean id="usersService" class="com.service.impl.UsersServiceImpl" />
</beans>

    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<UsersBean> findAllUser() {
// TODO Auto-generated method stub
return userDao.getAllUser();
} }

  3)application-tr.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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 "> <!-- 事务配置管理器 :jdbc->jdbc.datasource -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="ds"></property>
</bean>
<!-- 增强,通知 -->
<tx:advice id="advice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 此处规定开发的方法前缀规范: -->
<!-- 增删 改的方法REQUIRED(required) -->
<!-- 查询所有方法是SUPPORTS,read-only -->
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 定义aop -->
<aop:config>
<aop:pointcut expression="execution(* com.service.impl.*.*(..))"
id="pcut" />
<aop:advisor advice-ref="advice" pointcut-ref="pcut" />
</aop:config>
</beans>

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<UsersBean> 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<UsersBean> 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<UsersBean> 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<UsersBean> 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<UsersBean> 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<UsersBean> 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文件中被解析。

      

    有问题请留言!!

         

SSM框架整合(实现从数据库到页面展示)的更多相关文章

  1. SSM框架整合项目 :租房管理系统

    使用ssm框架整合,oracle数据库 框架: Spring SpringMVC MyBatis 导包: 1, spring 2, MyBatis 3, mybatis-spring 4, fastj ...

  2. JAVAEE——宜立方商城01:电商行业的背景、商城系统架构、后台工程搭建、SSM框架整合

    1. 学习计划 第一天: 1.电商行业的背景. 2.宜立方商城的系统架构 a) 功能介绍 b) 架构讲解 3.工程搭建-后台工程 a) 使用maven搭建工程 b) 使用maven的tomcat插件启 ...

  3. springmvc(二) ssm框架整合的各种配置

    ssm:springmvc.spring.mybatis这三个框架的整合,有耐心一步步走. --WH 一.SSM框架整合 1.1.整合思路 从底层整合起,也就是先整合mybatis与spring,然后 ...

  4. IDEA下基于MAVEN的SSM框架整合

    源码可以以上传github https://github.com/ingxx/ssm_first 最近把用IDEA把SSM框架整合一遍遇到了不少坑,在这里写出来 这里maven我使用的是自己下载的3. ...

  5. SSM框架整合思路

    SSM框架整合思路 Spring在整合中起到的作用(面试时常问到) Spring管理持久层的mapper. Spring管理业务层的service,service可以调用mapper接口.Spring ...

  6. SSM框架整合模板

    SSM框架整合--MAVEN依赖 spring方面(包含了springmvc): spring-webmvc:spring与mvc的整合依赖,主要包括spring的核心包和springmvc需要的包 ...

  7. 【计理01组08号】SSM框架整合

    [计理01组08号]SSM框架整合 数据库准备 本次课程使用 MySQL 数据库.首先启动 mysql : sudo service mysql start 然后在终端下输入以下命令,进入到 MySQ ...

  8. SpringMVC详解及SSM框架整合项目

    SpringMVC ssm : mybatis + Spring + SpringMVC MVC三层架构 JavaSE:认真学习,老师带,入门快 JavaWeb:认真学习,老师带,入门快 SSM框架: ...

  9. SpringMVC--从理解SpringMVC执行流程到SSM框架整合

    前言 SpringMVC框架是SSM框架中继Spring另一个重要的框架,那么什么是SpringMVC,如何用SpringMVC来整合SSM框架呢?下面让我们详细的了解一下. 注:在学习SpringM ...

随机推荐

  1. adb shell 命令详解,android, adb logcat

    http://www.miui.com/article-275-1.html http://noobjava.iteye.com/blog/1914348 adb shell 命令详解,android ...

  2. Java 执行jar文件出现版本错误信息

    Java 执行jar文件出现版本错误信息 一.问题 执行jar文件出现如下错误信息: 二.解决方案 是因为在创建工程的时候选择的jdk编译版本,和执行jar环境的jdk版本不一致: 更改工程的jdk版 ...

  3. [省选模拟]Rhyme

    考的时候脑子各种短路,用个SAM瞎搞了半天没搞出来,最后中午火急火燎的打了个SPFA才混了点分. 其实这个可以把每个模式串长度为$K-1$的字符串看作一个状态,这个用字符串Hash实现,然后我们发现这 ...

  4. SIFT在OpenCV中的调用和具体实现(HELU版)

    前面我们对sift算法的流程进行简要研究,那么在OpenCV中,sift是如何被调用的?又是如何被实现出来的了? 特别是到了3.0以后,OpenCV对特征点提取这个方面进行了系统重构,那么整个代码结构 ...

  5. poj Meteor Shower - 搜索

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16313   Accepted: 4291 Description Bess ...

  6. Android 开发环境配置图文教程(jdk+eclipse+android sdk)

    一 相关下载(1) java JDK下载:进入该网页: http://java.sun.com/javase/downloads/index.jsp (或者直接点击下载)如下图: 选择 Downloa ...

  7. Linq 对List的一些操作

    代码: public class Person { public int ID { get; set; } public string Name { get; set; } public int Ag ...

  8. BZOJ1045 [HAOI2008]糖果传递 && BZOJ3293 [Cqoi2011]分金币

    Description 有n个小朋友坐成一圈,每人有ai个糖果.每人只能给左右两人传递糖果.每人每次传递一个糖果代价为1. Input 第一行一个正整数nn<=1'000'000,表示小朋友的个 ...

  9. 全面理解虚拟DOM,实现虚拟DOM

    1.为什么需要虚拟DOM DOM是很慢的,其元素非常庞大,页面的性能问题鲜有由JS引起的,大部分都是由DOM操作引起的.如果对前端工作进行抽象的话,主要就是维护状态和更新视图:而更新视图和维护状态都需 ...

  10. 01_Spark基础

    1.1.Spark Ecosystem BlinkDB: 允许用户定义一个错误范围,BlinkDB将在用户给定的错误范围内,尽可能快的提供查询结果 1.2.Spark愿景 1.3.Spark简介 1) ...