开发环境搭建系列—IDEA多模块项目搭建—001

开发环境搭建

环境:IntelliJ IDEA 2017.1

 项目结构

  web-application是主工程,里面包含四个模块(Module):

  1. web-app是应用层,用于界面展示,依赖于web-service层的服务。
  2. web-service是服务层,用于给app层提供服务。
  3. web-dao是数据访问层,用于给service层提供服务。
  4. web-entity是数据模型层,用于给dao层提供服务。

多模块工程适用于大型模块化项目,本案例只做演示:

创建父工程

新建一个空白标准maven project(不要选择Create from archetype选项)

得到一个标准的maven项目,因为该项目是作为一个父工程存在,用于组织其下各模块工程,可以直接删除src文件夹。

 增加web-app模块(Module)

选择父工程——》new——》Module

选择从archetype创建(选择webapp选项)

groupId和version继承自Parent project,这里只需要填写artifactId即可。

增加web-service模块

用同样的方法创建web-service模块(不过该模块是一个空白maven标准项目,不要从archetype创建)

 参照web-service模块的创建方法创建web-dao模块、web-entity模块。

最终的项目结构

添加项目依赖

上面的操作是添加web-app对web-service模块的依赖,完成上述操作后web-Service中public的类已经在web-app模块中可见了。但是这个时候在app模块使用了service模块中的类,通过maven进行编译(compile)的时候还是会报错XX找不到(XX为service模块的类),要解决这个问题需要在app的pom中增加对service的依赖:

<dependency>
    <groupId>com.study.demo</groupId>
    <artifactId>web-service</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

以上,项目依赖的添加已经完成,其他模块依赖配置同样的配置方式。

开始编程 

web-entity模块编程

package com.dk.entity;

/**
 * @program: web-application
 * @description:
 * @author: Alexander
 * @create: 2018-12-23 21:10
 **/
public class User {
    public int user_id;
    public String user_name;
    public String password;
    public String phone;

    public int getUser_id(int user_id) {
        return this.user_id;
    }

    public void setUser_id(int user_id) {
        this.user_id = user_id;
    }

    public String getUser_name() {
        return user_name;
    }

    public void setUser_name(String user_name) {
        this.user_name = user_name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "User{" +
                "user_id=" + user_id +
                ", user_name='" + user_name + '\'' +
                ", password='" + password + '\'' +
                ", phone='" + phone + '\'' +
                '}';
    }

}

web-dao模块编程

package com.dk.dao;

import com.dk.entity.User;

import java.sql.*;

/**
 * @program: web-application
 * @description:
 * @author: Alexander
 * @create: 2018-12-23 21:12
 **/
public class UserDao {

    public static Connection getConnection() throws ClassNotFoundException, SQLException, SQLException {
        String URL="jdbc:mysql://127.0.0.1:3306/mytest?useUnicode=true&amp;characterEncoding=utf-8";
        String USER="root";
        String PASSWORD="1234";
        //1.加载驱动程序
        Class.forName("com.mysql.jdbc.Driver");
        //2.获得数据库链接
        Connection conn= DriverManager.getConnection(URL, USER, PASSWORD);
        return conn;
    }

    public static User getUser() throws Exception {
        String URL="jdbc:mysql://127.0.0.1:3306/mytest?useUnicode=true&amp;characterEncoding=utf-8";
        String USER="root";
        String PASSWORD="1234";
        //1.加载驱动程序
        Class.forName("com.mysql.jdbc.Driver");
        //2.获得数据库链接
        Connection conn=DriverManager.getConnection(URL, USER, PASSWORD);
        //3.通过数据库的连接操作数据库,实现增删改查(使用Statement类)
        Statement st=conn.createStatement();
        ResultSet rs=st.executeQuery("select * from t_user");
        //4.处理数据库的返回结果(使用ResultSet类)
        if(rs.next()){
            User user = new User();
            user.getUser_id(rs.getInt("user_id"));
            user.setUser_name(rs.getString("user_name"));
            user.setPassword(rs.getString("password"));
            user.setPhone(rs.getString("phone"));
            return user;
        }
        return null;
    }

    public static void main(String[] args) throws Exception {
        System.out.println(getUser());
    }
}

web-service模块编程

package com.dk.service;

import com.dk.dao.UserDao;
import com.dk.entity.User;

import java.sql.Connection;

/**
 * @program: web-application
 * @description:
 * @author: Alexander
 * @create: 2018-12-23 21:18
 **/
public class UserService {

    public static User getUserName() throws Exception {
        UserDao dao = new UserDao();
        return dao.getUser();
    }

    public static Connection getDataConnection() throws Exception {
        UserDao dao = new UserDao();
        return dao.getConnection();
    }

    public static void main(String[] args) throws Exception {
        UserService service = new UserService();
        System.out.println(service.getUserName());
        System.out.println(getDataConnection());

        UserDao dao = new UserDao();


    }
}

web-app模块编程

package com.dk.controller;

import com.dk.entity.User;
import com.dk.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

/**
 * @program: web-application
 * @description:
 * @author: Alexander
 * @create: 2018-12-24 23:29
 **/
public class ServletDemo extends HttpServlet {

    /**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     *
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
        out.println("<HTML>");
        out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
        out.println("  <BODY>");
        out.print("    This is ");
        out.print(this.getClass());
        out.println(", using the GET method");
        try {
            UserService service = new UserService();
            User userName = service.getUserName();
            out.println(userName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        out.println("  </BODY>");
        out.println("</HTML>");
        out.flush();
        out.close();
    }

    /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     *
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
        out.println("<HTML>");
        out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
        out.println("  <BODY>");
        out.print("    This is ");
        out.print(this.getClass());
        out.println(", using the POST method");
        out.println("  </BODY>");
        out.println("</HTML>");
        out.flush();
        out.close();
    }

}

web-目录结构: 

web.xml 

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>

  <servlet>
    <servlet-name>ServletDemo</servlet-name>
    <servlet-class>com.dk.controller.ServletDemo</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ServletDemo</servlet-name>
    <url-pattern>/ServletDemo</url-pattern>
  </servlet-mapping>
</web-app>

index.jsp 

<%@ page import="com.dk.service.UserService" %>
<html>
<body>
<h2>Hello World!</h2>
<%UserService service = new UserService();%>
<%=service.getUserName()%>
</body>
</html>

运行 -配置Tomcat

在浏览器中输入:http://localhost:8080/ServletDemo

得到运行结果:

发布了234 篇原创文章 · 获赞 12 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/Coder_Boy_/article/details/85255740