Good Room 01_ Set up environment

Good House: Build Environment

1. Project introduction

1. Introduction

Shang Hao Fang is a second-hand housing management service platform, which opens up high-quality resources and online capabilities, aggregates online and offline second-hand housing resources, creates an all-round second-hand housing service ecological market, and provides consumers with high-quality housing service resources.

2. Core Technology

Infrastructure: ssm
Distributed Framework: ssm + Dubbo + zk
spring session redis for session sharing
Data Cache: redis
Picture Server: Qiniuyun
Background administrative privilege control: spring-security
Front-end user login judgment: interceptor
Prototype design: Ink knife
Background administration template: Thymeleaf
Front End Technology: Vue+Axios

3. Project Modules

Final Distributed Architecture Module

shf-parent: root directory, management sub-module:

common-util: public class module

model: Entity class module

Service:dubbo service parent node

service-acl: Rights Service Module

service-house: House Source Service Module

service-user: user service module

service-api:dubbo service API interface

web: front end (dubbo service consumer)

web-admin: Background Management System

web-front: front-end of website

Module call relationships, as shown in the following figure:

4. Project presentation address

Good Home Background Management System: http://139.198.152.148:8001/
Good room front end: http://139.198.152.148:8002/

Understanding project business from presentation

5. Database

The database is obtained from the resource file and imported into the database with the following tables:

6. Other Resources

For example, entity classes and tool classes are copied directly to the project module for use without further explanation and introduced when other resources are needed.

2. Setting up environment

At present, we first set up a "good house" background management system, first set up as a single architecture: SSM framework, and then we transition to the distributed architecture of Dubbo communications through the SSM architecture.

SSM monolithic architecture module:

shf-parent: root directory, management sub-module:

common-util: public class module

model: Entity class module

web-admin: Background Management System

Building SSM architecture is a three-step process:

  1. Build project structure, configure dependencies

  2. Configure SSM environment

  3. Test SSM environment

1. Build project structure

