Mybatis框架实现简单的学生管理系统

使用工具

IDEA2018.2 MySQL5.6 JDK1.8 Tomcat8.5.33

使用jar包

asm-3.3.1.jar
cglib-2.2.2.jar
commons-logging-1.1.1.jar
javassist-3.17.1-GA.jar
log4j-1.2.17.jar
log4j-api-2.0-rc1.jar
log4j-core-2.0-rc1.jar
mybatis-3.2.7.jar
mysql-connector-java-5.1.38-bin.jar
slf4j-api-1.7.5.jar
slf4j-log4j12-1.7.5.jar
jar下载微云链接:https://share.weiyun.com/5uwa3Pq

环境搭建

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

代码示例

java目录

Student.java
/**
 * @author VVcat
 * @date 2019/10/12 10:08
 **/
package com.vvcat.bean;

public class Student {
    private String name;
    private int age;
    private String sex;
    private int id;

    public Student() {
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                ", id=" + id +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

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

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}
Common.java
package com.vvcat.common;

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;

/**
 * @author VVcat
 * @date 2019/10/12 17:17
 **/
public class Common {
    public SqlSession getSession() throws IOException {
        //1.先获取会话工厂构造器
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        //指定连接配置文件
        InputStream is = Resources.getResourceAsStream("sqlMapConfig.xml");

        //2.根据构造器,构造会话工厂
        SqlSessionFactory factory = builder.build(is);

        //3.开启连接会话
        SqlSession session = factory.openSession();
        return session;
    }

