mybatis的框架员工信息crud
时间: 2024-05-13 15:20:00 浏览: 116
MyBatis 是一种持久化框架,可以轻松地实现数据库操作。以下是使用 MyBatis 实现员工信息的 CRUD 操作的示例:
1. 首先,创建一个 Employee 类,该类包含员工的基本信息:
```java
public class Employee {
private int id;
private String name;
private String email;
private String gender;
private Department department;
// 省略 getter 和 setter 方法
}
```
2. 创建一个 Department 类,该类包含部门的基本信息:
```java
public class Department {
private int id;
private String name;
// 省略 getter 和 setter 方法
}
```
3. 创建一个 DAO 接口,该接口定义了员工信息的 CRUD 操作:
```java
public interface EmployeeMapper {
// 添加员工信息
public int addEmployee(Employee employee);
// 根据员工编号删除员工信息
public int deleteEmployeeById(int id);
// 更新员工信息
public int updateEmployee(Employee employee);
// 根据员工编号查询员工信息
public Employee getEmployeeById(int id);
// 查询所有员工信息
public List<Employee> getAllEmployees();
}
```
4. 创建一个映射文件(employeeMapper.xml),该文件包含了 SQL 语句和参数映射:
```xml
<mapper namespace="com.example.EmployeeMapper">
<!-- 添加员工信息 -->
<insert id="addEmployee" parameterType="com.example.Employee">
insert into employee(name, email, gender, dept_id)
values(#{name}, #{email}, #{gender}, #{department.id})
</insert>
<!-- 根据员工编号删除员工信息 -->
<delete id="deleteEmployeeById" parameterType="int">
delete from employee where id = #{id}
</delete>
<!-- 更新员工信息 -->
<update id="updateEmployee" parameterType="com.example.Employee">
update employee set name = #{name}, email = #{email},
gender = #{gender}, dept_id = #{department.id}
where id = #{id}
</update>
<!-- 根据员工编号查询员工信息 -->
<select id="getEmployeeById" parameterType="int"
resultType="com.example.Employee">
select e.id, e.name, e.email, e.gender,
d.id as dept_id, d.name as dept_name
from employee e left join department d on e.dept_id = d.id
where e.id = #{id}
</select>
<!-- 查询所有员工信息 -->
<select id="getAllEmployees" resultType="com.example.Employee">
select e.id, e.name, e.email, e.gender,
d.id as dept_id, d.name as dept_name
from employee e left join department d on e.dept_id = d.id
</select>
</mapper>
```
5. 创建一个 MyBatis 的配置文件,该文件包含了数据库连接信息和映射文件的路径:
```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/test" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/example/EmployeeMapper.xml"/>
</mappers>
</configuration>
```
6. 最后,在 Java 代码中使用 MyBatis 进行员工信息的 CRUD 操作:
```java
// 加载 MyBatis 配置文件
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 获取 SqlSession 对象
SqlSession sqlSession = sqlSessionFactory.openSession();
// 获取 EmployeeMapper 接口的代理对象
EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
// 添加员工信息
Employee employee = new Employee();
employee.setName("张三");
employee.setEmail("zhangsan@163.com");
employee.setGender("男");
Department department = new Department();
department.setId(1);
employee.setDepartment(department);
employeeMapper.addEmployee(employee);
sqlSession.commit();
// 根据员工编号查询员工信息
Employee employee = employeeMapper.getEmployeeById(1);
System.out.println(employee.getName());
// 查询所有员工信息
List<Employee> employeeList = employeeMapper.getAllEmployees();
for (Employee emp : employeeList) {
System.out.println(emp.getName());
}
// 关闭 SqlSession 对象
sqlSession.close();
```
以上就是使用 MyBatis 实现员工信息的 CRUD 操作的整个过程。
阅读全文