"Java Tutorial" SSM: Environment Building

Source: Internet
Author: User
Tags aliases connection pooling

SSM Environment Setup

    1. Directory creation

    2. Pom.xml

    3. SSM per layer Configuration

First, Directory

1.1 The package under the Src/main/java directory (the following packages are placed under the project package, such as: COM.IMOOC. Project name)

    • Entity: Store Entities Class

    • Web: Storing a controller, equivalent to an action in struts

    • Service: Inside the business logic layer, create a packeage. Impl (EG:COM.IMOOC. Project name. Service.impl)

    • DAO: Data-related operations such as dealing with databases, file read and write operations, Redis cache operations, and so on. Not built-in Impl package, because the MyBatis, in the configuration file implementation interface method, is in Resources.mapper,

    • DTOs: Mainly compensate for the deficiency of the entity, encapsulating a layer

    • Enums: Storing enumerations

    • Interceptor: Interceptor

    • Util: Universal Tool Storage

1.2 Src/main/resources

    • Mapper: Storing SQL for each method in DAO without writing the implementation class of DAO

    • Spring: Store the Spring configuration file

1.3 Src/test Storage Test

    • Java

    • Resources

Second, pom.xml configuration

jar package can be queried: http://mvnrepository.com/

2.1 Test Jar Package

JUnit: Specify Range <scope>test</scope>

    <dependency>      <groupId>junit</groupId>      <artifactId>junit</artifactId>      <version>4.12</version>      <scope>test</scope>    </dependency>

2.2 Log Jar Package

Logback

    <dependency>      <groupId>ch.qos.logback</groupId>      <artifactid>logback-classic </artifactId>      <version>1.2.3</version>    </dependency>

2.3 Spring related JAR packages

2.3.1 Spring-core

Spring Framework's basic core tool class. Other components of spring use the classes in this package, and the basic core of other components

2.3.2 Spring-beans

All applications are used, including access to configuration files, creation and management of beans, and all classes related to IOS/DI operations. A Java-like reflection mechanism

If the application requires only basic Ioc/di support, the introduction of Spring-core.jar and Spring-beans.jar files is possible.

2.3.3 Spring-context

Provides a number of extensions to spring to find all the classes required to use the Spring ApplicationContext feature, instrumentation components, and related classes for validating validation

2.3.4 Spring-jdbc

Spring encapsulates all classes of JDBC data access

2.3.5 SPRING-TX

Provides consistent declarative and programmatic transaction management for JDBC,HIBERNATE,JDO,JPA, etc.

2.3.6 Spring-web

Contains the core classes required to use the spring framework when developing web apps, including automatic onboarding of Webapplicationcontext

2.3.7 SPRING-WEBMVC

Contains all classes related to the SPRINGMVC framework, where the more important class is Dispatcher-servlet

2.3.8 Spring-test

Simple encapsulation of test frameworks such as JUnit

Specify range:<scope>test</scope>

2.4 Introduction of database-related jar packages

2.4.1 MyBatis

Class Library with MyBatis base

2.4.2 mybatis-spring

Linked to the spring framework

2.4.3 Mysql-connector-java

Support for JDBC interaction with MySQL

2.4.4 C3P0

Connection pool

2.5 Other JAR Packages

2.5.1 Javax.servlet-api

that provides the servlet service

2.5.2 Jackson-databind

Used to parse JSON,

in Springmvc, when the controller interacts with the front-end, JSON parsing is used in many cases
.

2.5.6 commons-collections

Map tool class, extension to Java Collection
Spring-core.jar requires Commons-collections.jar to provide support for the underlying class

2.6 Complete Pom.xml need to modify the section

Create a Pom.xml file for a MAVEN project
① fill in the jar package dependencies that you just described

