无框架,简单maven webapp骨架跑一个项目 (快速回忆基本使用及其流程用)

无框架,简单maven webapp骨架跑一个项目 (快速回忆基本使用及其流程用),第1张

无框架,简单maven webapp骨架跑一个项目 (快速回忆基本使用及其流程用)

文章目录
  • 0、先建数据库
  • 一、建立IDEA基本环境
      • 1.1 新建module
      • 1.2 选择maven->webapp骨架
      • 1.3 改名称和包名
      • 1.4 第一次要修改maven仓库配置
      • 1.5 finish,项目建立完毕
  • 二、新建包结构
      • 2.1 main目录下右键新建java包和resource包
  • 三、两个核心文件
    • 3.1、 web.xml 修改如下
    • 3.2、resource下新建 druid.properties
  • 四、pom文件下各种依赖,仓库里,没有就写一个刷一下
    • 4.1 编译版本1.7修改为1.8
    • 4.2 各种依赖
  • 五、先写前端,看能否执行
      • 5.1 index.jsp
      • 5.2 form.jsp 引入jstl
      • 5.3 最终跳转页面
  • 六、部署项目,第一次测试运行
      • 6.1 选jdk、tomcat,端口等
      • 6.2 选择部署哪个项目
  • 七、庞大的后端代码
      • 7.0 Customer.java
      • 7.1 JdbcUtils.java
      • 7.2 CustomerDao.java
      • 7.3 CustomerDaoImpl.java
      • 7.4 CustomerService.java
      • 7.5 CustomerServiceImpl.java
      • 7.6 SaveCustomerServlet.java
  • 八、 最终运行,添加数据到数据库

0、先建数据库
mysql -uroot -proot
create database testdb1;
use testdb1

create table cst_customer(
	cust_id int primary key auto_increment,
	cust_name varchar(20),
	cust_source varchar(20),
	cust_industry varchar(20),
	cust_level varchar(20),
	cust_phone varchar(20),
	cust_mobile varchar(20)
);

desc cst_customer;
select * from cst_customer;

一、建立IDEA基本环境 1.1 新建module

1.2 选择maven->webapp骨架

1.3 改名称和包名

1.4 第一次要修改maven仓库配置

1.5 finish,项目建立完毕

二、新建包结构 2.1 main目录下右键新建java包和resource包


就这两个,直接建立

再在java里新建 cn.ahpu 等各种package

三、两个核心文件 3.1、 web.xml 修改如下


web.xml:







3.2、resource下新建 druid.properties

druid.properties 注意修改密码和数据库名

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/testdb1
username=root
password=root
initialSize=5
maxActive=10
maxWait=3000
maxIdle=6
minIdle=3
四、pom文件下各种依赖,仓库里,没有就写一个刷一下 4.1 编译版本1.7修改为1.8


修改为:

4.2 各种依赖
 
      mysql
      mysql-connector-java
      5.1.26
    
    
      com.alibaba
      druid
      1.0.9
    
    
      javax.servlet
      javax.servlet-api
      3.1.0
    
    
      javax.servlet
      jsp-api
      2.0
    
    
      javax.servlet.jsp.jstl
      jstl
      1.2
    
    
      taglibs
      standard
      1.1.2
    
    
      commons-beanutils
      commons-beanutils
      1.9.3
    
    
      org.springframework
      spring-jdbc
      4.2.4.RELEASE
    
    
      org.projectlombok
      lombok
      1.14.8
    

lombok失效需要设置里打一个对勾

五、先写前端,看能否执行

5.1 index.jsp


Hello World!
<% System.out.printf("index.jsp执行了 n"+request.getContextPath()+"/form.jsp");
    response.sendRedirect(request.getContextPath()+"/form.jsp");
%>



5.2 form.jsp 引入jstl
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


    Title


     hello "${pageContext.request.contextPath}/SaveCustomer" 此行测试用
    


5.3 最终跳转页面

success.jsp

<%--
  Created by IntelliJ IDEA.
  User: hanzhuan
  Date: 2021/12/31
  Time: 1:25
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title


