Write in front:

Hello, everyone! Last time we looked at dependency injection in Spring

We are starting our Maven learning journey this time!

Mind mapping:

I. Introduction to Maven;

Maven is a software project management tool based on the Project Object Model (POM) that manages project builds, reports, and documentation with a short description. Simple: Replace the old way of importing native JAR packages to facilitate team collaboration dependency management tools

Maven installation and configuration;

  • Maven download:

  • Maven installation:

First make sure the JDK is installed and configured; Extract the Maven download and put it on disk D

Maven Usage; 3.

  • First we set up a Maven project in IDEA.

  • Once built, we will first modify the Maven configuration file.

Since the JAR in Maven is pulled from the warehouse, we directly set it here to get it from the image of Ali Cloud, which is super fast;

Go to maven’s settings. XML configuration file and add the following code:

  • Let’s take MySQL to connect to a database as an example:

    package com.java;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    
    public class Test {
        String dbUrl="jdbc:mysql://localhost:3306/db_book? useSSL=false";
        String username="root";
        String password="123456";
        String driver="com.mysql.jdbc.Driver";
    
        private Connection getCon(a) throws Exception{
            Class.forName(driver);
            Connection con= DriverManager.getConnection(dbUrl,username,password);
            return con;
        }
    
        public static void main(String[] args) throws Exception{
            String sql="select * from t_user";
            Test t=new Test();
            Connection con=t.getCon();
            PreparedStatement pstmt=con.prepareStatement(sql);
            ResultSet rs=pstmt.executeQuery();
            while(rs.next()){
                System.out.println(rs.getInt(1));
                System.out.println(rs.getString(2));
                System.out.println(rs.getInt(3)); }}}Copy the code

    In maven, jar files are not required. In Maven, jar files are not required.

  • We find mysql dependencies in Maven:

Mysql jar maven Then find the corresponding driver version:

Then add the following code to the pom.xml file (which is a mysql dependency) :

Copy and paste;

  • Then we run the mysql database connection code above;

It can run successfully!

4. We configure Maven in IDEA.

1. Open Settings. Find maven.

2, as shown in the picture, fill in and then Apply.


Well, today is the first share here, next time to continue to bring you the Spring series of follow-up explanation! Welcome to follow my original technology public number ~