更改仓库地址
- 进入:
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啥的
自己去看代码
Maven与SSM整合实战
本文介绍如何通过配置Maven使用国内镜像加速下载,并详细讲解了SpringMVC、MyBatis与Maven整合的过程,包括依赖管理、配置文件布局及常见错误排查。
166

被折叠的 条评论
为什么被折叠?



