Maven的一些小细节(包含SSM整合)

更改仓库地址

  • 进入:
    C:\Users\Bam\Desktop\apache-maven-3.5.3-bin\apache-maven-3.5.3\conf
    这里写图片描述

增加国内仓库镜像

  • 阿里云
<mirror>
        <id>nexus-aliyun</id>
        <mirrorOf>central</mirrorOf>
        <name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
  • 开源中国
<mirror>  
        <id>CN</id>  
        <name>OSChina Central</name>                                                                                     
        <url>http://maven.oschina.net/content/groups/public/</url 
        <mirrorOf>central</mirrorOf>  
</mirror>  
  • 官方中国仓库镜像
<mirror>
         <id>maven.net.cn</id>
         <name>oneof the central mirrors in china</name>
         <url>http://maven.net.cn/content/groups/public/</url>
         <mirrorOf>central</mirrorOf>
</mirror>

dependency的scope(依赖的范围)

  • compile:编译范围,指A在编译时依赖B,此范围为默认依赖范围。编译范围的依赖会用在编译、测试、运行,由于运行时需要所以编译范围的依赖会被打包。

  • provided:provided依赖只有在当JDK或者一个容器已提供该依赖之后才使用, provided依赖在编译和测试时需要,在运行时不需要,比如:servlet api被tomcat容器提供。

  • runtime:runtime依赖在运行和测试系统的时候需要,但在编译的时候不需要。比如:jdbc的驱动包。由于运行时需要所以runtime范围的依赖会被打包。

  • test:test范围依赖 在编译和运行时都不需要,它们只有在测试编译和测试运行阶段可用,比如:junit。由于运行时不需要所以test范围依赖不会被打包。

maven的依赖管理

  • 打开方法:
    这里写图片描述

  • 结果:
    这里写图片描述

SSM整合

整合SpringMVC
  • pom.xml里边导springmvc的包
    这里写图片描述
  • 配置webapp中的web.xml
    这里写图片描述
  • 配置applicationContext.xml
    这里写图片描述
  • controller里边写代码
    这里写图片描述
  • 搞定
    这里写图片描述
整合mybatis
  • pom.xml里边导包
    <!--Mybatis 核心包-->
    <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.4.4</version>
    </dependency>

    <!-- Mybatis spring整合包 -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.3.0</version>
    </dependency>

    <!-- 数据库驱动 -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.33</version>
    </dependency>

    <!--spring对于orm框架的支持 springjdbc 以及spring tx-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.0.4.RELEASE</version>
    </dependency>

    <!--spring 测试包-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>5.0.4.RELEASE</version>
      <scope>test</scope>
    </dependency>

    <!-- 数据库连接池 -->
    <dependency>
      <groupId>com.mchange</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.5.2</version>
    </dependency>
小插曲

java.lang.IllegalStateException: Failed to load ApplicationContext
报错原因:没有导入servlet-api的依赖包

 <dependency>
      <groupId>tomcat</groupId>
      <artifactId>servlet-api</artifactId>
      <version>5.5.23</version>
</dependency>
布局

这里写图片描述

  • applicationContext.xml、mybatis等文件写在resources文件夹的根目录下
  • applicationContext.xml(可以命名为spring-mvc.xml)里边放的是spring的例如注解扫描、连接池、注解管理等功能,也可以配置mybatis的、sqlSessionFactory等功能。
  • Mapper.xml需要放到与java代码对应的文件夹中,这样打包时候会把他们放置在同一个文件夹下。
  • web.xml永远在webapp里边,别乱放。
  • web.xml里头放的是mvc的基础配置——DispatcherServlet等
  • test文件夹里边放置的是测试用例,记得格式(上边两个注解)
import com.bamzhy.bean.User;
import com.bamzhy.dao.UserDao;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class testDao {
    @Autowired
    UserDao dao;

    @Test
    public void testUser(){
        User user = dao.findUserById(1);
        System.out.println(user);
    }

}

整合实例

不想写了…一个个截图真麻烦,那些写教程的真伟大。

事务管理

workspace——maventest——UserService啥的
自己去看代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值