I believe that you have learned the SSM framework, but how to use these techniques to build a better project in IDEA? I’m going to use a simple example to record the process, nanny record, I hope you enjoy.

DAO layerThe implementation of the

1. Create projects and dependencies

Simply create a Maven project, don’t need to check in advance, we will need to use the pom. XML supplement, which is used to test and log, plus Spring core dependencies.

Two official sites for checking Maven dependencies are recommended: mvnrepository.com/ search.maven.org/

		<dependency>
            <! -- Use junit4 -->
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <! -- Complete project dependencies -->
        <! - 1: journal of Java: slf4j, log4j, logback, common - logging slf4j is specification/interface Logging implementation: log4j, logback, common - logging use: slf4j + logback - >
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.12</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>1.1.1</version>
        </dependency>
        <! Implement slF4J interface and integrate
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.1.1</version>
        </dependency>

		<! --1) Spring core dependencies -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.1.7. RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.1.7. RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.7. RELEASE</version>
        </dependency>
Copy the code
2. Database design and coding

Once the database is designed, it is time to supplement the database dependencies in POM.xml

<! --2: database dependencies -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>
Copy the code
3. Realize DAO programming based on MyBatis

First, import the dependencies related to Mybatis. When writing entity classes, the attributes of the best entity should correspond to the columns of the tables in the database. Then write some interface classes that operate on the database and some CORRESPONDING XML files

<! --2: database dependencies -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>
Copy the code

      
<! DOCTYPEconfiguration
        PUBLIC "- / / mybatis.org//DTD Config / 3.0 / EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <! -- Configure global properties -->
    <settings>
        <! GetGeneratedKeys; getGeneratedKeys; getGeneratedKeys;
        <setting name="useGeneratedKeys" value="true"/>
        <! Default :true select name as title from table -->
        <setting name="useColumnLabel" value="true"/>
        <! Table(create_time) -> Entity(createTime) ->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
</configuration>
Copy the code

      
<! DOCTYPEmapper
        PUBLIC "- / / mybatis.org//DTD Mapper / 3.0 / EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.seckill.dao.SeckillDao">
    <! SQL statement configuration for DAO interface methods -->

    <update id="reduceNumber">
        <! SQL > select * from 'SQL';update seckill set number = number - 1 where seckill_id = #{seckillId} and start_time <! [CDATA[ <= ]]> #{killTime} and end_time >= #{killTime} and number > 0;</update>

</mapper>
Copy the code
4. MyBatis integration with Spring

Import the Spring integration dependency implemented by Mybatis itself and compile the corresponding XML file

 <! Mybats' own implementation of Spring integration dependency
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.2.3</version>
        </dependency>

		<! Spring DAO layer dependencies -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.1.7. RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>4.1.7. RELEASE</version>
        </dependency>

		<! -- 4) Spring test dependencies -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.1.7. RELEASE</version>
        </dependency>
Copy the code

      
<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 http://www.springframework.org/schema/context/spring-context.xsd">
    <! Configure and integrate mybatis process
    <! ${url} --> ${url} -->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <! -- 2: Database connection pool -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <! -- Set connection pool properties -->
        <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 connection pool private property -->
        <property name="maxPoolSize" value="30"/>
        <property name="minPoolSize" value="10"/>
        <! -->
        <property name="autoCommitOnClose" value="false"/>
        <! Get connection timeout time -->
        <property name="checkoutTimeout" value="1000"/>
        <! Retry times -->
        <property name="acquireRetryAttempts" value="2"/>
    </bean>

    <! -- Convention greater than configuration -->
    <! SqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <! Insert into database pool -->
        <property name="dataSource" ref="dataSource"/>
        <! MyBatis -config.xml -->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <! -- Scan entity package using alias -->
        <property name="typeAliasesPackage" value="org.seckill.entity"/>
        <! SQL > select * from mapper;
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>
    </bean>

    <! -- 4: Configure the scan Dao interface package, dynamically implement the Dao interface, and inject it into the Spring container -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <! SqlSessionFactory -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <! The Dao interface package needs to be scanned
        <property name="basePackage" value="org.seckill.dao"/>
    </bean>
</beans>
Copy the code

Note: After completing a DAO interface, you should write a corresponding test class to test it. CTRL + Shift + T can quickly generate test classes

Implementation of the Web layer

1. Design Restful interfaces

Design Restful interfaces, this is very important, I recommend you to learn Restful


2. SpringMVC integrates Spring

Add spring Web-related dependencies and any Servlet Web-related dependencies you need to use to write the corresponding XML file

SpringMVC runs as follows:

		<! --3) Spring Web dependencies -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.1.7. RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.1.7. RELEASE</version>
        </dependency>

		<! -- 3:Servlet Web dependencies -->
		<dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.5.4</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
Copy the code

      
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
       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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <! -- Configure SpringMVC -->
    <! -- 1: Enable SpringMVC annotation pattern -->
    <! Simplified configuration: (1) automatic registration DefaultAnnotationHandlerMapping, AnnotationMethodHandlerAdapter (2) provide a series of data binding, number and date format @numberFormat, @datatimeFormat, XML, JSON default read and write support.
    <mvc:annotation-driven/>

    <! -- 2: Static resource default servlet configuration 1: add static resource processing :js, GIF, PNG 2: allow "/" for overall mapping -->
    <mvc:default-servlet-handler/>

    <! --3: Configure JSP to display ViewResolver -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>

    </bean>

    <! --4: Scan for web-related beans -->
    <context:component-scan base-package="org.seckill.web"/>
</beans>
Copy the code

Implementation of the Service layer

Service layer is mainly about the design and implementation of some business interfaces

Hosted Service implementation classes based on Spring

The corresponding XML is as follows:


      
<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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    <! -- Scan service package for all types of annotations -->
    <context:component-scan base-package="org.seckill.service"/>

    <! -- Configure transaction manager -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <! Insert into database pool -->
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <! By default, annotations are used to manage transaction behavior -->
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
Copy the code

conclusion

The whole SSM project implementation process is basically like this, I hope friends can get harvest!

Source: github.com/Ming-P/seck…

Thanks: Yijun Zhang in the moOCs free Java implementation of high concurrency kill API course, you must have a look, although the course is simple, but feel the author’s idea to implement this project is more important, will be very productive.

Link: www.imooc.com/u/2145618/c…