Senior Maven

回顾
	Springmvc中的拦截器
i.	自定义拦截器
ii.	配置拦截器的拦截范围

SSM整合
a>	搭建mybatis的运行环境
b>	搭建springioc的运行环境
c>	整合mybatis和spring (1.sqlSessionFactory,2.mappersacnnerConfigurer,3.事务 )
d>	搭建springmvc的运行环境
e>	整合ssm

方式一:将所有内容交给springmvc容器管理
方式二:使用监听器加载spring容器

1. Recalls MAVEN basis
dependency management: automated engineering all depend jar package to download
a key building: Use a series of commands maven provide complete test project, compile, package, deploy, operate, function

1.1 Maven command
Clean
the compile
the Test
Package Penalty for
install
Deploy: to publish the project to PW

1.2 JBLJavatoWeb plug
an ordinary jar project: a packaging jar
Web project: a packaging war

2. The transfer dependent
transfer dependency: A project depends on B, B project depends on C, the item A is indirectly dependent on C

2.1 dependent solution of the conflict

2.1.1 rely on the principles of mediation
first statement finite principle: who should configure, who will use
the path recently, the principle of priority: to find the corresponding path dependence from the project, that near the

2.1.2 negative dependence
Here Insert Picture Description
2.1.3 version locked
Here Insert Picture Description
3. polymerization Engineering
3.1 Requirements
Here Insert Picture Description
3.2 Procedure
Here Insert Picture Description
polymerization Engineering:
Split: three-layer structure according to the resolution (web, service, dao)
Polymerization: unified for all sub-modules through the parent project management

Inheritance: sub-module can inherit all the coordinates of the parent project as defined in

4.Ssm integrated Case
Here Insert Picture Description
4.1 create the parent project

Create a common project to maven

