Spring整合Mybatis实现登录功能

前面学习了MyBatis和Spring两个框架,MyBatis用于数据库连接,Spring用于解决企业应用开发的复杂性,现在把MyBatis纳入Spring中,整合搭建登录功能项目

Spring整合Mybatis实现登录功能

构建web项目–>在web.WEB-INF下构建lib库,导入开发Spring需要的jar包
我把需要的jar包放在下面:
https://download.csdn.net/download/qq_43171656/12650800
其中Spring和MyBatis整合需要的jar包如下:
mybatis-spring-1.2.3.jar (mybatis与spring的整合包)
spring-jdbc-4.1.6.RELEASE.jar
依赖于:
spring-aop-4.1.6.RELEASE.jar
spring-tx-4.1.6.RELEASE.jar 事务

首先在src目录下引入数据库连接配置文件db.propertries和Spring的全局配置文件applicationContext-service.xml
再根据MVC架构大致搭建
com.spring.mapper:数据访问层(持久化层)——数据的增删改查,与数据库建立连接;封装了对数据库的curd操作
com.spring.pojo:简单的Java对象
com.spring.service:业务逻辑层——做一些业务逻辑地处理,并给控制层返回结果
com.spring.view:视图层
大致结构如下:
在这里插入图片描述
在applicationContext-service.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: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.xsd">
</beans>

使用注解注入的方式扫描扫描service.impl下面所有的类

<?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.xsd">
<!--    扫描service下面所有的类-->
    <context:component-scan base-package="com.spring.service.impl"></context:component-scan>
</beans>

连接数据库配置,底层使用的是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: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.xsd">
<!--    扫描service下面所有的类-->
    <context:component-scan base-package="com.spring.service.impl"></context:component-scan>
<!--    加载数据库-->
    <context:property-placeholder location="db.properties"></context:property-placeholder>
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
<!--    加载工厂类,生产session的工厂类,把数据库配置注入到工厂类中-->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
<!--    把所有的mapper中的接口都交给Spring管理-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.spring.mapper"></property>
    </bean>
</beans>

db.properties:

jdbc.driver=oracle.jdbc.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
jdbc.username=servlet
jdbc.password=servlet

可见,Spring底层用bean都实现了org.springframework.jdbc.datasource.DriverManagerDataSource
驱动类,用于连接数据库。
org.mybatis.spring.SqlSessionFactoryBean工厂类,用于生成SqlSession实例的工厂,还可以看到在xml中把dataSource注入到了工厂类当中,连接数据库
org.mybatis.spring.mapper.MapperScannerConfigurer mapper配置类
把mapper下面所有的类都交给Spring管理

项目代码:

com.spring.mapper.UserMapper:

package com.spring.mapper;

import com.spring.pojo.User;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

@Repository //dao层的接口交给Spring管理声明
public interface UserMapper {
    @Select("select * from t_user where uname = #{uname} and pwd = #{pwd}")
    public User select(User user);
}

com.spring.pojo.User:

package com.spring.pojo;

import java.io.Serializable;

public class User implements Serializable {
    private int tid;
    private String uname;
    private String pwd;
    private int sex;

    public User() {
    }

    public User(int tid, String uname, String pwd, int sex) {
        this.tid = tid;
        this.uname = uname;
        this.pwd = pwd;
        this.sex = sex;
    }

    public int getTid() {
        return tid;
    }

    public void setTid(int tid) {
        this.tid = tid;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public int getSex() {
        return sex;
    }

    public void setSex(int sex) {
        this.sex = sex;
    }

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    @Override
    public String toString() {
        return "User{" +
                "tid=" + tid +
                ", uname='" + uname + '\'' +
                ", pwd='" + pwd + '\'' +
                ", sex=" + sex +
                '}';
    }
}

com.spring.service.impl.UserServiceImpl:

package com.spring.service.impl;

import com.spring.mapper.UserMapper;
import com.spring.pojo.User;
import com.spring.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Autowired  //自动按照类型注入
    private UserMapper mapper;
    @Override
    public User select(User user) {
        return mapper.select(user);
    }
}

com.spring.service.UserService:

package com.spring.service;

import com.spring.pojo.User;

public interface UserService {
    public User select(User user);
}

com.spring.view.TestSpring:

package com.spring.view;

import com.spring.pojo.User;
import com.spring.service.impl.UserServiceImpl;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSpring {
    @Autowired
    private UserServiceImpl usi;
    public TestSpring(){
        //导入配置
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext-dao.xml");
        usi = (UserServiceImpl)ac.getBean("userServiceImpl");
    }
    @Test
    public void select(){
        User user = new User();
        user.setUname("王五");
        user.setPwd("123456");
        User u = usi.select(user);
        System.out.println(u);
    }
}

运行结果:
在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现Spring整合Mybatis登录功能,你需要以下步骤: 1. 配置SpringMybatisSpring的配置文件中,配置Mybatis的数据源、事务管理器以及Mapper扫描器。具体可以参考Mybatis官方文档。 2. 编写Mapper 编写用户信息查询的Mapper接口和对应的Mapper.xml文件。例如,可以编写一个查询用户名和密码的方法: ```java public interface UserMapper { User findByNameAndPassword(@Param("name") String name, @Param("password") String password); } ``` ```xml <select id="findByNameAndPassword" resultType="User"> select * from user where name = #{name} and password = #{password} </select> ``` 3. 编写Service 编写用户登录的Service类,调用Mapper接口中的方法查询用户信息。例如: ```java @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public User login(String name, String password) { User user = userMapper.findByNameAndPassword(name, password); return user; } } ``` 4. 编写Controller 编写处理登录请求的Controller类,调用Service类中的方法进行登录验证。例如: ```java @RestController public class LoginController { @Autowired private UserService userService; @PostMapping("/login") public Result login(@RequestParam String name, @RequestParam String password) { User user = userService.login(name, password); if (user != null) { return Result.success(); } else { return Result.error("用户名或密码错误"); } } } ``` 以上就是实现Spring整合Mybatis登录功能的基本步骤。当然,具体实现还需要根据具体的业务需求进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值