    public void closeSession(SqlSession session){
        session.commit();
        session.close();
    }
}
IStudentDao.java
package com.vvcat.dao;



import com.vvcat.bean.Student;

import java.util.List;

/**
 * @author VVcat
 * @date 2019/10/12 17:14
 **/
public interface IStudentDao {
    //测试增删改查
    int addStu(Student student);
    int delStu(int id);
    int updStu(Student student);
    Student findStuById(int id);
    List<Student> findAll();
}
IStudentDao.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.vvcat.dao.IStudentDao">
    <!--  添加  -->
    <insert id="addStu" parameterType="com.vvcat.bean.Student">
        insert into tb_student(name,sex,age) values (#{name},#{sex},#{age})
    </insert>

    <!--  删除  -->
    <delete id="delStu" parameterType="java.lang.Integer">
        delete from tb_student where id=#{id}
    </delete>

    <!--  修改  -->
    <update id="updStu" parameterType="com.vvcat.bean.Student">
        update tb_student set name=#{name},sex=#{sex},age=#{age} where id=#{id}
    </update>

    <!--  单查询  -->
    <select id="findStuById" parameterType="java.lang.Integer" resultType="com.vvcat.bean.Student">
        select *
        from tb_student
        where id=#{id}
    </select>

    <!--  全查询  -->
    <select id="findAll" resultType="com.vvcat.bean.Student">
        select *
        from tb_student
    </select>


</mapper>
Main.java
/**
 * @author VVcat
 * @date 2019/10/12 17:20
 **/
package com.vvcat.main;


import com.vvcat.bean.Student;
import com.vvcat.service.IStudentService;
import com.vvcat.service.impl.StudentServiceImpl;

import java.io.IOException;
import java.util.List;
import java.util.Scanner;

/**
 * @author VVcat
 * @date 2019/10/12 13:21
 **/
public class Main {
    public static void main(String[] args) throws IOException {
        IStudentService studentService = new StudentServiceImpl();
        boolean flag=true;
        while (flag){
            System.out.println("学生管理系统");
            System.out.println("-----------------------------");
            System.out.println("1.添加学生信息");
            System.out.println("2.删除学生信息");
            System.out.println("3.更改学生信息");
            System.out.println("4.查询学生信息");
            System.out.println("5.查询所有学生信息");
            System.out.println("6.退出系统");
            System.out.println("----------------------------");
            Scanner input = new Scanner(System.in);
            System.out.print("请选择功能:");
            boolean flag2 = true;
            int i = input.nextInt();
            switch (i){
                case 1:
                    while (flag2){
                        Student student = new Student();
                        System.out.print("请输入学生姓名:");
                        String name = input.next();
                        System.out.print("请输入学生性别:");
                        String sex = input.next();
                        System.out.print("请输入学生年龄:");
                        int age = input.nextInt();
                        student.setName(name);
                        student.setSex(sex);
                        student.setAge(age);
                        boolean b = studentService.addStu(student);
                        if (b){
                            System.out.println("添加成功");
                            System.out.println("---------------------");
                            System.out.println("1.退出添加");
                            System.out.println("2.继续添加");
                            System.out.println("请输入您的选择");
                            int i1 = input.nextInt();
                            flag2 = studentService.isNext(i1, flag2);
                        }else {
                            System.out.println("添加失败");
                            System.out.println("---------------------");
                            System.out.println("1.退出添加");
                            System.out.println("2.继续添加");
                            System.out.println("请输入您的选择");
                            int i1 = input.nextInt();
                            flag2 = studentService.isNext(i1, flag2);
                        }
                    }
                    break;
                case 2:
                    while (flag2){
                        System.out.print("请输入要删除学生的ID:");
                        int id = input.nextInt();
                        boolean b = studentService.delStu(id);
                        if (b){
                            System.out.println("删除成功");
                            System.out.println("---------------------");
                            System.out.println("1.退出删除");
                            System.out.println("2.继续删除");
                            System.out.println("请输入您的选择");
                            int i1 = input.nextInt();
                            flag2 = studentService.isNext(i1, flag2);
                        }else {
                            System.out.println("删除失败,没有该用户");
                            System.out.println("---------------------");
                            System.out.println("1.退出删除");
                            System.out.println("2.继续删除");
                            System.out.println("请输入您的选择");
                            int i1 = input.nextInt();
                            flag2 = studentService.isNext(i1, flag2);
                        }
                    }
                    break;
                case 3:
                    while (flag2){
                        Student student = new Student();
                        System.out.print("请输入要更改学生的ID:");
                        int id = input.nextInt();
                        Student studentById = studentService.findStuById(id);
                        if (studentById != null){
                            boolean gengai = true;
                            while (gengai){
                                System.out.println("此用户的信息为:");
                                System.out.println("ID:"+studentById.getId());
                                System.out.println("name:"+studentById.getName());
                                System.out.println("sex:"+studentById.getSex());
                                System.out.println("age:"+studentById.getAge());
                                System.out.println("-------------------------");
                                System.out.println("是否更改该用户");
                                System.out.println("1.更改");
                                System.out.println("2.换一个更改");
                                System.out.println("3.退出更改");
                                int j = input.nextInt();
                                switch (j){
                                    case 1:
                                        System.out.print("请输入学生姓名:");
                                        String name = input.next();
                                        System.out.print("请输入学生性别:");
                                        String sex = input.next();
                                        System.out.print("请输入学生年龄:");
                                        int age = input.nextInt();
                                        student.setId(id);
                                        student.setName(name);
                                        student.setSex(sex);
                                        student.setAge(age);
                                        boolean b = studentService.updStu(student);
                                        if (b){
                                            System.out.println("更改成功");
                                            System.out.println("---------------------");
                                            System.out.println("1.退出更改");
                                            System.out.println("2.继续更改");
                                            System.out.println("请输入您的选择");
                                            int i1 = input.nextInt();
                                            flag2 = studentService.isNext(i1, flag2);
                                            gengai = false;
                                        }else {
                                            System.out.println("更改失败");
                                            System.out.println("---------------------");
                                            System.out.println("1.退出更改");
                                            System.out.println("2.继续更改");
                                            System.out.println("请输入您的选择");
                                            int i1 = input.nextInt();
                                            flag2 = studentService.isNext(i1, flag2);
                                            gengai = false;
                                        }
                                        break;
                                    case 2:
                                        flag2 = true;
                                        gengai = false;
                                        break;
                                    case 3:
                                        flag2 = false;
                                        gengai = false;
                                        break;
                                    default:
                                        gengai = true;
                                        break;
                                }
                            }
                        } else {
                            System.out.println("查无此人");
                            System.out.println("---------------------");
                            System.out.println("1.退出更改");
                            System.out.println("2.继续更改");
                            System.out.println("请输入您的选择");
                            int i1 = input.nextInt();
                            flag2 = studentService.isNext(i1, flag2);
                        }
                    }
                    break;
                case 4:
                    while (flag2){
                        System.out.print("请输入要查询学生的ID:");
                        int id = input.nextInt();
                        Student studentById = studentService.findStuById(id);
                        if (studentById != null){
                            System.out.println("此用户的信息为:");
                            System.out.println("ID:"+studentById.getId());
                            System.out.println("name:"+studentById.getName());
                            System.out.println("sex:"+studentById.getSex());
                            System.out.println("age:"+studentById.getAge());
                            System.out.println("-------------------------");
                            System.out.println("1.退出查询");
                            System.out.println("2.继续查询");
                            System.out.println("请输入您的选择");
                            int i1 = input.nextInt();
                            flag2 = studentService.isNext(i1, flag2);
                        }else {
                            System.out.println("查无此人");
                            System.out.println("---------------------");
                            System.out.println("1.退出查询");
                            System.out.println("2.继续查询");
                            System.out.println("请输入您的选择");
                            int i1 = input.nextInt();
                            flag2 = studentService.isNext(i1, flag2);
                        }
                    }
                    break;
                case 5:
                    while (flag2){
                        List<Student> studentList = studentService.findAll();
                        for (Student student : studentList) {
                            System.out.print("ID:"+student.getId()+",");
                            System.out.print("name:"+student.getName()+",");
                            System.out.print("sex:"+student.getSex()+",");
                            System.out.print("age:"+student.getAge()+",");
                            System.out.println();
                        }
                        System.out.println("-------------------------");
                        System.out.println("1.退出查询");
                        System.out.println("2.更新查询");
                        System.out.println("请输入您的选择");
                        int i1 = input.nextInt();
                        flag2 = studentService.isNext(i1, flag2);
                    }
                    break;
                case 6:
                    flag=false;
                    break;
                default:
                    flag = true;
                    break;
            }
        }
        System.out.println("欢迎下次光临");
    }
}
StudentServiceImpl.java
/**
 * @author VVcat
 * @date 2019/10/12 17:13
 **/
package com.vvcat.service.impl;



import com.vvcat.bean.Student;
import com.vvcat.common.Common;
import com.vvcat.dao.IStudentDao;
import com.vvcat.service.IStudentService;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.util.List;

/**
 * @author VVcat
 * @date 2019/10/12 14:13
 **/
public class StudentServiceImpl implements IStudentService {
    private Common common = new Common();
    @Override
    public boolean addStu(Student student) throws IOException {
        SqlSession session = this.common.getSession();
        //先获取接口引用 - 类的类对象
        IStudentDao studentDao = session.getMapper(IStudentDao.class);
        int i = studentDao.addStu(student);
        common.closeSession(session);
        if (i>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean delStu(int id) throws IOException {
        SqlSession session = common.getSession();
        //先获取接口引用 - 类的类对象
        IStudentDao studentDao = session.getMapper(IStudentDao.class);
        int i = studentDao.delStu(id);
        common.closeSession(session);
        if (i>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean updStu(Student student) throws IOException {
        SqlSession session = common.getSession();
        //先获取接口引用 - 类的类对象
        IStudentDao studentDao = session.getMapper(IStudentDao.class);
        int update = studentDao.updStu(student);
        common.closeSession(session);
        if (update>0){
            return true;
        }
        return false;
    }

    @Override
    public Student findStuById(int id) throws IOException {
        SqlSession session = common.getSession();
        //先获取接口引用 - 类的类对象
        IStudentDao studentDao = session.getMapper(IStudentDao.class);
        Student stuById = studentDao.findStuById(id);
        return stuById;
    }

    @Override
    public List<Student> findAll() throws IOException {
        SqlSession session = common.getSession();
        //先获取接口引用 - 类的类对象
        IStudentDao studentDao = session.getMapper(IStudentDao.class);
        List<Student> studentList = studentDao.findAll();
        return studentList;
    }

    @Override
    public boolean isNext(int i, boolean flag2) {
        boolean flag3 = true;
        while (flag3){
            switch (i){
                case 1:
                    flag2 = false;
                    flag3 = false;
                    break;
                case 2:
                    flag2 = true;
                    flag3 = false;
                    break;
                default:
                    flag3 = true;
                    flag2 = true;
                    break;
            }
        }
        return flag2;
    }
}
IStudentService.java
package com.vvcat.service;



import com.vvcat.bean.Student;

import java.io.IOException;
import java.util.List;

/**
 * @author VVcat
 * @date 2019/10/12 15:55
 **/
public interface IStudentService {
    //增加
    boolean addStu(Student student) throws IOException;
    //删除
    boolean delStu(int id) throws IOException;
    //修改
    boolean updStu(Student student) throws IOException;
    //单查询
    Student findStuById(int id) throws IOException;
    //全查
    List<Student> findAll() throws IOException;
    //判断功能是否继续
    boolean isNext(int i, boolean flag2);
}
StuTest.java
package com.vvcat.test;



import com.vvcat.bean.Student;
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;
import org.junit.Test;
/**
 * @author VVcat
 * @date 2019/10/12 16:23
 **/
public class StuTest {

    @Test
    public void insertTest() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = factory.openSession();
        Student student = new Student();
        student.setName("hehe");
        student.setAge(15);
        student.setSex("男");
        int studentMapper = sqlSession.insert("StudentMapper.insertStu", student);
        sqlSession.commit();
        sqlSession.close();

    }
}

resources目录

sqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!--声明数据库连接配置-->
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/item?userSSL=true&amp;characterEncoding=utf-8"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper class="com.vvcat.dao.IStudentDao"></mapper>
    </mappers>
</configuration>

数据库

item.sql
CREATE DATABASE item;
USE `item`;
CREATE TABLE `tb_student` (
  `id` INT(11) NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(50) NULL,
  `sex` VARCHAR(50) NULL,
  age INT(10) NULL,
  PRIMARY KEY (`id`)
) ENGINE=INNODB AUTO_INCREMENT=1  CHARSET=utf8;

效果展示

界面

在这里插入图片描述

添加功能

在这里插入图片描述

查询功能

在这里插入图片描述

查询全部功能

在这里插入图片描述

删除功能

在这里插入图片描述

更改功能

在这里插入图片描述

  • 13
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
MyBatis是一个开源的持久层框架,它可以帮助我们简化数据库操作。下面是使用MyBatis框架实现学生管理系统查询的步骤: 1. 配置数据库连接:在MyBatis的配置文件中,我们需要配置数据库的连接信息,包括数据库驱动、URL、用户名和密码等。 2. 创建实体类:根据学生管理系统的需求,我们需要创建一个学生实体类,包含学生的各种属性,如学号、姓名、年龄等。 3. 创建Mapper接口:Mapper接口是用于定义数据库操作方法的接口。我们可以在接口中定义查询学生信息的方法。 4. 创建Mapper映射文件:Mapper映射文件是用于将Mapper接口中的方法与SQL语句进行映射的文件。在映射文件中,我们可以编写SQL语句,并将其与对应的方法进行关联。 5. 配置Mapper映射文件:在MyBatis的配置文件中,我们需要将Mapper映射文件进行配置,告诉MyBatis框架去哪里找到这些映射文件。 6. 创建SqlSessionFactory:SqlSessionFactory是MyBatis框架的核心对象,用于创建SqlSession对象。 7. 创建SqlSession:SqlSession是用于执行SQL语句的对象。我们可以通过SqlSession对象调用Mapper接口中定义的方法来查询学生信息。 8. 调用Mapper接口方法:通过SqlSession对象调用Mapper接口中定义的方法,即可执行对应的SQL语句,并获取查询结果。 9. 处理查询结果:根据查询结果,我们可以进行相应的处理,如打印学生信息或者将查询结果封装成对象返回。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值