Understand the construction and operation process of SSM

Create maven project Introduce dependency <!-- https://mvnrepository.com/artifact/org.springframework/spring-contex...
  • Create maven project

  • Introduce dependency
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.9</version> </dependency> <!-- spring mvc relevant--> <!--WebApplicationContextUtils--> <!-- https://mvnrepository.com/artifact/org.springframework/spring-web --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.3.9</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.9</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>5.3.9</version> </dependency> <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.7</version> <scope>runtime</scope> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.3.9</version> </dependency> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.12.3</version> </dependency> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.12.4</version> </dependency> <!-- mybatis relevant--> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.37</version> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.6</version> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.5</version> </dependency> <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.16</version> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.6</version> </dependency>
  • Spring MVC configuration file
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- Scan only controller--> <context:component-scan base-package="com.ssm.crud"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- Annotation driven --> <mvc:annotation-driven/> <!-- Processing of static resources--> <mvc:default-servlet-handler/> <!-- view resolver --> <bean> <property name="prefix" value="/WEB-INF/pages/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- <mvc:interceptors>--> <!-- <mvc:interceptor>--> <!-- <mvc:mapping path="/**"/>--> <!-- <mvc:exclude-mapping path="/admin/**"/>--> <!-- <bean></bean>--> <!-- </mvc:interceptor>--> <!-- </mvc:interceptors>--> </beans>
  • spring configuration file
<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"> <context:component-scan base-package="com.ssm.crud"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- Configure data sources--> <bean id="dataSource"> <property name="password" value="root"></property> <property name="username" value="root"></property> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://47.93.56.27:3306/ssm"></property> </bean> <!-- to configure mybatis And spring Integration, that is, when we are service Layer automatic injection Mapper When, At this time Mapper no spring Created, but mybatis Created, so let mybaits Create good mapper After implementation, automatic Put spring of IOC In container--> <!-- this bean Responsible for scanning mapper Interface to which it is bound xml File to create a proxy object, but To create a proxy object, you also need sqlSessionFactroy This class, so this class also needs to be put into spring In container--> <bean> <property name="basePackage" value="com.ssm.crud.mapper"></property> </bean> <bean> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:mybatisConfig.xml"></property> <property name="mapperLocations" value="classpath:mapper/*.xml"></property> </bean> <!-- spring Declarative transaction--> <!-- 1.Configure transaction manager--> <bean id="transactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- The configuration notification (enhancement) is generally written by ourselves. We don't need to configure it. We just need to configure it However, for this transaction notification, we can configure some properties, such as transaction propagation behavior, rollback exception, etc--> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*"/> <tx:method name="get*" read-only="true"/> <tx:method name="list*" read-only="true"/> </tx:attributes> </tx:advice> <!-- Configure weaving, which methods need which notifications--> <!-- <aop:config>--> <!-- <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.ssm.crud.service..*.*(..))"></aop:advisor>--> <!-- </aop:config>--> <!-- amount to springboot of @EnableTranstionManagment--> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
  • Mapper file for mybatis
<?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.crud.mapper.UserMapper"> <insert id="save"> insert into user(`name`,age)values (#,#) </insert> <select id="list" resultType="user"> select * from user </select> </mapper>
  • web.xml file
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!-- to configure spring The listener of the project is created as soon as the project is started IOC Container, inject dependencies, and IOC Container in ServletContext in--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <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-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> <!-- Character encoding filter--> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>forceRequestEncoding</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>forceResponseEncoding</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
  • Comparison between ordinary Java Web Engineering and SSM engineering.
    SSM is further encapsulated in the Java Web project. Previously, it was configured in web.xml. When the server receives the request, tomcat creates a Servlet object and executes doGet or doPost methods. At this time, the Servlet calls the Service method, and the Service layer calls the Dao layer method to save the data to the database.
    When SSM is used, we only need to configure a small amount of information in web.xml to run the project. The function of spring listener is to load the configuration file when the server starts, create objects through reflection, and solve the attribute dependency, that is, it does not need new xxxservice () in the servlet, nor new xxxDao in the service. This enables control reversal. Then there is the DispatcherServlet of spring MVC, which intercepts all requests. When the server receives the request, it gives it to the DispatcherServlet for processing. In this servlet, spring MVC will find all controllers according to the requested url and other information, and then find the method marked @ RequestMapping in each controller, or through reflection, To execute the processor method. After execution, spring MVC returns it to the front end. This involves a problem. When spring MVC creates a controller object, if it automatically injects a service, and then the service is in the spring container, how can spring MVC find it? After the spring listener creates the IOC container, it will copy the container to the servletContext. The objects in the spring container can be obtained through the WebApplicationContextUtils provided by spring. At this time, spring MVC can complete dependency injection. How does spring integrate with mybatis? At this time, we introduce the adaptation package of spring and mybatis, let spring create the sqlsessionfactorionbean of mybatis, and tell mybatis where the proxy interface is. Let spring create the mappercannerconfirue object and execute the interface package. This object will get the SqlSessionFactory from the container, and then create a proxy object for each interface and put it into the spring container, At this point, you can automatically inject the interface implementation object of mybatis in the service layer.

8 October 2021, 15:23 | Views: 2001

Add new comment

For adding a comment, please log in
or create account

0 comments