Maven基础——自定义Maven骨架步骤、Maven案例

目录

  • 自定义Maven骨架详细步骤
  • Maven案例

一、 自定义Maven骨架

跳转到目录

1、首先在 pom.xml 加入插件

        
          org.apache.maven.plugins
          maven-archetype-plugin
          3.0.0
        

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


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

4、执行maven命令
	install

5、最后执行
	archetype:crawl

1.1 首先使用maven创建一个web项目

maven_gujia
Maven基础——自定义Maven骨架步骤、Maven案例_第1张图片

1.2 将这个maven_gujia当做模板

将一些常用的包结构,文件等都在这个web项目中创建好.

1.3 模板的目录结构

这一个模板根据自己的项目结构来自定义
Maven基础——自定义Maven骨架步骤、Maven案例_第2张图片
Maven基础——自定义Maven骨架步骤、Maven案例_第3张图片
将上面的xml中改为:


<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表达式,及一些其他功能.

Maven基础——自定义Maven骨架步骤、Maven案例_第4张图片

1.4 使用插件

首先在 pom.xml 加入插件

    
      org.apache.maven.plugins
      maven-archetype-plugin
      3.0.0
    

1.5 执行命令

archetype:create-from-project
Maven基础——自定义Maven骨架步骤、Maven案例_第5张图片
Maven基础——自定义Maven骨架步骤、Maven案例_第6张图片
Maven基础——自定义Maven骨架步骤、Maven案例_第7张图片
Maven基础——自定义Maven骨架步骤、Maven案例_第8张图片
这个文件夹目前报错,先不用管.

1.6 将生成的archetype装入本地仓库中

Maven基础——自定义Maven骨架步骤、Maven案例_第9张图片
Maven基础——自定义Maven骨架步骤、Maven案例_第10张图片
装入成功后,发现那个目录就没有报错了, 此时已经将这个项目添加到了本地仓库, 可以在仓库中查看: 我的仓库位置: C:\develop\maven\apache-maven-3.5.2\repository
然后在 repository\com\sunny\maven_gujia-archetype\1.0-SNAPSHOT可以找到
Maven基础——自定义Maven骨架步骤、Maven案例_第11张图片

1.7 将本地仓库中的这个模板骨架添加到maven骨架中

Maven基础——自定义Maven骨架步骤、Maven案例_第12张图片
想要获取这几个空的内容,需要再执行一个命令,这时候选哪个位置都可以
archetype:crawl 这个命令是爬取整个本地仓库
在这里插入图片描述
C:\develop\maven\apache-maven-3.5.2\repository这个目录最后会出现一个archetype-catalog.xml文件
Maven基础——自定义Maven骨架步骤、Maven案例_第13张图片
前三个的参数就是填到刚才的空上即可!
Maven基础——自定义Maven骨架步骤、Maven案例_第14张图片
Maven基础——自定义Maven骨架步骤、Maven案例_第15张图片

1.8 通过自定义的Maven骨架来创建maven项目

Maven基础——自定义Maven骨架步骤、Maven案例_第16张图片
此时就已经完成了!


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

跳转到目录

项目

2.1 需求分析

完成添加客户信息的操作

2.2 创建数据表

Maven基础——自定义Maven骨架步骤、Maven案例_第17张图片

2.3 创建Maven项目

Maven基础——自定义Maven骨架步骤、Maven案例_第18张图片

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.0modelVersion>
  <groupId>cn.itcatgroupId>
  <artifactId>maven_testartifactId>
  <packaging>warpackaging>
  <version>1.0-SNAPSHOTversion>
  <name>maven_test Maven Webappname>
  <url>http://maven.apache.orgurl>


  <dependencies>
   
    <dependency>
      <groupId>mysqlgroupId>
      <artifactId>mysql-connector-javaartifactId>
      <version>5.1.26version>
    dependency>
    
    <dependency>
      <groupId>com.alibabagroupId>
      <artifactId>druidartifactId>
      <version>1.0.9version>
    dependency>
    
    <dependency>
      <groupId>javax.servletgroupId>
      <artifactId>javax.servlet-apiartifactId>
      <version>3.1.0version>
      <scope>providedscope>
    dependency>
    
    <dependency>
      <groupId>javax.servletgroupId>
      <artifactId>jsp-apiartifactId>
      <version>2.0version>
      <scope>providedscope>
    dependency>
    
    <dependency>
      <groupId>javax.servlet.jsp.jstlgroupId>
      <artifactId>jstlartifactId>
      <version>1.2version>
    dependency>
    <dependency>
      <groupId>taglibsgroupId>
      <artifactId>standardartifactId>
      <version>1.1.2version>
    dependency>
    
    <dependency>
      <groupId>commons-beanutilsgroupId>
      <artifactId>commons-beanutilsartifactId>
      <version>1.9.3version>
    dependency>

    
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-jdbcartifactId>
      <version>4.2.4.RELEASEversion>
    dependency>

  dependencies>

  
  <build>
    <plugins>
      
      <plugin>
        <groupId>org.apache.maven.pluginsgroupId>
        <artifactId>maven-compiler-pluginartifactId>
        <version>3.2version>
        <configuration>
          <source>1.7source>
          <target>1.7target>
          <encoding>UTF-8encoding>
        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>

<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>Titletitle>
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 测试

Maven基础——自定义Maven骨架步骤、Maven案例_第19张图片

你可能感兴趣的:(Maven)