环境:IntelliJ IDEA 2017.1
web-application是主工程,里面包含四个模块(Module):
- web-app是应用层,用于界面展示,依赖于web-service层的服务。
- web-service是服务层,用于给app层提供服务。
- web-dao是数据访问层,用于给service层提供服务。
- 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的依赖:
com.study.demo web-service 1.0-SNAPSHOT 以上,项目依赖的添加已经完成,其他模块依赖配置同样的配置方式。
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&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&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.
*
* 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("");
out.println("");
out.println(" A Servlet ");
out.println(" ");
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(" ");
out.println("");
out.flush();
out.close();
}
/**
* The doPost method of the servlet.
*
* 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("");
out.println("");
out.println(" A Servlet ");
out.println(" ");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" ");
out.println("");
out.flush();
out.close();
}
}
web.xml
Archetype Created Web Application
ServletDemo
com.dk.controller.ServletDemo
ServletDemo
/ServletDemo
index.jsp
<%@ page import="com.dk.service.UserService" %>
Hello World!
<%UserService service = new UserService();%>
<%=service.getUserName()%>
在浏览器中输入:http://localhost:8080/ServletDemo
得到运行结果: