maven用聚合方式整合SSM框架
步骤
- 1.创建一个父工程(不用项目模版)
- 2.修改父工程pom.xml文件
- 3.创建子工程
- 4.在父工程的pom.xml文件中添加子功能模块
- 5.在每一个子工程pom.xml文件中添加,所继承的父工程坐标
- 6.在resources非java代码资源文件夹中放上Spring、mybatis、SpringMVC、jdbc等配置文件
注意:每次修改pom.xml文件后都要及时刷新
1.创建一个父工程(不用项目模版)
2.修改父工程pom.xml文件
1.添加打包类型:
<packaging>pom</packaging>
2.pom.xml文件中导入整个项目需要用到的其他坐标:
1.spring相关
spring-context
aspectjweaver
spring-jdbc
spring-tx
spring-test
spring-webmvc
javax.servlet-api
javax.servlet.jsp-api
2.mybatis相关
mybatis
3.MyBatis 与 Spring 整合
mybatis-spring
mysql-connector-java
c3p0
4.junit测试、jstl标签、log4j日志
junit
jstl
log4j
5.数据源依赖
commons-pool
commons-dbcp
commons-collections
lombok
gson
6.相应插件:
资源打包插件
JDK编译插件
Tomcat插件
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- spring相关 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!-- servlet和jsp -->
<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>javax.servlet.jsp-api</artifactId>
<version>2.2.1</version>
<scope>provided</scope>
</dependency>
<!-- mybatis相关 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.1</version>
</dependency>
<!-- MyBatis 与 Spring 整合 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.12</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!--junit测试、jstl标签、log4j日志-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!--数据源依赖 -->
<!-- https://mvnrepository.com/artifact/commons-pool/commons-pool -->
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-dbcp/commons-dbcp -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-collections/commons-collections -->
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
<scope>provided</scope>
</dependency>
<!--导入gson-->
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>
</dependencies>
<build>
<!--资源打包插件 -->
<!--
Maven在打包时默认只将src/main/resources里的配置文件拷贝到项目中并做打包处理,
而非resource目录下的配置文件在打包时不会添加到项目中。
配置好该插件以后,那么你设置的位置下的配置文件都会被打包了
-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
<plugins>
<!-- JDK编译插件-->
<plugin>
<!-- 插件坐标-->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!--Tomcat插件-->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8080</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
3.创建子工程
注意:
1.在父类工程中以模块的形式创建子工程
2.普通工程使用 quickstart项目模版
3.web工程使用 webapp项目模版
注意:
当子工程项目之间有依赖关系时,需要在对应的pom.xml文件<dependencies> 中导入对应的坐标
如:
<dependencies>
<!--导入 以来的子工程 centity 坐标-->
<dependency>
<groupId>com.jr.dz10b</groupId>
<artifactId>centity</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
4.在父工程的pom.xml文件中添加子功能模块
<!--聚合项目子模块内容-->
<modules>
<module>centity</module>
<module>cmapper</module>
<module>cservice</module>
<module>cweb</module>
<module>cvo</module>
<module>cutil</module>
</modules>
5.在每一个子工程pom.xml文件中添加,所继承的父工程坐标
<!--配置聚合项目,继承父项目-->
<parent>
<groupId>com.jr.dz10b</groupId>
<artifactId>mavenssm02</artifactId>
<version>1.0-SNAPSHO</version>
</parent>
6.在resources非java代码资源文件夹中放上Spring、mybatis、SpringMVC、jdbc等配置文件
appSpringContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<!--1.加载外部属性文件,获得连接数据库的四要素-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--2.配置数据源(数据库连接池)-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driverClass}"/>
<property name="url" value="${url}"/>
<property name="username" value="${user}"/>
<property name="password" value="${password}"/>
</bean>
<!--3.配置SQLSessionFactory 对象-->
<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:SqlMapConfig.xml"/>
</bean>
<!--4.配置mapper映射文件的扫描器对象-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.jr.mapper"/>
<property name="sqlSessionFactoryBeanName" value="factory"/>
</bean>
<!-- <!–配置注解扫描路径–>
<context:component-scan base-package="com.jr"/>-->
<!--6.配置自动创建aop代理类-->
<aop:aspectj-autoproxy/>
<!--7.声明事务管理对象-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--8.配置事务传播策略
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<!–name: 使用通配符 REQUIRED 有事务就加入 没有就开启 –><tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="upd*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="select*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
9.配置XML方式的声明式事务
<aop:config>
<aop:pointcut id="txpointcut" expression="execution(* com.jr.service.impl.DeptServiceImpl.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txpointcut"/>
</aop:config>-->
<!--10.配置事务支持注解-->
<tx:annotation-driven transaction-manager="txManager"/>
<!--单独配置类型适配器-->
<bean id="conService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="com.jr.util.DateConverter"/>
</list>
</property>
</bean>
</beans>
jdbc.properties
driverClass=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/studz10b?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
user=root
password=root
log4j.properties
# Global logging configuration
#\u5728\u5f00\u53d1\u73af\u5883\u4e0b\u65e5\u5fd7\u7ea7\u522b\u8981\u8bbe\u7f6e\u6210DEBUG\uff0c\u751f\u4ea7\u73af\u5883\u8bbe\u7f6e\u6210info\u6216error
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
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:mvc="http://www.springframework.org/schema/mvc"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--配置映射器和适配器-->
<mvc:annotation-driven/>
<!-- 配置 处理器映射器 和 处理器适配器 .....器 -->
<mvc:annotation-driven conversion-service="formatc"/>
<!-- 配置 类型适配器 -->
<bean id="formatc" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="com.jr.util.DateConverter"/>
</list>
</property>
</bean>
<!--视图解析器-->
<!--配置注解扫描路径-->
<context:component-scan base-package="com.jr"/>
<!--配置静态资源放行路径-->
<mvc:resources location="/lib/" mapping="/lib/**"></mvc:resources>
</beans>
SqlMapConfig.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>
<!-- 配置类型别名-->
<typeAliases>
<package name="com.jr.entity"/>
<package name="com.jr.vo"/>
</typeAliases>
</configuration>