在这里不插入JSP的内容,仅仅只写出dao、servlet、service、bean的代码,用以记录典型的分层思想
dao层在没有使用mybatis等框架的时候,完成注册、获取连接、执行sql并返回结果
service则对应一个servlet,完成调用dao的功能,返回dao层输入的结果
servlet则完成获取jsp页面信息,并反馈结果到页面中
bean则是对结果的封装(实质将已赋值的bean存在list中,然后返回list到jsp页面中)
这是servlet的核心代码
从页面中得到两个参数(command,description)的值,经过处理后返回一个list,最后完成跳转
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("UTF-8"); //或者这里写成过滤器
String command = request.getParameter("command");
String description = request.getParameter("description");
//这两个参数从jsp中获取,当其中输入了值,就做sql拼接,没有值或者是空字符串则不进行拼接
request.setAttribute("command", command);
request.setAttribute("description", description); //这里,让查询框中输入的内容作为保留。对应的,需要改jsp中标签value属性的内容 value="${command}"
ListService listService = new ListService();
request.setAttribute("messageList", listService.queryMessageList(command, description));
request.getRequestDispatcher("/WEB-INF/lib/jsp/list.jsp").forward(request, response);
}
以下是封装的bean
public class Message {
private String id;
private String command;
private String description;
private String content;
public String getId() {
return id;
}
public String getCommand() {
return command;
}
public String getDescription() {
return description;
}
public String getContent() {
return content;
}
public void setId(String id) {
this.id = id;
}
public void setCommand(String command) {
this.command = command;
}
public void setDescription(String description) {
this.description = description;
}
public void setContent(String content) {
this.content = content;
}
}
DAO层的内容
Dao层的内容
public List<Message> queryMessageList(String command, String description){
List<Message> messageList = new ArrayList<Message>();
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/micro_message", "root", "root"); //需要改一下
//String sql = "select ID, COMMAND, DESCRIPTION, CONTENT from MESSAGE";
//这里,直接写列名,不要用*号-----因为会先解析*再执行sql
StringBuilder sql = new StringBuilder("select ID, COMMAND, DESCRIPTION, CONTENT from MESSAGE where 1=1");
List<String> paramList = new ArrayList<String>(); //在这里添加一个list作为缓冲,解决参数问题
if (command != null && !"".equals(command.trim())){
sql.append(" and COMMAND=?");
paramList.add(command);
}
if (description != null && !"".equals(description.trim())){
sql.append(" and DESCRIPTION like '%' ? '%' ");
paramList.add(description);
}
PreparedStatement statement = conn.prepareStatement(sql.toString());
for (int i=0;i<paramList.size();i++){
statement.setString(i+1, paramList.get(i));
}
//检索条件不固定的情况下,怎么处理?? ---- sql语句拼接,有条件就拼接,没有条件不拼接
ResultSet rs = statement.executeQuery();
// List<Message> messageList = new ArrayList<Message>();
//Message是一个bean,泛型....
while (rs.next()){
Message message = new Message();
messageList.add(message);
message.setId(rs.getString("ID"));
message.setCommand(rs.getString("COMMAND"));
message.setDescription(rs.getString("DESCRIPTION"));
message.setContent(rs.getString("CONTENT"));
}
}catch (ClassNotFoundException e){
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return messageList;
}
}
service层的内容
/**
*
* @author Administrator
* 列表相关的业务功能
*/
public class ListService {
/**
* service是与servlet对应的
*/
public List<Message> queryMessageList(String command, String description){
MessageDao messageDao = new MessageDao();
List<Message> list = new ArrayList<Message>();
list = messageDao.queryMessageList(command, description);
return list;
}
}