<?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>cn.itcast</groupId>
    <artifactId>ssm_parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!--pom:表明配置父工程-->
    <packaging>pom</packaging>

    <!-- 统一管理jar包版本 -->
    <properties>
        <spring.version>5.0.5.RELEASE</spring.version>
        <slf4j.version>1.6.6</slf4j.version>
        <log4j.version>1.2.12</log4j.version>
        <shiro.version>1.2.3</shiro.version>
        <mysql.version>5.1.6</mysql.version>
        <mybatis.version>3.4.5</mybatis.version>
        <spring.security.version>5.0.1.RELEASE</spring.security.version>
    </properties>

    <!-- 锁定jar包版本 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</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-webmvc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-tx</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>${mybatis.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <!-- 项目依赖jar包 -->
    <dependencies>
        <!-- spring -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.6.8</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</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-orm</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-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</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-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <!-- log start -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <!-- log end -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>${mybatis.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.0</version>
        </dependency>
        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>${spring.security.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>${spring.security.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
            <version>${spring.security.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-taglibs</artifactId>
            <version>${spring.security.version}</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.9</version>
        </dependency>
    </dependencies>
    <!-- 添加tomcat7插件 -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
            </plugin>
        </plugins>
    </build>
</project>

4.2 Create a sub-module
selected parent project -> file -> new -> module

Here Insert Picture Description
4.3 Configuration sub-dependencies between modules
grammar rules: grammar rules and configuration dependency is exactly the same
Here Insert Picture Description

At compile time (write the code): import is dependent source submodule
run (test run time): introducing a local repository sub-module dependent jar package

4.4 Analysis of the integration step
Here Insert Picture Description
4.4.1 ssm_dao:
operation of the database layer (entity class, dao Interface)
applicationContext-dao.xml

<!--SqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <property name="typeAliasesPackage" value="cn.itcast.domain"></property>
</bean>

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    <property name="url" value="jdbc:mysql:///springmvcdb"></property>
    <property name="username" value="root"></property>
    <property name="password" value="111111"></property>
</bean>

<!--MapperSacnnerConfigurer-->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="cn.itcast.dao"></property>
</bean>

4.4.2 ssm_service
annotation (to create objects and dependency injection)
applicationContext-service.xml

<!--包扫描-->
<context:component-scan base-package="cn.itcast.service"></context:component-scan>

<!--
    引入dao的配置文件
-->
<import resource="applicationContext-dao.xml"></import>

<!--配置事务-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"></property>
</bean>

<!--事务通知-->
<tx:advice id="txAdvice">
    <tx:attributes>
        <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
        <tx:method name="*"></tx:method>
    </tx:attributes>
</tx:advice>

<!--事务的AOP-->
<aop:config>
    <aop:pointcut id="pt" expression="execution(* cn.itcast.service.impl.*.*(..))"></aop:pointcut>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"></aop:advisor>
</aop:config>

Ssm_web 4.4.3
Web relevant code layer (Controller)

4.4.3.1 springmvc profile

<context:component-scan base-package="cn.itcast.controller"></context:component-scan>

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

<mvc:annotation-driven></mvc:annotation-driven>

4.4.3.2 web.xml configuration file

<!--监听器创建spring容器-->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--加载spring的配置文件-->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <!--
    applicationContext-service.xml中已经引入的dao的配置
    -->
    <param-value>classpath:applicationContext-service.xml</param-value>
</context-param>

<!--处理中文乱码-->
<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>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<!--核心控制器-->
<servlet>
    <servlet-name>mvc</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>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>mvc</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

The two ways to start tomcat
5.1 tomcat local installation
routine use

5.2 Use maven plug-in starts Tomcat
5.2.1 to configure the plug
add pom file in a web project

<!-- 添加tomcat7插件 -->
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
            <!--tomcat的端口-->
            <configuration>
                <port>8888</port>
            </configuration>
        </plugin>
    </plugins>
</build>

Use plug
Here Insert Picture Description
6. maven PW (understand)
Warehouse 6.1 maven of
Here Insert Picture Description
6.2 PW installation and start unloading

Here Insert Picture Description
6.3 Web administration page
request Address: HTTP: // localhost: 8081 / Nexus / # is available for purchase
access to the user / password: ADMIN / admin123
6.4 Warehouse Type
Here Insert Picture Description
6.5 will publish the project to PW
6.5.1 PW configured in the maven of setting.xml user name and password

在setting.xml的servers节点中添加
<server>
		<id>releases</id>
		<username>admin</username>
		<password>admin123</password>
	</server>
	<server>
		<id>snapshots</id>
		<username>admin</username>
		<password>admin123</password>
    	</server>
	<server>
		<id>thirdparty</id>
		<username>admin</username>
		<password>admin123</password>
	</server>

6.5.2 the need to publish the works to the path specified PW
add pom need to publish project files

<distributionManagement>
    <repository>
        <id>releases</id>
        <url>http://localhost:8081/nexus/content/repositories/releases/</url>
    </repository>
    <snapshotRepository>
        <id>snapshots</id>
        <url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
    </snapshotRepository>
</distributionManagement>

6.5.3 Use deploy on the need to publish the project release
Here Insert Picture Description
6.6 jar package downloaded from the PW need
to add profile in the maven 6.6.1 setting.xml configuration file
to add profiles node in setting.xml in the following configuration
Here Insert Picture Description
6.6.2 activate profilre in the maven setting.xml configuration file
to add activated in the configuration setting.xml

  <activeProfiles>
    <activeProfile>dev</activeProfile>
  </activeProfiles>

  1. The private jar package is installed to a local warehouse or PW
    7.1 install a local warehouse
    ---- into the jar package directory run
mvn deploy:deploy-file -DgroupId=com.alibaba -DartifactId=fastjson -Dversion=1.1.37 -Dpackaging=jar -Dfile=fastjson-1.1.37.jar -Durl=http://localhost:8081/nexus/content/repositories/thirdparty/ -DrepositoryId=thirdparty

DgroupId	 自定义的公司名称  cn.itcast
DartifactId 自定义的工程名  heima92
Dversion    自定义的版本号  1.0.1
Dfile	     jar包的名称
Durl         私服的请求路径

Guess you like

Origin blog.csdn.net/AdamCafe/article/details/91484073