Maven基礎——自定義Maven骨架步驟、Maven案例

目錄


一、 自定義Maven骨架

跳轉到目錄

1、首先在 pom.xml 加入插件
<!--創建骨架的插件-->
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-archetype-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>

2、執行:
archetype:create-from-project	


3、進入到 生成好的 target -->generated-resources-->arthetype下

4、執行maven命令
	install

5、最後執行
	archetype:crawl

1.1 首先使用maven創建一個web項目

maven_gujia
在這裏插入圖片描述

1.2 將這個maven_gujia當做模板

將一些常用的包結構,文件等都在這個web項目中創建好.

1.3 模板的目錄結構

這一個模板根據自己的項目結構來自定義
在這裏插入圖片描述
在這裏插入圖片描述
將上面的xml中改爲:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
</web-app>

如果不改, 無法使用EL表達式,及一些其他功能.

在這裏插入圖片描述

1.4 使用插件

首先在 pom.xml 加入插件

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-archetype-plugin</artifactId>
      <version>3.0.0</version>
    </plugin>

1.5 執行命令

archetype:create-from-project
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
這個文件夾目前報錯,先不用管.

1.6 將生成的archetype裝入本地倉庫中

在這裏插入圖片描述
在這裏插入圖片描述
裝入成功後,發現那個目錄就沒有報錯了, 此時已經將這個項目添加到了本地倉庫, 可以在倉庫中查看: 我的倉庫位置: C:\develop\maven\apache-maven-3.5.2\repository
然後在 repository\com\sunny\maven_gujia-archetype\1.0-SNAPSHOT可以找到
在這裏插入圖片描述

1.7 將本地倉庫中的這個模板骨架添加到maven骨架中

在這裏插入圖片描述
想要獲取這幾個空的內容,需要再執行一個命令,這時候選哪個位置都可以
archetype:crawl 這個命令是爬取整個本地倉庫
在這裏插入圖片描述
C:\develop\maven\apache-maven-3.5.2\repository這個目錄最後會出現一個archetype-catalog.xml文件
在這裏插入圖片描述
前三個的參數就是填到剛纔的空上即可!
在這裏插入圖片描述
在這裏插入圖片描述

1.8 通過自定義的Maven骨架來創建maven項目

在這裏插入圖片描述
此時就已經完成了!


二、 Maven案例:使用Maven搭建Servlet+JSP+dbutils

跳轉到目錄

項目

2.1 需求分析

完成添加客戶信息的操作

2.2 創建數據表

在這裏插入圖片描述

2.3 創建Maven項目

在這裏插入圖片描述

2.4 導入座標

2.4.1 導入所需的依賴(jar包)和插件

  • mysql驅動程序
  • dbutils
  • servlet-api (Servlet的API)
  • jsp-api (可以是EL表達式)

2.4.2 maven座標

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>cn.itcat</groupId>
  <artifactId>maven_test</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>maven_test Maven Webapp</name>
  <url>http://maven.apache.org</url>


  <dependencies>
   <!--mysql驅動-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.26</version>
    </dependency>
    <!--druid連接池-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.0.9</version>
    </dependency>
    <!--servlet的api-->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>
    <!--jsp的api-->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.0</version>
      <scope>provided</scope>
    </dependency>
    <!-- jstl的依賴-->
    <dependency>
      <groupId>javax.servlet.jsp.jstl</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>
    <dependency>
      <groupId>taglibs</groupId>
      <artifactId>standard</artifactId>
      <version>1.1.2</version>
    </dependency>
    <!-- beanUtils工具類 -->
    <dependency>
      <groupId>commons-beanutils</groupId>
      <artifactId>commons-beanutils</artifactId>
      <version>1.9.3</version>
    </dependency>

    <!-- jdbctemplate-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>4.2.4.RELEASE</version>
    </dependency>

  </dependencies>

  <!-- 配置插件 -->
  <build>
    <plugins>
      <!-- jdk版本插件 -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.2</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
          <encoding>UTF-8</encoding>
        </configuration>
      </plugin>

    </plugins>
  </build>

</project>


2.4.3 編寫代碼

2.4.3.1 頁面編寫

  • 表單頁面
<%@ page language="java" contentType="text/html; charset=utf-8"
         pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"></c:set>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>客戶錄入頁面</title>
</head>
<body>
<form action="${ctx}/add" method="post">
    客戶名稱:<input type="text" name="custName"/><br/>
    客戶來源:<input type="text" name="custSource"/><br/>
    客戶級別:<input type="text" name="custLevel"/><br/>
    客戶行業:<input type="text" name="custIndustry"/><br/>
    客戶地址:<input type="text" name="custAddress"/><br/>
    客戶電話:<input type="text" name="custPhone"/><br/>
    <input type="submit" value="保存"/><br/>
