A detailed explanation of building SSM framework for Java Web Development

1. Jar package needed: because many jar packages are not easy to download, I directly upload them to Baidu online disk:

 

 

A lot of them are hard to download. I've sorted them out:

Links: https://pan.baidu.com/s/1iIFprmstp86uKz8OgHDMWg Password: oewl

 

 

About SSM framework: it has been introduced in previous blogs

Spring framework: https://www.cnblogs.com/xuyiqing/category/1164340.html

Spring MVC framework: https://www.cnblogs.com/xuyiqing/category/1269718.html

MyBatis framework: https://www.cnblogs.com/xuyiqing/category/1181308.html

 

Step 1: create a Dynamic Web Project

 

First, the directory structure after integration:

What we need to pay attention to is what I drew on the red line:

 

First, copy all the jar packages into the lib folder: different from the traditional java project, the path will be built automatically here. In a word, copying in will be finished

 

The configuration files of mybatis and spring are respectively in two folders:

 

First, configure mybatis: SqlMapConfig.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>

    <!-- alias -->
    <typeAliases>
        <package name="org.dreamtech.ssm.pojo"/>
    </typeAliases>

</configuration>

 

Configure Spring:

Dao layer: applicationContext-dao.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

    <!-- Configuration read properties file jdbc.properties -->
    <context:property-placeholder location="classpath:jdbc.properties" />

    <!-- Configure data sources -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>
    
    <!-- To configure SqlSessionFactory -->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- Set up MyBatis Core profile -->
        <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
        <!-- set up data sources -->
        <property name="dataSource" ref="dataSource" />
    </bean>
    
    <!-- To configure Mapper scanning -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- Set up Mapper Scanning package -->
        <property name="basePackage" value="org.dreamtech.ssm.mapper" />
    </bean>
    
</beans>

 

 

Service layer: applicationContext-service.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

    <!-- To configure Service scanning -->
    <context:component-scan base-package="org.dreamtech.ssm.service" />
</beans>

 

Configure spring MVC: 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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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">
    <!-- To configure Controller scanning -->
    <context:component-scan base-package="org.dreamtech.ssm.controller" />
    
        <context:property-placeholder location="classpath:resource.properties" />
    
    <!-- Configure annotation driver -->
    <mvc:annotation-driven />
    
    <!-- Release of static resources  -->
    <mvc:resources location="/css/" mapping="/css/**"/>
    <mvc:resources location="/js/" mapping="/js/**"/>
    <mvc:resources location="/fonts/" mapping="/fonts/**"/>
    <!-- Configure view resolver -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- prefix -->
        <property name="prefix" value="/WEB-INF/jsp/" />
        <!-- Suffix -->
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

 

 

Next, even the most critical step: web.xml configuration

<?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_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>SSM</display-name>
    <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>

    <!-- To configure spring -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/applicationContext-*.xml</param-value>
    </context-param>

    <!-- Configure listener loading spring -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>


    <!-- Configuring filters, resolving post The random code problem of -->
    <filter>
        <filter-name>encoding</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>encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
    <!-- To configure SpringMVC -->
    <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/springmvc.xml</param-value>
        </init-param>
        <!-- To configure springmvc When to start, the parameter must be an integer -->
        <!-- If 0 or greater then springMVC along with TOMCAT Container started -->
        <!-- If it is less than 0, it will start when the first request comes in -->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>ssm</servlet-name>
        <!-- All requests in springMVC -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

 

 

Other miscellaneous configurations:

1. Database configuration: user name, database name, connection configuration, etc

jdbc.properties:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root

 

2. Log file configuration:

log4j.properties:

# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

 

3. As for resource.properties, it is required by the back project, which is not related to the SSM framework

Tags: Java Spring xml JDBC Mybatis

Posted on Mon, 06 Jan 2020 02:53:05 -0800 by Splynx