Maven高级

Maven高级

1.maven基础知识回顾

1.1 maven介绍

maven 是一个项目管理工具,主要作用是在项目开发阶段对Java项目进行依赖管理和项目构建。

依赖管理:就是对jar包的管理。通过导入maven坐标,就相当于将仓库中的jar包导入了当前项目中。

项目构建:通过maven的一个命令就可以完成项目从清理、编译、测试、报告、打包,部署整个过程。

​ [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bsAplOjP-1580050744410)(.\img\图片1.png)]

1.2 maven的仓库类型

1.本地仓库

2.远程仓库

①maven中央仓库(地址:http://repo2.maven.org/maven2/)

②maven私服(公司局域网内的仓库,需要自己搭建)

③其他公共远程仓库(例如apache提供的远程仓库,地址:http://repo.maven.apache.org/maven2/)

1.3 maven常用命令

clean: 清理

compile:编译

test: 测试

package:打包

install: 安装

1.4 maven坐标书写规范

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4N6Vb2U3-1580050744411)(.\img\图片2.png)]

1.5 maven的依赖范围

依赖范围 对于编译classpath有效 对于测试classpath有效 对于运行时classpath有效 例子
compile Y Y Y spring-core
test - Y - Junit
provided Y Y - servlet-api
runtime - Y Y JDBC驱动
system Y Y - 本地的,maven仓库之外的类库

2. maven的依赖传递

2.1 什么是依赖传递

在maven中,依赖是可以传递的,假设存在三个项目,分别是项目A,项目B以及项目C。假设C依赖B,B依赖A,那么我们可以根据maven项目依赖的特征不难推出项目C也依赖A。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ctFCqbZi-1580050744415)(.\img\图片3.png)]

​ [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JFAuIkvq-1580050744417)(.\img\图片4.png)]

通过上面的图可以看到,我们的web项目直接依赖了spring-webmvc,而spring-webmvc依赖了sping-aop、spring-beans等。最终的结果就是在我们的web项目中间接依赖了spring-aop、spring-beans等。

2.2 什么是依赖冲突

由于依赖传递现象的存在, spring-webmvc 依赖 spirng-beans-4.2.4,spring-aop 依赖 spring-beans-5.0.2,但是发现 spirng-beans-4.2.4 加入到了工程中,而我们希望 spring-beans-5.0.2 加入工程。这就造成了依赖冲突。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2lTcUJev-1580050744418)(.\img\图片5.png)]

2.3 如何解决依赖冲突

1.使用maven提供的依赖调解原则

   第一声明者优先原则

​ 路径近者优先原则

2.排除依赖

3.锁定版本

2.4 依赖调节原则——第一声明者优先原则

在 pom 文件中定义依赖,以先声明的依赖为准。其实就是根据坐标导入的顺序来确定最终使用哪个传递过来的依赖。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tRFhhRvJ-1580050744419)(.\img\图片6.png)]

结论:通过上图可以看到,spring-aop和spring-webmvc都传递过来了spring-beans,但是因为spring-aop在前面,所以最终使用的spring-beans是由spring-aop传递过来的,而spring-webmvc传递过来的spring-beans则被忽略了。

2.5 排除依赖

可以使用exclusions标签将传递过来的依赖排除出去。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3rURdzvz-1580050744421)(.\img\图片7.png)]

2.6 版本锁定

采用直接锁定版本的方法确定依赖jar包的版本,版本锁定后则不考虑依赖的声明顺序或依赖的路径,以锁定的版本为准添加到工程中,此方法在企业开发中经常使用。

版本锁定的使用方式:

第一步:在dependencyManagement标签中锁定依赖的版本

第二步:在dependencies标签中声明需要导入的maven坐标

①在dependencyManagement标签中锁定依赖的版本

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8lwdHkKE-1580050744423)(.\img\图片8.png)]

②在dependencies标签中声明需要导入的maven坐标

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yW5SJitC-1580050744425)(.\img\图片9.png)]

3.基于maven构建SSM工程案例

3.1 需求描述

本案例基于maven构建 SSM(Spring+SpringMVC+Mybatis)工程,通过maven坐标进行依赖管理。最终实现根据 id 查询商品信息的功能。

3.2 构建maven工程

1.数据库环境搭建

​ ①创建数据库ssmtest

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-67rWJGH2-1580050744427)(.\img\图片10.png)]

​ ②创建商品表item

CREATE TABLE `item` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(255) default NULL,
  `price` float default NULL,
  `createtime` datetime default NULL,
  `detail` varchar(255) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

2.maven项目构建

①创建maven web项目

②配置pom.xml文件

③实现spring+mybatis整合

创建POJO类

public class Item {
   
private Integer id;
private String name;
private Float price;
private Date createtime;
private String detail;
//省略setter、getter
}

持久层DAO接口编写

public interface ItemMapper {
   
       public Item findById(int id);
}

Mapper映射文件编写

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.itheima.ssm.dao.ItemMapper">
<select id="findById" parameterType="int" resultType="item">
         select * from item where id=#{id}</select>
</mapper>

业务层Service编写

package com.itheima.ssm.service;
import com.itheima.ssm.pojo.Item;
public interface ItemService {
   
    public Items findById(int id);
} 
@Service
@Transactional
public class ItemServiceImpl implements ItemService {
   
@Autowired
private ItemMapper itemMapper;
public Item findById(int id) {
   
return itemMapper.findById(id);
}
}

spring配置文件applicationContext-dao.xml编写

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"      xmlns:context="http://www.springframework.org/schema/context"   xmlns:p="http://www.springframework.org/schema/p"       xmlns:aop="http://www.springframework.org/schema/aop"       xmlns:tx="http://www.springframework.org/schema/tx"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xsi:schemaLocation="http://www.springframework.org/schema/bean       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd       http://www.springframework.org/schema/context      http://www.springframework.org/schema/context/spring-context-4.0.xsd      http://www.springframework.org/schema/aop   http://www.springframework.org/schema/aop/spring-aop-4.0.xsd     http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-4.0.xsd      http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 数据库连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<!-- 驱动 -->
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<!-- url -->
<property name="url" value="jdbc:mysql://localhost:3306/ssmtest"/>
<!-- 用户名 -->
<property name="username" value="root"/>
<!-- 密码 -->
<property name="password" value="root"/></bean>
<!-- mapper配置 --> <!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">        <!-- 数据库连接池 -->
<property name="dataSource" ref="dataSource"/>
<!--为指定包下的所有实体类创建别名-->
<property name="typeAliasesPackage" value="com.itheima.ssm.pojo"/></bean>
<!-- mapper扫描器 :用来产生代理对象-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.itheima.ssm.dao"></property>  
</bean>
</beans>

spring配置文件applicationContext-service.xml编写

④加入springmvc相关配置

表现层Controller编写

@Controller
@RequestMapping("/item")
public class ItemController {
   
   @Autowired
   private ItemService itemService;
  @RequestMapping("/showItem/{id}")
  public String showItem(@PathVariable("id") int id, Model model){
   
        Item item = itemService.findById(id);
        model.addAttribute("item",item);
        return "item";   
  }
}

springmvc.xml文件编写

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:context="http://www.springframework.org/schema/context"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      xsi:schemaLocation="http://www.springframework.org/schema/beans       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd       http://www.springframework.org/schema/context      http://www.springframework.org/schema/context/spring-context-4.0.xsd">    <context:component-scan base-package="com.itheima.ssm.controller"/>
<!--  配置视图解析器的前缀和后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">           <property name="prefix“ value="/WEB-INF/jsp/"></property>
    <property name="suffix" value=".jsp"></property>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值