Let’s briefly talk about maven building ssm project (using database oracle is a little more troublesome than mysql, so let’s talk about it here)
When creating maven web projects, two folders: main/java and main/test are often missing.
Solution:
①: Right-click on the project and select properties, then click java build path, under Librarys, edit JRE System Library, and select workspace default jre. (This is recommended)
②: Create a directory manually. The switch view uses the Navigator view and directly creates a Java directory in the src/main directory.
Project directory structure:
Important configuration files:
Object model configuration file: pom.xml
Spring's configuration file: applicationContext.xml
spring MVC configuration file: springmvc.xml
Database configuration file: jdbc.properties
Log configuration file: log4j.properties
mybatis configuration file: mybatis-config.xml
Network program configuration file: web.xml
First configure pom.xml
pom.xml mainly describes the project's maven coordinates, dependencies, and automatically introduces jar packages.
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.krry</groupId> <artifactId>maven_SSM</artifactId> <version>0.0.1-SNAPSHOT</version> <name>maven_SSM</name> <url>http://maven.apache.org</url> <dependencies> <!--Introduce junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!--Introduce servlet --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>3.0-alpha-1</version> <scope>provided</scope> </dependency> <!--Introducing the package of jstl--> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> <scope>provided</scope> </dependency> <!--Introducing the compilation dependency of jsp--> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> <scope>provided</scope> </dependency> <!--Introducing the compilation dependency of jsp--> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!--spring springmvc mybatis --> <!-- spring and springmvc related construction jar --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.2.1.RELEASE</version> </dependency> <!-- springmvc-related--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.2.1.RELEASE</version> </dependency> <!--springmvc requires json conversion package jackson --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.5.4</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.5.4</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.5.4</version> </dependency> <!--JSR303 background verification hibernate validator --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>5.1.1.Final</version> </dependency> <!--upload file-related jar packages--> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.3.2</version> </dependency> <!--codeC related to encryption algorithms --> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.9</version> </dependency> <!--orm or jdbc component requires mybatis --> <!--oracle database driver --> <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc6</artifactId> <version>12.1.0.2.0</version> </dependency> <!--mysql database driver (not used here, the above oracle driver is used) --> <!-- <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.0.8</version> <scope>runtime</scope> </dependency> --> <!-- proxool connection pool--> <dependency> <groupId>com.cloudhopper.proxool</groupId> <artifactId>proxool</artifactId> <version>0.9.1</version> </dependency> <dependency> <groupId>com.cloudhopper.proxool</groupId> <artifactId>proxool-cglib</artifactId> <version>0.9.1</version> </dependency> <!--Introducing the jar package required by mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.3.1</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.4</version> </dependency> <!-- The jar package required for pagination management is not used here --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>4.2.1</version> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>2.4</version> <configuration> <version>3.0</version> </configuration> </plugin> </plugins> <finalName>maven_SSM</finalName> </build> </project>
Here is a question about the maven project using pom.xml to import oracle driver package:
Due to Oracle authorization issues, Maven does not provide Oracle JDBC driver. In order to apply Oracle JDBC driver in Maven projects, it must be manually added to the local repository.
If the Oracle database is already installed on the computer, there is a database driver under the installation path and can be used directly. D:/Oracle/oraclexe/app/oracle/product/10.2.0/server/jdbc/lib
You can also download the Oracle database driver directly from the Oracle official website and use SQL statements to query the version of the database driver: SELECT * FROM v$instance
Then confirm the version download: http://www.oracle.com/technetwork/database/features/jdbc/default-2280470.html
Open the command line interface of Windows, enter the directory of the driver package ojdbc6, and then run:
mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=12.1.0.2.0 -Dpackaging=jar -Dfile=ojdbc6.jar
If "BUILD SUCCESS" is displayed successfully, your maven local repository will be automatically imported.
Then you can add dependency to the maven project, and each coordinate corresponds to the elements of the above command, as follows:
<dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc6</artifactId> <version>12.1.0.2.0</version> </dependency>
Spring's configuration file: 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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-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/util http://www.springframework.org/schema/util/spring-util-3.0.xsd "> <!-- Configuration package scanning --> <context:component-scan base-package="com.krry"></context:component-scan> <!-- Import external resource file--> <!-- <context:property-placeholder location="classpath:jdbc.properties" /> --> <bean p:location="classpath:jdbc.properties" p:fileEncoding="utf-8" /> <!-- proxool connection pool--> <bean id="dataSource"> <!-- driver name, mysql --> <property name="driver" value="${db.driver}"></property> <!-- proxool The URL connection string of this must determine the user name and password --> <property name="driverUrl" value="${db.url}"></property> <!-- Username (proxool is not used, but cannot be left out) --> <property name="user" value="${db.username}"></property> <!-- Password (proxool is not used, but cannot be left out) --> <property name="password" value="${db.password}"></property> <!-- Time interval for proxool to automatically reconnaise each connection state (milliseconds). When reconnaissance is done, recycle it immediately. The timeout destruction is now set to 4 seconds) --> <property name="houseKeepingSleepTime" value="3000"></property><!-- Automatically check whether the connection is broken--> <property name="testBeforeUse" value="true"></property> <!-- If an idle database connection is found, the house keeper will be tested with this statement. This statement is best executed very quickly. If it is not defined, the test process will be ignored--> <property name="houseKeepingTestSql" value="SELECT count(1) from dual"></property> <!-- If housekeeper Detecting that the activity time of a thread is greater than this value. It will kill the thread. So confirm the bandwidth of your server. Then set a suitable value. The default is 5 minutes. Now set 10 seconds --> <property name="maximumActiveTime" value="10000"></property> <!-- The minimum number of idle connections to be kept (now set 20) --> <property name="prototypeCount" value="20"></property> <!-- The maximum number of connections (now set 100) --> <property name="maximumConnectionCount" value="200"></property> <!-- The minimum number of connections (now set 50) --> <property name="minimumConnectionCount" value="50"></property> <!-- If true, then each executed SQL statement will be logged during the execution period (DEBUG LEVEL). You can also register a ConnectionListener (see ProxoolFacade) to get this information. --> <property name="trace" value="false"></property> <property name="verbose" value="true"></property> </bean> <!-- Register Transaction Manager--> <bean id="txMgr" > <property name="dataSource" ref="dataSource"></property> </bean> <!-- Turn on the transaction annotation driver --> <tx:annotation-driven transaction-manager="txMgr" /> <!-- Configure mybatis' sqlSessionFactory --> <bean id="sqlSessionFactory"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:mybatis-config.xml"></property> </bean> <!-- Configure a scanner that can scan the mapper as a whole --> <bean> <!-- If there are multiple reports, separate them with commas --> <property name="basePackage" value="com.krry.mapper"></property> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> </beans> spring MVC configuration file: springmvc.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/util" 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/util http://www.springframework.org/schema/util/spring-util-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd "> <!-- Turn on the annotation mode driver --> <bean /> <!-- Turn on the annotation mode of mvc user also registers a ConversionService subclass FormattingConversionServiceFactoryBean--> <mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <bean> <property name="supportedMediaTypes"> <list> <value>text/plain;charset=UTF-8</value> <value>text/html;charset=UTF-8</value> </list> </property> </bean> <bean/> <bean/> <bean> <bean> <property name="prefixJson" value="false" /> <property name="objectMapper"> <bean> <!-- Handle the date type in the responseBody--> <property name="dateFormat"> <bean> <constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss" /> </bean> </property> <!-- Not displayed when the field is null --> <property name="serializationInclusion"> <value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value> </property> </bean> </property> <property name="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> <value>application/x-www-form-urlencoded;charset=UTF-8</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> <!--Scan package--> <context:component-scan base-package="com.krry.controller"></context:component-scan> <!-- Access to static resource files must be set, because in the springmvc configuration, all requests (.do, addUser, js/image/css) in this project will be parsed by springmvc, and all static resource files must be filtered and released--> <!-- Choose one of the following static resource filtering--> <!--<mvc:default-servlet-handler//> --> <mvc:resources mapping="/resourse/**" location="/resourse/" /> <!-- Interceptor definition--> <mvc:interceptors> <mvc:interceptor> <!-- Personal center also needs to log in to configurations starting with admin to intercept --> <mvc:mapping path="/admin/**"></mvc:mapping> <!-- This is the path that will not enter the interceptor --> <mvc:exclude-mapping path="/resourse/**"/> <!-- The class that the interceptor enters, returns false to indicate that the input will not enter --> <bean /> </mvc:interceptor> </mvc:interceptors> <!-- Configuration file parser--> <bean id="multipartResolver" p:defaultEncoding="utf-8"> <property name="uploadTempDir" value="/temp"></property> <property name="maxUploadSize"> <value>209715200</value><!-- 200MB --> </property> <property name="maxInMemorySize"> <value>4096</value><!-- 4KB size read and write--> </property> </bean> <!-- View rendering jsp/freemaker/velocity--> <bean> <!-- Create a path to the page storage --> <property name="prefix" value="/WEB-INF/pages/"></property> <!-- File suffix--> <property name="suffix" value=".jsp"></property> </beans>
Database configuration file: jdbc.properties
db.driver=oracle.jdbc.OracleDriver db.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl db.username=4m+la23KCA4= db.password=WWijcIyMPaU/=
I've used the encryption algorithm here
Log configuration file: log4j.properties
log4j.rootLogger=DEBUG, CONSOLE, FILElog4j.appender.CONSOLE=org.apache.log4j.ConsoleAppenderlog4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayoutlog4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p [%t] %10l - %m%nlog4j.appender.FILE=org.apache.log4j.RollingFileAppenderlog4j.appender.FILE.File=D:/logs/log4j.loglog4j.appender.FILE.MaxFileSize=1MBlog4j.appender.FILE.Append = truelog4j.appender.FILE.layout=org.apache.log4j.PatternLayoutlog4j.appender.FILE.layout.ConversionPattern=%d{yyyy/MM/dd/HH:mm:ss} %-5p [%t] %10l - %m%n mybatis configuration file: mybatis-config.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"> <configuration> <settings> <!-- Global mapper enable cache--> <setting name="cacheEnabled" value="true" /> <!-- Turn off the instant loading of the associated object for performance--> <setting name="lazyLoadingEnabled" value="true" /> <!-- Set the form of loading of the associated object. Here is a load field on demand (the load field is specified by SQL). All fields of the associated table will not be loaded to improve performance-> <setting name="aggressiveLazyLoading" value="false" /> <!-- For unknown SQL queries, different result sets are allowed to be returned to achieve a common effect--> <setting name="multipleResultSetsEnabled" value="true" /> <!-- Allow to use column labels instead of column names--> <setting name="useColumnLabel" value="true" /> <!-- Allow to use custom primary key values (such as UUID 32-bit encoding generated by the program as key values), and the PK generation strategy of the data table will be overwritten --> <setting name="useGeneratedKeys" value="true" /> <!-- Give nested resultMap support for field-attribute mapping --> <setting name="autoMappingBehavior" value="FULL" /> <!-- Cache SQL for batch update operations to improve performance --> <setting name="defaultExecutorType" value="BATCH" /> <!-- Timeout if the database has not responded for more than 25,000 seconds --> <setting name="defaultStatementTimeout" value="25" /> <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/> </settings> <typeAlias> <!--Alias for custom user object--> <!-- <typeAlias type="com.krry.mybatis.sysmanage.entity.User" alias="user"/> --> <!-- Batch definition alias--> <package name="com.krry.entity" /> </typeAliases> </configuration>
Network program configuration file: web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>maven_SSM</display-name> <welcome-file-list> <welcome-file>index</welcome-file> </welcome-file-list> <!-- Loading Spring IOC container --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- spring context listener--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Introspector cache clear listener --> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <filter> <filter-name>encoding</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>encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Configure DispatcherServlet --> <servlet> <servlet-name>maven_SSM</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- Configure springMVC configuration file --> <!-- If the following options are not configured, the system defaults to load the file named [servlet-name]-servlet.xml under classpath springmvc01-servlet.xml --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>maven_SSM</servlet-name> <url-pattern>/index</url-pattern> </servlet-mapping> <!-- You can match *.do, *.action (understanding) / (key): All requests will be parsed by spring mvc, but static resource files must be filtered and released. It is recommended that you use this method/*: It is not recommended that you use --> <servlet-mapping> <servlet-name>maven_SSM</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
At this point, the basic configuration is complete and the jar package is automatically relied on. Test and write subsequent java code
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.