网上经常有一些SSM的框架,根本都运行不起来,还要发出来骗人,本人非常抵制这些行为。为了让初学者继续学习下去,本人花费了一点小时间,帮大家建立起学习JAVA的自信。好了废话不多说,交您一步一步搭建SSM框架(Spring+SpringMvc+MyBatis)

第一步:打开IDEA软件 选择新建项目按图操作

java 登录框架 java框架搭建_xml

第二步:点击next

java 登录框架 java框架搭建_spring_02

第三步:点击NEXT

java 登录框架 java框架搭建_java 登录框架_03

这块,需要您安装MAVEN ,我本机已经安装了,如果您安装了请直接next直到完成,项目建立完毕,否则您直接去网上找教程安装配置MAVEN 非常简单的,这里就不在教大家如何安装。

第四步:在SRC/main 下建立java和resources 两个文件夹

java 登录框架 java框架搭建_xml_04

第五步:设置这两个文件夹,可以点击文件下来菜单里的和快捷里的快捷按钮都可以

java 登录框架 java框架搭建_bc_05

java 登录框架 java框架搭建_bc_06

打开对话框:

java 登录框架 java框架搭建_java 登录框架_07

上面操作完成点击apply和ok 此时工作区域的文件夹为如下所示

java 登录框架 java框架搭建_bc_08

项目名称不一致没有关系。其它一致即可。

第六步:我们打开pom.xml,建立如下依赖

<dependencies>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>5.2.9.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>5.2.9.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.9.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-expression</artifactId>
      <version>5.2.9.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>5.2.9.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>5.2.9.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.2.9.RELEASE</version>
    </dependency>


    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-orm</artifactId>
      <version>5.2.9.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>5.2.9.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aspects</artifactId>
      <version>5.2.9.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jcl</artifactId>
      <version>5.2.9.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-framework-bom</artifactId>
      <version>5.2.9.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-messaging</artifactId>
      <version>5.2.9.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.2.9.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.2.9.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.2</version>
    </dependency>

    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.2.1</version>
    </dependency>

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.21</version>
    </dependency>

    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>2.0.5</version>
    </dependency>

    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.5.5</version>
    </dependency>

    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.5</version>
    </dependency>

    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.7.12</version>
    </dependency>

    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

上面依赖大家都能看懂,无非就是Spring 的依赖和SpringMVC的依赖 mysql数据库连接 阿里的连接池还有日志log4j 以及commons-log等 保存后,maven将自动下载这些依赖

我们在resources文件夹下建立以下文件

(1)mapper文件夹/MyUserMapper.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.dzeco.dao.MyUserDao">

    <select id="getById" parameterType="LONG" resultType="com.dzeco.pojo.myuser">
        SELECT * FROM myuser WHERE id=#{id}
    </select>

</mapper>

(2)数据库配置

jdbc.user=root
jdbc.password=123456
jdbc.driverClass=com.mysql.cj.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=Asia/Shanghai

(3)log4j配置

#//注意这里不要用INFO,DEBUG等 改个名字
log4j.rootLogger=infoA,errorA,stdout,DEBUGA

#过滤掉spring框架下的额外日志
log4j.category.org.springframework = WARN

#输出到控制台
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.Threshold = debug
log4j.appender.stdout.layout.ConversionPattern=%d %-5p %c{1}:%L - %m%n

#输出error到指定文件
log4j.logger.errorA=error
log4j.appender.errorA=org.apache.log4j.DailyRollingFileAppender
log4j.appender.errorA.layout=org.apache.log4j.PatternLayout
log4j.appender.errorA.layout.ConversionPattern=%d{yyyy-MM-dd-HH-mm} [%t] [%c] [%p] - %m%n
log4j.appender.errorA.datePattern=yyyy-MM-dd'.log'
log4j.appender.errorA.Threshold = error
log4j.appender.errorA.append=true
log4j.appender.error.File=d:/log/error.log

#输出DEBUG信息到指定文件
log4j.logger.DEBUGA=DEBUG
log4j.appender.DEBUGA=org.apache.log4j.DailyRollingFileAppender
log4j.appender.DEBUGA.layout=org.apache.log4j.PatternLayout
log4j.appender.DEBUGA.layout.ConversionPattern=%d{yyyy-MM-dd-HH-mm} [%t] [%c] [%p] - %m%n
log4j.appender.DEBUGA.datePattern=yyyy-MM-dd'.log'
log4j.appender.DEBUGA.Threshold = DEBUG
log4j.appender.DEBUGA.append=true
log4j.appender.DEBUGA.File=d:/log/debug_log.log

#打印sql语句
log4j.logger.com.ibatis=DEBUG
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG

(4)mybatis配置

<?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>
    <settings>
        <setting name="jdbcTypeForNull" value="NULL"/>
    </settings>
</configuration>

(5)spring配置

