手把手搭建最简单的SSM框架Maven,idea(spring+springmvc+mybatis)附源代码

时间:2024-11-07 08:10:11

总结了在网上看到的几篇ssm教程,希望有一份自己自己的框架能够随时使用,并且是最简单的。所以在这里没有附加上log4j和junit,需要的同学可衣看其他教程,我这边就是要搭建一个最简单的ssm项目,方便以后能够再次基础上面进行扩展

当然,前提是要会使用maven,与本篇博客无关,就不说了。采用idea,因为idea越来越成为主流了。

具体步骤:

1、搭建maven项目




之后等待一会,maven会去获取一些基础包。

完成以后:


我们需要在src目录下面新建java文件夹,并且标记为资源文件夹


之后在这个基础上进行搭建,这是我搭建完成后的图:


2、配置一个tomcat:


右边是输入项目名的地方,最后访问需要用到。


3、

<project xmlns="/POM/4.0.0" xmlns:xsi="http:///2001/XMLSchema-instance"
         xsi:schemaLocation="/POM/4.0.0 /maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>SSMCreater</groupId>
    <artifactId></artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name> Maven Webapp</name>
    <url></url>

    <properties>
        <>UTF-8</>
        <>UTF-8</>
        <!-- spring版本号 -->
        <>4.2.</>
        <!-- mybatis版本号 -->
        <>3.2.8</>
        <!-- mysql驱动版本号 -->
        <>5.1.29</>
    </properties>

    <dependencies>

        <!-- 添加spring核心依赖 -->
        <dependency>
            <groupId></groupId>
            <artifactId>spring-core</artifactId>
            <version>${}</version>
        </dependency>
        <dependency>
            <groupId></groupId>
            <artifactId>spring-web</artifactId>
            <version>${}</version>
        </dependency>
        <dependency>
            <groupId></groupId>
            <artifactId>spring-oxm</artifactId>
            <version>${}</version>
        </dependency>
        <dependency>
            <groupId></groupId>
            <artifactId>spring-tx</artifactId>
            <version>${}</version>
        </dependency>
        <dependency>
            <groupId></groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${}</version>
        </dependency>
        <dependency>
            <groupId></groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${}</version>
        </dependency>
        <dependency>
            <groupId></groupId>
            <artifactId>spring-context</artifactId>
            <version>${}</version>
        </dependency>
        <dependency>
            <groupId></groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${}</version>
        </dependency>
        <dependency>
            <groupId></groupId>
            <artifactId>spring-aop</artifactId>
            <version>${}</version>
        </dependency>

        <dependency>
            <groupId></groupId>
            <artifactId>spring-test</artifactId>
            <version>${}</version>
        </dependency>

        <!-- 添加mybatis依赖 -->
        <dependency>
            <groupId></groupId>
            <artifactId>mybatis</artifactId>
            <version>${}</version>
        </dependency>

        <!-- 添加mybatis/spring整合包依赖 -->
        <dependency>
            <groupId></groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.2.2</version>
        </dependency>
        <!-- 添加mysql驱动依赖 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${}</version>
        </dependency>
        <!-- 添加数据库连接池依赖 -->
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.2.2</version>
        </dependency>
        <!-- 导入dbcp的jar包,用来在中配置数据库 -->
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.2.2</version>
        </dependency>
        <!-- JSTL标签类 -->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

    </dependencies>
    <build>
        <finalName></finalName>
    </build>
</project>

这边提一点,maven下载慢的同学,估计用的是国外地址,百度一下阿里的maven镜像地址,跟着做就好了,速度一下就上去了。

4、

driverClasss=
jdbcUrl=jdbc:mysql://localhost:3306/world?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
username=root
password=root

#定义初始连接数
initialSize=0
#定义最大连接数
maxActive=20
#定义最大空闲
maxIdle=20
#定义最小空闲
minIdle=1
#定义最长等待时间
maxWait=60000

如果复制上面的,每行后面可能会有空格,记得删除。

5、

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
       xmlns:xsi="http:///2001/XMLSchema-instance" xmlns:p="/schema/p"
       xmlns:context="/schema/context"
       xmlns:mvc="/schema/mvc"
       xsi:schemaLocation="/schema/beans
                        /schema/beans/spring-beans-4.
                        /schema/context
                        /schema/context/spring-context-4.
                        /schema/mvc
                        /schema/mvc/spring-mvc-4.">

    <!-- 自动扫描  @Controller-->
    <context:component-scan base-package=""/>
    <!-- 定义跳转的文件的前后缀 ,视图模式配置 -->
    <bean class="">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

