前言:在校學的東西做下筆記!想學習的小夥伴們可以找我要下配置檔案及所需的jar包和mybatis-generator-gui-0.6.1
1. 先在myeclipse建立一個Javaweb專案,建立專案時JavaEE Sepcification level處選擇JavaEE6.0 2. 在專案右鍵選擇匯入spring的jar包,選擇前4個,在JAR Library Installation選第2個點選finish完成


<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<!-- 開啟註解 -->
<context:annotation-config />
<!-- 自動掃描(service)-->
<context:component-scan base-package="com.gx.service">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Service" />
<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository" />
</context:component-scan>
</beans>
複製程式碼
注意:base-package="com.gx.service"的包名一定要對應你建立的包名否則會報錯或導致你查不出資料 (2) 修改jdbc.properties檔案 [圖片上傳失敗...(image-d05a42-1513065767184)] (3)修改spring-mvc.xml檔案


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml,classpath:spring-mybatis.xml</param-value>
</context-param>
<listener>
<description>spring監聽器</description>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<description>spring mvc servlet</description>
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<description>spring mvc 配置檔案</description>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<init-param>
<param-name>activeReverseAjaxEnabled</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
複製程式碼
7.把專案部署到tomcat上執行,如果沒報錯就說明框架搭建成功,報錯的話請仔細檢查上面步驟是否有遺漏
8.開啟mybatis-generator-gui-0.6.1建立po、mapper、dao所需檔案(我以部門表為例) MySQL的部門表:





</mapper>
內



<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="ctx" value="${pageContext.request.contextPath }"></c:set>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h2>登入</h2>
<form action="${ctx}/Login/gologin.do" method="post">
<label>使用者名稱:</label><input type="text" name="ename">
<label>密碼:</label><input type="text" name="password">
<input type="submit" value="登入">
</form>
</body>
</html>
複製程式碼
16.在WebRoot下建立loginsuccess.jsp檔案並寫好登入後的程式碼
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'loginsuccess.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<div style="text-align: center;width:100%;"><input id="aaa" autofocus="autofocus" onkeydown=" if(event.keyCode == 13) {likeSelect(this.value)};" type="text" placeholder="請輸入">
<table style="text-align: center;">
<tr style="background: #c37e00;color:white">
<td width="200" >序號</td>
<td width="200">姓名</td>
<td width="200">年齡</td>
<td width="200">密碼</td>
<td width="200">入職日期</td>
<td width="200">性別</td>
<td width="200">部門id</td>
<td width="200">新增</td>
<td width="200">修改</td>
<td width="200">刪除</td>
</tr >
<c:forEach items="${emplist}" var="emp" varStatus="status">
<tr style="background:#d3d7d4">
<td>${status.index+1}</td>
<td>${emp.ename}</td>
<td >${emp.age}</td>
<td>${emp .password}</td>
<td>
<fmt:formatDate value="${emp.workdate }" pattern="yyyy-MM-dd" />
</td>
<td>${emp.gender }</td>
<td>${emp.deptid }</td>
<td><a href="add.jsp">新增</a></td>
<td ><a href="Login/goUpdate.do?eid=${emp.eid}">修改</a></td>
<td><a href="Login/delete.do?eid=${emp.eid}">刪除</a></td>
</tr>
</c:forEach>
</table>
</div>
</body>
</html>
複製程式碼
17.執行後的效果

