1、导入包

2、创建一个请求文件发送请求

 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/student/login.html" method="post">
用户名:<input type="text" name="studentName" /> <br />
密码:<input type="text" name="studentPwd" /> <br />
<input type="submit" value="登录">
</form>
</body>
</html>

3、① 创建 web.xml 文件(核心控制器,拦截所有的请求)

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>springmvc-mybatis-001</display-name>
<!-- 配置过滤器 -->
<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>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置核心控制器 -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 加载spring配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-*.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

② 创建扫描组件的文件

 <?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <context:component-scan base-package="cn.mgy" /> </beans>

4、创建业务层处理请求

① 创建一个StudentController

 package cn.mgy.controller;

 import javax.servlet.http.HttpServletRequest;

 import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import cn.mgy.mapper.StudentMapper;
import cn.mgy.pojo.Student; @Controller
@RequestMapping("/student")
public class StudentController { @RequestMapping("/login")
public String login(Student student,HttpServletRequest req) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-*.xml");
StudentMapper studentMapper = context.getBean(StudentMapper.class);
Student student2 = studentMapper.findByStudent(student.getStudentName(), student.getStudentPwd());
req.setAttribute("student", student2);
context.close();
return "/view/login.jsp";
}
}

② 创建一个Student实体类

 package cn.mgy.pojo;

 import java.io.Serializable;
import java.util.Date; public class Student implements Serializable { private static final long serialVersionUID = 7276888295138830869L;
private Long studentId;
private String studentName;
private String studentPwd;
private Integer studentStatus;
private Date createDate;
private String studentAccount;
public Long getStudentId() {
return studentId;
}
public void setStudentId(Long studentId) {
this.studentId = studentId;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getStudentPwd() {
return studentPwd;
}
public void setStudentPwd(String studentPwd) {
this.studentPwd = studentPwd;
}
public Integer getStudentStatus() {
return studentStatus;
}
public void setStudentStatus(Integer studentStatus) {
this.studentStatus = studentStatus;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
public String getStudentAccount() {
return studentAccount;
}
public void setStudentAccount(String studentAccount) {
this.studentAccount = studentAccount;
}
}

5、配置数据源(整合Mybatis配置)

分三步:① 创建会话工厂;② 创建一个扫描器,将操作接口的操作对象扫描到Spring容器;③ 配置事务代理

 <?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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> <!-- 配置连接池 -->
<bean name="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<!-- 四要素:-->
<!-- 驱动 -->
<property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>
<!-- 连接字符串 -->
<property name="url" value="jdbc:mysql://localhost:3306/sms?useSSL=true"/>
<!-- 用户名 -->
<property name="username" value="root"/>
<!-- 密码 -->
<property name="password" value="root"/>
</bean> <!--
如何让 Mybatis 使用 Spring 的连接池?
Mybatis 整合包的实现方式,是让 Spring 代理 Mybatis 创建会话工厂,然后实现一个扫描器,
将 Mybatis 的操作接口加到 Spring 容器里面
-->
<!-- 1. 创建 Spring 代理的会话工厂 -->
<bean name="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 会话工厂引用连接池 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 指定别名 -->
<property name="typeAliasesPackage" value="cn.mgy.pojo"></property>
<!-- 配置映射文件,Spring 支持通配符*,表示加载 -mapper.xml 结尾映射文件 -->
<property name="mapperLocations" value="classpath:cn/mgy/mapper/xml/*-mapper.xml"></property>
<property name="configuration">
<!-- 对应 mybatis-config.xml 中的 settings 标签 -->
<bean class="org.apache.ibatis.session.Configuration">
<!-- 支持驼峰命名法 -->
<property name="mapUnderscoreToCamelCase" value="true"></property>
</bean>
</property>
</bean>
<!-- 2. 使用扫描器将操作的接口扫描到 Spring 容器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 使用会话工厂 -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"></property>
<!-- 扫描的接口所在的包 -->
<property name="basePackage" value="cn.mgy.mapper"></property>
</bean>
<!-- 3. Spring 支持 Mybatis 的事务代理,代理 Mybatis 的事务 -->
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 对那个数据源使用事务代理 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 编程式事务代理,事务注解支持事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

6、创建映射接口

 package cn.mgy.mapper;

 import org.apache.ibatis.annotations.Param;

 import cn.mgy.pojo.Student;

 public interface StudentMapper {
/**
* 查询学生
* @param student
* @return
*/
Student findByStudent(@Param("studentName")Object studentName,@Param("studentPwd")Object studentPwd);
}

7、创建映射文件

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd" >
<mapper namespace="cn.mgy.mapper.StudentMapper">
<select id="findByStudent" resultType="Student">
SELECT * FROM tb_student WHERE STUDENT_NAME=#{studentName} AND STUDENT_PWD=#{studentPwd}
</select>
</mapper>

附:数据库

spring-mybatis的整合的更多相关文章

  1. struts2 + spring + mybatis 框架整合详细介绍

    struts2 + spring + mybatis  框架整合详细介绍 参考地址: https://blog.csdn.net/qq_22028771/article/details/5149898 ...

  2. SpringMvc+Spring+Mybatis+Maven整合

    一.建立数据库表,使用generator自动生成相关代码: /* SQLyog Ultimate v11.24 (32 bit) MySQL - 5.1.62-community : Database ...

  3. JavaWeb_(SpringMVC框架)测试SpringMVC&Spring&MyBatis三大整合

    搭建 SpringMVC&Spring&MyBatis三大整合 传送门 1.准备 测试搭建S pringMVC&Spring&MyBatis三大整合 用例   a)准备 ...

  4. SSM框架-----------SpringMVC+Spring+Mybatis框架整合详细教程

    1.基本概念 1.1.Spring Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One  ...

  5. 1.springMVC+spring+Mybatis的整合思路

    SSM整合的过程:就是把一些东西交给spring管理,也就是添加配置文件的一个过程.那么有哪些东西我们要交给spring管理呢?大概有以下几个: 1.数据源(可配置数据库连接池) 2.SqlSessi ...

  6. idea+springmvc+spring+mybatis+maven整合返回json数据webapi

    首先看一张目录结构图: : 创建步骤: 1.创建maven  webapp工程, 创建完后的目录结构为: 2.添加项目依赖(添加jar包) 需要的jar包: spring-webmvc, spring ...

  7. spring mybatis springmvc整合

    使用SSM(Spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没有记录SSM整合 ...

  8. SSM(Spring MVC +Spring+Mybatis)整合——maven工程

    所谓的SSM 其实就是Spring MVC下整合mybatis. 具体的定义网络上都有,很详细. 这里只说项目的搭建步骤. 第一步 新建maven工程 工程目录如下: 配置pom.xml文件,引入所需 ...

  9. Spring+mybatis+postgresql整合

    最近做了一个项目,需要使用Spring+mybatis+postgresql,下面记录一下整合步骤: 一.准备JAR包: 我使用的是maven,所以直接晒出pom.xml <project xm ...

  10. SpringMVC Spring MyBatis 框架整合 Annotation MavenProject

    项目结构目录 pom.xml   jar包管理 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&q ...

随机推荐

  1. loadrunner断言多结果返回

    有这么一个场景,接口返回的多个状态都是正常的,那么在压测的时候,断言就需要多 init里面执行登录,根据返回获取到tokenId action中,执行登录后的操作,获取响应返回的状态,把正确的状态个数 ...

  2. [模板]fhqTreap

    用途 平衡树(可实现区间翻转) 原理 和treap一样,中序遍历表示权值的顺序,并且每个点有一个随机的附加值,形成一个堆来保证复杂度 但是不旋转,所有操作通过split和merge实现 分为两种spl ...

  3. 第四十六篇--解析和保存xml文件

    新建assets资源文件夹,右键app --> new --> Folder --> Assets Folder,将info.xml放入此文件夹下面. info.xml <?x ...

  4. virtualbox+ubuntu

    https://jingyan.baidu.com/article/7f766daff541cd4101e1d0cd.html ubuntu 安装 这台计算机似乎没有安装操作系统 待解决 注意ubun ...

  5. Inheritance: 'A' is an inaccessible base of 'B'

    'boost::enable_shared_from_this<net::Session>' is an inaccessible base of 'net::Session' BOOST ...

  6. P4180 严格次小生成树[BJWC2010] Kruskal,倍增

    题目链接\(Click\) \(Here\). 题意就是要求一个图的严格次小生成树.以前被题面吓到了没敢做,写了一下发现并不难. 既然要考虑次小我们就先考虑最小.可以感性理解到一定有一种次小生成树,可 ...

  7. 《Exception团队》第一次作业:团队亮相

    一.项目基本介绍 项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 作业链接地址 团队名称 Exception 作业学习目标 深入了解软件思想,强化编程技术 二.正文 1. ...

  8. R语言入门(1)-初识R语言

    设置R语言环境为英文环境 其实不设置也行...就是报错提示的内容是中文的话, 会不太好理解.. 1. 首先在用户根目录下cat查看一下, 发现没有.Renviron文件, 这个是R语言的环境配置文件. ...

  9. Node.js目录

    [相关学习] npm入门教程 [基础] (1) 初识Node.js (2) 开发环境和调试工具 (3) commonJs 规范 (4) node 概念(global.process进程.调试) (5) ...

  10. JAVA集合2--Collection架构

    Collectin有两个分支:List和Set List是有序集合,可以有重复元素:而Set不允许有重复元素 为了方便,抽象出AbstractCollection这个抽象类,其实现了Collectio ...