6、

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
       xmlns:xsi="http:///2001/XMLSchema-instance"
       xmlns:context="/schema/context" xmlns:tx="/schema/tx"
       xsi:schemaLocation="/schema/beans
                        /schema/beans/spring-beans-3.
                        /schema/context
                        /schema/context/spring-context-3.
                        /schema/tx
                        /schema/tx/">

    <!-- 自动扫描 -->
    <context:component-scan base-package=""/>
    <!--加载配置文件-->
    <bean  class="">
        <property name="location" value="classpath:"/>
    </bean>
    <!-- 配置数据源 -->
    <bean  class=""
          destroy-method="close">
        <property name="driverClassName" value="${driverClasss}"/>
        <property name="url" value="${jdbcUrl}"/>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
        <!-- 初始化连接大小 -->
        <property name="initialSize" value="${initialSize}"></property>
        <!-- 连接池最大数量 -->
        <property name="maxActive" value="${maxActive}"></property>
        <!-- 连接池最大空闲 -->
        <property name="maxIdle" value="${maxIdle}"></property>
        <!-- 连接池最小空闲 -->
        <property name="minIdle" value="${minIdle}"></property>
        <!-- 获取连接最大等待时间 -->
        <property name="maxWait" value="${maxWait}"></property>
    </bean>

    <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
    <bean  class="">
        <property name="dataSource" ref="dataSource" />
        <!-- 自动扫描文件 -->
        <property name="mapperLocations" value="classpath:com/kevenwu/mapper/*.xml"></property>
    </bean>

    <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
    <bean class="">
        <property name="basePackage" value="" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>
</beans>
7、

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="/xml/ns/javaee"
         xmlns:xsi="http:///2001/XMLSchema-instance"
         xsi:schemaLocation="/xml/ns/javaee
          /xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
  <display-name>Archetype Created Web Application</display-name>
<!--加载配置文件-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:</param-value>
  </context-param>

  <context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath:</param-value>
  </context-param>

  <!-- 编码过滤器 -->
  <filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class></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></listener-class>
  </listener>

  <!-- 防止spring内存溢出监听器,比如quartz -->
  <listener>
    <listener-class></listener-class>
  </listener>

  <!-- spring mvc servlet-->
  <servlet>
    <servlet-name>SpringMVC</servlet-name>
    <servlet-class></servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    <async-supported>true</async-supported>
  </servlet>
  <servlet-mapping>
    <servlet-name>SpringMVC</servlet-name>
    <!-- 此处也可以配置成 *.do 形式 -->
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <welcome-file-list>
    <welcome-file>/</welcome-file>
  </welcome-file-list>

  <!-- session配置 -->
  <session-config>
    <session-timeout>15</session-timeout>
  </session-config>
</web-app>

因为是最简单的框架,没有加入log4j和junit,需要的同学自己加上。到这里,项目应该可以启动,显示在中的helloworld.

现在开始项目本身功能的搭建。

本人数据库中有一张city表,现在要做的是通过id查出这个city的name

8、完成后的项目结构:



9、citydao:

package ;

import ;

@Repository
public interface CityDao {
    String queryById(String id);
}

10、cityservice:

package ;

public interface CityService {
     String getCityName(String id);
}

11、cityserviceimpl:

package ;
 import ;
 import ;
 import ;
 import ;

@Service("cityService")
public class CityServiceImpl implements CityService {
    @Autowired
    CityDao cityDao;
    public String getCityName(String  id) {
        return (id);
    }
}

:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-////DTD Mapper 3.0//EN"
        "/dtd/">
<mapper namespace="">
    <!-- 目的:为dao接口方法提供sql语句配置 -->

    <select   resultType="">
        <!-- 具体的sql -->
        SELECT
        Name
        FROM
        city
        WHERE
        id = #{id}
    </select>

</mapper>

:

package ;

import ;
import ;
import ;
import ;
import ;
import ;
import ;

import ;

@Controller
@RequestMapping("/city")
public class CityController {

    @Autowired
    private CityServiceImpl cityService;

    @RequestMapping("/getcityname")
    public ModelAndView hello(ModelAndView mv,@RequestParam String id){
        String name=(id);
        ("cityname",name);
        ("cityname");
        return mv;
    }
}

14,:在webinfo下的jsp文件夹中:

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2017/8/22
  Time: 16:56
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
${cityname}
</body>
</html>

经过上面这么多我们就是要去根据id取得name,开始运行,浏览器中输入:

http://localhost:8080/ssmcreater/city/getcityname?id=1

结果:



——————————

数据库中:


好了,已成成功取得了id为1的name了~~

重点:这里在pojo中的实体类并没有实际用到,纯粹去查数据库了,加在项目中是为了项目完整

在****下载:/download/wujunwen/9946722

同时,我上传到了github:/BabyWords/ssmcreater