目录

  • 使用工具
  • 使用jar包
  • 环境搭建
  • 代码示例
    • java目录
      • Student.java
      • Common.java
      • IStudentDao.java
      • IStudentDao.xml
      • Main.java
      • StudentServiceImpl.java
      • IStudentService.java
      • StuTest.java
    • resources目录
      • sqlMapConfig.xml
    • 数据库
      • item.sql
  • 效果展示
    • 界面
    • 添加功能
    • 查询功能
    • 查询全部功能
    • 删除功能
    • 更改功能

使用工具

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() {}@Overridepublic 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 mapperPUBLIC "-//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_studentwhere 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();@Overridepublic 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;}@Overridepublic 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;}@Overridepublic 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;}@Overridepublic Student findStuById(int id) throws IOException {SqlSession session = common.getSession();//先获取接口引用 - 类的类对象IStudentDao studentDao = session.getMapper(IStudentDao.class);Student stuById = studentDao.findStuById(id);return stuById;}@Overridepublic List<Student> findAll() throws IOException {SqlSession session = common.getSession();//先获取接口引用 - 类的类对象IStudentDao studentDao = session.getMapper(IStudentDao.class);List<Student> studentList = studentDao.findAll();return studentList;}@Overridepublic 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 {@Testpublic 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 configurationPUBLIC "-//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;

效果展示

界面

添加功能

查询功能

查询全部功能

删除功能

更改功能

Mybatis框架实现简单的学生管理系统相关推荐

  1. python小项目实例流程-Python小项目:快速开发出一个简单的学生管理系统

    原标题:Python小项目:快速开发出一个简单的学生管理系统 本文根据实际项目中的一部分api 设计抽象出来,实例化成一个简单小例子,暂且叫作「学生管理系统」. 这个系统主要完成下面增删改查的功能: ...

  2. python小项目案例-Python小项目:快速开发出一个简单的学生管理系统

    本文根据实际项目中的一部分api 设计抽象出来,实例化成一个简单小例子,暂且叫作「学生管理系统」. 这个系统主要完成下面增删改查的功能: 包括: 学校信息的管理 教师信息的管理 学生信息的管理 根据A ...

  3. python项目开发实例-Python小项目:快速开发出一个简单的学生管理系统

    本文根据实际项目中的一部分api 设计抽象出来,实例化成一个简单小例子,暂且叫作「学生管理系统」. 这个系统主要完成下面增删改查的功能: 包括: 学校信息的管理 教师信息的管理 学生信息的管理 根据A ...

  4. 基于ThinkPHP框架的简单的后台管理系统

    基于ThinkPHP框架的简单的后台管理系统 一个简单的后台管理系统,可能还不全面,可以自己改,有登录功能 实例如图:

  5. JAVA swing实现简单的学生管理系统

    JAVA swing实现简单的学生管理系统 基本功能介绍 本系统实现了一个简单而实用的学生管理系统,通过这个学生管理系统我们可以进行一些基本的学生管理操作,它可以进行简单的添加学生.删除学生.修改学生 ...

  6. Android——一个简单的学生管理系统

    一个简单的学生管理系统 效果演示 实现功能总览 代码 效果演示 随手做的一个小玩意,还有很多功能没有完善,倘有疏漏,万望海涵. 实现功能总览 实现了登录.注册.忘记密码.成绩查询.考勤情况.课表查看. ...

  7. Python编写简单的学生管理系统

    Python编写简单的学生管理系统 一共两个文件,其中一个定义函数,另一个是主程序,调用函数,运行程序 CMS.py ''' 编写"学生信息管理系统",要求如下: 必须使用自定义函 ...

  8. iOS 简单的学生管理系统(增删改查)

    思路 这个简单的学生管理系统学生信息只有姓名,班级,年龄 我在app执行时加了个开始界面,在AppDelegate.m 中将根视图设为需要显示的开机界面即可 首先写登陆注册界面,注册需要查重,并转到登 ...

  9. python简单项目-Python小项目:快速开发出一个简单的学生管理系统

    本文根据实际项目中的一部分api 设计抽象出来,实例化成一个简单小例子,暂且叫作「学生管理系统」. 这个系统主要完成下面增删改查的功能: 包括: 学校信息的管理 教师信息的管理 学生信息的管理 根据A ...

最新文章

  1. 测序技术有4个指标:读长、成本、准确度、通量
  2. xmind快速上手使用教程,提高工作效率
  3. 【数据竞赛】竞赛宝典黑科技:基于开源结果的高端融合策略
  4. 安卓案例:View动画 - 弹球碰壁
  5. ConcurrentModificationException的情况
  6. MySQL与Oracle的数据迁移注意事项,另附转换工具链接
  7. 自然数幂与伯努利数,分数相加
  8. 学习getRequestDispatcher()与sendRedirect()笔记
  9. ros讯飞语音交互学习记录
  10. endnote插入参考文献
  11. VMware虚拟机在Windows10下不兼容解决办法
  12. unity制作子弹击砖块过程分析
  13. windows 开机速度优化
  14. RecyclerView实现条目拖拽,左滑、右滑移除效果
  15. vpc经典网络区别_阿里云经典网络与VPC网络互通的实现
  16. ARMv8 Linux内核head.S源码分析
  17. 矩估计和最大似然估计关系
  18. 计算机三级网络技术路由配置,计算机三级网络技术(7):路由器配置及使用(上)...
  19. linux重启命令有哪些,​ linux中常用的关机/重启命令有哪些
  20. pass在c语言中的作用,Python语句中pass语句有什么作用?浅谈pass语句的用法

热门文章

  1. 史上最全最强SpringMVC详细示例实战教程
  2. iOS开发UINavigation系列四——导航控制器UINavigationController
  3. DPM系列之一:安装dpm与attach dpmagent
  4. 【C#文件锁】C#加密解密文件小工具
  5. 【转】谈谈三层架构中MODEL的作用
  6. [浅谈 演示] 你所不知道的HTML - 从 XHTML2 到 HTML5 (1)
  7. 使用DOM操作样式表
  8. Google C++编程风格指南
  9. nginx-http服务器
  10. mybaits二十一:1连接池以及事务控制