- 1.什么是Maven?
- 2.配置并使用Maven
- 2.1 Maven依赖管理流程
- 2.2Maven配置
- 3.创建第一个Maven项目
- 3.1maven项目目录介绍:
- 3.2 maven添加外部jar
- 3.3使用JDBC实现查询功能
- 3.4Maven生命周期
1.Maven 是⼀个项目构建⼯具,创建的项⽬只要遵循 Maven 规范(称为Maven项⽬),即可使⽤ Maven 来进⾏:管理 jar 包、编译项目,打包项目等功能。
2.Maven作用:
(1)导入外部jar包
(2)打包项目
(3)发布项目
3.项目构成:
(1)自己的代码
(2)别人的代码(jar包->多个.java的合集)
Maven不需要自己安装,因为IDEA已经自带了,打开IDEA,选中settings搜索"Maven":
Maven 项⽬中可以引⼊依赖包(引⼊外部框架的 jar 包),引⼊后,加载依赖包的⽅式为在 Maven 仓库 中搜索。
Maven仓库可以理解为存放依赖包的仓库,分为本地仓库和远程仓库两种。
本地仓库地址在idea中可以找到,如图:
Maven 默认情况下使⽤的国外源,所以下载 jar 会很慢,且经常出差,所以⼀定要配置本机的 Maven 源 为国内源,它的配置⽅法如下:
1.Maven数据源配置(默认是国外的数据源):
(1)找到Maven的settings.xml配置文件:
a.默认自己的电脑上有此文件
b.如果电脑上没有此文件
settings.xml:
user.home}/.m2/settings.xml.
|
| NOTE: This location can be overridden with the CLI option:
|
| -s /path/to/user/settings.xml
|
| 2. Global Level. This settings.xml file provides configuration for all Maven
| users on a machine (assuming they're all using the same Maven
| installation). It's normally provided in
| ${maven.conf}/settings.xml.
|
| NOTE: This location can be overridden with the CLI option:
|
| -gs /path/to/global/settings.xml
|
| The sections in this sample file are intended to give you a running start at
| getting the most out of your Maven installation. Where appropriate, the default
| values (values used when the setting is not specified) are provided.
|
|-->
user.home}/.m2/repository
/path/to/local/repo
-->
alimaven
aliyun maven
http://maven.aliyun.com/nexus/content/groups/public/
central
tomcatPath}
|
|
| ...
|
| NOTE: If you just wanted to inject this configuration whenever someone set 'target-env' to
| anything, you could just leave off the inside the activation-property.
|
env-dev
target-env
dev
/path/to/tomcat/instance
-->
(2)配置settings.xml,检查 settings.xml 是否配置了国内源。(自己电脑带有settings.xml时,需要配置此步骤,如果复制上边的setting.xml则忽略此步骤):
alimaven aliyun maven http://maven.aliyun.com/nexus/content/groups/public/ central
(3)勾选中maven配置文件和本地仓库:
(4)配置新项目的maven国内源:
重复前面3步操作来配置新项目的maven国内源。
1.创建一个Maven项目
1.具体步骤:
(1)先去mvn中央库,找到对应的依赖信息。
https://mvnrepository.com/
(2)将依赖信息复制到项目中的pom.xml中
(3)重新加载当前项目的依赖
此时mysql5.1.49在External Libraries中不存在,点击图标进行加载:
2.maven项目jar导入失败的情况处理
(1)检查maven下的settings.xml是否配置并且勾选了国内源:
(2)删除本地maven仓库的所有jar
(3)使用maven重新生成依赖
(4)更换网络,尝试前三步操作。
查询数据库中学生表的信息:
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class App {
public static void main(String[] args) throws SQLException {
//1.得到DataSource
MysqlDataSource dataSource=new MysqlDataSource();
dataSource.setURL("jdbc:mysql://127.0.0.1:3306/java33?character=utf8&useSSL=true");
dataSource.setUser("root");
dataSource.setPassword("123456");
//2.得到Connection
Connection connection=(Connection)dataSource.getConnection();
//3.拼接sql,并执行
String sql=" select * from student where id=?";
PreparedStatement statement=connection.prepareStatement(sql);
statement.setInt(1,2);
//4.执行查询
ResultSet resultSet=statement.executeQuery();
//statement.executeUpdate();//增加,删除,修改
if(resultSet.next()){
//有数据
System.out.println("用户名:"+resultSet.getString("username"));
System.out.println("email:"+resultSet.getString("mail"));
}
//关闭资源
resultSet.close();
statement.close();
connection.close();
}
}
3.4Maven生命周期



