Home >Java >javaTutorial >[Java Tutorial] SSM: Environment Setup

[Java Tutorial] SSM: Environment Setup

php是最好的语言
php是最好的语言Original
2018-08-09 17:13:512313browse

SSM environment setup

  1. Directory creation

  2. pom.xml

  3. SSM layer by layer Configuration

1. Directory

1.1 Packages in the src/main/java directory (the following packages should be placed under the project package, such as: com.imooc. project name )

  • entity: stores entity classes

  • web: stores controller, equivalent to action

  • in Struts

    service: Business logic layer-> Create another package .impl internally (eg: com.imooc.Project name.service.impl)

  • dao: Mainly deals with the database , file read and write operations, Redis cache operations and other data-related operations. There is no need to build in the impl package, because using mybatis, the way to implement the interface in the configuration file is in resources.mapper,

  • dto: mainly makes up for the shortcomings of entity and encapsulates one layer

  • enums: Stores enumerations

  • interceptor: Interceptor

  • util: Universal tool Class storage

1.2 src/main/resources

  • mapper: Stores the SQL corresponding to each method in DAO, without having to write the implementation of DAO Class

  • spring: Storage of spring configuration files

1.3 src/test Storage of tests

  • java

  • resources

2. Pom.xml configuration

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

2.1 Test jar package

junit: Specify scope06db57cb000bdd2564c5b32a302b10e2test03b1008234ba0cf6ad3c873aea327e8a

    <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 package

2.3.1 spring-core

The basic core tool class of Spring framework. Other Spring components must use the classes in this package. The basic core of other components

2.3.2 spring-beans

must be used by all applications, including accessing configuration files, creating and Manage beans and all classes related to IOS/DI operations. Java-like reflection mechanism

If the application only needs basic IOC/DI support, just introduce the spring-core.jar and spring-beans.jar files

2.3.3 spring-context

Provides a large number of extensions for Spring. You can find all the classes required to use the Spring ApplicationContext features, instrumentation components and related classes for verification

2.3.4 spring-jdbc

All classes that Spring encapsulates 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 when using the Spring framework when developing web applications, including automatically loading WebApplicationContext

2.3.7 spring -webmvc

Contains all classes related to the SpringMVC framework, the more important class is dispatcher-servlet

2.3.8 spring-test

is simple for test frameworks such as JUNIT Encapsulation

Specify the scope: 06db57cb000bdd2564c5b32a302b10e2test03b1008234ba0cf6ad3c873aea327e8a

2.4 Introduce the jar package related to the database

2.4.1 mybatis

includes Mybatis basic class library

2.4.2 mybatis-spring

Connected with Spring framework

2.4.3 mysql-connector-java

Support JDBC Interaction with MySQL

2.4.4 c3p0

Connection pool

2.5 Other jar packages

2.5.1 javax.servlet-api

Providing servlet services

2.5.2 jackson-databind

is used to parse JSON,

It is used in many cases when the Controller in SpringMVC interacts with the front end. JSON parsing

2.5.6 commons-collections

Map tool class, extension to JAVA collection
spring-core.jar requires commons-collections.jar to provide basic class support

2.6 The complete parts of pom.xml that need to be modified

Create the pom.xml file of the maven project
① Fill in the jar package dependencies just explained


    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>1.2.3</version>
    </dependency>
    
    
    
      org.springframework
      spring-core
      ${spring.version}
    
    
    
      org.springframework
      spring-beans
      ${spring.version}
    
    
    
      org.springframework
      spring-context
      ${spring.version}
    
    
    
      org.springframework
      spring-jdbc
      ${spring.version}
    
    
    
      org.springframework
      spring-tx
      ${spring.version}
    
    
    
      org.springframework
      spring-web
      ${spring.version}
    
    
    
      org.springframework
      spring-webmvc
      ${spring.version}
    
    
    
      org.springframework
      spring-test
      ${spring.version}
      test
    
    
    
      javax.servlet
      javax.servlet-api
      3.1.0
    
    
    
      com.fasterxml.jackson.core
      jackson-databind
      2.8.7
    
    
    
      commons-collections
      commons-collections
      3.2
    
    
    
      org.mybatis
      mybatis
      3.4.2
    
    
      org.mybatis
      mybatis-spring
      1.3.1
    
    
    
      mysql
      mysql-connector-java
      5.1.37
    
    
      c3p0
      c3p0
      0.9.1.2
    
    
    
    
      net.coobird
      thumbnailator
      0.4.8
    
    
    
      com.github.penggle
      kaptcha
      2.3.2
    
    
      commons-fileupload
      commons-fileupload
      1.3.2
    
    
    
      redis.clients
      jedis
      2.9.0
    
  

② Fill in spring.version statement

  <properties>
    <spring.version>4.3.7.RELEASE</spring.version>
  </properties>
is placed before fce2022be5e87c17c94245fd7ccbf1d9

3. SSM layer-by-layer configuration

  1. jdbc.properties

  2. mybatis-config.xml

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

  4. web.xml

3.1 jdbc.properties