<dependencies> <dependency> <groupId>junit</groupId> <artifactid>junit</arti  factid> <version>4.12</version> <scope>test</scope> </dependency> <!-- Https://mvnrepository.com/artifact/ch.qos.logback/logback-classic-<dependency> <groupid>ch.qos .logback</groupid> <artifactId>logback-classic</artifactId> &LT;VERSION&GT;1.2.3&LT;/VERSION&G    T </dependency> <!--spring-to-<!--1) contains the basic core tool classes of the Spring framework.      The other components of Spring use the classes in this package, which is the basic core of other components--<dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependen Cy> <!--2) This jar file is used by all applications, including access to configuration files, creation and management of beans, and inversion of control/dependency injection (IOC/DI) All classes related to it. If the application requires only basic Ioc/di support, introduce Spring-core.jar and spring-beThe Ans.jar file is available. -<dependency> <groupId>org.springframework</groupId> &LT;ARTIFACTID&GT;SPRING-BEANS&L T;/artifactid> <version>${spring.version}</version> </dependency> <!--3) This jar file is Sprin G-Core provides a large number of extensions. You can find all the classes you need to use the Spring ApplicationContext feature, Jdni all the classes you need, instrumentation components, and related classes for validating validation. -<dependency> <groupId>org.springframework</groupId> <artifactid>spring-context </artifactId> <version>${spring.version}</version> </dependency> <!--4) This jar file contains the Spring encapsulates all classes of JDBC data access. -<dependency> <groupId>org.springframework</groupId> <artifactid>spring-jdbc&lt ;/artifactid> <version>${spring.version}</version> </dependency> <!--5) for JDBC, Hibernat Consistent declarative and programmatic transaction management provided by E, JDO, JPA, etc. -<dependency> <groupId>org.springframework</groupId>      <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </depende Ncy> <!--6) Spring Web contains the core classes required to use the spring framework when developing web apps, including classes that automatically load Webapplicationcontext features, struts and JSF integration classes, File upload support classes, Filter class and a large number of tool helper classes. -<dependency> <groupId>org.springframework</groupId> <artifactId>spring-web< /artifactid> <version>${spring.version}</version> </dependency> <!--7) contains SPRINGMVC framework-related All classes. -<dependency> <groupId>org.springframework</groupId> <artifactid>spring-webmvc& Lt;/artifactid> <version>${spring.version}</version> </dependency> <!--8) Spring test pair Simple package for test frameworks like JUnit-<dependency> <groupId>org.springframework</groupId> &LT;ARTIFACTID&G T;spring-test</artifactid> <version>${spring.version}</version> <scope>test</scope&gt    ; </dependency> <!--Servlet Web--<dependency> <groupId>javax.servlet</groupId>    <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <!--JSON parsing-<dependency> <groupId>com.fasterxml.jackson.core</groupId> <art  Ifactid>jackson-databind</artifactid> <version>2.8.7</version> </dependency> <!-- The map tool class extends Spring-core.jar to standard Java collection Commons-collections.jar-<dependency> <groupid>com Mons-collections</groupid> <artifactId>commons-collections</artifactId> <version>3.2< /version> </dependency> <!--dao:mybatis--<dependency> &LT;GROUPID&GT;ORG.MYBATIS&L t;/groupid> <artifactId>mybatis</artifactId> <version>3.4.2</version> </depende Ncy> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <ve Rsion>1.3.1</version> </dependency> <!--database--<dependency> &LT;GROUPID&GT;MYSQ L</groupid> <artifactId>mysql-connector-java</artifactId> <version>5.1.37</version&gt    ; </dependency> <dependency> <groupId>c3p0</groupId> &LT;ARTIFACTID&GT;C3P0&LT;/ARTIFAC Tid> <version>0.9.1.2</version> </dependency> <!--image Processing-<!--https://mvnre Pository.com/artifact/net.coobird/thumbnailator-<dependency> <groupid>net.coobird</groupid&      Gt <artifactId>thumbnailator</artifactId> <version>0.4.8</version> </dependency> &lt ;! --Https://mvnrepository.com/artifact/com.github.penggle/kaptcha <dependency> <groupid>com.gith Ub.penggle</groupid> <artifactId>kaptcha</artifactId> <version>2.3.2</version> </depe ndency> <dependency> <groupId>commons-fileupload</groupId> <artifactid>commons-fil Eupload</artifactid> <version>1.3.2</version> </dependency> <!--redis client: Jedis--&G    T      <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> </dependencies>

② Filling Spring.version Declaration

  <properties>    <spring.version>4.3.7.RELEASE</spring.version>  </properties>
before you put it in <dependencies>

Iii. SSM-by-layer configuration

    1. Jdbc.properties

    2. Mybatis-config.xml

    3. Spring-dao.xml,spring-service.xml,spring-web.xml

    4. Xml

3.1 jdbc.properties

The database configuration file is created in the Src/main/resources directory with the following file contents:

jdbc.driver=com.mysql.jdbc.driverjdbc.url=jdbc:mysql://localhost:3306/{fill in the project name}?useunicode=true& Characterencoding=utf8jdbc.username={databases's username}jdbc.password={databases's password}

Temporarily write your account and password in clear text.

JDBC will use the Mysql.jdbc.Driver drive to access the Jdbc.url's address database, which can be connected to MySQL with username and password.

3.2 Mybatis-config.xml

The MyBatis configuration file is created in the Src/main/resources directory with the following file contents:

<?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>    <!--Configure global Properties--    <settings>        <!-- Get database self-increment primary key value using JDBC Getgeneratedkeys-        <setting name= "Usegeneratedkeys" value= "true"/>        <!-- Replace column aliases with column labels default: true--        <setting name= "Usecolumnlabel" value= "true"/>        <!--turn on hump naming conversions: Table{create _time}-Entity{createtime}-        <setting name= "Mapunderscoretocamelcase" value= "true"/>    </ Settings></configuration>

3.3 Spring Related configuration

Store in src/main/resources/spring directory

3.3.1 Creating Spring-dao.xml

    1. Read the JDBD configuration file

    2. Database Connection Pool Configuration

    3. Create a database connection pool object [notice the package name modification inside]

    4. Configure which package needs to be scanned at the DAO layer, and the objects that automatically create the connection pool are passed in [notice the package name modification inside]

