SSM框架的介绍与搭建

  一、简要介绍

  1. Spring

  Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE Development and Design中阐述的部分理念和原型衍生而来。它是为了解决企业应用开发的复杂性而创建的。Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。 简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。

  2. SpringMVC

  Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring MVC 分离了控制器、模型对象、分派器以及处理程序对象的角色,这种分离让它们更容易进行定制。

  3. MyBatis

  MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 。MyBatis是一个基于Java的持久层框架。iBATIS提供的持久层框架包括SQL Maps和Data Access Objects(DAO)MyBatis 消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索。MyBatis 使用简单的 XML或注解用于配置和原始映射,将接口和 Java 的POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。

  二、SSM的搭建

  1. 创建maven项目。

  2. 编辑pom.xml文件,添加相关jar包。

  3. 新建db.properties。

  在maven项目->src->resources目录下,新建db.properties文件并配置如下语句:

  mysql.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8

  mysql.driverClassName=com.mysql.jdbc.Driver

  mysql.username=root

  mysql.password=root

  4. 新建application-context.xml,将spring框架整合到web工程中。

  在maven项目->src->resources目录下,新建一个application-context.xml文件,在此文件中写上如下语句:

  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:mvc="http://www.springframework.org/schema/mvc"

  xmlns:context="http://www.springframework.org/schema/context"

  xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"

  xsi:schemaLocation="http://www.springframework.org/schema/beans

  http://www.springframework.org/schema/beans/spring-beans-3.2.xsd

  http://www.springframework.org/schema/mvc

  http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd

  http://www.springframework.org/schema/context

  http://www.springframework.org/schema/context/spring-context-3.2.xsd

  http://www.springframework.org/schema/aop

  http://www.springframework.org/schema/aop/spring-aop-3.2.xsd

  http://www.springframework.org/schema/tx

  http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

  5. 新建springmvc.xml

  在maven项目->src->resources目录下,新建一个springmvc.xml文件,在此文件中写上如下语句:

  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:mvc="http://www.springframework.org/schema/mvc"

  xmlns:context="http://www.springframework.org/schema/context"

  xmlns:aop="http://www.springframework.org/schema/aop"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-3.2.xsd

  http://www.springframework.org/schema/mvc

  http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd

  http://www.springframework.org/schema/context

  http://www.springframework.org/schema/context/spring-context-3.2.xsd

  http://www.springframework.org/schema/aop

  http://www.springframework.org/schema/aop/spring-aop-3.2.xsd

  http://www.springframework.org/schema/tx

  http://www.springframework.org/schema/tx/spring-tx-3.2.xsdhttp://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util.xsd">

  6. 新建mybatis.xml

  在maven项目->src->resources目录下,新建一个mybatis.xml文件,在此文件中写上如下语句:

  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

  "http://mybatis.org/dtd/mybatis-3-config.dtd">

  7. 新建web.xml

  在maven项目->src->main下,新建一个webapp文件夹,在webapp下新建WEB-INF文件夹,在WEB-INF下新建web.xml文件,在web.xml文件下写上如下语句:

  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

  version="2.5">

  contextConfigLocation

  classpath:application-context.xml

  spring-mvc

  org.springframework.web.servlet.DispatcherServlet

  contextConfigLocation

  classpath:springmvc.xml

  spring-mvc

  /

  org.springframework.web.context.ContextLoaderListener

  三、简单的web项目测试

  1. 建包

  在maven项目->src->main->java下,分别新建如下包:

  com.dream.controller

  com.dream.model

  com.dream.service

  com.dream.service

  2. 新建view文件夹

  在maven项目->src->main->webapp->WEB-INF下,新建view文件夹

  3. 新建 index.jsp 文件

  Hello,${name}

  Thisismyteacher!

  4. 新建IndexController.java类

  packagecom.dream.controller;

  importcom.dream.service.UserService;

  importorg.springframework.beans.factory.annotation.Autowired;

  importorg.springframework.stereotype.Controller;

  importorg.springframework.ui.Model;

  importorg.springframework.web.bind.annotation.RequestMapping;

  /**郑州治疗妇科的医院 http://www.120kdfk.com/

  *@description:入口

  *@author:snower

  *@create:2018-04-08 16:01

  **/

  @Controller

  publicclassIndexController{

  @Autowired

  UserServiceuserService;

  @RequestMapping("/")

  publicStringindex(Modelmodel){

  Strings=userService.getName();

  model.addAttribute("name",s);

  return"index";

  }

  }

  5. 新建UserService.java类

  packagecom.dream.service;

  importcom.dream.mapper.UserMapper;

  importorg.springframework.beans.factory.annotation.Autowired;

  importorg.springframework.stereotype.Service;

  /**

  *@description:服务

  *@author:snower

  *@create:2018-04-08 16:06

  **/

  @Service

  publicclassUserService{

  @Autowired

  UserMapperuserMapper;

  publicStringgetName(){

  Stringname=userMapper.getName();

  return name;

  }

  }

  6. 新建UserMapper.java接口

  packagecom.dream.mapper;

  /**

  *@description:mapper

  *@author:snower

  *@create:2018-04-0816:16

  **/

  publicinterfaceUserMapper{

  StringgetName();

  }

  7. 新建UserMapper.xml接口

  selectname

  fromuserWHEREid=1;

  8. 配置Tomcat服务器,点击运行


猜你喜欢

转载自blog.51cto.com/14335413/2461630