目录
1.题目
2.文件目录
3. 引入依赖——pom.xml
4.配置文件——myBatis-config.xml
5.映射文件——EmployeeMapper.xml
6.java类——Employee.java
7.接口类——EmployeeMapper.java
8.测试类——MyBatisTest.java
(1)根据id查询员工信息
(2)新增员工信息
(3)根据id更新员工信息
(4)根据id删除员工信息
9.数据库运行截图
(1)根据id查询员工信息
(2)新增员工信息
(3)根据id更新员工信息
(4)根据id删除员工信息
1.题目
在数据库创建一个employee表,在MyBatis环境下完成一个员工管理系统。该系统实现以下几个功能。
(1)在MyBatisTest测试类中添加findByIdTest()方法,用于根据id查询员工信息。
(2)在MyBatisTest测试类中添加insertEmployeeTest()方法,用于新增员工信息。
(3)在MyBatisTest测试类中添加updateEmployeeTest()方法,用于根据id更新员工信息。
(4)在MyBatisTest测试类中添加deleteEmployeeTest()方法,用于根据id删除员工信息。
员工编号(id) | 员工名称(name) | 员工年龄(age) | 员工职位(position) |
---|---|---|---|
1 | 张三 | 20 | 员工 |
2 | 李四 | 18 | 员工 |
3 | 王五 | 35 | 经理 |
2.文件目录
3. 引入依赖——pom.xml
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" 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"> <parent> <artifactId>mybatistest</artifactId> <groupId>org.example</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <groupId>org.example1</groupId> <artifactId>Mybatis_demo2</artifactId> <packaging>jar</packaging> <dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.28</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies></project>
4.配置文件——myBatis-config.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"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis"/> <property name="username" value="root"/> <property name="password" value="12345678"/> </dataSource> </environment> </environments> <mappers> <mapper resource="mappers/EmployeeMapper.xml"/> </mappers></configuration>
5.映射文件——EmployeeMapper.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="org.example1.mapper.EmployeeMapper"> <select id="findById" resultType="org.example1.pojo.Employee"> select * from employee where id = 01 </select> <insert id="insertEmployee"> insert into employee values(02,'李四',18,'员工') </insert> <update id="updateEmployee"> update employee set name='刘六',age=27,position='经理' where id=02 </update> <delete id="deleteEmployee"> delete from employee where id = 03 </delete></mapper>
6.java类——Employee.java
package org.example1.pojo;public class Employee { private Integer id; private String name; private Integer age; private String position; public Employee(Integer id, String name, Integer age, String position) { this.id = id; this.name = name; this.age = age; this.position = position; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getPosition() { return position; } public void setPosition(String position) { this.position = position; } @Override public String toString() { return "Employee{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + ", position='" + position + '\'' + '}'; }}
7.接口类——EmployeeMapper.java
package org.example1.mapper;import org.example1.pojo.Employee;public interface EmployeeMapper { //新增员工信息 int insertEmployee(); //根据id查询员工信息 Employee findById(); //更新员工信息 void updateEmployee(); //删除员工信息 void deleteEmployee();}
8.测试类——MyBatisTest.java
(1)根据id查询员工信息
package org.example1.test;import org.example1.mapper.EmployeeMapper;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.example1.pojo.Employee;import org.junit.Test;import java.io.IOException;import java.io.InputStream;public class MyBatisTest { @Test public void insertEmployeeTest() throws IOException { //加载核心配置文件 InputStream is = Resources.getResourceAsStream("mybatis-config.xml"); //获取SqlSessionFactoryBuilder SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder(); //获取SqlSessionFactory SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is); //获取SqlSession SqlSession sqlSession = sqlSessionFactory.openSession(true); //获取mapper接口对象 EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class); //测试功能 int result = mapper.insertEmployee(); //提交事务 //sqlSession.commit(); System.out.println("result:"+result); } @Test public void findByIdTest() throws IOException { InputStream is = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); SqlSession sqlSession = sqlSessionFactory.openSession(true); EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class); //mapper.updateEmployee(); //mapper.deleteEmployee(); Employee employee = mapper.findById(); System.out.println(employee); }}
(2)新增员工信息
package org.example1.test;import org.example1.mapper.EmployeeMapper;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.Test;import java.io.IOException;import java.io.InputStream;public class MyBatisTest { @Test public void insertEmployeeTest() throws IOException { //加载核心配置文件 InputStream is = Resources.getResourceAsStream("mybatis-config.xml"); //获取SqlSessionFactoryBuilder SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder(); //获取SqlSessionFactory SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is); //获取SqlSession SqlSession sqlSession = sqlSessionFactory.openSession(true); //获取mapper接口对象 EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class); //测试功能 int result = mapper.insertEmployee(); //提交事务 //sqlSession.commit(); System.out.println("result:"+result); } /*@Test public void updateEmployeeTest() throws IOException { InputStream is = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); SqlSession sqlSession = sqlSessionFactory.openSession(true); EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class); mapper.updateEmployee(); //mapper.deleteEmployee(); //Employee employee = mapper.findById(); //System.out.println(employee); }*/}
(3)根据id更新员工信息
package org.example1.test;import org.example1.mapper.EmployeeMapper;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.example1.pojo.Employee;import org.junit.Test;import java.io.IOException;import java.io.InputStream;public class MyBatisTest { @Test public void insertEmployeeTest() throws IOException { //加载核心配置文件 InputStream is = Resources.getResourceAsStream("mybatis-config.xml"); //获取SqlSessionFactoryBuilder SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder(); //获取SqlSessionFactory SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is); //获取SqlSession SqlSession sqlSession = sqlSessionFactory.openSession(true); //获取mapper接口对象 EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class); //测试功能 int result = mapper.insertEmployee(); //提交事务 //sqlSession.commit(); System.out.println("result:"+result); } @Test public void updateEmployeeTest() throws IOException { InputStream is = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); SqlSession sqlSession = sqlSessionFactory.openSession(true); EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class); mapper.updateEmployee(); //mapper.deleteEmployee(); //Employee employee = mapper.findById(); //System.out.println(employee); }}
(4)根据id删除员工信息
package org.example1.test;import org.example1.mapper.EmployeeMapper;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.example1.pojo.Employee;import org.junit.Test;import java.io.IOException;import java.io.InputStream;public class MyBatisTest { @Test public void insertEmployeeTest() throws IOException { //加载核心配置文件 InputStream is = Resources.getResourceAsStream("mybatis-config.xml"); //获取SqlSessionFactoryBuilder SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder(); //获取SqlSessionFactory SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is); //获取SqlSession SqlSession sqlSession = sqlSessionFactory.openSession(true); //获取mapper接口对象 EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class); //测试功能 //int result = mapper.insertEmployee(); //提交事务 //sqlSession.commit(); //System.out.println("result:"+result); } @Test public void deleteEmployeeTest() throws IOException { InputStream is = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); SqlSession sqlSession = sqlSessionFactory.openSession(true); EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class); //mapper.updateEmployee(); mapper.deleteEmployee(); //Employee employee = mapper.findById(); //System.out.println(employee); }}
9.数据库运行截图
(1)根据id查询员工信息
(2)新增员工信息
(3)根据id更新员工信息
(4)根据id删除员工信息