我之前一直搞不明白JAVABEAN在MVC中到底充當一個什麼角色。現在來看一個實例
定義一個JAVABEAN
package com.bean;
public class User {
private String userName;
private String userPwd;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPwd() {
return userPwd;
}
public void setUserPwd(String userPwd) {
this.userPwd = userPwd;
}
}
JAVABEAN的作用:是一個PO對象之前我以為是VO對象。搞錯了!
恩我們可以這樣來理解JAVABEAN的作用其實就是用來傳遞數據的一個東西。在我們這次PHPCMS項目中相當於一個結果集的傳輸。控制器接收到一個來自M層的數據之後如何傳遞到視圖層作渲染處理。可以依據這樣的方式。
再來看我們的Action類
package com.dao;
import java.sql.*;
import com.bean.User;
public class DAO {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
PreparedStatement pstmt = null
public DAO(){
try {
Class.forName("com.mysql.jdbc.Driver");
conn = getConnection();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Connection getConnection(){
String url = "jdbc:mysql://localhost:3306/person";
String user = "root";
String password = "123456";
try {
conn = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public int addPerson(User user){
int flag = 0;
String sql = "insert into user(user_name,user_pwd) values(?,?)";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, user.getUserName());
pstmt.setString(2, user.getUserPwd());
flag = pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return flag;
}
public ResultSet showPerson(){
String sql = "select * from user";
try {
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
public void close(){
try {
if(rs!=null)rs.close();
if(pstmt!=null)pstmt.close();
if(conn!=null)conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
這個類把數據庫連接操作也放在這裡了。其實是不正確的處理方式。正確的方式應當是將數據庫層單獨寫到一個CLASS類裡面去。但這裡面我們來看一下:
public int addPerson(User user){
int flag = 0;
String sql = "insert into user(user_name,user_pwd) values(?,?)";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, user.getUserName());
pstmt.setString(2, user.getUserPwd());
flag = pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return flag;
}
這個DAO對象它的作用是接收來自JAVABEAN層的數據。
再看是如何SET值到這個BEAN的
package com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.bean.User;
import com.dao.DAO;
public class AddUser extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String userName = request.getParameter("userName");
String userPwd = request.getParameter("userPwd");
User user = new User();
user.setUserName(userName);//依據的是SET與GET方式了吧!!!!
user.setUserPwd(userPwd);
DAO dao = new DAO();