maven高級

Maven高級

maven概念:

​ 項目管理工具前端

​ 做用:1.統一進行jar包管理java

​ 2.統一開發規範和工具,適合團隊協同開發mysql

​ 依賴管理:指的就是經過座標引入jar包git

​ 倉庫引入順序: 本地倉庫 ---------------遠程倉庫-------------------中央倉庫github

    <groupId>org.mybatis</groupId>   :公司名稱 com是盈利組織 org非盈利組織
   <artifactId>mybatis</artifactId> :項目名
   <version>${mybatis.version}</version>:當前使用的版本

​ 一鍵構建web

​ 生命週期:spring

​ clean生命週期: 清理 mvn cleansql

​ default生命週期:編譯數據庫

​ site生命週期: 生成站點文檔apache

​ 經常使用命令:

​ compile : 編譯 : 將java文件編譯成class文件

​ test :測試: 執行test下的測試案例

​ package:打包: 將項目達成jar包 或war包

​ install :安裝:將項目打包安裝到本地倉庫

  deploy :部署:將項目打包並上傳到私服(遠程倉庫)

SSM整合複習

​ 1.建立數據庫及items表

​ 2.建立了maven web工程 archetypeCatalog internal

​ 3.導入pom.xml

​ 直接依賴 :A項目直接引入B項目的座標

​ 依賴傳遞(間接依賴):A項目直接依賴與B項目,B項目直接依賴於C項目,那麼A項目間接依賴與C項目

​ 依賴版本衝突: (1)第一聲明優先 (2)路徑近者優先 (3)直接排除 exclusions

 <properties>
   <spring.version>5.0.2.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>

​ 4.編寫Dao

​ 建立實體類items、建立itemsDao接口


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

​ 建立applicationContext.xml ,進行mybatis的配置

​ (1) 數據源 (2)sqlSessionFactory (3) mapper映射掃描


<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
       <property name="driverClass" value="com.mysql.jdbc.Driver"/>
       <property name="jdbcUrl" value="jdbc:mysql:///maven"/>
       <property name="user" value="root"/>
       <property name="password" value="root"/>
   </bean>

   <!--建立一個生產SqlSeesion對象的工廠對象-->
   <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
       <!--引入數據源-->
       <property name="dataSource" ref="dataSource"/>
       <!--給pojo對象起別名【此處註解開發能夠省略】-->
       <property name="typeAliasesPackage" value="com.itheima.domain"/>
   </bean>

   <!--咱們對全部dao接口包下的接口進行掃描,使用SqlSeesion對象給其建立代理對象,而且放入到容器中-->
   <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
       <property name="basePackage" value="com.itheima.dao"/>
   </bean>

​ 測試:


   @Test
   public void daoTest(){
       //獲得spring容器
       ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
       //從spring容器中獲得所需dao接口的代理對象
       ItemsDao itemsDao = applicationContext.getBean(ItemsDao.class);
       //調用方法
       Items items = itemsDao.findById(1);
       System.out.println(items.getName());
  }

​ 5.編寫service

​ 建立service的接口及實現類

​ 實現類上加@Service: 建立service的實例對象並存到IOC容器中

​ 實現類上加@Transtional: 在service層進行事務管理

​ 編寫applicationContext.xml中service部分:(1)IOC註解掃描 (2)事務管理器 (3)事務註解掃描


@Service
@Transactional
public class ItemsServiceImpl implements ItemsService {
   @Autowired
   private ItemsDao itemsDao;
   @Override
   public Items findById(Integer id) {
       return itemsDao.findById(id);
  }
}

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

   <!--組建掃描-->
   <context:component-scan base-package="com.itheima.service"/>

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

   <!--開啓事務註解的支持-->
   <tx:annotation-driven/>

​ 測試


   @Test
   public void serviceTest(){
       //獲得spring容器
       ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
       //從spring容器中獲得所需dao接口的代理對象
       ItemsService itemsService = applicationContext.getBean(ItemsService.class);
       //調用方法
       Items items = itemsService.findById(1);
       System.out.println(items.getName());
  }

​ 6.編寫web層步驟:

​ 建立controller類,類上@Controller:生成實例對象放到springmvc的IOC容器中,方法上

​ @RequestMapping註解:用於設置映射路徑

​ 編寫web.xml: (1)前端控制器 (2)中文亂碼過濾器 (3)spring的監聽器

​ 編寫springmvc.xml: (1)IOC註解掃描 (2)視圖解析器(3)開啓springmvc註解支持 (4) 靜態資源放行

​ 測試

controller類:


@Controller
@RequestMapping("/itmes")
public class ItemsController {
   @Autowired
   private ItemsService itemsService;
   @RequestMapping("/showDetail")
   public String showDetail(Model model){
       Items items = itemsService.findById(1);
       model.addAttribute("item", items);
       return "itemDetail";
  }
}

web.xml


 <!--中文亂碼過濾器-->
 <filter>
   <filter-name>encodingFilter</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>encodingFilter</filter-name>
   <url-pattern>/*</url-pattern>
 </filter-mapping>

 <!--前端控制器-->
 <servlet>
   <servlet-name>springmvc</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>
 </servlet>
 <servlet-mapping>
   <servlet-name>springmvc</servlet-name>
   <url-pattern>/</url-pattern>
 </servlet-mapping>

 <!--spring核心監聽器-->
 <listener>
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 <context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath:applicationContext.xml</param-value>
 </context-param>

springmvc.xml


  <!--組建掃描-->
   <context:component-scan base-package="com.itheima.controller"/>

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

   <!--視圖解析器-->
   <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
       <property name="prefix" value="/WEB-INF/pages/"/>
       <property name="suffix" value=".jsp"/>
   </bean>

   <!--釋放靜態資源-->
   <mvc:default-servlet-handler/>

 

使用聚合工程完成SSM整合

​ 1.建立maven 父工程 打包方式 pom

  2.建立maven子工程 maven_dao jar maven_service jar maven_web war

​ 3.複製代碼到父子工程中,並讓子工程產生依賴關係

​ 4.測試:啓動方式3種

​ 1.選中父工程 tomcat7:run

​ 2.使用本地tomcat,添加項目啓動 項目依賴:本地倉庫中能夠沒有當前項目的jar包

​ 3.選中maven_web工程 tomcat7:run jar包依賴:依賴本地倉庫中的jar包

 

私服的安裝與使用(瞭解)

​ 1.安裝私服: 解壓nexus打目錄下,進入到bin目錄下,執行nexus.bat install nexus.bat start

  訪問地址:http://localhost:8081/nexus

​ 2.私服的倉庫類型: hosted:宿主倉庫:上傳本身的jar包 分爲releases:發行版 snapshots:測試版

​ proxy:代理倉庫,用於代理遠程的公共倉庫 如maven中央倉庫

相關文章
相關標籤/搜索