SSM development novice guide (with educational administration system project)
preface
When learning the SSM framework, friends who learn Java often encounter many problems. Whether they create new projects themselves or download other people's projects and use them, they will encounter some problems that have nowhere to start. So I wrote this blog, hoping to help more people, and I also provided some tools and files to download, trying to solve all problems in one stop.
1, Required development tools
1.IDEA
2.jdk1.8
3.maven
4.mysql
5.navicat
6.tomcat
You can find it in the github warehouse provided by me at: apache-tomcat-9.0.37-windows-x64
2, Create web project
Open IDEA and create a new project (new project)
Select Maven - > select Java JDK - > check create from Archetype - > select Maven archetype webapp, as shown in the following figure:
Fill in location (must be an empty folder), GroupId (domain name, company name, project name), and fabricated (module name), as shown in the following figure:
Set maven home directory
Set User setting file (alicloud image can be configured)
Set the Local Repository (used to store the downloaded jar packages. You can define your own path)
The first two files can be found in the github repository provided by me.
Address 1: apache-maven-3.6.3-bin
Address 2: settings.xml
As shown in the figure below:
Create two new directories under the main directory: java and resources. Be careful not to name them yourself, but click the option, as shown in the figure:
Create a new package com. In the java directory System and other subordinate packages, as shown in the figure below:
Edit POM XML to pull dependencies. Insert the tag of dependent packages in the dependencies tag, and maven will pull these dependent packages. maven pull dependency is to find the jar package from the local library (). If it is not available locally, it will be downloaded from the Internet. In the domestic network environment, the download probably fails, so it should be in settings Configure alicloud image in XML. It is recommended to use the settings provided by me XML, alicloud image has been configured.
Dependencies used in SSM development:
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!--spring Core package--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.1.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>3.1.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>3.1.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>3.1.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>3.1.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>3.1.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>3.1.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>3.1.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>3.1.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>3.1.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>3.1.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>3.1.0.RELEASE</version> </dependency> <!-- database --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.31</version> </dependency> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <!--mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.2</version> </dependency> <!-- General package--> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <!-- other --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.47</version> </dependency> </dependencies>
Create a new jsp directory under the WEB-INF directory for unified management of jsp files, and add login jsp move in. As shown in the figure below:
Edit the configuration files in the resources directory: mybatis configuration file, spring configuration file, log4j configuration file, mysql configuration file, web The XML configuration file is shown in the following figure:
Configure mybatis cfg. xml
<?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"> <!-- MyBatis Master profile --> <configuration> <!-- by JAVA PO Class alias --> <typeAliases> <!-- Alias mode 2, automatic scanning, will JAVA The class name of the class is used as the class alias of the class --> <package name="com.system.po" /> </typeAliases> <!--<mappers>--> <!--Due to the use of spring And mybati Integration package, mapper Scanning, no configuration is required here--> <!--</mappers>--> </configuration>
Configure ApplicationContext Dao 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:aop="http://www.springframework.org/schema/aop" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--Load database configuration file--> <context:property-placeholder location="classpath:mysql.properties" /> <!--Configure data sources c3p0l Connection pool--> <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}" /> </bean> <!--to configure SqlSessionFactory--> <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!--load mybatis configuration file--> <property name="configLocation" value="classpath:mybatis/mybatis.cfg.xml"/> <!--data source--> <property name="dataSource" ref="dataSource"/> </bean> <!--Mapper Batch scan, from Mapper Package scanning interface, automatically create proxy objects, and Spring Automatic registration in container use Mybatis And Spring This of the integration package Mapper After the scanner, Mybatis The scanner in the configuration file can be cancelled The specifications followed remain unchanged Automatically scanned Mapper of bean of id by Mapper Class name (initial lowercase) --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!--If it is necessary to scan multiple messages mapper,Each package is separated by a comma--> <property name="basePackage" value="com.system.mapper" /> <property name="sqlSessionFactoryBeanName" value="sessionFactory" /> </bean> </beans>
Configure ApplicationContext service 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:aop="http://www.springframework.org/schema/aop" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--Component scanning. If you want the class to be scanned by the component, scan to and Spring If registered in the container You must annotate the class name @Repository,@Service,@Controller,@Component (The four annotation functions are the same, and the names are different just to distinguish different functions) @Component Is a common component --> <context:component-scan base-package="com.system.service.impl"/> <!--<bean id="studentService" class="com.system.service.impl.StudentServiceImpl"></bean>--> <!--<bean id="teacherServiceImpl" class="com.system.service.impl.TeacherServiceImpl"></bean>--> <!--<bean id="courseServiceImpl" class="com.system.service.impl.CourseServiceImpl"></bean>--> <!--<bean id="SelectedCourseServiceImpl" class="com.system.service.impl.SelectedCourseServiceImpl"></bean>--> </beans>
Configure spring 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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--Static resource resolution includes: js,css,img,..--> <!--<mvc:resources mapping="/js/" location="/js/**"/>--> <!--<mvc:resources mapping="/css/" location="/css/**"/>--> <!--<mvc:resources mapping="/fonts/" location="/fonts/**"/>--> <!--<mvc:resources mapping="/images/" location="/images/**"/>--> <!--Load static resources--> <mvc:default-servlet-handler/> <!--<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<Enable annotation method: Configure>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>--> <!--use annotation-driven Annotation driven can replace the configuration of annotation mapper and annotation adapter--> <!--conversion-service Inject custom parameter binding component into the processor adapter.--> <!--validator Inject a verifier into the processor adapter--> <mvc:annotation-driven conversion-service="conversionService"> </mvc:annotation-driven> <!--Custom parameter binding component--> <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <!-- converter --> <property name="converters"> <set> <!-- Date type conversion --> <bean class="com.system.controller.converter.CustomDateConverter"/> </set> </property> </bean> <!--Component scan, can scan controller,Service,... Register and add to spring In container Scan here controller,appoint controller My bag --> <context:component-scan base-package="com.system.controller"/> <!--Global error message processor Just realize HandlerExceptionResolver Interface is the global exception handler --> <bean class="com.system.exception.CustomExceptionResolver"/> <!--view resolver --> <!-- Configuration resolution required jsp View parser for jsp Parsing, used by default jstl Label resolution --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!--Prefix and suffix of splice view address--> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
Configure log4j properties
### direct log messages to stdout ### log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.err log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n ### direct messages to file mylog.log ### log4j.appender.file=org.apache.log4j.FileAppender log4j.appender.file.File=c:\mylog.log log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n ### set log levels - for more verbose logging change 'info' to 'debug' ### log4j.rootLogger=info, stdout
Configure mysql peoperties
jdbc.driver = com.mysql.jdbc.Driver jdbc.url = jdbc:mysql://localhost:3306/examination_system jdbc.username = root jdbc.password = 111111
Configure web xml
<?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_3_1.xsd" version="3.1"> <!--load spring container--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/applicationContext-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--springMVC Front end controller loading--> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--contextConfigLocation to configure SpringMVC Loaded configuration file (configuration processor, mapper, etc.) If not configured contextConfigLocation,By default:/WEB-INF/servlet name-servlet.xml(springmvc-servlet.xml) --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!--spring Garbled filter provided for us--> <filter> <filter-name>CharacterEncodingFilter</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> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- name Want and applicationContext.xml Corresponding in bean of id agreement --> <!--Shiro Interceptor shiro entrance--> <filter> <filter-name>shiroFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> <init-param> <param-name>targetFilterLifecycle</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>shiroFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> </web-app>
github address:
https://github.com/JaceyRx/Examination_System