java mysql sesssion_Java Mysql+Mybatis 实现用户登录功能

相关配置:

1.相关配置

首先java的编译器有许多,我使用的编译器是idea,jkd使用的是1.8版本,Tomcat是8.5.45版本。至于maven,我在这里没用到,所以跟我不一样的同学可以自行上网找maven的环境搭建,关于这方面网上有许多。

功能实现

首先打开项目包,下面是我的项目结构

876ebc6317f95a7f3c606e65b235853f.png

这里说明一下UserDaoTest是我对接口UserDao里方法的测试,建议一下,建方法时最好测试一下方法的可行性,这对后面改错很有帮助。

具体操作步骤

第一步:获取SqlSessionFactory对象和sqlsession实例

package cn.kgc.util;

import org.apache.ibatis.io.Resources;

import org.apache.ibatis.session.SqlSession;

import org.apache.ibatis.session.SqlSessionFactory;

import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;

import java.io.InputStream;

public class MapperConfig {

private static SqlSessionFactory factory;

//static初始化就加载

static {

SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();

try {

//两行代码死的,mybatis第一步:获取SqlSessionFactory对象

InputStream is= Resources.getResourceAsStream("mybatis-cfg.xml");

factory=builder.build(is);

} catch (IOException e) {

e.printStackTrace();

}

}

//获取SqlSessionFactory对象后,获得sqlsession实例

//sqlsession包含了几乎所有sql语句

public static SqlSession getSession(){

return factory.openSession(true);

}

//关闭SqlSession

public static void closeSession(SqlSession session){

if(session!=null){

session.close();

}

}

private MapperConfig(){

}

}

第二步,建实体类User

package cn.kgc.entity;

public class User {

private int user_id;

private String user_name;

private String password;

private int grade;

private String phone;

private String user_type;

public User(){}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

public User(int user_id, String user_name, String password, int grade, String phone, String user_type) {

this.user_id = user_id;

this.user_name = user_name;

this.password = password;

this.grade = grade;

this.phone = phone;

this.user_type = user_type;

}

public int getUser_id() {

return user_id;

}

public void setUser_id(int user_id) {

this.user_id = user_id;

}

public String getUser_name() {

return user_name;

}

public void setUser_name(String user_name) {

this.user_name = user_name;

}

public int getGrade() {

return grade;

}

public void setGrade(int grade) {

this.grade = grade;

}

public String getPhone() {

return phone;

}

public void setPhone(String phone) {

this.phone = phone;

}

public String getUser_type() {

return user_type;

}

public void setUser_type(String user_type) {

this.user_type = user_type;

}

//重写toString方法

@Override

public String toString() {

return "User{" +

"user_id=" + user_id +

", user_name='" + user_name + '\'' +

", password='" + password + '\'' +

", grade=" + grade +

", phone='" + phone + '\'' +

", user_type='" + user_type + '\'' +

'}';

}

}

第三步、数据访问层Dao接口的创建,编写实现方法

package cn.kgc.dao;

import cn.kgc.entity.User;

import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface UserDao {

//这里注意一下,参数的写法建议用下面这种方式

User login(@Param("uname") String uname, @Param("password") String password);

}

第四步、Mapper层实现接口方法

建一个mapper文件,实现接口功能,命名没有限制,但最好有意义,比如说接口名加上Mapper,文件类型为xml。这里的namespace指向了UserDao接口,这里的标签中的id必须与接口中的方法名对应。 resultType 为返回值类型,当然我这里的“user” 在我的mybatis-cfg.xml 中为它配置了别名。

select * from user_info

where user_name=#{uname}

and password=#{password}

第五步、service层的实现

再建一个service包,这个包下的文件会实现具体需要的功能,比如说注册和登录。创建接口UserService。

package cn.kgc.service;

import cn.kgc.entity.User;

public interface UserService {

User login(String user_name, String password);

}

创建实现类UserServiceImpl

package cn.kgc.service.impl;

import cn.kgc.dao.UserDao;

import cn.kgc.entity.User;

import cn.kgc.service.UserService;

import cn.kgc.util.MapperConfig;

import org.apache.ibatis.session.SqlSession;

public class UserServiceImpl implements UserService {

private SqlSession session;

public UserServiceImpl(){

session= MapperConfig.getSession();

}

@Override

public User login(String uname, String password) {

//1、对参数判空

if(uname==null||password==null||uname.trim().equals("")||

password.trim().equals("")){

return null;

}else{

//2、执行登陆查询

User user=session.getMapper(UserDao.class).login(uname,password);

session.commit();

session.close();

return user;

}

}

}

第六步、servlet层的实现

package cn.kgc.servlet;

import cn.kgc.entity.User;

import cn.kgc.service.UserService;

import cn.kgc.service.impl.UserServiceImpl;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import java.io.IOException;

public class LoginServlet extends HttpServlet {

@Override

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

}

@Override

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

req.setCharacterEncoding("utf-8");

resp.setCharacterEncoding("utf-8");

String uname=req.getParameter("userName");

String password=req.getParameter("password");

UserServiceImpl service=new UserServiceImpl();

User user= service.login(uname,password);

if(user==null){

req.getSession().setAttribute("msg","用户名和密码不正确");

resp.sendRedirect("index.jsp");

}else{

//查询成功,可以登陆,把用户名塞入session中

req.getSession().setAttribute("msg","登陆成功");

req.getSession().setAttribute("user",user);

resp.sendRedirect("index.jsp");

}

}

}

第七步:web.xml 配置:

光有上面的代码肯定是不够的,从哪连接数据库,从哪设置登录界面什么的都没有定。这里我们先来配置最主要的web.xml文件!

loginServlet

cn.kgc.servlet.LoginServlet

loginServlet

/login.do

第八步:jsp实现用户登陆模块

用户登陆

Title

登陆成功

最后别忘了数据库表的创建,下面贴上表信息

CREATE TABLE `user_info` (

`user_id` int(10) unsigned NOT NULL AUTO_INCREMENT,

`user_name` varchar(20) DEFAULT NULL,

`password` varchar(20) DEFAULT NULL,

`grade` int(10) DEFAULT NULL,

`phone` varchar(20) DEFAULT NULL,

`user_type` varchar(20) DEFAULT NULL,

PRIMARY KEY (`user_id`)

) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8

下面贴上效果展示:

af635adf2fdcc45abe227c5b7ce5f698.pngda973ef644770454a9e3142588826479.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值