myeclipse+mybatisgeneratorgui0.6.1快速搭建ssm框架並且實現登錄

NO IMAGE

前言:在校學的東西做下筆記!想學習的小夥伴們可以找我要下配置文件及所需的jar包和mybatis-generator-gui-0.6.1

1. 先在myeclipse創建一個Javaweb項目,創建項目時JavaEE Sepcification level處選擇JavaEE6.0
2. 在項目右鍵選擇導入spring的jar包,選擇前4個,在JAR Library Installation選第2個點擊finish完成

myeclipse+mybatisgeneratorgui0.6.1快速搭建ssm框架並且實現登錄

[圖片上傳失敗…(image-f975d5-1513065767184)]
3. 在lib添加mybatis的jar及mysql驅動jar包
[圖片上傳失敗…(image-3d0202-1513065767184)]
4. 添加一些配置文件
[圖片上傳失敗…(image-f15030-1513065767184)]
5. 分層建包(可以按自己的習慣來建)

myeclipse+mybatisgeneratorgui0.6.1快速搭建ssm框架並且實現登錄

6. 修改配置文件
(1) 修改applicationContext.xml,將下面代碼替換applicationContext.xml裡原有的代碼

<?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文件

myeclipse+mybatisgeneratorgui0.6.1快速搭建ssm框架並且實現登錄

(4)修改spring-mybatis.xml文件

myeclipse+mybatisgeneratorgui0.6.1快速搭建ssm框架並且實現登錄

(5)修改web.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的部門表:

myeclipse+mybatisgeneratorgui0.6.1快速搭建ssm框架並且實現登錄
myeclipse+mybatisgeneratorgui0.6.1快速搭建ssm框架並且實現登錄
myeclipse+mybatisgeneratorgui0.6.1快速搭建ssm框架並且實現登錄

9.將生成的文件放到項目對應的包下

myeclipse+mybatisgeneratorgui0.6.1快速搭建ssm框架並且實現登錄

10.在dao層的TbEmpMapper.java添加登錄要用到的方法

myeclipse+mybatisgeneratorgui0.6.1快速搭建ssm框架並且實現登錄

11.在mapper層的TbEmpMapper.xml添加上面方法的查詢語句,在底部</mapper>

myeclipse+mybatisgeneratorgui0.6.1快速搭建ssm框架並且實現登錄

12.在service層建一個EmpService接口

myeclipse+mybatisgeneratorgui0.6.1快速搭建ssm框架並且實現登錄

13.在service層下再建一個impl包(用來實現EmpService接口),在impl包下創建 EmpServiceImpl類實現接口

myeclipse+mybatisgeneratorgui0.6.1快速搭建ssm框架並且實現登錄

14.在web層(controller層)下創建Login類,實現登錄功能
[圖片上傳失敗…(image-39f84c-1513065767184)]
15.在index.jsp寫頁面代碼

<%@ 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.運行後的效果

myeclipse+mybatisgeneratorgui0.6.1快速搭建ssm框架並且實現登錄

myeclipse+mybatisgeneratorgui0.6.1快速搭建ssm框架並且實現登錄

相關文章

@XmlRootElement的配置

Vue安裝antdesignvue和ElementUI

Java時間戳與時間的相互轉換(只是為了記住才寫,望原諒)

SpringJpa分頁