前言:在校学的东西做下笔记!想学习的小伙伴们可以找我要下配置文件及所需的jar包和mybatis-generator-gui-0.6.1
1. 先在myeclipse创建一个Javaweb项目,创建项目时JavaEE Sepcification level处选择JavaEE6.0
2. 在项目右键选择导入spring的jar包,选择前4个,在JAR Library Installation选第2个点击finish完成
QQ截图20171211112608.png
3. 在lib添加mybatis的jar及mysql驱动jar包
4. 添加一些配置文件
5. 分层建包(可以按自己的习惯来建)
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文件
(3)修改spring-mvc.xml文件
这里写图片描述
(4)修改spring-mybatis.xml文件
这里写图片描述
(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的部门表:
这里写图片描述
这里写图片描述
9.将生成的文件放到项目对应的包下
这里写图片描述
10.在dao层的TbEmpMapper.java添加登录要用到的方法
这里写图片描述
11.在mapper层的TbEmpMapper.xml添加上面方法的查询语句,在底部
</mapper>
内这里写图片描述
12.在service层建一个EmpService接口
这里写图片描述
13.在service层下再建一个impl包(用来实现EmpService接口),在impl包下创建 EmpServiceImpl类实现接口
这里写图片描述
14.在web层(controller层)下创建Login类,实现登录功能
(按步骤来是不会出现下面报错的!)
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.运行后的效果
网友评论