跳转到目录
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
将一些常用的包结构,文件等都在这个web项目中创建好.
这一个模板根据自己的项目结构来自定义
将上面的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表达式,及一些其他功能.
首先在 pom.xml 加入插件
org.apache.maven.plugins
maven-archetype-plugin
3.0.0
archetype:create-from-project
这个文件夹目前报错,先不用管.
装入成功后,发现那个目录就没有报错了, 此时已经将这个项目添加到了本地仓库, 可以在仓库中查看: 我的仓库位置: C:\develop\maven\apache-maven-3.5.2\repository
然后在 repository\com\sunny\maven_gujia-archetype\1.0-SNAPSHOT
可以找到
想要获取这几个空的内容,需要再执行一个命令,这时候选哪个位置都可以
archetype:crawl
这个命令是爬取整个本地仓库
在C:\develop\maven\apache-maven-3.5.2\repository
这个目录最后会出现一个archetype-catalog.xml
文件
前三个的参数就是填到刚才的空上即可!
跳转到目录
完成添加客户信息的操作
<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>
<%@ 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>
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;
}
}
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);
}
}
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);
}
}
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);
}
}
}
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();
}
}
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