save success!!!



六、部署项目,第一次测试运行


6.1 选jdk、tomcat,端口等

6.2 选择部署哪个项目


选短的那个

运行成功

七、庞大的后端代码

7.0 Customer.java
package cn.ahpu.domain;

import lombok.Data;

@Data
public class Customer {
    private Long custId;
    private String custName;
    private String custSource;
    private String custLevel;
    private String custIndustry;
    private String custAddress;
    private String custPhone;
}

7.1 JdbcUtils.java
package cn.ahpu.utils;


import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class JdbcUtils {
    private static DataSource ds;

    static {
        InputStream is = JdbcUtils.class.getResourceAsStream("/druid.properties");
        Properties pp = new Properties();
        try {
            pp.load(is);
            ds = DruidDataSourceFactory.createDataSource(pp);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static DataSource getDataSource() {
        return ds;
    }
    // 5.定义关闭资源的方法
    public static void close(Connection conn, Statement stmt, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {}
        }

        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {}
        }

        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {}
        }
    }

    // 6.重载关闭方法
    public static void close(Connection conn, Statement stmt) {
        close(conn, stmt, null);
    }


}

7.2 CustomerDao.java
package cn.ahpu.dao;

import cn.ahpu.domain.Customer;

public interface CustomerDao {
    void save(Customer customer);
}
7.3 CustomerDaoImpl.java
package cn.ahpu.dao.impl;

import cn.ahpu.dao.CustomerDao;
import cn.ahpu.domain.Customer;
import cn.ahpu.utils.JdbcUtils;
import org.springframework.jdbc.core.JdbcTemplate;

public class CustomerDaoImpl implements CustomerDao {

    JdbcTemplate jdbcTemplate=new JdbcTemplate(JdbcUtils.getDataSource());

    @Override
    public void save(Customer customer) {
        String sql = "insert into cst_customer(cust_name,cust_source,cust_industry,cust_level,cust_phone)  " +
                "values(?,?,?,?,?)";
        jdbcTemplate.update(
                sql,
                customer.getCustName(),
                customer.getCustSource(),
                customer.getCustIndustry(),
                customer.getCustLevel(),
                customer.getCustPhone());
    }
}

7.4 CustomerService.java
package cn.ahpu.service;

import cn.ahpu.domain.Customer;

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

7.5 CustomerServiceImpl.java
package cn.ahpu.service.impl;

import cn.ahpu.dao.CustomerDao;
import cn.ahpu.dao.impl.CustomerDaoImpl;
import cn.ahpu.domain.Customer;
import cn.ahpu.service.CustomerService;

public class CustomerServiceImpl implements CustomerService {

    private CustomerDao customerDao = new CustomerDaoImpl();

    @Override
    public void save(Customer customer) {
        customerDao.save(customer);
    }
}

7.6 SaveCustomerServlet.java
package cn.ahpu.web;

import cn.ahpu.domain.Customer;
import cn.ahpu.service.CustomerService;
import cn.ahpu.service.impl.CustomerServiceImpl;

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;

@WebServlet("/SaveCustomer")
public class SaveCustomerServlet extends HttpServlet {
    private CustomerService customerService=new CustomerServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println(" hello 执行了! hello!");

        //接收参数
        req.setCharacterEncoding("utf-8");
        String custName = req.getParameter("custName");
        String custSource = req.getParameter("custSource");
        String custLevel = req.getParameter("custLevel");
        String custIndustry = req.getParameter("custIndustry");
        String custPhone = req.getParameter("custPhone");

        //封装数据
        Customer customer = new Customer();
        customer.setCustName(custName);
        customer.setCustIndustry(custIndustry);
        customer.setCustPhone(custPhone);
        customer.setCustLevel(custLevel);
        customer.setCustSource(custSource);

        //调用业务
        customerService.save(customer);

        //重定向
        resp.sendRedirect(req.getContextPath()+"/success.jsp");

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}

八、 最终运行,添加数据到数据库



查询添加结果

完美回忆完毕

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5687638.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存