The database configuration file is created in the src/main/resources directory, and the file content is as follows:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/{填写项目名}?useUnicode=true&characterEncoding=utf8
jdbc.username={databases的username}
jdbc.password={databases的password}

Temporarily write the account number and password in plain text.

jdbc will use the mysql.jdbc.Driver driver to access the database at the address of jdbc.url. You can connect to MySQL using username and password.

3.2 mybatis-config.xml

The mybatis configuration file is created in the src/main/resources directory. The file content is as follows:

<?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>
        <!-- 使用jdbc的getGeneratedKeys获取数据库自增主键值 -->
        <setting name="useGeneratedKeys" value="true" />

        <!-- 使用列标签替换列别名 默认:true -->
        <setting name="useColumnLabel" value="true" />

        <!-- 开启驼峰命名转换:Table{create_time} -> Entity{createTime} -->
        <setting name="mapUnderscoreToCamelCase" value="true" />
    </settings>
</configuration>

3.3 Spring related configuration

Stored in the src/main/resources/spring directory

3.3.1 Create spring-dao.xml

  1. Read the jdbd configuration file

  2. Database connection pool configuration

  3. Create the object of the database connection pool [note the modification of the package name inside]

  4. Configuration requirements Which package to scan the DAO layer and pass in the automatically created connection pool object [note the modification of the package name]

注意包名的填写:我用中文写了的位置,IDE会标红
<?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">
    <!-- 配置整合mybatis过程 -->
    <!-- 1.配置数据库相关参数properties的属性:${url} -->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!-- 2.数据库连接池 -->
    <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}" />
        <!-- c3p0连接池的私有属性 -->
        <property name="maxPoolSize" value="30" />
        <property name="minPoolSize" value="10" />
        <!-- 关闭连接后不自动commit -->
        <property name="autoCommitOnClose" value="false" />
        <!-- 获取连接超时时间 -->
        <property name="checkoutTimeout" value="10000" />
        <!-- 当获取连接失败重试次数 -->
        <property name="acquireRetryAttempts" value="2" />
    </bean>

    <!-- 3.配置SqlSessionFactory对象 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 注入数据库连接池 -->
        <property name="dataSource" ref="dataSource" />
        <!-- 配置MyBaties全局配置文件:mybatis-config.xml -->
        <property name="configLocation" value="classpath:mybatis-config.xml" />
        <!-- 扫描entity包 使用别名 -->
        <property name="typeAliasesPackage" value="{实体类包所在的位置}" />
        <!-- 扫描sql配置文件:mapper需要的xml文件 -->
        <property name="mapperLocations" value="classpath:mapper/*.xml" />
    </bean>

    <!-- 4.配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 注入sqlSessionFactory -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
        <!-- 给出需要扫描Dao接口包 -->
        <property name="basePackage" value="{找到需要填写的dao包}" />
    </bean>
</beans>

3.3.2 spring-service.xml

  1. 扫描Service包下所有使用注释的类型 [注意填写包名]

  2. 配置事务管理器:多个DAO的协同操作,保证操作的原子性

  3. 配置基于注解的声明式事务

<?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">
    <!-- 扫描service包下所有使用注解的类型 -->
    <context:component-scan base-package="{service包所在的目录,如com.cat.o2o.service}" />

    <!-- 配置事务管理器 -->
    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 注入数据库连接池 -->
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- 配置基于注解的声明式事务 -->
    <tx:annotation-driven transaction-manager="transactionManager" />
</beans>

3.3.3 spring-web.xml

  • 开启Spring注解模式,自动实现Controller,不用做bean的配置

  • 静态资源默认servlet配置,需要在webapp下创建resource目录,图片等静态资源存放位置

  • 视图解析器配置

  • 扫描controller所在的包[需要填写你的controller所在的包]

文件内容如下:

<?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">
    <!-- 配置SpringMVC -->
    <!-- 1.开启SpringMVC注解模式 -->
    <mvc:annotation-driven />

    <!-- 2.静态资源默认servlet配置 (1)加入对静态资源的处理:js,gif,png (2)允许使用"/"做整体映射 告知SpringMVC的核心类dispatcher-servlet不要对此拦截,交由默认的servlet-handler处理-->
    <mvc:resources mapping="/resources/**" location="/resources/" />
    <mvc:default-servlet-handler />

    <!-- 3.定义视图解析器 -->
    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/html/"></property>
        <property name="suffix" value=".html"></property>
    </bean>

    <!-- 4.扫描web(放置controller的包)相关的bean -->
    <context:component-scan base-package="需要修改的地方" />

</beans>

3.4 配置web.xml

  • 配置index文件,暂时默认即可

  • 配置SpringMVC的Servlet,注意填写初始参数(指定目录)

  • 配置Servlet-mapping,默认匹配所有的请求(url-pattern填写/即可)

<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-name>
  <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>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring-dispatcher</servlet-name>
    <!-- 默认匹配所有的请求 -->
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

相关推荐:

JavaWeb环境搭建

PHP环境搭建要点

The above is the detailed content of [Java Tutorial] SSM: Environment Setup. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn