①导入的jar包:

②在src下创建并配置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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><!--配置DataSource的bean对象:存储数据库连接参数--><bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="url" value="jdbc:mysql://localhost:3306/spring4?characterEncoding=utf8"></property><property name="driverClassName" value="com.mysql.jdbc.Driver"></property><property name="username" value="root"></property><property name="password" value="root"></property></bean><!--配置SQLSessionFactory的bean对象:生产SQLSession--><bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="datasource"></property></bean><!--配置Mapper扫描的bean对象:使用SQLSession扫描mapper包获取Mapper接口的实例化对象--><bean id="a" class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="sqlSessionFactory" ref="factory"></property><property name="basePackage" value="com.java.mapper"></property></bean><!--配置service对象--><bean id="us" class="com.java.service.UserServiceImpl"><property name="um" ref="userMapper"></property></bean>
</beans>

③在web.xml文件中配置Spring容器对象的配置文件路径

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"version="3.0"><!--配置全局参数:被项目中所有servlet共享--><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationcontext.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
</web-app>

④在Servlet的init方法中完成初始化资源的加载(从Spring容器对象中获取业务层对象)

package com.java.controller;import com.java.pojo.User;
import com.java.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.request.async.WebAsyncUtils;
import org.springframework.web.context.support.WebApplicationContextUtils;import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;@WebServlet(value = "/user", loadOnStartup = 1)
public class UserServlet extends HttpServlet {UserService userService;//在初始化方法中完成Spring容器资源的加载@Overridepublic void init() throws ServletException {//获取spring容器对象ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());//获取业务层对象userService = (UserService) ac.getBean("us");}@Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {req.setCharacterEncoding("utf-8");resp.setContentType("text/html;charset=utf-8");resp.setCharacterEncoding("utf-8");String uname = req.getParameter("uname");String pwd = req.getParameter("pwd");//处理请求//调用业务层方法User user = userService.userLoginService(uname, pwd);//响应结果//直接响应if (user != null) {resp.getWriter().write("登录成功");} else {resp.getWriter().write("登录失败");}}
}

⑤在业务层中声明mapper层的属性,并声明对应的get/set方法

package com.bjsxt.service;
import com.bjsxt.mapper.UserMapper;
import com.bjsxt.pojo.User;public class UserServiceImpl implements UserService{private UserMapper um;public UserMapper getUm() {return um;}public void setUm(UserMapper um) {this.um = um;}@Overridepublic User userLoginService(String uname, String pwd) {User user = um.userLoginMapper(uname, pwd);return user;}
}

⑥正常完成功能开发即可

Spring整合Mybatis-完成用户登录相关推荐

  1. Spring整合MyBatis:实现登录功能

    一.项目搭建 1.创建 web 项目:spring_mybatis 2.搭建项目的目录结构 3.导入 jar 包 注意: 导入 web/WEB-INF/lib 中,一下 jar 包都需要导入 4.创建 ...

  2. 【Spring+SpringMVC+Mybatis】利用SSM整合,完成用户登录、注册、修改密码系统

    近年来,由于Struts2+Hibernate3+Spring3,这套SSH框架,Struts2屡次爆出安全漏洞,Hibernate就只会推行它HQL那套而越来越远离SQL查询关系数据库的本质,所以S ...

  3. Spring源码深度解析(郝佳)-学习-源码解析-Spring整合MyBatis

    了解了MyBatis的单独使用过程之后,我们再来看看它也Spring整合的使用方式,比对之前的示例来找出Spring究竟为我们做了什么操作,哪些操作简化了程序开发. 准备spring71.xml &l ...

  4. spring整合mybatis(入门级简单教程1)--在spring中配置c3p0,并成功测试

    引子:spring整合mybatis.因为,我们看完(我就是这样的)spring和mybatis之后,本想自己写一个小小的项目,以便加深理解,但是我发现在spring中整合mybatis并不是一件容易 ...

  5. Spring学习笔记:Spring整合Mybatis(mybatis-spring.jar)(二:mybatis整合spring)

    http://blog.csdn.net/qq598535550/article/details/51703190 二.Spring整合mybatis其实是在mybatis的基础上实现Spring框架 ...

  6. Spring Security默认的用户登录表单 页面源代码

    Spring Security默认的用户登录表单 页面源代码 <html><head><title>Login Page</title></hea ...

  7. SSM之二(Spring整合Mybatis)

    项目与外界交互大概过程如下图: 一般过程是: 前端发送请求,查询数据.增加数据.修改数据.删除数据 中间件经过处理后,对数据发送请求 数据库返回数据,中间件再对数据处理 中间件响应前端请求 上一节关注 ...

  8. Spring整合Mybatis之注解方式,(注解整合Junit)

    Spring整合Mybatis之注解方式 我有一篇博客详细写了我自己使用xml的方法Spring整合MyBatis,现在我就把核心配置文件中的每个bean的配置使用注解的方式实现 注解整合MyBati ...

  9. spring整合mybatis基于注解

    数据库 /* Navicat MySQL Data Transfer Source Server         : mysql Source Server Version : 50549 Sourc ...

最新文章

  1. 【转】NG:垂枝桦基因组图谱构建(2+3组装)及重测序分析
  2. 组成新数python_大数相加 简单实现 Python 版本
  3. ADO.NET知识要点
  4. 当HTTP状态代码不足时:处理Web API错误报告
  5. 常见问题摘要(生活篇)
  6. java.net.noroute,java.net.NoRouteToHostException: No route to host
  7. ssm如何支持热部署_Pipedrive如何在每天部署50+次的情况下支持质量发布?
  8. 优化的交换排序(冒泡排序)_C程序实现优化的冒泡排序
  9. ROM RAM FLASH说明
  10. 3097: Hash Killer I
  11. 核磁共振测量动物脂肪、水分、瘦肉、代谢
  12. 服务器数据恢复过程(服务器数据恢复通用方法)
  13. linux学习(跟着b站尚硅谷老师学习)
  14. GeoHash在空间道路密度计算中的应用-以mobike骑行轨迹为例
  15. vector 通俗易懂描述
  16. CentOS7 安装 chrome
  17. 【老鸟进阶】deepfacelab训练参数详解
  18. android 局部tab页,安卓TabLayout+ViewPager实现切页
  19. element-ui改变树形菜单小箭头
  20. 导轨电表、万用表、计量插座、工控物联网、学习机、仪器仪表等低功耗高抗干扰段码屏LCD液晶显示驱动IC-VK2C22A/B LQFP52/48,完全兼容替代16C22,44*4/40*4显示

热门文章

  1. 数据结构与算法--实现Singleton模式
  2. hbuilderx怎么添加断点_【高考语文题库】高考一直提分提不上去该怎么办?同一卷高考押题语文答案,助你再提30分...
  3. laradock双版本php,自己撸一个 LaraDock(使用 Docker LNMP 部署 PHP 开发环境)
  4. 网络时延——发送时延和传播时延
  5. 数据库事务及隔离级别
  6. 背包dp的核心思想(动态规划)
  7. Codeforces Round #632 (Div. 2) F. Kate and imperfection 数论 + 贪心
  8. 123. 买卖股票的最3佳时机 III
  9. 2020牛客国庆集训派对day1 Zeldain Garden
  10. [NOI Online 2022 提高组] 丹钓战(单调栈 + 树状数组 / 主席树)