ssm project construction (learning notes)

1. Construction of environment

Import the corresponding jar package using maven. The dependencies involved are as follows

<dependencies>
        <!--Junit-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <!--Database driven-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
        <!-- Database connection pool -->
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.2</version>
        </dependency>

        <!--Servlet - JSP -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <!--Mybatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.2</version>
        </dependency>

        <!--Spring-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
        <!--Lombok Auto build get set. . method-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.10</version>
        </dependency>
    </dependencies>

In order to load resources better, set this resource filter

    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>

After the relevant jar packages are imported, the next step is to create modules to let different packages complete their tasks

 

Four packages are simply created. First, the controller package is responsible for the servlet, that is, receiving the front-end data, and then calling the service layer accordingly.

dao package is responsible for the task of operating the database. We create an interface BookMapper to encapsulate various methods of operating the database, and then bind the interface to BookMapper Implement the mapping with specific sql in XML. Next, the pojo package is used to store entity classes (whose attributes correspond to database fields), which is more convenient to encapsulate an object taken from the database. With this object, it is more convenient to operate it. The last is the service layer, that is, the business layer, which is responsible for realizing various front-end requirements by integrating various methods of the dao layer.

People - > front end - > controller - > Service - > Dao - > database, and then package and submit it to the front end layer by layer

2. Preparation of relevant configuration files

In the resource directory, we need to inject relevant bean s and configure mybatis

 1. The other three bean s (spring Dao, spring MVC, spring service) are import ed in the ApplicationContext

database.properties stores the related properties used to link MySQL

Note: useSSL=false I don't know why the previous write useSSL=true always reported an error

Mybatis config is the configuration of 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">
<!-- Root element of configuration file -->
<configuration>
<!--    Configure the data source and give it to spring Do it-->
<typeAliases>
    <package name="com.real.pojo"/>
<!--    <typeAlias type="com.real.pojo.Books" alias="Books"/>-->
</typeAliases>
<mappers>
    <mapper class="com.real.dao.BookMapper"/>
</mappers>
</configuration>

dao, mvc and service are:

<?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-3.0.xsd
        http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<!--Associated database profile-->
    <context:property-placeholder location="classpath:database.properties"/>
    <!--Create a connection pool so that links stay in the pool without having to request every time to improve efficiency-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <!-- c3p0 Private properties of connection pool -->
        <property name="maxPoolSize" value="30"/>
        <property name="minPoolSize" value="10"/>
        <!-- Not automatically after closing the connection commit -->
        <property name="autoCommitOnClose" value="false"/>
        <!-- Get connection timeout -->
        <property name="checkoutTimeout" value="10000"/>
        <!-- Number of retries when getting connection failed -->
        <property name="acquireRetryAttempts" value="2"/>

    </bean>
<!--establish sqlSessionFactory,This creates SqlSession object-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
    <!--    binding mybatis configuration file-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>

    <!--realization dao Interface scanning package, dynamic dao Interface injection spring in-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--injection sqlSessionFactory-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <!--To scan dao package-->
        <property name="basePackage" value="com.real.dao"/>
    </bean>
</beans>

The main purpose of dao is to connect to the database to create a SqlSessionFactory, and create a sqlsession instance through this factory

The sqlsession instance can execute the corresponding sql statements

<?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-3.0.xsd
        http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!--Add annotation driven controller Comments in can be checked)-->
    <mvc:annotation-driven/>
<!--    Static resource filtering-->
    <mvc:default-servlet-handler/>
<!--Scan package (scan the context in the class)-->
    <context:component-scan base-package="com.real.controller"/>
<!--View parser (complete by splicing) jsp (path)-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <context:component-scan base-package="com.real.controller" />

</beans>

mvc is mainly used to parse the string returned in the servlet and splice it into a complete jsp path

<?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-3.0.xsd
        http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <!--scanning service Under the package, make sure service The annotations in the package can be used-->
    <context:component-scan base-package="com.real.service"/>
<!--Inject all our business into spring In, use configuration or annotation-->
    <bean id="bookServiceImpl" class="com.real.service.BookServiceImpl">
        <property name="bookMapper" ref="bookMapper"/>
    </bean>
<!--Declarative transaction-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--Injection data source-->
        <property name="dataSource" ref="dataSource"/>
    </bean>

</beans>

The overall process is roughly like this. After each package is created, each package is handed over to spring for hosting

The controller package is hosted to spring MVC to add annotation drivers, scan the following classes, and splice the returned jsp names into a complete jsp path to achieve a successful jump

The dao package is hosted to the connection pool of the database created by spring dao, and connects to the database through the org. Org. Package provided in spring mybatis. spring. Sqlsessionfactorybean to create SQLSessionFactory to create their own sqlsession instances to execute sql statements

Hosting the service package to spring service is to host its classes, add annotation drivers, and finally declare its transaction injection data source

@The Controller is automatically bound to this servlet
@RequestMapping() automatically adds the access path of the url
@Autowired automatically injects this property or object into spring
@Qualifier () automatically assigns values to this property or object

Keywords: Java Spring

Added by bionicdonkey on Sat, 08 Jan 2022 15:22:50 +0200