目录
使用工具
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&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;