Mybatis框架实现简单的学生管理系统
生活随笔
收集整理的這篇文章主要介紹了
Mybatis框架实现简单的学生管理系统
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
- 使用工具
- 使用jar包
- 環(huán)境搭建
- 代碼示例
- 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
環(huán)境搭建
代碼示例
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.繼續(xù)添加");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.繼續(xù)添加");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.繼續(xù)刪除");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.繼續(xù)刪除");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.繼續(xù)更改");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.繼續(xù)更改");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.繼續(xù)更改");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.繼續(xù)查詢");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.繼續(xù)查詢");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;//判斷功能是否繼續(xù)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&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框架实现简单的学生管理系统的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: spring框架实现一个学生管理系统
- 下一篇: HashMap的遍历方法