Spring整合Mybatis-完成用户登录

①导入的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/javaee
        http://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容器资源的加载
    @Override
    public void init() throws ServletException {
        //获取spring容器对象
        ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
        //获取业务层对象
        userService = (UserService) ac.getBean("us");
    }

    @Override
    protected 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;
    }
    @Override
    public User userLoginService(String uname, String pwd) {
        User user = um.userLoginMapper(uname, pwd);
        return user;
    }
}

⑥正常完成功能开发即可

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AloneDrifters

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值