The first time I wrote a blog, I was judged by AI that I was not an original, intelligent hammer.
Next,
Creating Engineering
Open IDEA, open IDEA for the first time, IDEA home page will appear, click "Create New Project" and start to create SSM project (the second opening of IDEA will automatically load the previous project, new project operation click File - New - Project in the upper left corner)
Select the left Maven option, check Create From archetype, select org.apache.maven.archetypes:maven-archetype-webapp, and click Next
Enter "GroupId" and "ArtifactId" and click Next
Maven home directory, select Bundled (Maven 3), and click Next
Enter "Project name" and "Project location" and click Finish. ok.
Download Dependent jar
1) Enter the required jar package. SSM manages jar with Maven, and Maven is managed through the pom.xml configuration file in the project. So we enter the jar package we use in pom.xml.
After the project is created, IDEA opens pom.xml automatically. In the lower right corner of IDEA editor, a prompt float window pops up and clicks Enable Auto-Import. This function is to download the jar package automatically when modifying pom.xml.
2) In pom.xml, find the label, enter the versions of each framework package in the label, for the unification of the subsequent jar package versions, the code is typed according to the graph or I sent out!!!
<spring.version>4.3.12.RELEASE</spring.version> <hibernate.version>5.1.0.Final</hibernate.version> <jackson.version>2.8.1</jackson.version> <log4j.version>1.2.17</log4j.version> <slf4j.version>1.7.24</slf4j.version>
3) Enter the jar package configuration required by the spring MVC framework under the dependencies tag. At the bottom of IDEA, you can see that maven automatically downloads the spring jar package.
4) Put up my pom.xml. In the sample project, I'm sure I don't need so many jar packages, just copy them.
<?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>ssmpro</groupId> <artifactId>ssmpro</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>ssmpro Maven Webapp</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> <spring.version>4.3.12.RELEASE</spring.version> <hibernate.version>5.1.0.Final</hibernate.version> <jackson.version>2.8.1</jackson.version> <log4j.version>1.2.17</log4j.version> <slf4j.version>1.7.24</slf4j.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!-- spring MVC --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</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-beans</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-orm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <!-- Servlet Spec From the previous 2.5 Upgrade to 3.1.0 Spring 4.0 Need to get servlet 3.0 Support for versions above --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- spring mvc Yes html5 Support--> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.23</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.3.10.RELEASE</version> </dependency> <!--mybaits --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.1</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.3</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.11</version> </dependency> <!-- c3p0 data source --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <!-- mysql Connect --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.39</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.6</version> </dependency> <!--springmvc File upload support--> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <!-- json --> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.7</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.6.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.6.0</version> </dependency> <!--slf4j--> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <!--log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <!-- excel Parsing tools--> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.15</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.15</version> </dependency> <!-- cas client Support for single sign-on--> <dependency> <groupId>org.jasig.cas.client</groupId> <artifactId>cas-client-core</artifactId> <version>3.2.1</version> </dependency> <!-- pdf Written words or Picture Watermarking--> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itext-asian</artifactId> <version>5.2.0</version> </dependency> <dependency> <groupId>com.lowagie</groupId> <artifactId>itext</artifactId> <version>2.1.7</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.28</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.5</version> </dependency> </dependencies> <build> <finalName>ssmpro</finalName> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> </plugins> </pluginManagement> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> </build> </project>
set up a database
Library name: ssmpro, just a table: userInfo, there are three fields in the table: id, name, pwd.
Hand-on code
1) Establish the catalogue structure of the project, as shown below. There are three directories under src: java, resources and webapp. Java directory generally stores files related to Java code; resources directory stores configuration files; webapp stores web pages, css, js, pictures and other resource files.
Under the java directory, the subdirectory "com" is established, and under the com directory, the five directories "controller", "entity", "mapper", "mapping", "service" are established. Conntroller directory is stored in the classes of page interaction, providing the interface for page access; entity stores entity classes; mapper stores classes mapped with mybatis configuration; mapping stores mybatis configuration files; service stores classes interacting with databases
2) Right-click on com - > Mark Directory as - > Source root, set com as the resource root directory, and create java classes at the nodes below com and com. Right-click entity node, select New, click Java Class, enter name, click ok
3) Write code in User.java
package com.entity; import java.io.Serializable; public class User implements Serializable { private static final long serialVersionUID = 1L; /** User ID */ private String id; /** Full name */ private String name; /** Password */ private String pwd; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() {return pwd;} public void setPwd(String pwd) {this.pwd = pwd;} }
Adding mybatis mapping interface
mybatis is a bunch of configuration files, to add, delete and modify the database, check the specific principles, I can't say it well, I can't understand it.
1) Create a new interface named UserMapper.java under the mapper directory. The only difference between a new interface and a new java class is that Kind chooses Interface. In the interface file, add login interface, copy, code is too little, not pasted.
Add mybatis configuration file
1) In IDEA, click File --- Settings... In the pop-up settings window, expand the Editor node on the left, click File and Code Templates, click the plus button, and add a new file template. The template name is mapping, the type is xml, enter the template content (copy the red box below), and finally click ok button.
2) Right-click the mapping folder and click new. In the right floating window, you can see the mapping configuration file template just configured. Click and enter the name UserMapping. Under mapping, a UserMapping.xml file is produced.
3) c. Enter the query statement in UserMapping.xml and copy the following figure
Increase business interfaces and business classes
1) Build impl folder in service folder to store the specific implementation of business interface. Create a new loginService interface file under the service folder and a new loginService Impl class under the impl folder
2) Add login service to login service
3) Inheritance and implementation of loginService in loginService Impl class. Note that adding the "@Service" annotation on the line of the class name automatically registers with the spring container and does not require separate configuration in the spring.xml configuration file.
Adding Logical Processing Layer
1) Create a new loginController.java under the controller folder. Write the code to get page parameters. The following will describe the code components in detail.
In loginController.java, there are three functions to receive page parameters, process data and return data. The controller layer interacts with HTML pages because spring MVC abstracts the request mapping of a class (Bean class) containing @RequestMapping tags. For example: http://www.baidu.com/login, such a link, springmvc automatically maps to classes with @RequestMapping("login")
Declare service for invocation of data queries
The function toLogin(), which provides the toLogin interface, adds the @RequestMapping tag above the function name. It also automatically maps the toLogin interface. In the control layer of the sample project, loginController provides two network interfaces, one above the class name and the other above the function. In a class, you can have more than two levels of interfaces, such as loginController, you can add another delUser to delete users. User invocation method: http://IP Port/Website Name/Interface/Subinterface 1
Login function, provide login interface, call service interface in function, query database
Added Web Page
1) Add a new "jsp" folder in the webapp-WEB-INF directory, and create three new JSP pages under the JSP folder, as shown below. Right-click jsp-new-JSP/JSPX, enter the name, and click ok
2) Double-click to open index.jsp. Add the following code to access the login/toLogin interface
3) Add the following code in home.jsp and error.jsp
4) Add the following code to login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html class="loginHtml"> <head> <meta charset="utf-8"> <title>Login System</title> <script type="text/javascript"> function logining() { var name = document.getElementById("userName").value; var pwd = document.getElementById("password").value; document.getElementById("formLogin").submit(); } </script> </head> <body > <form id="formLogin" method="post" action="${pageContext.request.contextPath }/login/login"> <div> <label for="userName">User name</label> <input id="userName" type="text" placeholder="enter one user name" name="name"/> </div> <div> <label for="password">Password</label> <input id="password" type="password" placeholder="Please input a password" name="pwd"/> </div> <div > <button id="loginButton" type="submit" onclick="logining()">Sign in</button> </div> </form> </body> </html>
SSM configuration file
In the framework of ssm, the key is a bunch of configurations. In the resources directory, there will be the following configuration files. The function of application-dao.xml configuration file is to configure and configure sqlSessionFactory with database connection; "application-service.xml" configuration scan service layer; "application Context.xml" integration of the above two configuration files; "db.properties" to store database connection information; "mybatis.cfg.xml" ” Used to configure mybatis settings; "springmvc.xml" used to configure springmvc framework settings, such as page skip filtering
1) First, create a new db.properties, right-click the resources node, select New Resource Bundle, and create the database connection information configuration file. In the pop-up window, enter the name dB and click the OK button
The database connection information is generally formatted, and the specific content is copied from the following figure. The first line represents the way to connect to the database; the second line is the string connecting to the database, in which the localhost represents the address of the database. If the database is stored in the external network server, the localhost is modified to the IP address of the external network server.
2) Configure application-dao.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> <!-- analysis db.properties because db.properties There are username=root If used in the following data sources ${username}It takes the login name of the current system. //If you want to use username in db.properties, you must add system-properties-mode="FALLBACK" -->. <context:property-placeholder location="classpath:db.properties" system-properties-mode="FALLBACK" /> <!-- Configuring data sources --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${driver}" /> <property name="url" value="${url}" /> <property name="username" value="${username}" /> <property name="password" value="${password}" /> </bean> <!-- To configure sqlSessinoFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!--mybatis Configuration file --> <property name="configLocation" value="classpath:mybatis.cfg.xml" /> <!--scanning XXXmapper.xml Mapping file,Configure the scanning path, which is not configurable, but if not, the following dao and xxxMapper.xml Must be placed under the same package --> <property name="mapperLocations"> <array> <value>classpath:com/mapping/*.xml</value> </array> </property> </bean> <!-- Mapper The package name of the interface, Spring Classes that are automatically searched --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- The following configuration can only point to one package. If more than one package is configured, add it in the middle of the package., --> <!-- <property name="basePackage" value="com.sxt.mapper,com.bjsxt.mapper"/> --> <property name="basePackage" value="com.mapper" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> </beans>
3) Configure application-service.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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.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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- scanning service --> <context:component-scan base-package="com.service.impl"></context:component-scan> <!-- 1,Configuration transaction --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 2 Declare Transaction Aspects --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="insert*" propagation="REQUIRED" /> <tx:method name="save*" propagation="REQUIRED" /> <tx:method name="start*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="load*" propagation="REQUIRED" read-only="true" /> <tx:method name="get*" propagation="REQUIRED" read-only="true" /> <tx:method name="*" propagation="REQUIRED" read-only="true" /> </tx:attributes> </tx:advice> <!-- Conduct aop Configuration --> <aop:config> <!-- Statement entry point --> <aop:pointcut expression="execution(* com.service.impl.*.*(..))" id="pc1" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="pc1" /> </aop:config> </beans>
4) Configure applicatonContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> <import resource="classpath:application-dao.xml" /> <import resource="classpath:application-service.xml" /> </beans>
5) mybatis.cfg.xml configuration
<?xml version="1.0" encoding="UTF-8"?> <!-- Header file --> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <!-- mybatis Core Profile --> <configuration> <!-- Configuration of log output --> <settings> <setting name="logImpl" value="LOG4J" /> </settings> <!-- Alias optimization,Point to the entity class directory --> <typeAliases> <package name="com.entity"/> </typeAliases> </configuration>
6) Spring mvc. XML configuration
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> <!-- scanning controller --> <context:component-scan base-package="com.controller"></context:component-scan> <!-- Configuration of mappers and adapters --> <mvc:annotation-driven></mvc:annotation-driven> <!-- Configuration view parser --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property> </bean> <!-- Filtering static resources --> <mvc:default-servlet-handler /> </beans>
7) Web.xml configuration file, which is the entry point of the program, configures spring's global listener, servlet interception and so on in web.xml
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <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_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Archetype Created Web Application</display-name> <!-- Coding filter start --> <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> <!-- To configure spring Monitor --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- Configuration of Front-end Controller --> <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:springmvc.xml</param-value> </init-param> <!--Used to mark whether or not the project is added here at startup Servlet,0 Or a positive number means that the container loads this at application startup Servlet, When it is a negative number or not specified, //It indicates that the container is loaded only when the servlet is selected. The smaller the positive value, the higher the start priority value - >. <load-on-startup>1</load-on-startup> </servlet> <!--by DispatcherServlet Mapping --> <servlet-mapping> <servlet-name>springmvc</servlet-name> <!-- Intercept all requests,Be careful(/)Instead of(/*) --> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
Running websites
1) In IDEA, click "Add Configuation" to configure Tomcat
2) In the Run/Debug Configuration window, find the "Tomcat Server" option and click on the "Local" option.
3) Click "Configura..." Button, in the pop-up Tomcat Server window, select the Tomcat 8 green version directory, and click OK. When the Tomcat 8 directory configuration is complete, in the drop-down box of the Application server, you will see the Tomcat 8 option you just configured, and select it.
4) Select the Deployment option, click the + button on the right, and click Artifact.
5) In the pop-up Select Artifacts to Deploy window, select "ssmpro:war expolded" and click OK. The purpose here is to put the project into Tomcat for debugging
6) In the Run/Debug Configuration window, click the + button in the upper left corner and select Tomcat Server-Local. On "Update" action and On frame deactivation on the right side both select the Update classes and resources options to modify background and foreground code without restarting Tomcat during debug. Expand the JRE drop-down box and select 1.8 (JDK installed initially). Click OK
7) j. The local debugging environment is configured and started to run. Click on the debug button, there will be debugging information below, debug compilation process takes about 3 minutes, after compilation, it will open with the default browser.
8) To draw up a running chart
Oh, a simple ssm landing page has been completed. Some parts of it may not be applicable to all computers. The general process is like this. I don't know where it won't be. Sample project, uploaded, under review, through the replay. School is over