1、Maven项目是什么?
Maven是一个项目管理工具,可以对 Java 项目进行构建、依赖管理。基于项目对象模型(缩写:POM),Maven 也可被用于构建和管理各种项目。
Maven的核心功能便是合理叙述项目间的依赖关系。所有的Maven项目包含着一个名为pom.xml的文件,在文件中记录着自己的<groupId><artifactId><version>等字段,这些字段在创建Maven项目时填写,Maven会依据它们来定位到该项目。
在pom中的另一个关键标签是<dependencies>,该标签下可以包含若干个<dependency>标签,而<dependency>下则是上面介绍的<groupId><artifactId><version>等依赖字段,它们确定着一个唯一的项目版本。
2、Maven项目创建
本项目在jdk1.8环境下运行,首先新建一个项目如下操作:
等下载完成后,右侧项目栏多出了src包
pom.xml文件是Maven项目的配置文件,打开如下
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>mavendemo1</artifactId>
<version>1.0-SNAPSHOT</version>
<name>mavendemo1</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<!-- 测试框架-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
其中所有Maven引用的jar包都会包含在<dependencies></dependencies>里面,我们要导入mysql数据库的jar包,打开Maven中央仓库https://mvnrepository.com/搜索mysql并选择第一个jar包,下拉选择当前通用的5.1.48版本
复制框中的代码:
<!-- 操作mysql数据库-->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.48</version>
</dependency>
复制到pom.xml文件中<dependencies></dependencies>里面,再点击右侧项目栏中的Maven进行更新;
3、打开APP.java文件,编写代码连接数据库如下
package org.example;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args ) throws SQLException {
//1.获得DataSource
MysqlDataSource dataSource = new MysqlDataSource();
//有了数据源就要定位到具体的服务器,给三个参数url,user,password
//charactionEncoding=utf8
dataSource.setURL("jdbc:mysql://127.0.0.1:3306/java18?charactionEncoding=utf8&useSSL=false");
//协议名jdbc:mysql:,本地IP地址127.0.0.1,端口号3306,数据库名为java18
dataSource.setUser("root");
//写自己的mysql密码
dataSource.setPassword("111111");
//2.获取连接connection
Connection connection = dataSource.getConnection();
//3.拼接SQL
String sql = "select * from student where id=?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1,2);
//4.得到ResetSet对象
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
System.out.println("查询的数据name:"+
resultSet.getString("name"));
}
//5.释放链接
resultSet.close();
statement.close();
connection.close();
}
}
点击运行得到结果:
说明实现了Maven连接数据库的查询功能!!!
验证:
查询数据库中student表中确实存在数据name为MySQL的数据;