Maven工程的拆分与聚合(父子工程、工程模块关系以及继承和依赖概念、ssm三层架构父子工程实例、父子工程三种启动方式)
1.拆分与聚合思想
以实际生活为例子:
买家:买家看订单,必须把数据库中订单数据查询出来
卖家:卖家看订单,同样查找数据库中订单数据
买家用的是tb项目,卖家用的是管理系统项目,所属项目不同,可是代码却可相互重用
有的人可能觉得,把代码复制过去不就行了。
一份代码重用:修改维护,只用修改一份。
一份代码复制粘贴到不同地方:复制粘贴几个地方,就要修改几个地方
maven解决代码可重用和便于维护的问题:maven把一个完整的项目分成不同的独立模块,这些模块都有各自独立的坐标,哪个地方需要其中某个模块,就直接引用该模块的坐标即可。
今后开发项目,我们不应先考虑dao、service、utils、domain如何编写,而是应该先考虑这些模块是否已经存在,如果存在直接引用。
以上都为拆分的思想
我们可以把拆分零散的模块聚合到一起编写一个完整的项目,这就是maven聚合思想。
2.maven父子工程的创建(idea)
创建父工程
new project/module都行,父工程只需要有pom.xml即可,父工程用于管理jar包,即所有依赖都添加于父工程之中。
创建子工程
右键点击父工程,new module
子工程的pom,其坐标为:groupId+artifactId+version
父工程的pom,多了module
最终结果:其中dao,service不需要与前端进行交互,无需使用骨架,web使用webapp骨架
3.工程模块关系以及继承和依赖概念
工程和模块的区别
工程不等于完整的项目,模块也不等于完整的项目,一个完整的项目看的是代码,代码完整,就可以说这是一个完整的项目。
工程天生只能使用自己内部资源,工程天生是独立的。后天可以和其它工程或模块建立关联关系。
模块天生不是独立的,模块天生是属于父工程的,模块一般创建,所有父工程资源都可以使用。
继承与依赖
父子工程之间,子模块天生继承父工程,可以使用父工程所有资源。父子工程之间叫做继承(先天的)。
子模块之间是没有任何关系的,可以建立关系。平级之间的引用叫做依赖(后天建立的)。
service调用dao资源:
web调用service资源:
4.传递依赖下的包是否可用问题(了解)
compile:编译、测试、运行(部署)都有效
provided:编译、测试有效
runtime:测试、运行有效
test:测试有效
以junit为例:
5.ssm三层架构父子工程实例
三个子工程都无需添加依赖,依赖统一由父工程管理。
同时,service工程添加对dao工程的依赖,web工程添加对service工程的依赖
dao层
spring配置文件仅保留dao相关部分:
<?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:aop="http://www.springframework.org/schema/aop"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--Spring整合Mybatis框架-->
<!--配置连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/maven?serverTimezone=UTC"></property>
<property name="username" value="root"></property>
<property name="password" value="xxxxxx"></property>
</bean>
<!--配置SqlSessionFactory工厂-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!--扫描pojo包,给包下所有pojo对象起别名-->
<property name="typeAliasesPackage" value="com.czy.domain"></property>
</bean>
<!--配置Dao接口所在的包:扫描接口包路径,生成包下所有接口的代理对象,并放入spring容器中-->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.czy.dao"></property>
</bean>
</beans>
service层
spring配置文件仅保留service相关部分:
<?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:aop="http://www.springframework.org/schema/aop"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--开启注解的扫描,希望处理service和dao,controller不需要spring去处理-->
<context:component-scan base-package="com.czy.service">
</context:component-scan>
<!--配置连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/maven?serverTimezone=UTC"></property>
<property name="username" value="root"></property>
<property name="password" value="xxxxxxx"></property>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED"></tx:method>
<tx:method name="update*" propagation="REQUIRED"></tx:method>
<tx:method name="delete*" propagation="REQUIRED"></tx:method>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"></tx:method>
<tx:method name="*" propagation="REQUIRED"></tx:method>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="pt" expression="execution(* com.czy.service.impl.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt"></aop:advisor>
</aop:config>
</beans>
web层
web.xml与springmvc仍与普通整合ssm工程相同,有区别的是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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<import resource="classpath:spring/applicationContext-dao.xml"></import>
<import resource="classpath:spring/applicationContext-service.xml"></import>
</beans>
6.父子工程三层启动方式
方式一:对父工程通过maven插件tomcat7:run启动
方式二:对web工程使用tomcat7:run启动
web工程中添加了对service的依赖,可是本地仓库并没有service的jar包。
我们需要对父工程执行maven:install,即可对web工程使用tomcat7:run
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)