</form>
</body>
</html>

  • 添加成功頁面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h1>save success!!!</h1>
</body>
</html>

2.4.3.2 編寫實體類

package com.itheima.maven.domain;

import java.io.Serializable;

public class Customer implements Serializable {

    private Long custId;
    private String custName;
    private String custSource;
    private String custLevel;
    private String custIndustry;
    private String custAddress;
    private String custPhone;

    public Long getCustId() {
        return custId;
    }

    public void setCustId(Long custId) {
        this.custId = custId;
    }

    public String getCustName() {
        return custName;
    }

    public void setCustName(String custName) {
        this.custName = custName;
    }

    public String getCustSource() {
        return custSource;
    }

    public void setCustSource(String custSource) {
        this.custSource = custSource;
    }

    public String getCustLevel() {
        return custLevel;
    }

    public void setCustLevel(String custLevel) {
        this.custLevel = custLevel;
    }

    public String getCustIndustry() {
        return custIndustry;
    }

    public void setCustIndustry(String custIndustry) {
        this.custIndustry = custIndustry;
    }

    public String getCustAddress() {
        return custAddress;
    }

    public void setCustAddress(String custAddress) {
        this.custAddress = custAddress;
    }

    public String getCustPhone() {
        return custPhone;
    }

    public void setCustPhone(String custPhone) {
        this.custPhone = custPhone;
    }
}

2.4.3.3 Servlet編寫

package com.itheima.web.servlet;

import com.itheima.domain.Customer;
import com.itheima.service.CustomerService;
import com.itheima.utils.BeanFactory;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/add")
public class CustomerServlet extends HttpServlet {
    private CustomerService customerService= BeanFactory.newInstance(CustomerService.class);
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //獲取提交參數
        Map<String, String[]> map = request.getParameterMap();
        //封裝javabean
        Customer customer = new Customer();

        try {
            BeanUtils.populate(customer,map);
        } catch (Exception e) {
        }

        //調用service 保存提交數據
        customerService.save(customer);


        //請求轉發成功頁面
        request.getRequestDispatcher("/success.jsp").forward(request,response);


    }
}

2.4.3.4 Service編寫

  • 接口編寫
package com.itheima.maven.service;

import com.itheima.maven.domain.Customer;

public interface CustomerService {
    public void save(Customer customer);
}

  • 實現編寫
package com.itheima.service.impl;

import com.itheima.dao.CustomerDao;
import com.itheima.domain.Customer;
import com.itheima.service.CustomerService;
import com.itheima.utils.BeanFactory;

public class CustomerServiceImpl implements CustomerService {
    private CustomerDao dao=BeanFactory.newInstance(CustomerDao.class);
    @Override
    public void save(Customer customer) {
        dao.save(customer);

    }
}

2.4.3.5 Dao編寫

  • 接口編寫
package com.itheima.maven.dao;

import com.itheima.maven.domain.Customer;

public interface CustomerDao {
    void save(Customer customer);
}

  • 實現編寫
package com.itheima.dao.impl;

import com.itheima.dao.CustomerDao;
import com.itheima.domain.Customer;
import com.itheima.utils.DataSourceUtil;
import org.apache.commons.dbutils.QueryRunner;

import java.sql.SQLException;

public class CustomerDaoImpl implements CustomerDao{

    @Override
    public void save(Customer c) {
        //創建qr
        QueryRunner qr = new QueryRunner(DataSourceUtil.getDataSource());
        //編寫sql
        /**
         *  private Long custId;
         private String custName;
         private String custSource;
         private String custLevel;
         private String custIndustry;
         private String custPhone;
         */

        String sql="insert into cst_customer values(null,?,?,?,?,?)";


        //執行sql


        try {
            qr.update(
                    sql,
                    c.getCustName(),
                    c.getCustSource(),
                    c.getCustLevel(),
                    c.getCustIndustry(),
                    c.getCustPhone()
                    );
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }


    }
}


  • JdbcUtils工具
package com.itheima.utils;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

public class DataSourceUtil {
	private static final DataSource dataSource=new ComboPooledDataSource();
	public static DataSource getDataSource(){
		return dataSource;
	}
	public static Connection getConnection() throws SQLException{
		return dataSource.getConnection();
	}
}

  • c3p0配置文件
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/maven
username=root
password=root
initialSize=5
maxActive=10
maxWait=3000
maxIdle=6
minIdle=3

2.5 啓動IDEA集成的Tomcat 測試

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章