SSM construction notes
-
Components of ssm: spring+spring mvc+mybatis
-
This tutorial uses Java 8, idea, and maven
-
Start the tutorial:
-
Create a maven web project using idea. Note that you must select your configured Alibaba cloud maven, or the creation will fail
-
If you see build success in the console, it will be regarded as a success. If the creation fails, please refer to the first article. The load is slow during the first creation. Please wait patiently
-
After creation, find POM XML (project dependency management file) writes the following code in dependencies:
<dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.7.0</version> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!-- c3p0 Database connection pool start --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <!-- c3p0 Database connection pool end --> <!-- jsp-servlet support start --> <dependency> <groupId>javax.servlet.jsp.jstl</groupId> <artifactId>jstl-api</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.1</version> </dependency> <dependency> <groupId>org.glassfish.web</groupId> <artifactId>jstl-impl</artifactId> <version>1.2</version> <exclusions> <exclusion> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> </exclusion> <exclusion> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> </exclusion> <exclusion> <groupId>javax.servlet.jsp.jstl</groupId> <artifactId>jstl-api</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <!-- jsp-servlet support end --> <!-- Tag library start --> <dependency> <groupId>org.apache.taglibs</groupId> <artifactId>taglibs-standard-spec</artifactId> <version>1.2.1</version> </dependency> <dependency> <groupId>org.apache.taglibs</groupId> <artifactId>taglibs-standard-impl</artifactId> <version>1.2.1</version> </dependency> <!-- Tag library end --> <!-- apache of commons Series package start --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.1</version> </dependency> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.2.1</version> </dependency> <!-- apache of commons Series package end --> <!-- apache tool kit start --> <dependency> <groupId>org.apache.xmlbeans</groupId> <artifactId>xmlbeans</artifactId> <version>2.6.0</version> </dependency> <!-- apache tool kit end --> <!-- spring start --> <dependency> <groupId>aopalliance</groupId> <artifactId>aopalliance</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.0.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${spring.version}</version> </dependency> <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> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-websocket</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc-portlet</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-messaging</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-instrument-tomcat</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-instrument</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <!-- spring start --> <!-- mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.46</version> </dependency> <!-- mybatis start --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.0.5</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.0.1</version> </dependency> <!-- mybatis end --> <!--Log management start--> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.1.2</version> <scope>compile</scope> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.1.2</version> <scope>compile</scope> </dependency> <!--Log management end--> <!-- https://mvnrepository.com/artifact/javax.mail/mail --> <!--getter setter annotation start--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.10</version> </dependency> <!--getter setter annotation end--> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-asl</artifactId> <version>1.9.13</version> </dependency> <dependency> <groupId>net.sf.ezmorph</groupId> <artifactId>ezmorph</artifactId> <version>1.0.6</version> </dependency> <!--json start--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.15</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.verson}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>${jackson.verson}</version> </dependency> <!--json end-->
-
Fill in the following code in properties
<spring.version>4.0.6.RELEASE</spring.version> <jackson.verson>2.8.7</jackson.verson>
-
Create java (blue) source files and resources (gray with three bars) resource folders under the main folder
-
Create the following packages in the java source folder:
- cn
- kgc
- ssm
- common (store public files in the project, such as unified return classes)
- controller (control layer in the project)
- dao (data access layer in the project)
- service (business logic layer interface in the project)
- impl (implementation class of business logic layer interface in the project)
- util (tool classes in the project, such as paging tool class and date conversion class)
- vo (view Object, view Object, vo class is used when the display content in the current page is inconsistent with the entity class)
- Entity (entity class, mainly responsible for one-to-one correspondence between attributes and database fields in java)
- ssm
- kgc
- cn
-
Create the following folder under the resources folder:
- mappers (the distribution center of sql mapping files, where the xml files should correspond to the interfaces in the dao layer one by one)
- applicationContext.xml (spring configuration file, which mainly configures data sources and session factories in the project)
- applicationContext-mvc.xml (spring mvc configuration file, which mainly configures MVC direction related contents, such as view parser, static resources and message converter)
- db.properties (this is mainly used to store the resource configuration related to the database)
- logbak.xml (here is the information about the configuration log)
-
Open web XML changes its header information because 2.3 does not support the execution of EL expressions
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
-
On the web Write the following code in XML
<!-- Listening context loading --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Context initialization, configuration spring configuration file --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:applicationContext.xml</param-value> </context-param> <!-- to configure spring mvc--> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:applicationContext-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- Set a filter, which is mainly used to solve the problem of Chinese garbled code --> <filter> <filter-name>encodingFilter</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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
-
applicationContext.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 http://www.springframework.org/schema/context/spring-context.xsd"> <!-- Enable annotation scanning--> <context:component-scan base-package="cn.kgc"></context:component-scan> <!-- When reading the configuration file, be sure to add classpath*--> <context:property-placeholder location="classpath*:jdbc.properties"/> <!-- Configure data sources--> <bean id="dataSource" class="com.mchange.v2.c3p0.DriverManagerDataSource"> <property name="driverClass" value="${jdbc.driver}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.user}"/> <property name="password" value="${jdbc.password}"/> </bean> <!-- Configure session factory--> <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sessionFactory"> <property name="dataSource" ref="dataSource"></property> <!-- The same is true here when reading the mapping file. You need to add classpath--> <property name="mapperLocations" value="classpath*:mappers/*Mapper.xml"/> <!-- Set alias for entity class--> <property name="typeAliasesPackage" value="cn.kgc.ssm.entity"/> </bean> <!-- scanning dao layer--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.kgc.ssm.dao"/> </bean> <!-- spring In charge of transaction processing--> <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- Since this exercise does not involve addition, deletion or modification, declarative transactions are not written --> <!-- todo Implementing declarative transactions using annotations --> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
-
applicationContext-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 http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- Enable annotation scanning--> <context:component-scan base-package="cn.kgc"/> <!-- Configure view parser--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> <!-- Dimension control layer --> <mvc:annotation-driven conversion-service="conversionService2"/> <!-- Configure static resources --> <mvc:resources mapping="/statics/**" location="/statics/"/> <!-- Message converter --> <bean class="org.springframework.context.support.ConversionServiceFactoryBean" id="conversionService2"> <property name="converters"> <set> <bean class="cn.kgc.ssm.util.DataConveter"></bean> </set> </property> </bean> <!-- Profile upload--> <bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver" id="multipartResolver"> <property name="defaultEncoding" value="utf-8"/> <property name="maxUploadSize" value="4096000000"/> <property name="maxInMemorySize" value="4096"/> </bean> <!-- Global exception handling --> <!--<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="defaultErrorView" value="error"/> <property name="exceptionAttribute" value="e"/> </bean>--> </beans>
-
-
Introduction to notes in the project
- @Service ---- it is mainly used on the entity class of the service layer to mark that the current layer is service
- @Controller ----------- it is mainly used in the controller layer, marking the current class as the control layer
- @RequestMapping - this annotation can be used on a class or a method. The purpose is that the marked method can be used to process network requests
- @ResponseBody - mainly used in methods to tell the front-end control layer to return data directly to the front-end page without using the view parser
- @RequestParam - used to mark the parameters of the control layer. If the parameter name passed by the front end is inconsistent with the parameter name of the control layer, this annotation can be used to keep it consistent, or this annotation can be used to emphasize when the parameter requirements are mandatory
- @PathVariable - used to get parameters from the path
- @Slf4j - used on the class. The whole class of the current class can use the log record
- @Resource - used to inject data types
- @Param - when there are more than two mybatis dao layer parameters, use this annotation to distinguish the parameters