<?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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd">
    <!--载入数据库配置文件-->

    <context:property-placeholder location="classpath:db.properties"/>

    <!--配置数据库连接池-->
    <bean  id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="url" value="${jdbc.jdbcUrl}"></property>
        <property name="driverClassName" value="${jdbc.driverClass}"></property>
        <property name="username" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
        <property name="initialSize" value="5"></property>
        <property name="maxActive" value="20"></property>
        <property name="maxWait" value="60000"></property>
        <property name="minIdle" value="10"></property>
        <property name="timeBetweenEvictionRunsMillis" value="60000"></property>
        <property name="minEvictableIdleTimeMillis" value="300000"></property>
        <property name="testOnBorrow" value="false"></property>
        <property name="testOnReturn" value="false"></property>
        <property name="testWhileIdle" value="true"></property>
        <property name="validationQuery" value="SELECT 1"></property>
        <property name="poolPreparedStatements" value="true"></property>
        <property name="maxOpenPreparedStatements" value="20"></property>
    </bean>

    <!--配置spring 扫描器-->
    <context:component-scan base-package="com.dzeco" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
    </context:component-scan>

    <!--配置视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/templates/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    <!--防止中文乱码-->
    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="supportedMediaTypes" value="text/html;charset=UTF-8"/>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

    <!--不处理静态文件-->
    <mvc:default-servlet-handler/>
    <!--配置事务-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--整合mybatis-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="mapperLocations" value="classpath:/mapper/*.xml"></property>
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
    </bean>
    <!--mapper 扫描-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.dzeco.dao"></property>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>


</beans>

 

我们在webapp\web-inf下建立文件夹templates

web.xml的配置如下,这里面配置的就是dispatcherservlet 和防止乱码

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <filter>
    <filter-name>CharacterEncodingFilter</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>
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath*:spring-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

我们在src/main/java 文件夹下建立com.dzeco这个包,我们在这个包中建立controller、dao、pojo,service,在service下建立iml文件夹此时如下所示

java 登录框架 java框架搭建_bc_09

我们在controller里建立MyUserController.java

package com.dzeco.controller;

import com.dzeco.pojo.myuser;
import com.dzeco.service.MyUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @autohr Alex
 * @create 2020-10-13 10:48
 */
@Controller
public class MyUserController {
    @Autowired
    private MyUserService myUserService;
    @ResponseBody
    @GetMapping("/index")
    public String index(){
        myuser user=myUserService.getById((long) 1001);
        return user.toString();
    }
}

我们在dao文件夹下建立接口MyUserDao.java

package com.dzeco.dao;

import com.dzeco.pojo.myuser;

/**
 * @autohr Alex
 * @create 2020-10-13 10:39
 */
public interface MyUserDao {
    public myuser getById(Long id);
}

我们在pojo文件夹下建立myuser.java

package com.dzeco.pojo;

import java.io.Serializable;
import java.util.Date;

/**
 * @autohr Alex
 * @create 2020-09-30 17:11
 */
public class myuser implements Serializable{
    private static final long serialVersionUID = 3210349269639326494L;
    private Integer id;
    private String user;
    private int roadid;
    private String gender;
    private String hoppy;
    private Date addtime;
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public int getRoadid() {
        return roadid;
    }

    public void setRoadid(int roadid) {
        this.roadid = roadid;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getHoppy() {
        return hoppy;
    }

    public void setHoppy(String hoppy) {
        this.hoppy = hoppy;
    }

    public Date getAddtime() {
        return addtime;
    }

    public void setAddtime(Date addtime) {
        this.addtime = addtime;
    }

    @Override
    public String toString() {
        return "myuser{" +
                "id=" + id +
                ", user='" + user + '\'' +
                ", roadid=" + roadid +
                ", gender='" + gender + '\'' +
                ", hoppy='" + hoppy + '\'' +
                ", addtime=" + addtime +
                '}';
    }
}

 

我们在service下建立接口MyUserService.java

package com.dzeco.service;

import com.dzeco.pojo.myuser;

/**
 * @autohr Alex
 * @create 2020-10-13 10:42
 */
public interface MyUserService {
    public myuser getById(Long id);
}

 

我们在iml文件夹下建立MyUserServiceIml.java 实现上面接口

package com.dzeco.service.iml;

import com.dzeco.dao.MyUserDao;
import com.dzeco.pojo.myuser;
import com.dzeco.service.MyUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @autohr Alex
 * @create 2020-10-13 10:42
 */
@Service
public class MyUserServiceIml implements MyUserService {
    @Autowired
    private MyUserDao myUserDao;
    @Override
    public myuser getById(Long id) {
        return myUserDao.getById(id);
    }
}

最后,我们配置下运行环境点击

java 登录框架 java框架搭建_java 登录框架_10

弹出选择左面+号,按图操作

java 登录框架 java框架搭建_java 登录框架_11

配置运行端口

java 登录框架 java框架搭建_bc_12

选择deploment

java 登录框架 java框架搭建_spring_13

更改成以下内容

java 登录框架 java框架搭建_xml_14

运行程序

java 登录框架 java框架搭建_xml_15

此时 SSM框架已经搭建完毕,最终结果

java 登录框架 java框架搭建_spring_16