SSM框架的搭建

ssm框架的搭建

一.所谓的SSM 就是Spring+SpringMVC+Mybatis三者的结合的框架。



二.至少需要的jar包有:

这里写图片描述

三.配置

1.首先在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>


  <!--过滤器,对编码转换说明,减少代码量  -->

  <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>
  </filter>
   <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

    <!-- 加载spring的配置文件 -->
   <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- ContextLoaderListener的作用是在web.xml声明spring的配置默认在WEB_INF下,也可以像下面一样自定义路径 -->
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring.xml</param-value>
  </context-param>


  <!-- DispachterServlet的配置 -->
  <servlet>
    <servlet-name>springmvc</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><!--springmvc的配置文件  -->
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>


  <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>
</web-app>

2.在spring.xml的配置

<beans xmlns="http://www.springframework.org/schema/beans"
    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-4.0.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.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:property-placeholder location="classpath:jdbc.properties"/>

  <!--对包进行扫描读取-->
<context:component-scan base-package="com.ssm.dao"/>
<context:component-scan base-package="com.ssm.service"/>
<!-- 配置数据源 -->
   <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="driverClass" value="${jdbc.driver}"></property>
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 第二步:创建Mybatis的sqlSessionFactory。生产sqlSession -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:mybatis-config.xml"></property> <!-- mybatis配置文件路径 -->
    </bean>

<!-- 配置mybatis接口代理开发
        * 接口类名和映射文件必须同名
        * 接口类和映射文件必须在同一个目录 下
        * 映射文件namespace名字必须是接口的全类路径名
        * 接口的方法名必须和映射Statement的id一致
     -->
     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
     <property name="basePackage" value="com.ssm.dao"></property><!-- 数据库操作的dao层的路径 -->
     <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
     </bean>
     </beans> 

3.在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: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-4.0.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.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.ssm.controller"></context:component-scan>

        <!-- 注解启动 -->
        <mvc:annotation-driven/>

        <!-- 配置视图解析器 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/views/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>


</beans>

4.其他文件的内容
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>

</configuration>

jdbc.properties

jdbc.url = jdbc\:mysql\://localhost:3306/user   (数据库名)
jdbc.driver = com.mysql.jdbc.Driver
jdbc.username= root(用户名)
jdbc.password= lanouhn(密码)

四.项目实现

做了一个简单的小例子测试ssm框架的运行,在数据库里面建了一个user数据库 user的表
这里写图片描述

项目的文件存储为

这里写图片描述


1.在com.ssm.controller中写入一个testcontroller.java,里面写入测试方法

package com.ssm.controller;



import java.util.Map;

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


import com.ssm.service.showIDservice;


@Controller
public class testcontroller {

    @Autowired
    private showIDservice showidservice;
    @RequestMapping("/test")
    public String  test(){


        Map<String ,Object> map=showidservice.showID("1");//调用showidservice的showID方法
        String username=(String)map.get("userName");
        System.out.println(username);
        return "hello";
    }

}


2.在com.ssm.service 中写入showIDservice.java

package com.ssm.service;

import java.util.Map;




public  interface showIDservice {


    public   Map<String,Object> showID(String userid) ;


}

3.在com.ssm.serviceimpl写入showIDserviceimpl,在showIDserviceimpl中实现showID方法,继续调用usermapper的showID方法

package com.ssm.service.impl;

import java.util.Map;

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

import com.ssm.dao.UserMapper;
import com.ssm.service.showIDservice;

@Service
public   class  showIDserviceimpl implements showIDservice {



    @Autowired
    public UserMapper usermapper;
    @Override
    public   Map<String,Object> showID(String userid) {
         return usermapper.showID(userid);
    }


}

4.在com.ssm.dao中写入UserMapper.java

package com.ssm.dao;

import java.util.Map;

public interface UserMapper  {
    public   Map<String,Object> showID(String userid) ;
}

5.然后写入UserMapper.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.ssm.dao.UserMapper" >

  <select id="showID" resultType="java.util.Map">
    select userId,userName
    from user1
    where userId = #{userid}
  </select>

</mapper>


6.hello.jsp中

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>
hello ssm!!!!

</body>
</html>

五.项目运行结果

最后在tomcat搭载运行

这里写图片描述

控制台中出现
这里写图片描述

搭建成功!

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SSM框架是指Spring+SpringMVC+Mybatis的组合,是一种常用于JavaWeb开发的三大框架的整合。下面我将用300字回答关于基于SSM框架搭建Java服务端的问题。 首先,我们需要搭建开发环境。首先,确保已经安装好Java、Tomcat、MySQL等软件。然后,在IDE中创建一个新的Web项目,导入SSM框架的相关依赖,如SpringSpringMVCMybatis等。接下来,配置web.xml文件,设置DispatcherServlet来分发HTTP请求,并配置SpringMybatis的配置文件。 其次,我们需要编代码。首先,创建一个实体类,用于与数据库中的表进行映射。然后,创建一个接口,定义好对该实体类进行CRUD操作的方法。接着,创建一个实现该接口的Mapper类,使用Mybatis提供的注解或XML来实现具体的SQL语句。最后,创建一个Service类来调用Mapper类的方法,并加入相应的业务逻辑。 然后,我们需要配置Spring的配置文件。在该文件中,我们需要配置数据源、事务管理器、扫描Mapper接口和Service类的路径等。通过配置文件,将Mapper接口和对应的实现类以及Service类注入到Spring容器中,方便进行管理和调用。 最后,我们需要配置SpringMVC的配置文件。在该文件中,我们需要配置Request、HandlerMapping、ViewResolver等相关信息。通过配置文件,将Controller类注入到SpringMVC容器中,并配置URL与方法的映射关系。 综上所述,基于SSM框架搭建Java服务端需要搭建开发环境,编代码,配置SpringSpringMVC的配置文件。通过整合SpringSpringMVCMybatis,我们可以快速开发出高效可靠的Java服务端。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值