D49-maven高級(工程聚合和拆分,父子工程)

一、maven基礎回顧

1.1 maven兩大核心功能

  • 依賴管理: 對jar包的管理過程。原來傳統工程項目中放置jar包,maven工程項目中方式jar包座標,。而真正的jar包放在中央倉庫。目的:實現代碼的可重用。
    • 倉庫分爲三類:本地倉庫,私服(遠程倉庫),中央倉庫。
  • 一鍵構建: 使用maven集成的tomcat插件對項目進行編譯,測試,打包,安裝,部署 等操作。

1.2 Maven的三套生命週期

  • 清理生命週期: clean 刪除target
  • 默認聲明週期:
  1. complie 編譯main下面的java文件,生成.class
  2. test:測試
  3. package:將工程打包到target中
  4. install:把maven項目打包到本地倉庫
  5. deploy:將打好的包安裝到遠程倉庫,供其他項目使用。
  • 站點生命週期: 瞭解

二、回顧SSM整合

2.1 jar包之間的依賴關係,直接依賴和傳遞依賴

直接依賴: A項目中導入了B包,A直接依賴於B。
傳遞依賴: A項目中導入了B包,B包直接依賴於C包,最終也可以在A項目中使用C,可以說A傳遞依賴於C。

2.2 導入jar包的三要素

2.2.1 第一聲明優先原則

哪個jar包在靠上的位置,最終項目進入的就是哪個jar包下的依賴包。

2.2.2 路徑近者優先原則

直接依賴路徑近與傳遞依賴路徑。

2.2.3 直接排除法

2.3、pom文件中可以放三類信息:

  • 項目自身座標信息
  • 項目運行所依賴的jar包的信息
  • 項目運行的環境信息

2.4 編寫代碼

創建MavenWeb工程:
在這裏插入圖片描述

2.4.1編寫dao層代碼

提供dao接口

public interface ItemsDao {
@Select("select * from items where id = #{id}")
   public Items findById(Integer id);
}

編寫mybatis的spring配置文件–applicationContext.xml

<!--dao配置開始-->
    <!--數據庫連接池-->
    <bean id="c3p0" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql:///day02_spring"/>
        <property name="user" value="root"/>
        <property name="password" value="root"/>
    </bean>

    <!--得到生產SqlSessionFactory的工廠對象-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="c3p0"></property>
    </bean>

    <!--自動掃描dao接口包,給其下所有接口生成代理對象,並放入到ioc容器中-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.zsl.dao"></property>
    </bean>
    <!--dao配置結束-->

測試類

@Test
    public void daoTest(){
        //得到ioc容器
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //從容器中拿到代理對象
        ItemsDao dao = context.getBean(ItemsDao.class);
        Items byId = dao.findById(1);
        System.out.println(byId.getName());
    }

2.4.2 編寫service層代碼

提供service接口

public interface ItemsService {
    Items findById(Integer id);
}

實現service的實現類

@Service
public class ItemsServiceImpl  implements ItemsService {
    @Autowired
    private ItemsDao itemsDao;

    @Override
    public Items findById(Integer id) {
        return itemsDao.findById(id);
    }
}

提供service配置文件—applicationContext.xml

<!--service配置開始-->

    <!--開啓組件掃描-->
    <context:component-scan base-package="com.zsl.service"></context:component-scan>

    <!--開啓事務管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="c3p0"></property>
    </bean>

    <!--配置事務的通知-->
<tx:advice id="advice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="find*" read-only="true"/>
        <tx:method name="*" propagation="REQUIRED"></tx:method>
    </tx:attributes>
</tx:advice>
    <!--配置切面-->
    <aop:config>
        <!--切入點表達式-->
        <aop:pointcut id="pointcut" expression="execution(* com.zsl.service.Serviceimpl.*.*(..))"></aop:pointcut>
        <aop:advisor advice-ref="advice" pointcut-ref="pointcut"></aop:advisor>
    </aop:config>
    <!--service配置結束-->

提供測試類

 @Test
    public void serviceTest(){
        //得到IOC容器
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        //從容器中拿到dao的代理對象
        ItemsService itemsService = ac.getBean(ItemsService.class);
        Items items = itemsService.findById(1);
        System.out.println(items.getName());
    }

2.4.3 編寫web層代碼

編寫處理器–ItemsController

@Controller
@RequestMapping("/items")
public class ItemsController {
    @Autowired
    private ItemsService itemsService;

    @RequestMapping("/showDetail")
    public String showDetail(Model model){
        Items byId = itemsService.findById(1);
        model.addAttribute("item",byId);
        return "itemDetail";
    }
}

編寫web.xml配置文件

<!--編寫編碼過濾器-->
    <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>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
   <servlet-mapping>
       <servlet-name>dispatcherServlet</servlet-name>
       <url-pattern>/</url-pattern>
   </servlet-mapping>

    <!--配置spring的核心監聽器-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!--重新指定spring配置文件的路徑和名稱-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

編寫spring-mvc.xml配置文件

 <!--開啓ioc掃描-->
    <context:component-scan base-package="com.zsl.controller"></context:component-scan>

    <!--開啓mvc註解支持-->
    <mvc:annotation-driven></mvc:annotation-driven>

    <!--視圖解析器-->
<bean id="internalResourceViewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/pages/"></property>
    <property name="suffix" value=".jsp"></property>
</bean>
    <!--釋放靜態資源-->
<mvc:default-servlet-handler></mvc:default-servlet-handler>

提供請求視圖 — index.jsp

<body>
<% response.sendRedirect(request.getContextPath()+"/items/showDetail"); %>
</body>

三、工程拆分與聚合的思想

  • maven工程拆分就是爲了把一個項目變成N個獨立的模塊,使代碼更充分的可重用。
    • maven工程的聚合 就是把多個模塊合在一起變成一個新的項目。

3.1 父子工程以及子模塊之間的關係

  • 父子關係直接是繼承關係,子模塊天生可以使用父工程的資源。
  • 子模塊本沒有任何關係,後天可以建立關係,這叫依賴。
    工程和模塊的區別:
  • 工程可以單獨創建
  • 模塊必須在工程下
    建立模塊之間的依賴關係: web依賴service,service依賴dao。

3.2 父子工程的創建

在這裏插入圖片描述

將上面的代碼按照子模塊的名稱依次複製過來即可。

3.3 注意

  • 需要在service子模塊中的applicationContext.xml中添加
 <import resource="classpath:applicationContext-dao.xml"></import>

目的: 繼承dao層實現的功能。

  • 應該將service層的配置文件改爲applicationContext-service.xml 爲了加以區分。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章