Note that the name of the package is filled in: I wrote the position in Chinese, the IDE red
<?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 "> <!--Configuration Integration MyBatis Process--<!--1. Configuring database-related Parameters properties: ${url}-<context:p Roperty-placeholder location= "Classpath:jdbc.properties"/> <!--2. Database connection pool--<bean id= "DataSource" class = "Com.mchange.v2.c3p0.ComboPooledDataSource" > <!--Configure connection pooling Properties--<property name= "Driverclass" Valu E= "${jdbc.driver}"/> <property name= "Jdbcurl" value= "${jdbc.url}"/> <property name= "user" Valu E= "${jdbc.username}"/> <property name="Password" value= "${jdbc.password}"/> <!--c3p0 Connection pool private Properties--<property name= ' maxpoolsize ' value= "/> <property name=" minpoolsize "value="/> <!--closed after connection not automatically commit---<propert Y name= "Autocommitonclose" value= "false"/> <!--get connection time-out time-<property name= "Checkouttimeout" V Alue= "10000"/> <!--when getting connection failure retries--<property name= "acquireretryattempts" value= "2"/> &L T;/bean> <!--3. Configuring Sqlsessionfactory Objects--<bean id= "sqlsessionfactory" class= "org.mybatis.spring.SqlSess Ionfactorybean "> <!--Inject database connection pool-<property name=" DataSource "ref=" DataSource "/> &lt ;! --Configure Mybaties Global profile: Mybatis-config.xml---<property name= "configlocation" value= "classpath:mybatis-config.xm        L "/> <!--scan the entity package using aliases--<property name=" Typealiasespackage "value=" {entity class package where} "/> <!--Scan SQL configuration file: MApper required XML file--<property name= "mapperlocations" value= "Classpath:mapper/*.xml"/> </bean> & lt;! --4. Configure the scan DAO interface package, dynamically implement the DAO interface, inject into the spring container--<bean class= "Org.mybatis.spring.mapper.MapperScannerConfigurer" > <!--inject sqlsessionfactory--<property name= "Sqlsessionfactorybeanname" value= "Sqlsessionfacto Ry "/> <!--given the need to scan the DAO Interface Pack--and <property name=" Basepackage "value=" {Find a DAO package to fill} "/> </be An></beans>

3.3.2 Spring-service.xml

    1. Scan all types of annotations under Service package [note fill in package name]

    2. Configuration transaction manager: Multiple DAO interoperability to ensure atomicity of operations

    3. Configuring annotation-based declarative transactions

<?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: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 All types of annotations under Service Pack--<context: Component-scan base-package= "{The directory where the service package resides, such as Com.cat.o2o.service}"/> <!--configuration transaction Manager--<bean id= "Transac  Tionmanager "class=" Org.springframework.jdbc.datasource.DataSourceTransactionManager "> <!--inject database connection pool --<property name= "DataSource" ref= "DAtasource "/> </bean> <!--configuring annotation-based declarative transactions---<tx:annotation-driven transaction-manager=" transact Ionmanager "/></beans>

3.3.3 Spring-web.xml

    • Open Spring annotation mode, automatically implement controller, do not need to do bean configuration

    • Static resource default servlet configuration, need to create resource directory, picture and other static resources storage location under WebApp

    • View parser Configuration

    • Scan the package where the controller is located [need to fill in the package where your controller resides]

The contents of the file are as follows:

<?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-3.2.xsd > <!--configuration Springmvc-<!--1. Turn on SPRI NGMVC annotation Mode--<mvc:annotation-driven/> <!--2. Static Resources default servlet configuration (1) joins the processing of static resources: Js,gif,png (2) allows for overall mapping using "/" Tell SPRINGMVC the core class Dispatcher-servlet do not intercept this, by default Servlet-handler processing--<mvc:resources mapping= "/resources/**" location= "/resources/"/> <mvc:default-servlet-handleR/> <!--3. Defining the View resolver--<bean id= "Viewresolver" class= "org.springframework.web.servlet.view.Int Ernalresourceviewresolver "> <property name=" prefix "value="/web-inf/html/"></property> &LT;PR    Operty name= "suffix" value= ". html" ></property> </bean> <!--4. Scan the Web (the package where the controller is placed) related beans-- <context:component-scan base-package= "Where to modify"/></beans>

3.4 Configuring Web. Xml

    • Configure the index file to temporarily default

    • Configure SPRINGMVC servlet, note fill in initial parameters (specify directory)

    • Configure servlet-mapping to match all requests by default (Url-pattern fill in/out)

<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 "metadata-complete=" true "> <display-name>archetype Created Web application</display-na  me> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>spring-dispatcher</servlet-name> <servlet-class> Org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param-name> Contextconfiglocation</param-name> <param-value>classpath:spring/spring-*.xml</param-value> & lt;/init-param> </servlet> <servlet-mapping> <servlet-name>spring-dispatcher</ Servlet-name> <!--default to match all requests--<url-pattern>/</Url-pattern> </servlet-mapping></web-app> 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.