1.1. Build parent project shf-parent

  1. Create a Maven Project named shf-parent with version number 1.0

  2. Specify the packaging method as POM in the pom.xml file

  3. Delete src directory

  4. Parent Project Management Dependency

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.atguigu</groupId>
        <artifactId>shf-parent</artifactId>
        <packaging>pom</packaging>
        <version>1.0</version>
        <properties>
            <maven.compiler.source>8</maven.compiler.source>
            <maven.compiler.target>8</maven.compiler.target>
            <!--version control-->
            <spring.version>5.0.5.RELEASE</spring.version>
            <thymeleaf.version>3.0.11.RELEASE</thymeleaf.version>
            <pagehelper.version>5.2.0</pagehelper.version>
            <servlet-api.version>2.5</servlet-api.version>
            <fastjson.version>1.2.29</fastjson.version>
            <mybatis.version>3.4.5</mybatis.version>
            <mybatis.spring.version>1.3.1</mybatis.spring.version>
            <mysql.version>5.1.46</mysql.version>
            <druid.version>1.1.12</druid.version>
            <commons-fileupload.version>1.3.1</commons-fileupload.version>
            <slf4j-version>1.7.30</slf4j-version>
            <logback-version>1.2.3</logback-version>
            <lombok.version>1.18.8</lombok.version>
            <dubbo.version>2.6.0</dubbo.version>
            <zookeeper.version>3.4.7</zookeeper.version>
            <junit.version>4.12</junit.version>
            <zkclient.version>0.1</zkclient.version>
            <qiniu.version>7.2.0</qiniu.version>
            <spring.security.version>5.2.7.RELEASE</spring.security.version>
            <thymeleaf-springsecurity5.version>3.0.4.RELEASE</thymeleaf-springsecurity5.version>
            <redis-session.version>1.3.5.RELEASE</redis-session.version>
        </properties>
        <!-- Dependency Management -->
        <dependencyManagement>
            <dependencies>
                <!--spring-session synchronization-->
                <dependency>
                    <groupId>org.springframework.session</groupId>
                    <artifactId>spring-session-data-redis</artifactId>
                    <version>${redis-session.version}</version>
                </dependency>
                <!--Be used for springsecurity5 Label-->
                <dependency>
                    <groupId>org.thymeleaf.extras</groupId>
                    <artifactId>thymeleaf-extras-springsecurity5</artifactId>
                    <version>${thymeleaf-springsecurity5.version}</version>
                </dependency>
                <!-- SpringMVC Relevant -->
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-webmvc</artifactId>
                    <version>${spring.version}</version>
                </dependency>
                <!--spring Encapsulated jdbc Database Access-->
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-jdbc</artifactId>
                    <version>${spring.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-tx</artifactId>
                    <version>${spring.version}</version>
                </dependency>
                <!--Spring Provided pairs AspectJ Framework Integration-->
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-aspects</artifactId>
                    <version>${spring.version}</version>
                </dependency>
                <!--Be used for spring test-->
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-test</artifactId>
                    <version>${spring.version}</version>
                </dependency>
    			<dependency>
                    <groupId>junit</groupId>
                    <artifactId>junit</artifactId>
                    <version>${junit.version}</version>
                </dependency>
                <!--Be used for springMVC Template-->
                <dependency>
                    <groupId>org.thymeleaf</groupId>
                    <artifactId>thymeleaf-spring5</artifactId>
                    <version>${thymeleaf.version}</version>
                </dependency>
    
                <!--mybatis Paging Plugin-->
                <dependency>
                    <groupId>com.github.pagehelper</groupId>
                    <artifactId>pagehelper</artifactId>
                    <version>${pagehelper.version}</version>
                </dependency>
                <!-- Mybatis -->
                <dependency>
                    <groupId>org.mybatis</groupId>
                    <artifactId>mybatis</artifactId>
                    <version>${mybatis.version}</version>
                </dependency>
                <!-- Mybatis and Spring Integrate what you need jar package -->
                <dependency>
                    <groupId>org.mybatis</groupId>
                    <artifactId>mybatis-spring</artifactId>
                    <version>${mybatis.spring.version}</version>
                </dependency>
                <!-- MySql -->
                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <version>${mysql.version}</version>
                </dependency>
                <!-- Connection Pool -->
                <dependency>
                    <groupId>com.alibaba</groupId>
                    <artifactId>druid</artifactId>
                    <version>${druid.version}</version>
                </dependency>
                <!-- File Upload Component -->
                <dependency>
                    <groupId>commons-fileupload</groupId>
                    <artifactId>commons-fileupload</artifactId>
                    <version>${commons-fileupload.version}</version>
                </dependency>
    
                <!-- fastjson -->
                <dependency>
                    <groupId>com.alibaba</groupId>
                    <artifactId>fastjson</artifactId>
                    <version>${fastjson.version}</version>
                </dependency>
    
                <!-- Journal -->
                <dependency>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-api</artifactId>
                    <version>${slf4j-version}</version>
                </dependency>
                <dependency>
                    <groupId>ch.qos.logback</groupId>
                    <artifactId>logback-classic</artifactId>
                    <version>${logback-version}</version>
                </dependency>
    
                <!--lombok-->
                <dependency>
                    <groupId>org.projectlombok</groupId>
                    <artifactId>lombok</artifactId>
                    <version>${lombok.version}</version>
                </dependency>
    
                <!-- dubbo Relevant -->
                <dependency>
                    <groupId>com.alibaba</groupId>
                    <artifactId>dubbo</artifactId>
                    <version>${dubbo.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.apache.zookeeper</groupId>
                    <artifactId>zookeeper</artifactId>
                    <version>${zookeeper.version}</version>
                </dependency>
                <dependency>
                    <groupId>com.github.sgroschupf</groupId>
                    <artifactId>zkclient</artifactId>
                    <version>${zkclient.version}</version>
                </dependency>
                <dependency>
                    <groupId>javassist</groupId>
                    <artifactId>javassist</artifactId>
                    <version>3.12.1.GA</version>
                </dependency>
                <dependency>
                    <groupId>commons-codec</groupId>
                    <artifactId>commons-codec</artifactId>
                    <version>1.10</version>
                </dependency>
    
                <!--Qiniuyun service platform, third-party services (picture upload)-->
                <dependency>
                    <groupId>com.qiniu</groupId>
                    <artifactId>qiniu-java-sdk</artifactId>
                    <version>${qiniu.version}</version>
                </dependency>
    
                <!-- spring security Security Framework -->
                <dependency>
                    <groupId>org.springframework.security</groupId>
                    <artifactId>spring-security-web</artifactId>
                    <version>${spring.security.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.springframework.security</groupId>
                    <artifactId>spring-security-config</artifactId>
                    <version>${spring.security.version}</version>
                </dependency>
            </dependencies>
        </dependencyManagement>
    	<!--Introduce servlet rely on-->
        <dependencies>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>servlet-api</artifactId>
                <version>${servlet-api.version}</version>
                <scope>provided</scope>
            </dependency>
        </dependencies>
    </project>
    

1.2. Building tool class module common-util

  1. Name the new module common-util as a child project under the parent project shf-parent

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactId>shf-parent</artifactId>
            <groupId>com.atguigu</groupId>
            <version>1.0</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
        <artifactId>common-util</artifactId>
        <packaging>jar</packaging>
        <properties>
            <maven.compiler.source>8</maven.compiler.source>
            <maven.compiler.target>8</maven.compiler.target>
        </properties>
    </project>
    
  2. common-util project introducing dependencies

    <dependencies>
        <!-- SpringMVC Relevant -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>
        <!--spring Encapsulated jdbc Database Access-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
        </dependency>
        <!--Spring Provided pairs AspectJ Framework Integration-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
        </dependency>
        <!--Be used for spring test-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
        </dependency>
    	    <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
        <!--Be used for springMVC Template-->
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring5</artifactId>
        </dependency>
    
        <!--mybatis Paging Plugin-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
        </dependency>
        <!-- Mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
        </dependency>
        <!-- Mybatis and Spring Integrate what you need jar package -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
        </dependency>
        <!-- MySql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!-- Connection Pool -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
        </dependency>
        <!-- File Upload Component -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
        </dependency>
    
        <!-- fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
        </dependency>
    
        <!-- Journal -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
        </dependency>
    
        <!-- dubbo Relevant -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
        </dependency>
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
        </dependency>
        <dependency>
            <groupId>javassist</groupId>
            <artifactId>javassist</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
        </dependency>
    
        <!--Qiniuyun service platform, third-party services (picture upload)-->
        <dependency>
            <groupId>com.qiniu</groupId>
            <artifactId>qiniu-java-sdk</artifactId>
        </dependency>
    </dependencies>
    
  3. Copy the tool class into the common-util project

1.3. Build entity class module model

  1. Name the new module model as a child project under the parent project shf-parent

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactId>shf-parent</artifactId>
            <groupId>com.atguigu</groupId>
            <version>1.0</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
        <artifactId>model</artifactId>
        <packaging>jar</packaging>
        <properties>
            <maven.compiler.source>8</maven.compiler.source>
            <maven.compiler.target>8</maven.compiler.target>
        </properties>
    </project>
    
  2. Introducing lombok dependency

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    
  3. Copy Entity Classes to model Project

1.4. Build project module web-admin

  1. Name the new module web-admin as a child project under the parent project shf-parent

  2. Convert to a web project using plug-ins

  3. Introducing dependencies

    <dependencies>
        <dependency>
            <groupId>com.atguigu</groupId>
            <artifactId>common-util</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>com.atguigu</groupId>
            <artifactId>model</artifactId>
            <version>1.0</version>
        </dependency>
    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>9.4.15.v20190215</version>
                <configuration>
                    <!-- Automatically hot deploy if project changes are detected every n Scan once per second. Default 0, no scan-->
                    <scanIntervalSeconds>2</scanIntervalSeconds>
                    <webAppConfig>
                        <!--Appoint web The root path of the project, by default/ -->
                        <contextPath>/</contextPath>
                    </webAppConfig>
                    <httpConnector>
                        <!--Port number, default 8080-->
                        <port>8000</port>
                    </httpConnector>
                </configuration>
            </plugin>
        </plugins>
    </build>
    

    Note: We use jetty for web containers, and like tomcat, jetty is lighter.

2. Configure SSM environment

Operation module: web-admin

2.1. Add Log Files

Add in the resources directory:

logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false">

    <!--Define the storage address for log files logs For the current project logs The directory can also be set to../logs -->
    <property name="LOG_HOME" value="logs" />

    <!--Console Log, Console Output -->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <!--Format output:%d Indicates the date,%thread Represents the thread name,%-5level: Level shows 5 character width from left,%msg: Log messages,%n Is a line break-->
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
        </encoder>
    </appender>
    <!-- Log Output Level -->
    <root level="DEBUG">
        <appender-ref ref="STDOUT" />
    </root>
</configuration>

2.2, SSM Layering

ssm is divided into three layers:

controller, service, and dao layers

We add a profile for each layer assignment to facilitate profile management:

​ spring-mvc.xml

​ spring-service.xml

​ spring-persist.xml

2.3. Add persistence layer profile

Create a spring directory in the resources directory and add: spring-persist.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:context="http://www.springframework.org/schema/context"
       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">

    <!--Configure Data Source-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
        <property name="username" value="${datasource.username}" />
        <property name="password" value="${datasource.password}" />
        <property name="driverClassName" value="${datasource.driverClassName}" />
        <property name="url" value="${datasource.url}" />
    </bean>

    <!--spring and mybatis integration-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!--Alias Configuration-->
        <property name="typeAliasesPackage" value="com.atguigu.entity"></property>
        <property name="configuration">
            <bean class="org.apache.ibatis.session.Configuration">
                <!--Camel Mapping-->
                <property name="mapUnderscoreToCamelCase" value="true"/>
            </bean>
        </property>
        <property name="mapperLocations">
            <array>
                <value>classpath:mappers/*.xml</value>
            </array>
        </property>

        <!--jPaginate-->
        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageInterceptor">
                    <property name="properties">
                        <props>
                            <!-- Set up reasonable by true Indicates that the page number will be rationalized. Valid range of page numbers: 1~PageCount -->
                            <prop key="reasonable">true</prop>
                            <!-- Database dialect: the same is true SQL Statement, get different databases, there will be differences in syntax -->
                            <!-- By default, follow the MySQL Run as database dialect -->
                            <prop key="helperDialect">mysql</prop>
                        </props>
                    </property>
                </bean>
            </array>
        </property>
    </bean>
    <!--Scan persistence layer interfaces-->
    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.atguigu.mapper"/>
    </bean>
</beans>

Add a jdbc.properties file to the resources directory

datasource.username=root
datasource.password=123456
datasource.url=jdbc:mysql://localhost:3306/db_house?characterEncoding=utf8&amp;serverTimezone=Asia/Shanghai&amp;allowPublicKeyRetrieval=true
datasource.driverClassName=com.mysql.jdbc.Driver

Create a mapping profile storage folder: Add: mappers folder in the resources directory

2.4, Add service Layer Profile

Add: spring-service.xml in the resources/spring directory:

<?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:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!--Packet Scanning-->
    <context:component-scan base-package="com.atguigu.service"/>
    <!--Import spring/spring-persist.xml-->
    <import resource="classpath:spring/spring-persist.xml"/>
    <!--Transaction Manager Object-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

2.5, Add controller Layer Profile

Add in the resources/spring directory: spring-mvc.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: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.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">
    <!--Packet Scanning-->
    <context:component-scan base-package="com.atguigu">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
    </context:component-scan>
    <!--Static Resource Processing-->
    <mvc:default-servlet-handler />
    <!-- open mvc annotation-->
    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <!-- To configure Fastjson Support -->
            <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/html;charset=UTF-8</value>
                        <value>application/json</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
    <!--view resolver-->
    <bean id="templateResolver" class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
        <!--Configure Prefix-->
        <property name="prefix" value="/WEB-INF/templates/"></property>
        <!--Configure Suffix-->
        <property name="suffix" value=".html"></property>
        <!--Configure Encoding Format-->
        <property name="characterEncoding" value="UTF-8"></property>
        <!--Set cache to null-->
        <property name="cacheable" value="false"></property>
        <!--Configuration template mode,
        HTML5:Represents a strict pattern
        LEGACYHTML5: Represents relaxed mode-->
        <property name="templateMode" value="LEGACYHTML5"></property>
    </bean>
    <!--To configure spring View Parser-->
    <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
        <!--Format Encoding-->
        <property name="characterEncoding" value="UTF-8"></property>
        <!--Set up template engine-->
        <property name="templateEngine" ref="templateEngine"/>
    </bean>
    <bean id="templateEngine" class="org.thymeleaf.spring5.SpringTemplateEngine">
        <!--Reference View Resolver-->
        <property name="templateResolver" ref="templateResolver"></property>
    </bean>
</beans>

2.6. Modify the web.xml file

<?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"
         version="2.5">
    <!--To configure DispatcherServlet-->
    <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/spring-mvc.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>

    <!--To configure ContextLoaderListener-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/spring-service.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!--To configure EncodingFilter-->
    <!-- Configure Filter Resolution POST Requested character scrambling problem -->
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <!-- encoding Parameter specifies the name of the character set to be used -->
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <!-- Request Enforcement -->
        <init-param>
            <param-name>forceRequestEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
        <!-- Response Enforcement Coding -->
        <init-param>
            <param-name>forceResponseEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

3. Testing SSM environment

For example, let's take user roles and get all the role data displayed on the page. The following actions are in the web-admin project

3.1, Create RoleMapper

In the com.atguigu.mapper package

package com.atguigu.mapper;
import com.atguigu.entity.Role;
import java.util.List;
public interface RoleMapper {
    List<Role> findAll();
}

3.2, Create RoleMapper.xml Mapping Profile

In the resources/mappers directory

<?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.atguigu.mapper.RoleMapper">
    <!--Field of multiplex query-->
    <sql id="column">
        select id,role_name,role_code,description,create_time,update_time,is_deleted
    </sql>
    <select id="findAll" resultType="Role">
        <include refid="column"></include>
        from acl_role
        where is_deleted = 0
    </select>
</mapper>

3.3. Creating a RoleService interface

In the com.atguigu.service package

package com.atguigu.service;
import com.atguigu.entity.Role;
import java.util.List;
public interface RoleService {
    List<Role> findAll();
}

3.4. Create RoleServiceImpl implementation class

In the com.atguigu.service.impl package

package com.atguigu.service.impl;
import com.atguigu.mapper.RoleMapper;
import com.atguigu.entity.Role;
import com.atguigu.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class RoleServiceImpl implements RoleService {
   @Autowired
   private RoleMapper roleMapper;
   public List<Role> findAll() {
      return roleMapper.findAll();
   }
}

3.5, Create RoleController class

In the com.atguigu.controller package

package com.atguigu.controller;
import com.atguigu.entity.Role;
import com.atguigu.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
@RequestMapping(value="/role")
public class RoleController {
   @Autowired
   private RoleService roleService;
   private final static String PAGE_INDEX = "role/index";
   /** 
    * list
    * @param model
    * @return
    */
   @RequestMapping
   public String index(Model model) {
      List<Role> list = roleService.findAll();
      model.addAttribute("list", list);
      return PAGE_INDEX;
   }
}

3.6. Creating Data Explicit Pages

Create index.html in the WEB-INF/templates/role s directory

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
        <table>
            <tr th:each="item,it : ${list}">
                <td class="text-center" th:text="${it.count}">11</td>
                <td th:text="${item.roleName}">22</td>
                <td th:text="${item.roleCode}">33</td>
                <td th:text="${item.description}">33</td>
                <td th:text="${#dates.format(item.createTime,'yyyy-MM-dd HH:mm:ss')}" >33</td>
            </tr>
        </table>
    </body>
</html>

3.7. Deploying a web-admin project

Step 1:

Step 2:

3.8, Access

Tags: Java Spring Dubbo

Posted by mohabitar on Thu, 01 Sep 2022 00:16:33 +0530