MyBatis_1
官方文档:http://www.mybatis.org/mybatis-3/zh/index.html
==========================================================
2-3 页面跳转
·JSP页面放入WEB-INF 文件夹下,便于控制和管理(只在后台控制),外部无法直接访问
·在WEB-INF 下通过文件夹合理管理各类文件
WEB-INF/
jsp/
back/
front/
JSP中的根目录:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!--讨论区滚动条begin-->
<link rel="stylesheet" type="text/css" href="<%= basePath %>resources/css/jscrollpane1.css" />
<script src="<%= basePath %>resources/js/common/jquery-1.8.0.min.js" type="text/javascript"></script>
<!-- the mousewheel plugin -->
<script type="text/javascript" src="<%= basePath %>resources/js/common/jquery.mousewheel.js"></script>
<!-- the jScrollPane script -->
<script type="text/javascript" src="<%= basePath %>resources/js/common/jquery.jscrollpane.min.js"></script>
<script type="text/javascript" src="<%= basePath %>resources/js/common/scroll-startstop.events.jquery.js"></script>
<!--讨论区滚动条end-->
<script type="text/javascript" src="<%= basePath %>resources/js/front/talk.js"></script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>微信公众号</title> <!--讨论区滚动条begin--> <link rel="stylesheet" type="text/css" href="<%= basePath %>resources/css/jscrollpane1.css" /> <script src="<%= basePath %>resources/js/common/jquery-1.8.0.min.js" type="text/javascript"></script> <!-- the mousewheel plugin --> <script type="text/javascript" src="<%= basePath %>resources/js/common/jquery.mousewheel.js"></script> <!-- the jScrollPane script --> <script type="text/javascript" src="<%= basePath %>resources/js/common/jquery.jscrollpane.min.js"></script> <script type="text/javascript" src="<%= basePath %>resources/js/common/scroll-startstop.events.jquery.js"></script> <!--讨论区滚动条end--> <script type="text/javascript" src="<%= basePath %>resources/js/front/talk.js"></script> </head> <body> <input type="hidden" value="<%= basePath %>" id="basePath"/> <br/> <div class="talk"> <div class="talk_title"><span>正在与公众号对话</span></div> <div class="talk_record"> <div id="jp-container" class="jp-container"> </div> </div> <div class="talk_word"> <input class="add_face" id="facial" type="button" title="添加表情" value="" /> <input id="content" class="messages emotion" /> <input class="talk_send" onclick="send();" type="button" title="发送" value="发送" /> </div> </div> <div style="text-align:center;margin:50px 0; font:normal 14px/24px 'MicroSoft YaHei';"></div> </body> </html>
--2-4 连接数据库-----------------------------------------
最原始的数据连接方式JDBC
private static final String URL="jdbc:mysql://127.0.0.1:3306/MicroMessage"
useUnicode=true&characterEncoding=utf-8";
private static final String USER="root";
private static final String PASSWORD="root";
private static Connection conn=null;
static {
try {
//1.加载驱动程序
Class.forName("com.mysql.jdbc.Driver");
//2.获得数据库的连接
conn=DriverManager.getConnection(URL, USER, PASSWORD);
//注意不要用*代替所有列,*在编译时会解析成列名降低效率
String sql="select ID,COMMAND,DESCRIPTION,CoNTENT from MESSAGE";
PreparedStatment statment = conn.prepareStatement(sql);
ResultSte rs= statement.executeQuery();
List<Message> messageList = new ArrayList<Message>();
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"));
}
//数据list存入request
req.setAttribute("messageList",messageList);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
--2-5 数据展示-----------------------------------------
IMOOC:JSP常用标签 http://www.imooc.com/learn/451
1 使用JSTL的C标签,做成表单
2 使用JSTL的C标签,设置隔行背景色
<c:forEach items="${messageList}" var="message" varStatus="status">
<tr <c:if test="${status.index % 2 != 0}">style='background-color:#ECF6EE;'</c:if>>
<td><input type="checkbox" name="id" value="${message.id}"/></td>
<td>${status.index + 1}</td>
<td>${message.command}</td>
<td>${message.description}</td>
<td>
<a href="#">修改</a>
<a href="${basePath}DeleteOneServlet.action?id=${message.id}">删除</a>
</td>
</tr>
</c:forEach>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <meta http-equiv="X-UA-Compatible"content="IE=9; IE=8; IE=7; IE=EDGE" /> <title>内容列表页面</title> <link href="<%= basePath %>resources/css/all.css" rel="stylesheet" type="text/css" /> <script src="<%= basePath %>resources/js/common/jquery-1.8.0.min.js"></script> <script src="<%= basePath %>resources/js/back/list.js"></script> </head> <body style="background: #e1e9eb;"> <form action="<%= basePath %>List.action" id="mainForm" method="post"> <input type="hidden" name="currentPage" id="currentPage" value="${page.currentPage}"/> <div class="right"> <div class="current">当前位置:<a href="javascript:void(0)" style="color:#6E6E6E;">内容管理</a> > 内容列表</div> <div class="rightCont"> <p class="g_title fix">内容列表 <a class="btn03" href="#">新 增</a> <a class="btn03" href="javascript:deleteBatch('<%=basePath%>');">删 除</a></p> <table class="tab1"> <tbody> <tr> <td width="90" align="right">指令名称:</td> <td> <input name="command" type="text" class="allInput" value="${command}"/> </td> <td width="90" align="right">描述:</td> <td> <input name="description" type="text" class="allInput" value="${description}"/> </td> <td width="85" align="right"><input type="submit" class="tabSub" value="查 询" /></td> </tr> </tbody> </table> <div class="zixun fix"> <table class="tab2" width="100%"> <tbody> <tr> <th><input type="checkbox" id="all" onclick="#"/></th> <th>序号</th> <th>指令名称</th> <th>描述</th> <th>操作</th> </tr> <c:forEach items="${messageList}" var="message" varStatus="status"> <tr <c:if test="${status.index % 2 != 0}">style='background-color:#ECF6EE;'</c:if>> <td><input type="checkbox" name="id" value="${message.id}"/></td> <td>${status.index + 1}</td> <td>${message.command}</td> <td>${message.description}</td> <td> <a href="#">修改</a> <a href="${basePath}DeleteOneServlet.action?id=${message.id}">删除</a> </td> </tr> </c:forEach> </tbody> </table> <div class='page fix'> 共 <b>${page.totalNumber}</b> 条 <c:if test="${page.currentPage != 1}"> <a href="javascript:changeCurrentPage('1')" class='first'>首页</a> <a href="javascript:changeCurrentPage('${page.currentPage-1}')" class='pre'>上一页</a> </c:if> 当前第<span>${page.currentPage}/${page.totalPage}</span>页 <c:if test="${page.currentPage != page.totalPage}"> <a href="javascript:changeCurrentPage('${page.currentPage+1}')" class='next'>下一页</a> <a href="javascript:changeCurrentPage('${page.totalPage}')" class='last'>末页</a> </c:if> 跳至 <input id="currentPageText" type='text' value='${page.currentPage}' class='allInput w28' /> 页 <a href="javascript:changeCurrentPage($('#currentPageText').val())" class='go'>GO</a> </div> </div> </div> </div> </form> </body> </html>
--2-6 列表查询-----------------------------------------
<input name="command" type="text" class="allInput" value="${command}"/>//查询值保留
<tr> <td width="90" align="right">指令名称:</td> <td> <input name="command" type="text" class="allInput" value="${command}"/> </td> <td width="90" align="right">描述:</td> <td> <input name="description" type="text" class="allInput" value="${description}"/> </td> <td width="85" align="right"><input type="submit" class="tabSub" value="查 询" /> </td> </tr>
代码添加:
private static final String URL="jdbc:mysql://127.0.0.1:3306/MicroMessage"
useUnicode=true&characterEncoding=utf-8";
private static final String USER="root";
private static final String PASSWORD="root";
private static Connection conn=null;
static {
try {
//1.加载驱动程序
Class.forName("com.mysql.jdbc.Driver");
//2.获得数据库的连接
conn=DriverManager.getConnection(URL, USER, PASSWORD);
//注意不要用*代替所有列,*在编译时会解析成列名降低效率
req.setCharacterEncoding("UTF-8");
String command = request.getParameter("command");
String description= request.getParameter("description");
req.setAttribute("description",description);
//String sql="select ID,COMMAND,DESCRIPTION,CoNTENT from MESSAGE";
//不是多线程操作同一变量时,可用StringBulider
StringBulider sql = new StringBulider("select ID,COMMAND,DESCRIPTION,CoNTENT from MESSAGE where 1=1");
List<String> paramList = new Array<String>();
//command.trim()
if(command !=null && !"".equals(command.trim())){
sql.append("and COMMAND=?");
paramList.add(command);
}
if(description!=null && !"".equals(description.trim())){
sql.append("and DESCRIPTION like '%' ? '%'");//MySql '%用空格拼
paramList.add(description);
}
PreparedStatment statment = conn.prepareStatement(sql);
for(int i=0;i<paramList.size();i++){
statment.setString(i+1,paramList.get(i));
}
ResultSte rs= statement.executeQuery();
List<Message> messageList = new ArrayList<Message>();
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"));
}
//数据list存入request
req.setAttribute("messageList",messageList);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
--2-7 代码重构-----------------------------------------
servlet:
public class ListServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //设置编码 req.setCharacterEncoding("UTF-8"); //接受页面传值 String command = request.getParameter("command"); String description= request.getParameter("description"); req.setAttribute("description",description); ListService listService =new ListSwrvice(); //查询消息列表数据list存入request req.setAttribute("messageList",listService .queryMessageList(command,description)); // 向页面跳转 req.getRequestDispatcher("/WEB-INF/jsp/back/list.jsp").forward(req, resp); }
service
public class QueryService { public List<Message> queryMessageList(String command,String description) { MessageDao messageDao = new MessageDao(); return messageDao.queryMessageList(command, description); } }
dao:
public class MessageDao { /** * 根据查询条件查询消息列表 */ public List<Message> queryMessageList(String command,String description) { try { List<Message> messageList = new ArrayList<Message>(); private static final String URL="jdbc:mysql://127.0.0.1:3306/MicroMessage" useUnicode=true&characterEncoding=utf-8"; private static final String USER="root"; private static final String PASSWORD="root"; private static Connection conn=null; //1.加载驱动程序 Class.forName("com.mysql.jdbc.Driver"); //2.获得数据库的连接 conn=DriverManager.getConnection(URL, USER, PASSWORD); //注意不要用*代替所有列,*在编译时会解析成列名降低效率 //String sql="select ID,COMMAND,DESCRIPTION,CoNTENT from MESSAGE"; //不是多线程操作同一变量时,可用StringBulider StringBulider sql = new StringBulider("select ID,COMMAND,DESCRIPTION,CoNTENT from MESSAGE where 1=1"); List<String> paramList = new Array<String>(); //command.trim() if(command !=null && !"".equals(command.trim())){ sql.append("and COMMAND=?"); paramList.add(command); } if(description!=null && !"".equals(description.trim())){ sql.append("and DESCRIPTION like '%' ? '%'");//MySql '%用空格拼 paramList.add(description); } PreparedStatment statment = conn.prepareStatement(sql); for(int i=0;i<paramList.size();i++){ statment.setString(i+1,paramList.get(i)); } ResultSte rs= statement.executeQuery(); 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")); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return messageList; }
--2-3 页面跳转-----------------------------------------
--2-3 页面跳转-----------------------------------------
--2-3 页面跳转-----------------------------------------
--2-3 页面跳转-----------------------------------------