任务:

在数据库中创建一个employee表,添加若干条记录,利用本章学习知识完成一个员工管理系统,实现以下功能:

(1)根据id查询员工信息。

(2)新增员工信息。

(3)根据id修改员工信息。

(4)根据id删除员工信息。

实现:

 1.配置pom.xml文件

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

jsj.hhtc

javaee1_01

1.0-SNAPSHOT

org.mybatis

mybatis

3.5.2

mysql

mysql-connector-java

8.0.11

junit

junit

4.12

test

src/main/java

**/*.properties

**/*.xml

true

src/main/resources

**/*.properties

**/*.xml

true

2.在resourses下配置文件db.properties和mybatis-config.xml

db.properties

mysql.driver=com.mysql.cj.jdbc.Driver

mysql.url=jdbc:mysql://localhost:3306/0311db?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false

mysql.username=root

mysql.password=123

mybatis-config.xml

"-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"

"http://mybatis.org/dtd/mybatis-3-config.dtd">

3.创建pojo类Student

package jsj.hhtc.pojo;

public class Student {

private int id;

private String name;

private int age;

private String position;

@Override

public String toString() {

return "Student{" +

"id=" + id +

", name='" + name + '\'' +

", age=" + age +

", position='" + position + '\'' +

'}';

}

public int getId() {

return id;

}

public void setId(int id) {

this.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 getPosition() {

return position;

}

public void setPosition(String position) {

this.position = position;

}

}

4.在mapper文件夹下配置文件StudentMapper.xml,进行增删查改的操作。

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

insert into employee(id,name,age,position) values(#{id},#{name},#{age},#{position})

delete from employee where id=#{id}

update employee set name=#{name},age=#{age},position=#{position} where id=#{id}

5.编写测试类

package Test;

import jsj.hhtc.pojo.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 org.junit.After;

import org.junit.Before;

import org.junit.Test;

import java.io.IOException;

import java.io.Reader;

import java.util.List;

public class MyBatisTest {

private SqlSessionFactory sqlSessionFactory;

private SqlSession sqlSession;

@Before

public void init() {

//定义读取文件名

String resources = "mybatis-config.xml";

//创建流

Reader reader = null;

try {

//读取mybatis-config.xml文件到reader对象中

reader = Resources.getResourceAsReader(resources);

//初始化mybatis,创建SqlSessionFactory类的对象

SqlSessionFactory sqlMapper = new

SqlSessionFactoryBuilder().build(reader);

//创建session对象

sqlSession = sqlMapper.openSession();

} catch (IOException e) {

e.printStackTrace();

}

}

@Test

public void findAllStudentTest() {

// SqlSession执行映射文件中定义的SQL,并返回映射结果

List list =

sqlSession.selectList("jsj.hhtc.mapper.StudentMapper." +

"findAllStudent");

for (Student student : list) {

System.out.println(student);

}

}

@Test

public void insertStudentTest() {

Student student = new Student();

student.setAge(25);

student.setName("李六");

student.setPosition("员工");

sqlSession.insert("jsj.hhtc.mapper.StudentMapper.addStudent",student);

System.out.printf(student.getId()+"________");

// SqlSession执行映射文件中定义的SQL,并返回映射结果

List list =

sqlSession.selectList("jsj.hhtc.mapper.StudentMapper." +

"findAllStudent");

for (Student s : list) {

System.out.println(s);

}

}

@Test

public void deleteStudentTest() {

// Student student = sqlSession.selectOne("jsj.hhtc.mapper.StudentMapper.findStudentById",6);

sqlSession.delete("jsj.hhtc.mapper.StudentMapper.deleteStudent",3);

List list =

sqlSession.selectList("jsj.hhtc.mapper.StudentMapper." +

"findAllStudent");

for (Student s : list) {

System.out.println(s);

}

}

@Test

public void updateStudentTest() {

Student student = sqlSession.selectOne("jsj.hhtc.mapper.StudentMapper.findStudentById",3);

student.setName("赵八");

student.setAge(30);

sqlSession.update("jsj.hhtc.mapper.StudentMapper.updateStudent",student);

List list =

sqlSession.selectList("jsj.hhtc.mapper.StudentMapper." +

"findAllStudent");

for (Student s : list) {

System.out.println(s);

}

}

@After

public void destory() {

//提交事务

sqlSession.commit();

//关闭事务

sqlSession.close();

}

}

结果截图:

查:

改:

删:

增:

参考链接

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: